From: Duncan Beevers Date: Tue, 2 Sep 2014 03:15:09 +0000 (-0500) Subject: draggable is enumerated attribute, `auto` default X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=d336d07e33b19f325911874bb40948886d559800;p=html-minifier.git draggable is enumerated attribute, `auto` default --- diff --git a/src/htmlminifier.js b/src/htmlminifier.js index 52254f7..8541579 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -167,8 +167,23 @@ ); } - function isBooleanAttribute(attrName) { - return (/^(?:allowfullscreen|async|autofocus|autoplay|checked|compact|controls|declare|default|defaultchecked|defaultmuted|defaultselected|defer|disabled|enabled|formnovalidate|hidden|indeterminate|inert|ismap|itemscope|loop|multiple|muted|nohref|noresize|noshade|novalidate|nowrap|open|pauseonexit|readonly|required|reversed|scoped|seamless|selected|sortable|spellcheck|truespeed|typemustmatch|visible)$/i).test(attrName); + var enumeratedAttributeValues = { + draggable: ['true', 'false'] // defaults to 'auto' + }; + + function isBooleanAttribute(attrName, attrValue) { + var isSimpleBoolean = (/^(?:allowfullscreen|async|autofocus|autoplay|checked|compact|controls|declare|default|defaultchecked|defaultmuted|defaultselected|defer|disabled|enabled|formnovalidate|hidden|indeterminate|inert|ismap|itemscope|loop|multiple|muted|nohref|noresize|noshade|novalidate|nowrap|open|pauseonexit|readonly|required|reversed|scoped|seamless|selected|sortable|spellcheck|truespeed|typemustmatch|visible)$/i).test(attrName); + if (isSimpleBoolean) { + return true; + } + + var attrValueEnumeration = enumeratedAttributeValues[attrName.toLowerCase()]; + if (!attrValueEnumeration) { + return false; + } + else { + return (-1 === attrValueEnumeration.indexOf(attrValue.toLowerCase())); + } } function isUriTypeAttribute(attrName, tag) { @@ -297,7 +312,7 @@ '?:down|up|over|move|out)|key(?:press|down|up)))$'); function canDeleteEmptyAttribute(tag, attrName, attrValue) { - var isValueEmpty = !attrValue || /^(["'])?\s*\1$/.test(attrValue); + var isValueEmpty = !attrValue || (/^\s*$/).test(attrValue); if (isValueEmpty) { return ( (tag === 'input' && attrName === 'value') || @@ -331,6 +346,7 @@ var attrName = options.caseSensitive ? attr.name : attr.name.toLowerCase(), attrValue = attr.escaped, attrFragment, + emittedAttrValue, isTerminalOfUnarySlash = unarySlash && index === attrs.length - 1; if ((options.removeRedundantAttributes && @@ -348,7 +364,10 @@ if (attrValue !== undefined && !options.removeAttributeQuotes || !canRemoveAttributeQuotes(attrValue) || isTerminalOfUnarySlash) { - attrValue = '"' + attrValue + '"'; + emittedAttrValue = '"' + attrValue + '"'; + } + else { + emittedAttrValue = attrValue; } if (options.removeEmptyAttributes && @@ -357,11 +376,11 @@ } if (attrValue === undefined || (options.collapseBooleanAttributes && - isBooleanAttribute(attrName))) { + isBooleanAttribute(attrName, attrValue))) { attrFragment = attrName; } else { - attrFragment = attrName + attr.customAssign + attrValue; + attrFragment = attrName + attr.customAssign + emittedAttrValue; } return (' ' + attr.customOpen + attrFragment + attr.customClose); diff --git a/tests/minifier.js b/tests/minifier.js index ccb28df..88baf94 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -715,6 +715,20 @@ equal(minify(input, { collapseBooleanAttributes: true, caseSensitive: true }), output); }); + test('collapsing enumerated attributes', function() { + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + equal(minify('
', { collapseBooleanAttributes: true }), '
'); + }); + test('keeping trailing slashes in tags', function() { equal(minify('', { keepClosingSlash: true }), ''); // https://github.com/kangax/html-minifier/issues/233