From: will Farrell Date: Mon, 8 Apr 2013 00:34:49 +0000 (-0400) Subject: Smarter collapseWhitespace - preserver space around inline tags X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=90b482e8b283631799f5010aa7f3a4746ec7260c;p=html-minifier.git Smarter collapseWhitespace - preserver space around inline tags --- diff --git a/src/htmlminifier.js b/src/htmlminifier.js index f0bafa3..e172b70 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -39,6 +39,28 @@ function collapseWhitespace(str) { return str.replace(/\s+/g, ' '); } + + function collapseWhitespaceSmart(str, prevTag, nextTag) { + // array of tags that will maintain a single space outside of them + var tags = ['a', 'b', 'big', 'button', 'em', 'font','i', 'img', 'mark', 's', 'small', 'span', 'strike', 'strong', 'sub', 'sup', 'tt', 'u']; + + if (prevTag && (prevTag.substr(0,1) !== '/' + || ( prevTag.substr(0,1) === '/' && tags.indexOf(prevTag.substr(1)) === -1))) { + str = str.replace(/^\s+/, ''); + } + + if (nextTag && (nextTag.substr(0,1) === '/' + || ( nextTag.substr(0,1) !== '/' && tags.indexOf(nextTag) === -1))) { + str = str.replace(/\s+$/, ''); + } + + if (prevTag && nextTag) { + // strip non space whitespace then compress spaces to one + return str.replace(/[\t\n\r]+/g, '').replace(/[ ]+/g, ' '); + } + + return str; + } function isConditionalComment(text) { return ((/\[if[^\]]+\]/).test(text) || (/\s*( foo bar

'; output = '

foo bar

'; equal(minify(input, { collapseWhitespace: true }), output); - - input = '

foo blah 22 bar

'; - output = '

fooblah 22bar

'; + + input = '

foo blah 22 bar

'; + output = '

foo blah 22 bar

'; equal(minify(input, { collapseWhitespace: true }), output); input = '';