script element with src must be preserved
authorDuncan Beevers <duncan@dweebd.com>
Tue, 2 Sep 2014 11:33:19 +0000 (06:33 -0500)
committerDuncan Beevers <duncan@dweebd.com>
Tue, 2 Sep 2014 11:33:19 +0000 (06:33 -0500)
src/htmlminifier.js
src/htmlparser.js

index 52254f7..faf52ac 100644 (file)
     return false;
   }
 
-  function canRemoveElement(tag) {
-    return tag !== 'textarea';
+  function canRemoveElement(tag, attrs) {
+    if (tag === 'textarea') {
+      return false;
+    }
+
+    if (tag === 'script') {
+      for (var i = attrs.length - 1; i >= 0; i--) {
+        if (attrs[i].name === 'src') {
+          return false;
+        }
+      }
+    }
+
+    return true;
   }
 
   function canCollapseWhitespace(tag) {
           buffer.push(token);
         }
       },
-      end: function( tag ) {
+      end: function( tag, attrs ) {
 
         if (isIgnoring) {
           buffer.push('</' + tag + '>');
         }
 
         var isElementEmpty = currentChars === '' && tag === currentTag;
-        if ((options.removeEmptyElements && isElementEmpty && canRemoveElement(tag))) {
+        if ((options.removeEmptyElements && isElementEmpty && canRemoveElement(tag, attrs))) {
           // remove last "element" from buffer, return
           for ( var i = buffer.length - 1; i >= 0; i-- ) {
             if ( /^<[^\/!]/.test(buffer[i]) ) {
index 48aa66f..f3f755b 100644 (file)
         // Close all the open elements, up the stack
         for ( var i = stack.length - 1; i >= pos; i-- ) {
           if ( handler.end ) {
-            handler.end( stack[ i ].tag );
+            handler.end( stack[ i ].tag, stack[ i ].attrs );
           }
         }