From e7bcf1680f2e5b3c4fe232a2ffface1c7a481ad1 Mon Sep 17 00:00:00 2001 From: Sam Grundman Date: Sat, 7 Apr 2018 15:14:09 -0400 Subject: [PATCH] Preserve whitespace around inline (#905) also: * Preserve whitespace inside tag (to buffer parentheses for very old browsers) * Preserve whitespace around inline tags * Mark as valid HTML fixes #904 --- src/htmlminifier.js | 6 +++--- tests/minifier.js | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/htmlminifier.js b/src/htmlminifier.js index e16c772..7748f1c 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -67,9 +67,9 @@ function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll) { var createMapFromString = utils.createMapFromString; // non-empty tags that will maintain whitespace around them -var inlineTags = createMapFromString('a,abbr,acronym,b,bdi,bdo,big,button,cite,code,del,dfn,em,font,i,ins,kbd,label,mark,math,nobr,object,q,rt,rp,s,samp,select,small,span,strike,strong,sub,sup,svg,textarea,time,tt,u,var'); +var inlineTags = createMapFromString('a,abbr,acronym,b,bdi,bdo,big,button,cite,code,del,dfn,em,font,i,ins,kbd,label,mark,math,nobr,object,q,rp,rt,rtc,ruby,s,samp,select,small,span,strike,strong,sub,sup,svg,textarea,time,tt,u,var'); // non-empty tags that will maintain whitespace within them -var inlineTextTags = createMapFromString('a,abbr,acronym,b,big,del,em,font,i,ins,kbd,mark,nobr,s,samp,small,span,strike,strong,sub,sup,time,tt,u,var'); +var inlineTextTags = createMapFromString('a,abbr,acronym,b,big,del,em,font,i,ins,kbd,mark,nobr,rp,s,samp,small,span,strike,strong,sub,sup,time,tt,u,var'); // self-closing tags that will maintain whitespace around them var selfClosingInlineTags = createMapFromString('comment,img,input,wbr'); @@ -395,7 +395,7 @@ var topLevelTags = createMapFromString('html,head,body'); var compactTags = createMapFromString('html,body'); var looseTags = createMapFromString('head,colgroup,caption'); var trailingTags = createMapFromString('dt,thead'); -var htmlTags = createMapFromString('a,abbr,acronym,address,applet,area,article,aside,audio,b,base,basefont,bdi,bdo,bgsound,big,blink,blockquote,body,br,button,canvas,caption,center,cite,code,col,colgroup,command,content,data,datalist,dd,del,details,dfn,dialog,dir,div,dl,dt,element,em,embed,fieldset,figcaption,figure,font,footer,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,image,img,input,ins,isindex,kbd,keygen,label,legend,li,link,listing,main,map,mark,marquee,menu,menuitem,meta,meter,multicol,nav,nobr,noembed,noframes,noscript,object,ol,optgroup,option,output,p,param,picture,plaintext,pre,progress,q,rp,rt,rtc,ruby,s,samp,script,section,select,shadow,small,source,spacer,span,strike,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,tt,u,ul,var,video,wbr,xmp'); +var htmlTags = createMapFromString('a,abbr,acronym,address,applet,area,article,aside,audio,b,base,basefont,bdi,bdo,bgsound,big,blink,blockquote,body,br,button,canvas,caption,center,cite,code,col,colgroup,command,content,data,datalist,dd,del,details,dfn,dialog,dir,div,dl,dt,element,em,embed,fieldset,figcaption,figure,font,footer,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,image,img,input,ins,isindex,kbd,keygen,label,legend,li,link,listing,main,map,mark,marquee,menu,menuitem,meta,meter,multicol,nav,nobr,noembed,noframes,noscript,object,ol,optgroup,option,output,p,param,picture,plaintext,pre,progress,q,rb,rp,rt,rtc,ruby,s,samp,script,section,select,shadow,small,source,spacer,span,strike,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,tt,u,ul,var,video,wbr,xmp'); function canRemoveParentTag(optionalStartTag, tag) { switch (optionalStartTag) { diff --git a/tests/minifier.js b/tests/minifier.js index f6946a5..4f71d66 100644 --- a/tests/minifier.js +++ b/tests/minifier.js @@ -207,8 +207,9 @@ QUnit.test('space normalization around text', function(assert) { assert.equal(minify('
foo <' + el + '> baz bar
', { collapseWhitespace: true }), '
foo <' + el + '>baz bar
'); assert.equal(minify('
foo<' + el + '> baz bar
', { collapseWhitespace: true }), '
foo<' + el + '> baz bar
'); }); + // Don't trim whitespace around element, but do trim within [ - 'bdi', 'bdo', 'button', 'cite', 'code', 'dfn', 'math', 'q', 'rt', 'rp', 'svg' + 'bdi', 'bdo', 'button', 'cite', 'code', 'dfn', 'math', 'q', 'rt', 'rtc', 'ruby', 'svg' ].forEach(function(el) { assert.equal(minify('foo <' + el + '>baz bar', { collapseWhitespace: true }), 'foo <' + el + '>baz bar'); assert.equal(minify('foo<' + el + '>bazbar', { collapseWhitespace: true }), 'foo<' + el + '>bazbar'); -- 2.34.1