From b599671a705e5683e33955ffbdbc4797f1e0429e Mon Sep 17 00:00:00 2001 From: Duncan Beevers Date: Thu, 13 Nov 2014 09:14:34 -0600 Subject: [PATCH] Special-case svg tags, preserve case and slashes --- src/htmlminifier.js | 26 +++++++++++++++++++++++--- tests/minifier.js | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 3516cf7..ae9e0ec 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -520,6 +520,8 @@ function minify(value, options) { options = options || {}; + var optionsStack = []; + value = trimWhitespace(value); setDefaultTesters(options); @@ -552,13 +554,26 @@ html5: typeof options.html5 !== 'undefined' ? options.html5 : true, start: function( tag, attrs, unary, unarySlash ) { - if (isIgnoring) { buffer.push('<' + tag, attrsToMarkup(attrs), unarySlash ? '/' : '', '>'); return; } - tag = options.caseSensitive ? tag : tag.toLowerCase(); + var lowerTag = tag.toLowerCase(); + + if (lowerTag === 'svg') { + optionsStack.push(options); + var nextOptions = {}; + for (var key in options) { + nextOptions[key] = options[key]; + } + nextOptions.keepClosingSlash = true; + nextOptions.caseSensitive = true; + options = nextOptions; + } + + tag = options.caseSensitive ? tag : lowerTag; + currentTag = tag; currentChars = ''; currentAttrs = attrs; @@ -600,6 +615,11 @@ return; } + var lowerTag = tag.toLowerCase(); + if (lowerTag === 'svg') { + options = optionsStack.pop(); + } + // check if current tag is in a whitespace stack if (options.collapseWhitespace) { if (stackNoTrimWhitespace.length && @@ -629,7 +649,7 @@ } else { // push end tag to buffer - buffer.push(''); + buffer.push(''); results.push.apply(results, buffer); } // flush buffer diff --git a/tests/minifier.js b/tests/minifier.js index ab9defe..b899c19 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -857,10 +857,10 @@ }); test('caseSensitive', function() { - input = ''; + input = '
'; - var caseSensitiveOutput = ''; - var caseInSensitiveOutput = ''; + var caseSensitiveOutput = '
'; + var caseInSensitiveOutput = '
'; equal(minify(input), caseInSensitiveOutput); equal(minify(input, { caseSensitive: true }), caseSensitiveOutput); @@ -882,6 +882,33 @@ equal(minify(input, { removeOptionalTags: true }), output); }); + test('mixed html and svg', function() { + input = '\n' + + ' \n' + + ''; + + output = '' + + '' + + ''; + + // Should preserve case-sensitivity and closing slashes within svg tags + equal(minify(input, { collapseWhitespace: true }), output); + }); + test('nested quotes', function() { input = '
'; output = '
'; -- 2.34.1