From: Juriy Zaytsev Date: Wed, 10 Feb 2010 19:34:08 +0000 (-0500) Subject: Add MIT license and mention it in htmlminifier.js; X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=fd0a2c227beccdbe01ab1159628889386335d7ca;p=html-minifier.git Add MIT license and mention it in htmlminifier.js; Add checkbox to remove empty elements (off by default); Add checkbox to remove comments from STYLE and SCRIPT elements; Collapse whitespace in textnodes, when it's safe to do so; Rename options to get rid of "should" prefix. --- diff --git a/index.html b/index.html index bf65a43..21634bc 100644 --- a/index.html +++ b/index.html @@ -17,7 +17,9 @@ @@ -83,16 +95,15 @@
TODO: diff --git a/master.js b/master.js index ec38a35..3601abc 100644 --- a/master.js +++ b/master.js @@ -10,13 +10,15 @@ function getOptions() { return { - shouldRemoveComments: byId('remove-comments').checked, - shouldCollapseWhitespace: byId('collapse-whitespace').checked, - shouldCollapseBooleanAttributes: byId('collapse-boolean-attributes').checked, - shouldRemoveAttributeQuotes: byId('remove-attribute-quotes').checked, - shouldRemoveRedundantAttributes: byId('remove-redundant-attributes').checked, - shouldUseShortDoctype: byId('use-short-doctype').checked, - shouldRemoveEmptyAttributes: byId('remove-empty-attributes').checked + 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 }; } diff --git a/src/htmlminifier.js b/src/htmlminifier.js index c79ac00..2216942 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -1,3 +1,12 @@ +/*! + * HTML Minifier v0.2 + * http://kangax.github.com/html-minifier/ + * + * Copyright (c) 2010 Juriy "kangax" Zaytsev + * Licensed under the MIT license. + * + */ + (function(global){ var log; @@ -92,30 +101,38 @@ return tag !== 'textarea'; } + function canCollapseWhitespace(tag) { + return !(/^(?:script|style|pre|textarea)$/.test(tag)); + } + + function canTrimWhitespace(tag) { + return !(/^(?:pre|textarea)$/.test(tag)); + } + function normalizeAttribute(attr, attrs, tag, options) { var attrName = attr.name.toLowerCase(), attrValue = attr.escaped, attrFragment; - if (options.shouldRemoveRedundantAttributes && + if (options.removeRedundantAttributes && isAttributeRedundant(tag, attrName, attrValue, attrs)) { return ''; } attrValue = cleanAttributeValue(tag, attrName, attrValue); - if (!options.shouldRemoveAttributeQuotes || + if (!options.removeAttributeQuotes || !canRemoveAttributeQuotes(attrValue)) { attrValue = '"' + attrValue + '"'; } - if (options.shouldRemoveEmptyAttributes && + if (options.removeEmptyAttributes && canDeleteEmptyAttribute(tag, attrName, attrValue)) { return ''; } - if (options.shouldCollapseBooleanAttributes && + if (options.collapseBooleanAttributes && isBooleanAttribute(attrName)) { attrFragment = attrName; } @@ -153,7 +170,7 @@ }, end: function( tag ) { var isElementEmpty = currentChars === '' && tag === currentTag; - if (options.shouldRemoveEmptyElements && isElementEmpty && canRemoveElement(tag)) { + if (options.removeEmptyElements && isElementEmpty && canRemoveElement(tag)) { // noop } else { @@ -164,14 +181,26 @@ currentChars = ''; }, chars: function( text ) { + if (options.removeCommentsFromCDATA && + (currentTag === 'script' || currentTag === 'style')) { + text = text.replace(/^\s*\s*$/, ''); + } + if (options.collapseWhitespace) { + if (canTrimWhitespace(currentTag)) { + text = trimWhitespace(text); + } + if (canCollapseWhitespace(currentTag)) { + text = collapseWhitespace(text); + } + } currentChars = text; - buffer.push(options.shouldCollapseWhitespace ? trimWhitespace(text) : text); + buffer.push(text); }, comment: function( text ) { - buffer.push(options.shouldRemoveComments ? '' : ('')); + buffer.push(options.removeComments ? '' : ('')); }, doctype: function(doctype) { - buffer.push(options.shouldUseShortDoctype ? '' : collapseWhitespace(doctype)); + buffer.push(options.useShortDoctype ? '' : collapseWhitespace(doctype)); } }); diff --git a/src/license.txt b/src/license.txt new file mode 100644 index 0000000..a299981 --- /dev/null +++ b/src/license.txt @@ -0,0 +1,22 @@ +Copyright (c) 2010 Juriy "kangax" Zaytsev + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/tests/index.html b/tests/index.html index be5fc4d..dd970ba 100644 --- a/tests/index.html +++ b/tests/index.html @@ -72,14 +72,14 @@ test('doctype normalization', function() { var html401doctype = ''; - var actual = minify(html401doctype, { shouldUseShortDoctype: true }); + var actual = minify(html401doctype, { useShortDoctype: true }); var expected = ''; equals(actual, expected); - equals(minify('', { shouldUseShortDoctype: true }), ''); + equals(minify('', { useShortDoctype: true }), ''); - actual = minify(html401doctype, { shouldUseShortDoctype: false }); + actual = minify(html401doctype, { useShortDoctype: false }); expected = ''; equals(actual, expected); @@ -87,111 +87,138 @@ test('removing comments', function(){ var input = ''; - equals(minify(input, { shouldRemoveComments: true }), ''); + equals(minify(input, { removeComments: true }), ''); var input = '
baz
'; - equals(minify(input, { shouldRemoveComments: true }), '
baz
'); - equals(minify(input, { shouldRemoveComments: false }), input); + equals(minify(input, { removeComments: true }), '
baz
'); + equals(minify(input, { removeComments: false }), input); var input = '

foo

'; - equals(minify(input, { shouldRemoveComments: true }), input); + equals(minify(input, { removeComments: true }), input); var input = ' - \ No newline at end of file