From 6db8e6e1bdc0e4df1cd964018f3aabf723ab98ed Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sat, 13 Feb 2016 05:25:47 +0800 Subject: [PATCH] add comments explaining tag omission rules --- dist/htmlminifier.js | 40 ++++++++++++++++++++++++++++++++-------- dist/htmlminifier.min.js | 2 +- src/htmlminifier.js | 40 ++++++++++++++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/dist/htmlminifier.js b/dist/htmlminifier.js index 1d6708d..10589e5 100644 --- a/dist/htmlminifier.js +++ b/dist/htmlminifier.js @@ -934,10 +934,17 @@ return text.replace(reStartDelimiter[tag], '').replace(reEndDelimiter[tag], ''); } + // Tag omission rules from http://www.w3.org/TR/html5/syntax.html#optional-tags var optionalStartTags = createMap('html,head,body'); var optionalEndTags = createMap('html,head,body,li,dt,dd,p,rb,rt,rtc,rp,optgroup,option,colgroup,thead,tbody,tfoot,tr,td,th'); var headerTags = createMap('meta,link,script,style,template'); - var removePrecedingParagraphTag = createMap('address,article,aside,blockquote,div,dl,fieldset,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,hr,main,nav,ol,p,pre,section,table,ul'); + var descriptionTags = createMap('dt,dd'); + var pTag = createMap('address,article,aside,blockquote,div,dl,fieldset,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,hr,main,nav,ol,p,pre,section,table,ul'); + var rubyTags = createMap('rb,rt,rtc,rp'); + var rtcTag = createMap('rb,rtc,rp'); + var optionTag = createMap('option,optgroup'); + var tableTags = createMap('tbody,tfoot'); + var cellTags = createMap('td,th'); function canRemovePrecedingTag(optionalEndTag, tag) { switch (optionalEndTag) { @@ -952,25 +959,25 @@ return tag === optionalEndTag; case 'dt': case 'dd': - return tag === 'dt' || tag === 'dd'; + return descriptionTags(tag); case 'p': - return removePrecedingParagraphTag(tag); + return pTag(tag); case 'rb': case 'rt': case 'rp': - return tag === 'rb' || tag === 'rt' || tag === 'rtc' || tag === 'rp'; + return rubyTags(tag); case 'rtc': - return tag === 'rb' || tag === 'rtc' || tag === 'rp'; + return rtcTag(tag); case 'option': - return tag === 'option' || tag === 'optgroup'; + return optionTag(tag); case 'thead': case 'tbody': - return tag === 'tbody' || tag === 'tfoot'; + return tableTags(tag); case 'tfoot': return tag === 'tbody'; case 'td': case 'th': - return tag === 'td' || tag === 'th'; + return cellTags(tag); } return false; } @@ -1320,10 +1327,14 @@ currentAttrs = attrs; if (options.removeOptionalTags) { + // may be omitted if first thing inside is not comment + // may be omitted if first thing inside is an element + // may be omitted if first thing inside is not space, comment, , ,