From d9b302d115b696d3bab39b336c7e3e5d31470bc2 Mon Sep 17 00:00:00 2001 From: Gilmore Davidson Date: Sun, 27 Feb 2011 20:24:40 +1100 Subject: [PATCH] Fixed issue #13 - Whitespace is munged in elements that are descendants of

---
 src/htmlminifier.js | 28 ++++++++++++++++++++++++++--
 tests/index.html    |  4 ++++
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/src/htmlminifier.js b/src/htmlminifier.js
index a4b7428..b6d63b6 100644
--- a/src/htmlminifier.js
+++ b/src/htmlminifier.js
@@ -254,6 +254,8 @@
         buffer = [ ],
         currentChars = '',
         currentTag = '',
+        stackNoTrimWhitespace = [],
+        stackNoCollapseWhitespace = [],
         lint = options.lint,
         t = new Date()
     
@@ -263,6 +265,16 @@
         currentTag = tag;
         currentChars = '';
         
+        // set whitespace flags for nested tags (eg.  within a 
)
+        if (options.collapseWhitespace) {
+          if (!canTrimWhitespace(tag)) {
+            stackNoTrimWhitespace.push(tag);
+          }
+          if (!canCollapseWhitespace(tag)) {
+            stackNoCollapseWhitespace.push(tag);
+          }
+        }
+        
         buffer.push('<', tag);
         
         lint && lint.testElement(tag);
@@ -275,6 +287,18 @@
         buffer.push('>');
       },
       end: function( tag ) {
+        // check if current tag is in a whitespace stack
+        if (options.collapseWhitespace) {
+          if (stackNoTrimWhitespace.length &&
+            tag == stackNoTrimWhitespace[stackNoTrimWhitespace.length - 1]) {
+            stackNoTrimWhitespace.pop();
+          }
+          if (stackNoCollapseWhitespace.length &&
+            tag == stackNoCollapseWhitespace[stackNoCollapseWhitespace.length - 1]) {
+            stackNoCollapseWhitespace.pop();
+          }
+        }
+        
         var isElementEmpty = currentChars === '' && tag === currentTag;
         if ((options.removeEmptyElements && isElementEmpty && canRemoveElement(tag))) {
           // remove last "element" from buffer, return
@@ -304,10 +328,10 @@
           }
         }
         if (options.collapseWhitespace) {
-          if (canTrimWhitespace(currentTag)) {
+          if (!stackNoTrimWhitespace.length && canTrimWhitespace(currentTag)) {
             text = trimWhitespace(text);
           }
-          if (canCollapseWhitespace(currentTag)) {
+          if (!stackNoCollapseWhitespace.length && canCollapseWhitespace(currentTag)) {
             text = collapseWhitespace(text);
           }
         }
diff --git a/tests/index.html b/tests/index.html
index c8cbf25..2b66529 100644
--- a/tests/index.html
+++ b/tests/index.html
@@ -467,6 +467,10 @@
           output = '
   hello     world 
'; equals(minify(input, { collapseWhitespace: true }), output); + input = '
   hello     world 
'; + output = '
   hello     world 
'; + equals(minify(input, { collapseWhitespace: true }), output); + input = '