From: Juriy Zaytsev Date: Wed, 10 Feb 2010 22:21:03 +0000 (-0500) Subject: Add option to strip CDATA sections from script and style elements. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=7c0620531d330af1fefd2c34c07b25567289990f;p=html-minifier.git Add option to strip CDATA sections from script and style elements. --- diff --git a/index.html b/index.html index b398087..f7dad7e 100644 --- a/index.html +++ b/index.html @@ -19,7 +19,11 @@ - ) + + +
  • + +
  • @@ -103,7 +107,6 @@
  • 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
  • Remove as many empty/blank attributes as possible (not just core ones)
  • -
  • Add option to remove CDATA sections from scripts/styles
  • Parser trips over xml declarations (need to ignore or strip them)
  • Generate a report of all applied transformations
  • diff --git a/master.js b/master.js index 3601abc..72dc7b8 100644 --- a/master.js +++ b/master.js @@ -10,15 +10,16 @@ function getOptions() { return { - removeComments: byId('remove-comments').checked, - removeCommentsFromCDATA: byId('remove-comments-from-cdata').checked, - collapseWhitespace: byId('collapse-whitespace').checked, - collapseBooleanAttributes: byId('collapse-boolean-attributes').checked, - removeAttributeQuotes: byId('remove-attribute-quotes').checked, - removeRedundantAttributes: byId('remove-redundant-attributes').checked, - useShortDoctype: byId('use-short-doctype').checked, - removeEmptyAttributes: byId('remove-empty-attributes').checked, - removeEmptyElements: byId('remove-empty-elements').checked + removeComments: byId('remove-comments').checked, + removeCommentsFromCDATA: byId('remove-comments-from-cdata').checked, + removeCDATASectionsFromCDATA: byId('remove-cdata-sections-from-cdata').checked, + collapseWhitespace: byId('collapse-whitespace').checked, + collapseBooleanAttributes: byId('collapse-boolean-attributes').checked,q + removeAttributeQuotes: byId('remove-attribute-quotes').checked, + removeRedundantAttributes: byId('remove-redundant-attributes').checked, + useShortDoctype: byId('use-short-doctype').checked, + removeEmptyAttributes: byId('remove-empty-attributes').checked, + removeEmptyElements: byId('remove-empty-elements').checked }; } diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 2216942..36a8cf3 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -181,9 +181,17 @@ currentChars = ''; }, chars: function( text ) { - if (options.removeCommentsFromCDATA && - (currentTag === 'script' || currentTag === 'style')) { - text = text.replace(/^\s*\s*$/, ''); + if (currentTag === 'script' || currentTag === 'style') { + if (options.removeCommentsFromCDATA) { + text = text.replace(/^\s*\s*$/, ''); + } + if (options.removeCDATASectionsFromCDATA) { + text = text + // /* */ or // ]]> + .replace(/(?:\/\*\s*\]\]>\s*\*\/|\/\/\s*\]\]>.*)$/, ''); + } } if (options.collapseWhitespace) { if (canTrimWhitespace(currentTag)) { diff --git a/tests/index.html b/tests/index.html index dd970ba..80b7530 100644 --- a/tests/index.html +++ b/tests/index.html @@ -117,6 +117,24 @@ equals(minify(input, { removeCommentsFromCDATA: true }), output); }); + test('remove CDATA sections from scripts/styles', function(){ + var input = '