From 29e5e71f8d7a56e4edbc02bfb6a4c4012fae7461 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Thu, 11 Feb 2010 13:30:57 -0500 Subject: [PATCH] Do not strip conditional comments (write tests for it and mention on a main page). Fix CDATA sections. --- index.html | 7 +++++-- src/htmlminifier.js | 19 +++++++++++++++---- tests/index.html | 33 ++++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/index.html b/index.html index f7dad7e..3f65e02 100644 --- a/index.html +++ b/index.html @@ -20,6 +20,9 @@ + + Conditional comments are left intact. +
  • @@ -99,13 +102,13 @@
    TODO:
      -
    • Write more unit tests (~60 so far)
    • +
    • Write more unit tests (~70 so far)
    • Detect repeating attributes (e.g. multiple styles, classes, etc.)
    • Strip whitespace from attributes where allowed
    • Report deprecated (or presentational) attributes (e.g.: <td width="..." height="...">)
    • Add option to collapse all whitespace to 1 character, instead of completely removing it (to preserve empty text nodes)
    • Figure out when it is safe to remove optional closing tags, so that it doesn't affect document tree
    • -
    • Do not strip IE conditional comments
    • +
    • Support IE "Downlevel-revealed Conditional Comments"
    • Remove as many empty/blank attributes as possible (not just core ones)
    • Parser trips over xml declarations (need to ignore or strip them)
    • Generate a report of all applied transformations
    • diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 37d281f..d9b419b 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -23,10 +23,15 @@ function trimWhitespace(str) { return (str.trim ? str.trim() : str.replace(/^\s+/, '').replace(/\s+$/, '')); } + function collapseWhitespace(str) { return str.replace(/\s+/g, ' '); } + function isConditionalComment(text) { + return /\[if[^]+\]/.test(text); + } + function canRemoveAttributeQuotes(value) { // http://www.w3.org/TR/html4/intro/sgmltut.html#attributes // avoid \w, which could match unicode in some implementations @@ -187,9 +192,9 @@ } if (options.removeCDATASectionsFromCDATA) { text = text - // /* */ or // ]]> + // "/* */" or "// ]]>" .replace(/(?:\/\*\s*\]\]>\s*\*\/|\/\/\s*\]\]>)\s*$/, ''); } } @@ -205,7 +210,13 @@ buffer.push(text); }, comment: function( text ) { - buffer.push(options.removeComments ? '' : ('')); + if (options.removeComments && !isConditionalComment(text)) { + text = ''; + } + else { + text = ''; + } + buffer.push(text); }, doctype: function(doctype) { buffer.push(options.useShortDoctype ? '' : collapseWhitespace(doctype)); diff --git a/tests/index.html b/tests/index.html index 19e1eca..862ce3b 100644 --- a/tests/index.html +++ b/tests/index.html @@ -89,20 +89,31 @@ var input = ''; equals(minify(input, { removeComments: true }), ''); - var input = '
      baz
      '; + input = '
      baz
      '; equals(minify(input, { removeComments: true }), '
      baz
      '); equals(minify(input, { removeComments: false }), input); - var input = '

      foo

      '; + input = '

      foo

      '; equals(minify(input, { removeComments: true }), input); - var input = '