From a1429cce4aa905f9c5234308e956937316704167 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Sat, 13 Mar 2010 17:57:32 -0500 Subject: [PATCH] Trim whitespace in Number-type attributes (e.g. rows, cols in TEXTAREA; tabindex in A, BUTTON, etc.). Make sure URI and Number -type attributes are matched on proper elements (e.g. should be trimmed, but not x). --- index.html | 2 +- src/htmlminifier.js | 31 ++++++++++++++++++++++++++++--- tests/index.html | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index 5dc96bc..b3988c4 100644 --- a/index.html +++ b/index.html @@ -10,7 +10,7 @@
-

HTML Minifier (ver. 0.3)

+

HTML Minifier (ver. 0.4)

Minifier is very draft and is not yet thoroughly tested. Use at your own risk.

diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 56a49b9..2bae5d1 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -85,8 +85,31 @@ return (/^(?:checked|disabled|selected|readonly)$/).test(attrName); } - function isUriTypeAttribute(attrName) { - return (/^(?:href|src|usemap|action|cite|longdesc|profile|codebase)$/).test(attrName); + function isUriTypeAttribute(attrName, tag) { + return ( + ((/^(?:a|area|link|base)$/).test(tag) && attrName === 'href') || + (tag === 'img' && (/^(?:src|longdesc|usemap)$/).test(attrName)) || + (tag === 'object' && (/^(?:classid|codebase|data|usemap)$/).test(attrName)) || + (tag === 'q' && attrName === 'cite') || + (tag === 'blockquote' && attrName === 'cite') || + ((tag === 'ins' || tag === 'del') && attrName === 'cite') || + (tag === 'form' && attrName === 'action') || + (tag === 'input' && (attrName === 'src' || attrName === 'usemap')) || + (tag === 'head' && attrName === 'profile') || + (tag === 'script' && (attrName === 'src' || attrName === 'for')) + ); + } + + function isNumberTypeAttribute(attrName, tag) { + return ( + ((/^(?:a|area|object|button)$/).test(tag) && attrName === 'tabindex') || + (tag === 'input' && (attrName === 'maxlength' || attrName === 'tabindex')) || + (tag === 'select' && (attrName === 'size' || attrName === 'tabindex')) || + (tag === 'textarea' && (/^(?:rows|cols|tabindex)$/).test(attrName)) || + (tag === 'colgroup' && attrName === 'span') || + (tag === 'col' && attrName === 'span') || + ((tag === 'th' || tag == 'td') && (attrName === 'rowspan' || attrName === 'colspan')) + ); } function cleanAttributeValue(tag, attrName, attrValue) { @@ -96,7 +119,9 @@ else if (attrName === 'class') { return collapseWhitespace(trimWhitespace(attrValue)); } - else if (isUriTypeAttribute(attrName) || attrName === 'style') { + else if (isUriTypeAttribute(attrName, tag) + || isNumberTypeAttribute(attrName, tag) + || attrName === 'style') { return trimWhitespace(attrValue); } return attrValue; diff --git a/tests/index.html b/tests/index.html index b8148b1..031cbe9 100644 --- a/tests/index.html +++ b/tests/index.html @@ -256,6 +256,38 @@ input = ''; output = ''; equals(minify(input, { cleanAttributes: true }), output); + + input = 'foo'; + equals(minify(input, { cleanAttributes: true }), input); + + input = '
blah
'; + equals(minify(input, { cleanAttributes: true }), input); + }); + + test('cleaning Number-based attributes', function() { + input = 'x'; + output = 'x'; + equals(minify(input, { cleanAttributes: true }), output); + + input = ''; + output = ''; + equals(minify(input, { cleanAttributes: true }), output); + + input = ''; + output = ''; + equals(minify(input, { cleanAttributes: true }), output); + + input = ''; + output = ''; + equals(minify(input, { cleanAttributes: true }), output); + + input = ''; + output = ''; + equals(minify(input, { cleanAttributes: true }), output); + + input = 'x'; + output = 'x'; + equals(minify(input, { cleanAttributes: true }), output); }); test('cleaning other attributes', function() { -- 2.34.1