htmlminifier.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. 'use strict';
  2. var CleanCSS = require('clean-css');
  3. var decode = require('he').decode;
  4. var HTMLParser = require('./htmlparser').HTMLParser;
  5. var RelateUrl = require('relateurl');
  6. var TokenChain = require('./tokenchain');
  7. var Terser = require('terser');
  8. var utils = require('./utils');
  9. function trimWhitespace(str) {
  10. return str && str.replace(/^[ \n\r\t\f]+/, '').replace(/[ \n\r\t\f]+$/, '');
  11. }
  12. function collapseWhitespaceAll(str) {
  13. // Non-breaking space is specifically handled inside the replacer function here:
  14. return str && str.replace(/[ \n\r\t\f\xA0]+/g, function(spaces) {
  15. return spaces === '\t' ? '\t' : spaces.replace(/(^|\xA0+)[^\xA0]+/g, '$1 ');
  16. });
  17. }
  18. function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll) {
  19. var lineBreakBefore = '', lineBreakAfter = '';
  20. if (options.preserveLineBreaks) {
  21. str = str.replace(/^[ \n\r\t\f]*?[\n\r][ \n\r\t\f]*/, function() {
  22. lineBreakBefore = '\n';
  23. return '';
  24. }).replace(/[ \n\r\t\f]*?[\n\r][ \n\r\t\f]*$/, function() {
  25. lineBreakAfter = '\n';
  26. return '';
  27. });
  28. }
  29. if (trimLeft) {
  30. // Non-breaking space is specifically handled inside the replacer function here:
  31. str = str.replace(/^[ \n\r\t\f\xA0]+/, function(spaces) {
  32. var conservative = !lineBreakBefore && options.conservativeCollapse;
  33. if (conservative && spaces === '\t') {
  34. return '\t';
  35. }
  36. return spaces.replace(/^[^\xA0]+/, '').replace(/(\xA0+)[^\xA0]+/g, '$1 ') || (conservative ? ' ' : '');
  37. });
  38. }
  39. if (trimRight) {
  40. // Non-breaking space is specifically handled inside the replacer function here:
  41. str = str.replace(/[ \n\r\t\f\xA0]+$/, function(spaces) {
  42. var conservative = !lineBreakAfter && options.conservativeCollapse;
  43. if (conservative && spaces === '\t') {
  44. return '\t';
  45. }
  46. return spaces.replace(/[^\xA0]+(\xA0+)/g, ' $1').replace(/[^\xA0]+$/, '') || (conservative ? ' ' : '');
  47. });
  48. }
  49. if (collapseAll) {
  50. // strip non space whitespace then compress spaces to one
  51. str = collapseWhitespaceAll(str);
  52. }
  53. return lineBreakBefore + str + lineBreakAfter;
  54. }
  55. var createMapFromString = utils.createMapFromString;
  56. // non-empty tags that will maintain whitespace around them
  57. 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');
  58. // non-empty tags that will maintain whitespace within them
  59. 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');
  60. // self-closing tags that will maintain whitespace around them
  61. var selfClosingInlineTags = createMapFromString('comment,img,input,wbr');
  62. function collapseWhitespaceSmart(str, prevTag, nextTag, options) {
  63. var trimLeft = prevTag && !selfClosingInlineTags(prevTag);
  64. if (trimLeft && !options.collapseInlineTagWhitespace) {
  65. trimLeft = prevTag.charAt(0) === '/' ? !inlineTags(prevTag.slice(1)) : !inlineTextTags(prevTag);
  66. }
  67. var trimRight = nextTag && !selfClosingInlineTags(nextTag);
  68. if (trimRight && !options.collapseInlineTagWhitespace) {
  69. trimRight = nextTag.charAt(0) === '/' ? !inlineTextTags(nextTag.slice(1)) : !inlineTags(nextTag);
  70. }
  71. return collapseWhitespace(str, options, trimLeft, trimRight, prevTag && nextTag);
  72. }
  73. function isConditionalComment(text) {
  74. return /^\[if\s[^\]]+]|\[endif]$/.test(text);
  75. }
  76. function isIgnoredComment(text, options) {
  77. for (var i = 0, len = options.ignoreCustomComments.length; i < len; i++) {
  78. if (options.ignoreCustomComments[i].test(text)) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. function isEventAttribute(attrName, options) {
  85. var patterns = options.customEventAttributes;
  86. if (patterns) {
  87. for (var i = patterns.length; i--;) {
  88. if (patterns[i].test(attrName)) {
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. return /^on[a-z]{3,}$/.test(attrName);
  95. }
  96. function canRemoveAttributeQuotes(value) {
  97. // https://mathiasbynens.be/notes/unquoted-attribute-values
  98. return /^[^ \t\n\f\r"'`=<>]+$/.test(value);
  99. }
  100. function attributesInclude(attributes, attribute) {
  101. for (var i = attributes.length; i--;) {
  102. if (attributes[i].name.toLowerCase() === attribute) {
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. function isAttributeRedundant(tag, attrName, attrValue, attrs) {
  109. attrValue = attrValue ? trimWhitespace(attrValue.toLowerCase()) : '';
  110. return (
  111. tag === 'script' &&
  112. attrName === 'language' &&
  113. attrValue === 'javascript' ||
  114. tag === 'form' &&
  115. attrName === 'method' &&
  116. attrValue === 'get' ||
  117. tag === 'input' &&
  118. attrName === 'type' &&
  119. attrValue === 'text' ||
  120. tag === 'script' &&
  121. attrName === 'charset' &&
  122. !attributesInclude(attrs, 'src') ||
  123. tag === 'a' &&
  124. attrName === 'name' &&
  125. attributesInclude(attrs, 'id') ||
  126. tag === 'area' &&
  127. attrName === 'shape' &&
  128. attrValue === 'rect'
  129. );
  130. }
  131. // https://mathiasbynens.be/demo/javascript-mime-type
  132. // https://developer.mozilla.org/en/docs/Web/HTML/Element/script#attr-type
  133. var executableScriptsMimetypes = utils.createMap([
  134. 'text/javascript',
  135. 'text/ecmascript',
  136. 'text/jscript',
  137. 'application/javascript',
  138. 'application/x-javascript',
  139. 'application/ecmascript',
  140. 'module'
  141. ]);
  142. var keepScriptsMimetypes = utils.createMap([
  143. 'module'
  144. ]);
  145. function isScriptTypeAttribute(attrValue) {
  146. attrValue = trimWhitespace(attrValue.split(/;/, 2)[0]).toLowerCase();
  147. return attrValue === '' || executableScriptsMimetypes(attrValue);
  148. }
  149. function keepScriptTypeAttribute(attrValue) {
  150. attrValue = trimWhitespace(attrValue.split(/;/, 2)[0]).toLowerCase();
  151. return keepScriptsMimetypes(attrValue);
  152. }
  153. function isExecutableScript(tag, attrs) {
  154. if (tag !== 'script') {
  155. return false;
  156. }
  157. for (var i = 0, len = attrs.length; i < len; i++) {
  158. var attrName = attrs[i].name.toLowerCase();
  159. if (attrName === 'type') {
  160. return isScriptTypeAttribute(attrs[i].value);
  161. }
  162. }
  163. return true;
  164. }
  165. function isStyleLinkTypeAttribute(attrValue) {
  166. attrValue = trimWhitespace(attrValue).toLowerCase();
  167. return attrValue === '' || attrValue === 'text/css';
  168. }
  169. function isStyleSheet(tag, attrs) {
  170. if (tag !== 'style') {
  171. return false;
  172. }
  173. for (var i = 0, len = attrs.length; i < len; i++) {
  174. var attrName = attrs[i].name.toLowerCase();
  175. if (attrName === 'type') {
  176. return isStyleLinkTypeAttribute(attrs[i].value);
  177. }
  178. }
  179. return true;
  180. }
  181. var isSimpleBoolean = createMapFromString('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,truespeed,typemustmatch,visible');
  182. var isBooleanValue = createMapFromString('true,false');
  183. function isBooleanAttribute(attrName, attrValue) {
  184. return isSimpleBoolean(attrName) || attrName === 'draggable' && !isBooleanValue(attrValue);
  185. }
  186. function isUriTypeAttribute(attrName, tag) {
  187. return (
  188. /^(?:a|area|link|base)$/.test(tag) && attrName === 'href' ||
  189. tag === 'img' && /^(?:src|longdesc|usemap)$/.test(attrName) ||
  190. tag === 'object' && /^(?:classid|codebase|data|usemap)$/.test(attrName) ||
  191. tag === 'q' && attrName === 'cite' ||
  192. tag === 'blockquote' && attrName === 'cite' ||
  193. (tag === 'ins' || tag === 'del') && attrName === 'cite' ||
  194. tag === 'form' && attrName === 'action' ||
  195. tag === 'input' && (attrName === 'src' || attrName === 'usemap') ||
  196. tag === 'head' && attrName === 'profile' ||
  197. tag === 'script' && (attrName === 'src' || attrName === 'for')
  198. );
  199. }
  200. function isNumberTypeAttribute(attrName, tag) {
  201. return (
  202. /^(?:a|area|object|button)$/.test(tag) && attrName === 'tabindex' ||
  203. tag === 'input' && (attrName === 'maxlength' || attrName === 'tabindex') ||
  204. tag === 'select' && (attrName === 'size' || attrName === 'tabindex') ||
  205. tag === 'textarea' && /^(?:rows|cols|tabindex)$/.test(attrName) ||
  206. tag === 'colgroup' && attrName === 'span' ||
  207. tag === 'col' && attrName === 'span' ||
  208. (tag === 'th' || tag === 'td') && (attrName === 'rowspan' || attrName === 'colspan')
  209. );
  210. }
  211. function isLinkType(tag, attrs, value) {
  212. if (tag !== 'link') {
  213. return false;
  214. }
  215. for (var i = 0, len = attrs.length; i < len; i++) {
  216. if (attrs[i].name === 'rel' && attrs[i].value === value) {
  217. return true;
  218. }
  219. }
  220. }
  221. function isMediaQuery(tag, attrs, attrName) {
  222. return attrName === 'media' && (isLinkType(tag, attrs, 'stylesheet') || isStyleSheet(tag, attrs));
  223. }
  224. var srcsetTags = createMapFromString('img,source');
  225. function isSrcset(attrName, tag) {
  226. return attrName === 'srcset' && srcsetTags(tag);
  227. }
  228. function cleanAttributeValue(tag, attrName, attrValue, options, attrs) {
  229. if (isEventAttribute(attrName, options)) {
  230. attrValue = trimWhitespace(attrValue).replace(/^javascript:\s*/i, '');
  231. return options.minifyJS(attrValue, true);
  232. }
  233. else if (attrName === 'class') {
  234. attrValue = trimWhitespace(attrValue);
  235. if (options.sortClassName) {
  236. attrValue = options.sortClassName(attrValue);
  237. }
  238. else {
  239. attrValue = collapseWhitespaceAll(attrValue);
  240. }
  241. return attrValue;
  242. }
  243. else if (isUriTypeAttribute(attrName, tag)) {
  244. attrValue = trimWhitespace(attrValue);
  245. return isLinkType(tag, attrs, 'canonical') ? attrValue : options.minifyURLs(attrValue);
  246. }
  247. else if (isNumberTypeAttribute(attrName, tag)) {
  248. return trimWhitespace(attrValue);
  249. }
  250. else if (attrName === 'style') {
  251. attrValue = trimWhitespace(attrValue);
  252. if (attrValue) {
  253. if (/;$/.test(attrValue) && !/&#?[0-9a-zA-Z]+;$/.test(attrValue)) {
  254. attrValue = attrValue.replace(/\s*;$/, ';');
  255. }
  256. attrValue = options.minifyCSS(attrValue, 'inline');
  257. }
  258. return attrValue;
  259. }
  260. else if (isSrcset(attrName, tag)) {
  261. // https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset
  262. attrValue = trimWhitespace(attrValue).split(/\s+,\s*|\s*,\s+/).map(function(candidate) {
  263. var url = candidate;
  264. var descriptor = '';
  265. var match = candidate.match(/\s+([1-9][0-9]*w|[0-9]+(?:\.[0-9]+)?x)$/);
  266. if (match) {
  267. url = url.slice(0, -match[0].length);
  268. var num = +match[1].slice(0, -1);
  269. var suffix = match[1].slice(-1);
  270. if (num !== 1 || suffix !== 'x') {
  271. descriptor = ' ' + num + suffix;
  272. }
  273. }
  274. return options.minifyURLs(url) + descriptor;
  275. }).join(', ');
  276. }
  277. else if (isMetaViewport(tag, attrs) && attrName === 'content') {
  278. attrValue = attrValue.replace(/\s+/g, '').replace(/[0-9]+\.[0-9]+/g, function(numString) {
  279. // "0.90000" -> "0.9"
  280. // "1.0" -> "1"
  281. // "1.0001" -> "1.0001" (unchanged)
  282. return (+numString).toString();
  283. });
  284. }
  285. else if (isContentSecurityPolicy(tag, attrs) && attrName.toLowerCase() === 'content') {
  286. return collapseWhitespaceAll(attrValue);
  287. }
  288. else if (options.customAttrCollapse && options.customAttrCollapse.test(attrName)) {
  289. attrValue = attrValue.replace(/\n+|\r+|\s{2,}/g, '');
  290. }
  291. else if (tag === 'script' && attrName === 'type') {
  292. attrValue = trimWhitespace(attrValue.replace(/\s*;\s*/g, ';'));
  293. }
  294. else if (isMediaQuery(tag, attrs, attrName)) {
  295. attrValue = trimWhitespace(attrValue);
  296. return options.minifyCSS(attrValue, 'media');
  297. }
  298. return attrValue;
  299. }
  300. function isMetaViewport(tag, attrs) {
  301. if (tag !== 'meta') {
  302. return false;
  303. }
  304. for (var i = 0, len = attrs.length; i < len; i++) {
  305. if (attrs[i].name === 'name' && attrs[i].value === 'viewport') {
  306. return true;
  307. }
  308. }
  309. }
  310. function isContentSecurityPolicy(tag, attrs) {
  311. if (tag !== 'meta') {
  312. return false;
  313. }
  314. for (var i = 0, len = attrs.length; i < len; i++) {
  315. if (attrs[i].name.toLowerCase() === 'http-equiv' && attrs[i].value.toLowerCase() === 'content-security-policy') {
  316. return true;
  317. }
  318. }
  319. }
  320. function ignoreCSS(id) {
  321. return '/* clean-css ignore:start */' + id + '/* clean-css ignore:end */';
  322. }
  323. // Wrap CSS declarations for CleanCSS > 3.x
  324. // See https://github.com/jakubpawlowicz/clean-css/issues/418
  325. function wrapCSS(text, type) {
  326. switch (type) {
  327. case 'inline':
  328. return '*{' + text + '}';
  329. case 'media':
  330. return '@media ' + text + '{a{top:0}}';
  331. default:
  332. return text;
  333. }
  334. }
  335. function unwrapCSS(text, type) {
  336. var matches;
  337. switch (type) {
  338. case 'inline':
  339. matches = text.match(/^\*\{([\s\S]*)\}$/);
  340. break;
  341. case 'media':
  342. matches = text.match(/^@media ([\s\S]*?)\s*{[\s\S]*}$/);
  343. break;
  344. }
  345. return matches ? matches[1] : text;
  346. }
  347. function cleanConditionalComment(comment, options) {
  348. return options.processConditionalComments ? comment.replace(/^(\[if\s[^\]]+]>)([\s\S]*?)(<!\[endif])$/, function(match, prefix, text, suffix) {
  349. return prefix + minify(text, options, true) + suffix;
  350. }) : comment;
  351. }
  352. function processScript(text, options, currentAttrs) {
  353. for (var i = 0, len = currentAttrs.length; i < len; i++) {
  354. if (currentAttrs[i].name.toLowerCase() === 'type' &&
  355. options.processScripts.indexOf(currentAttrs[i].value) > -1) {
  356. return minify(text, options);
  357. }
  358. }
  359. return text;
  360. }
  361. // Tag omission rules from https://html.spec.whatwg.org/multipage/syntax.html#optional-tags
  362. // with the following deviations:
  363. // - retain <body> if followed by <noscript>
  364. // - </rb>, </rt>, </rtc>, </rp> & </tfoot> follow https://www.w3.org/TR/html5/syntax.html#optional-tags
  365. // - retain all tags which are adjacent to non-standard HTML tags
  366. var optionalStartTags = createMapFromString('html,head,body,colgroup,tbody');
  367. var optionalEndTags = createMapFromString('html,head,body,li,dt,dd,p,rb,rt,rtc,rp,optgroup,option,colgroup,caption,thead,tbody,tfoot,tr,td,th');
  368. var headerTags = createMapFromString('meta,link,script,style,template,noscript');
  369. var descriptionTags = createMapFromString('dt,dd');
  370. var pBlockTags = createMapFromString('address,article,aside,blockquote,details,div,dl,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,hr,main,menu,nav,ol,p,pre,section,table,ul');
  371. var pInlineTags = createMapFromString('a,audio,del,ins,map,noscript,video');
  372. var rubyTags = createMapFromString('rb,rt,rtc,rp');
  373. var rtcTag = createMapFromString('rb,rtc,rp');
  374. var optionTag = createMapFromString('option,optgroup');
  375. var tableContentTags = createMapFromString('tbody,tfoot');
  376. var tableSectionTags = createMapFromString('thead,tbody,tfoot');
  377. var cellTags = createMapFromString('td,th');
  378. var topLevelTags = createMapFromString('html,head,body');
  379. var compactTags = createMapFromString('html,body');
  380. var looseTags = createMapFromString('head,colgroup,caption');
  381. var trailingTags = createMapFromString('dt,thead');
  382. 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');
  383. function canRemoveParentTag(optionalStartTag, tag) {
  384. switch (optionalStartTag) {
  385. case 'html':
  386. case 'head':
  387. return true;
  388. case 'body':
  389. return !headerTags(tag);
  390. case 'colgroup':
  391. return tag === 'col';
  392. case 'tbody':
  393. return tag === 'tr';
  394. }
  395. return false;
  396. }
  397. function isStartTagMandatory(optionalEndTag, tag) {
  398. switch (tag) {
  399. case 'colgroup':
  400. return optionalEndTag === 'colgroup';
  401. case 'tbody':
  402. return tableSectionTags(optionalEndTag);
  403. }
  404. return false;
  405. }
  406. function canRemovePrecedingTag(optionalEndTag, tag) {
  407. switch (optionalEndTag) {
  408. case 'html':
  409. case 'head':
  410. case 'body':
  411. case 'colgroup':
  412. case 'caption':
  413. return true;
  414. case 'li':
  415. case 'optgroup':
  416. case 'tr':
  417. return tag === optionalEndTag;
  418. case 'dt':
  419. case 'dd':
  420. return descriptionTags(tag);
  421. case 'p':
  422. return pBlockTags(tag);
  423. case 'rb':
  424. case 'rt':
  425. case 'rp':
  426. return rubyTags(tag);
  427. case 'rtc':
  428. return rtcTag(tag);
  429. case 'option':
  430. return optionTag(tag);
  431. case 'thead':
  432. case 'tbody':
  433. return tableContentTags(tag);
  434. case 'tfoot':
  435. return tag === 'tbody';
  436. case 'td':
  437. case 'th':
  438. return cellTags(tag);
  439. }
  440. return false;
  441. }
  442. var reEmptyAttribute = new RegExp(
  443. '^(?:class|id|style|title|lang|dir|on(?:focus|blur|change|click|dblclick|mouse(' +
  444. '?:down|up|over|move|out)|key(?:press|down|up)))$');
  445. function canDeleteEmptyAttribute(tag, attrName, attrValue, options) {
  446. var isValueEmpty = !attrValue || /^\s*$/.test(attrValue);
  447. if (!isValueEmpty) {
  448. return false;
  449. }
  450. if (typeof options.removeEmptyAttributes === 'function') {
  451. return options.removeEmptyAttributes(attrName, tag);
  452. }
  453. return tag === 'input' && attrName === 'value' || reEmptyAttribute.test(attrName);
  454. }
  455. function hasAttrName(name, attrs) {
  456. for (var i = attrs.length - 1; i >= 0; i--) {
  457. if (attrs[i].name === name) {
  458. return true;
  459. }
  460. }
  461. return false;
  462. }
  463. function canRemoveElement(tag, attrs) {
  464. switch (tag) {
  465. case 'textarea':
  466. return false;
  467. case 'audio':
  468. case 'script':
  469. case 'video':
  470. if (hasAttrName('src', attrs)) {
  471. return false;
  472. }
  473. break;
  474. case 'iframe':
  475. if (hasAttrName('src', attrs) || hasAttrName('srcdoc', attrs)) {
  476. return false;
  477. }
  478. break;
  479. case 'object':
  480. if (hasAttrName('data', attrs)) {
  481. return false;
  482. }
  483. break;
  484. case 'applet':
  485. if (hasAttrName('code', attrs)) {
  486. return false;
  487. }
  488. break;
  489. }
  490. return true;
  491. }
  492. function canCollapseWhitespace(tag) {
  493. return !/^(?:script|style|pre|textarea)$/.test(tag);
  494. }
  495. function canTrimWhitespace(tag) {
  496. return !/^(?:pre|textarea)$/.test(tag);
  497. }
  498. function normalizeAttr(attr, attrs, tag, options) {
  499. var attrName = options.name(attr.name),
  500. attrValue = attr.value;
  501. if (options.decodeEntities && attrValue) {
  502. attrValue = decode(attrValue, { isAttributeValue: true });
  503. }
  504. if (options.removeRedundantAttributes &&
  505. isAttributeRedundant(tag, attrName, attrValue, attrs) ||
  506. options.removeScriptTypeAttributes && tag === 'script' &&
  507. attrName === 'type' && isScriptTypeAttribute(attrValue) && !keepScriptTypeAttribute(attrValue) ||
  508. options.removeStyleLinkTypeAttributes && (tag === 'style' || tag === 'link') &&
  509. attrName === 'type' && isStyleLinkTypeAttribute(attrValue)) {
  510. return;
  511. }
  512. if (attrValue) {
  513. attrValue = cleanAttributeValue(tag, attrName, attrValue, options, attrs);
  514. }
  515. if (options.removeEmptyAttributes &&
  516. canDeleteEmptyAttribute(tag, attrName, attrValue, options)) {
  517. return;
  518. }
  519. if (options.decodeEntities && attrValue) {
  520. attrValue = attrValue.replace(/&(#?[0-9a-zA-Z]+;)/g, '&amp;$1');
  521. }
  522. return {
  523. attr: attr,
  524. name: attrName,
  525. value: attrValue
  526. };
  527. }
  528. function buildAttr(normalized, hasUnarySlash, options, isLast, uidAttr) {
  529. var attrName = normalized.name,
  530. attrValue = normalized.value,
  531. attr = normalized.attr,
  532. attrQuote = attr.quote,
  533. attrFragment,
  534. emittedAttrValue;
  535. if (typeof attrValue !== 'undefined' && (!options.removeAttributeQuotes ||
  536. ~attrValue.indexOf(uidAttr) || !canRemoveAttributeQuotes(attrValue))) {
  537. if (!options.preventAttributesEscaping) {
  538. if (typeof options.quoteCharacter === 'undefined') {
  539. var apos = (attrValue.match(/'/g) || []).length;
  540. var quot = (attrValue.match(/"/g) || []).length;
  541. attrQuote = apos < quot ? '\'' : '"';
  542. }
  543. else {
  544. attrQuote = options.quoteCharacter === '\'' ? '\'' : '"';
  545. }
  546. if (attrQuote === '"') {
  547. attrValue = attrValue.replace(/"/g, '&#34;');
  548. }
  549. else {
  550. attrValue = attrValue.replace(/'/g, '&#39;');
  551. }
  552. }
  553. emittedAttrValue = attrQuote + attrValue + attrQuote;
  554. if (!isLast && !options.removeTagWhitespace) {
  555. emittedAttrValue += ' ';
  556. }
  557. }
  558. // make sure trailing slash is not interpreted as HTML self-closing tag
  559. else if (isLast && !hasUnarySlash && !/\/$/.test(attrValue)) {
  560. emittedAttrValue = attrValue;
  561. }
  562. else {
  563. emittedAttrValue = attrValue + ' ';
  564. }
  565. if (typeof attrValue === 'undefined' || options.collapseBooleanAttributes &&
  566. isBooleanAttribute(attrName.toLowerCase(), attrValue.toLowerCase())) {
  567. attrFragment = attrName;
  568. if (!isLast) {
  569. attrFragment += ' ';
  570. }
  571. }
  572. else {
  573. attrFragment = attrName + attr.customAssign + emittedAttrValue;
  574. }
  575. return attr.customOpen + attrFragment + attr.customClose;
  576. }
  577. function identity(value) {
  578. return value;
  579. }
  580. function processOptions(values) {
  581. var options = {
  582. name: function(name) {
  583. return name.toLowerCase();
  584. },
  585. canCollapseWhitespace: canCollapseWhitespace,
  586. canTrimWhitespace: canTrimWhitespace,
  587. html5: true,
  588. ignoreCustomComments: [
  589. /^!/,
  590. /^\s*#/
  591. ],
  592. ignoreCustomFragments: [
  593. /<%[\s\S]*?%>/,
  594. /<\?[\s\S]*?\?>/
  595. ],
  596. includeAutoGeneratedTags: true,
  597. log: identity,
  598. minifyCSS: identity,
  599. minifyJS: identity,
  600. minifyURLs: identity
  601. };
  602. Object.keys(values).forEach(function(key) {
  603. var value = values[key];
  604. if (key === 'caseSensitive') {
  605. if (value) {
  606. options.name = identity;
  607. }
  608. }
  609. else if (key === 'log') {
  610. if (typeof value === 'function') {
  611. options.log = value;
  612. }
  613. }
  614. else if (key === 'minifyCSS' && typeof value !== 'function') {
  615. if (!value) {
  616. return;
  617. }
  618. if (typeof value !== 'object') {
  619. value = {};
  620. }
  621. options.minifyCSS = function(text, type) {
  622. text = text.replace(/(url\s*\(\s*)("|'|)(.*?)\2(\s*\))/ig, function(match, prefix, quote, url, suffix) {
  623. return prefix + quote + options.minifyURLs(url) + quote + suffix;
  624. });
  625. var cleanCssOutput = new CleanCSS(value).minify(wrapCSS(text, type));
  626. if (cleanCssOutput.errors.length > 0) {
  627. cleanCssOutput.errors.forEach(options.log);
  628. return text;
  629. }
  630. return unwrapCSS(cleanCssOutput.styles, type);
  631. };
  632. }
  633. else if (key === 'minifyJS' && typeof value !== 'function') {
  634. if (!value) {
  635. return;
  636. }
  637. if (typeof value !== 'object') {
  638. value = {};
  639. }
  640. (value.parse || (value.parse = {})).bare_returns = false;
  641. options.minifyJS = function(text, inline) {
  642. var start = text.match(/^\s*<!--.*/);
  643. var code = start ? text.slice(start[0].length).replace(/\n\s*-->\s*$/, '') : text;
  644. value.parse.bare_returns = inline;
  645. var result = Terser.minify(code, value);
  646. if (result.error) {
  647. options.log(result.error);
  648. return text;
  649. }
  650. return result.code.replace(/;$/, '');
  651. };
  652. }
  653. else if (key === 'minifyURLs' && typeof value !== 'function') {
  654. if (!value) {
  655. return;
  656. }
  657. if (typeof value === 'string') {
  658. value = { site: value };
  659. }
  660. else if (typeof value !== 'object') {
  661. value = {};
  662. }
  663. options.minifyURLs = function(text) {
  664. try {
  665. return RelateUrl.relate(text, value);
  666. }
  667. catch (err) {
  668. options.log(err);
  669. return text;
  670. }
  671. };
  672. }
  673. else {
  674. options[key] = value;
  675. }
  676. });
  677. return options;
  678. }
  679. function uniqueId(value) {
  680. var id;
  681. do {
  682. id = Math.random().toString(36).replace(/^0\.[0-9]*/, '');
  683. } while (~value.indexOf(id));
  684. return id;
  685. }
  686. var specialContentTags = createMapFromString('script,style');
  687. function createSortFns(value, options, uidIgnore, uidAttr) {
  688. var attrChains = options.sortAttributes && Object.create(null);
  689. var classChain = options.sortClassName && new TokenChain();
  690. function attrNames(attrs) {
  691. return attrs.map(function(attr) {
  692. return options.name(attr.name);
  693. });
  694. }
  695. function shouldSkipUID(token, uid) {
  696. return !uid || token.indexOf(uid) === -1;
  697. }
  698. function shouldSkipUIDs(token) {
  699. return shouldSkipUID(token, uidIgnore) && shouldSkipUID(token, uidAttr);
  700. }
  701. function scan(input) {
  702. var currentTag, currentType;
  703. new HTMLParser(input, {
  704. start: function(tag, attrs) {
  705. if (attrChains) {
  706. if (!attrChains[tag]) {
  707. attrChains[tag] = new TokenChain();
  708. }
  709. attrChains[tag].add(attrNames(attrs).filter(shouldSkipUIDs));
  710. }
  711. for (var i = 0, len = attrs.length; i < len; i++) {
  712. var attr = attrs[i];
  713. if (classChain && attr.value && options.name(attr.name) === 'class') {
  714. classChain.add(trimWhitespace(attr.value).split(/[ \t\n\f\r]+/).filter(shouldSkipUIDs));
  715. }
  716. else if (options.processScripts && attr.name.toLowerCase() === 'type') {
  717. currentTag = tag;
  718. currentType = attr.value;
  719. }
  720. }
  721. },
  722. end: function() {
  723. currentTag = '';
  724. },
  725. chars: function(text) {
  726. if (options.processScripts && specialContentTags(currentTag) &&
  727. options.processScripts.indexOf(currentType) > -1) {
  728. scan(text);
  729. }
  730. }
  731. });
  732. }
  733. var log = options.log;
  734. options.log = identity;
  735. options.sortAttributes = false;
  736. options.sortClassName = false;
  737. scan(minify(value, options));
  738. options.log = log;
  739. if (attrChains) {
  740. var attrSorters = Object.create(null);
  741. for (var tag in attrChains) {
  742. attrSorters[tag] = attrChains[tag].createSorter();
  743. }
  744. options.sortAttributes = function(tag, attrs) {
  745. var sorter = attrSorters[tag];
  746. if (sorter) {
  747. var attrMap = Object.create(null);
  748. var names = attrNames(attrs);
  749. names.forEach(function(name, index) {
  750. (attrMap[name] || (attrMap[name] = [])).push(attrs[index]);
  751. });
  752. sorter.sort(names).forEach(function(name, index) {
  753. attrs[index] = attrMap[name].shift();
  754. });
  755. }
  756. };
  757. }
  758. if (classChain) {
  759. var sorter = classChain.createSorter();
  760. options.sortClassName = function(value) {
  761. return sorter.sort(value.split(/[ \n\f\r]+/)).join(' ');
  762. };
  763. }
  764. }
  765. function minify(value, options, partialMarkup) {
  766. if (options.collapseWhitespace) {
  767. value = collapseWhitespace(value, options, true, true);
  768. }
  769. var buffer = [],
  770. charsPrevTag,
  771. currentChars = '',
  772. hasChars,
  773. currentTag = '',
  774. currentAttrs = [],
  775. stackNoTrimWhitespace = [],
  776. stackNoCollapseWhitespace = [],
  777. optionalStartTag = '',
  778. optionalEndTag = '',
  779. ignoredMarkupChunks = [],
  780. ignoredCustomMarkupChunks = [],
  781. uidIgnore,
  782. uidAttr,
  783. uidPattern;
  784. // temporarily replace ignored chunks with comments,
  785. // so that we don't have to worry what's there.
  786. // for all we care there might be
  787. // completely-horribly-broken-alien-non-html-emoj-cthulhu-filled content
  788. value = value.replace(/<!-- htmlmin:ignore -->([\s\S]*?)<!-- htmlmin:ignore -->/g, function(match, group1) {
  789. if (!uidIgnore) {
  790. uidIgnore = uniqueId(value);
  791. var pattern = new RegExp('^' + uidIgnore + '([0-9]+)$');
  792. if (options.ignoreCustomComments) {
  793. options.ignoreCustomComments = options.ignoreCustomComments.slice();
  794. }
  795. else {
  796. options.ignoreCustomComments = [];
  797. }
  798. options.ignoreCustomComments.push(pattern);
  799. }
  800. var token = '<!--' + uidIgnore + ignoredMarkupChunks.length + '-->';
  801. ignoredMarkupChunks.push(group1);
  802. return token;
  803. });
  804. var customFragments = options.ignoreCustomFragments.map(function(re) {
  805. return re.source;
  806. });
  807. if (customFragments.length) {
  808. var reCustomIgnore = new RegExp('\\s*(?:' + customFragments.join('|') + ')+\\s*', 'g');
  809. // temporarily replace custom ignored fragments with unique attributes
  810. value = value.replace(reCustomIgnore, function(match) {
  811. if (!uidAttr) {
  812. uidAttr = uniqueId(value);
  813. uidPattern = new RegExp('(\\s*)' + uidAttr + '([0-9]+)' + uidAttr + '(\\s*)', 'g');
  814. if (options.minifyCSS) {
  815. options.minifyCSS = (function(fn) {
  816. return function(text, type) {
  817. text = text.replace(uidPattern, function(match, prefix, index) {
  818. var chunks = ignoredCustomMarkupChunks[+index];
  819. return chunks[1] + uidAttr + index + uidAttr + chunks[2];
  820. });
  821. var ids = [];
  822. new CleanCSS().minify(wrapCSS(text, type)).warnings.forEach(function(warning) {
  823. var match = uidPattern.exec(warning);
  824. if (match) {
  825. var id = uidAttr + match[2] + uidAttr;
  826. text = text.replace(id, ignoreCSS(id));
  827. ids.push(id);
  828. }
  829. });
  830. text = fn(text, type);
  831. ids.forEach(function(id) {
  832. text = text.replace(ignoreCSS(id), id);
  833. });
  834. return text;
  835. };
  836. })(options.minifyCSS);
  837. }
  838. if (options.minifyJS) {
  839. options.minifyJS = (function(fn) {
  840. return function(text, type) {
  841. return fn(text.replace(uidPattern, function(match, prefix, index) {
  842. var chunks = ignoredCustomMarkupChunks[+index];
  843. return chunks[1] + uidAttr + index + uidAttr + chunks[2];
  844. }), type);
  845. };
  846. })(options.minifyJS);
  847. }
  848. }
  849. var token = uidAttr + ignoredCustomMarkupChunks.length + uidAttr;
  850. ignoredCustomMarkupChunks.push(/^(\s*)[\s\S]*?(\s*)$/.exec(match));
  851. return '\t' + token + '\t';
  852. });
  853. }
  854. if (options.sortAttributes && typeof options.sortAttributes !== 'function' ||
  855. options.sortClassName && typeof options.sortClassName !== 'function') {
  856. createSortFns(value, options, uidIgnore, uidAttr);
  857. }
  858. function _canCollapseWhitespace(tag, attrs) {
  859. return options.canCollapseWhitespace(tag, attrs, canCollapseWhitespace);
  860. }
  861. function _canTrimWhitespace(tag, attrs) {
  862. return options.canTrimWhitespace(tag, attrs, canTrimWhitespace);
  863. }
  864. function removeStartTag() {
  865. var index = buffer.length - 1;
  866. while (index > 0 && !/^<[^/!]/.test(buffer[index])) {
  867. index--;
  868. }
  869. buffer.length = Math.max(0, index);
  870. }
  871. function removeEndTag() {
  872. var index = buffer.length - 1;
  873. while (index > 0 && !/^<\//.test(buffer[index])) {
  874. index--;
  875. }
  876. buffer.length = Math.max(0, index);
  877. }
  878. // look for trailing whitespaces, bypass any inline tags
  879. function trimTrailingWhitespace(index, nextTag) {
  880. for (var endTag = null; index >= 0 && _canTrimWhitespace(endTag); index--) {
  881. var str = buffer[index];
  882. var match = str.match(/^<\/([\w:-]+)>$/);
  883. if (match) {
  884. endTag = match[1];
  885. }
  886. else if (/>$/.test(str) || (buffer[index] = collapseWhitespaceSmart(str, null, nextTag, options))) {
  887. break;
  888. }
  889. }
  890. }
  891. // look for trailing whitespaces from previously processed text
  892. // which may not be trimmed due to a following comment or an empty
  893. // element which has now been removed
  894. function squashTrailingWhitespace(nextTag) {
  895. var charsIndex = buffer.length - 1;
  896. if (buffer.length > 1) {
  897. var item = buffer[buffer.length - 1];
  898. if (/^(?:<!|$)/.test(item) && item.indexOf(uidIgnore) === -1) {
  899. charsIndex--;
  900. }
  901. }
  902. trimTrailingWhitespace(charsIndex, nextTag);
  903. }
  904. new HTMLParser(value, {
  905. partialMarkup: partialMarkup,
  906. continueOnParseError: options.continueOnParseError,
  907. customAttrAssign: options.customAttrAssign,
  908. customAttrSurround: options.customAttrSurround,
  909. html5: options.html5,
  910. start: function(tag, attrs, unary, unarySlash, autoGenerated) {
  911. if (tag.toLowerCase() === 'svg') {
  912. options = Object.create(options);
  913. options.caseSensitive = true;
  914. options.keepClosingSlash = true;
  915. options.name = identity;
  916. }
  917. tag = options.name(tag);
  918. currentTag = tag;
  919. charsPrevTag = tag;
  920. if (!inlineTextTags(tag)) {
  921. currentChars = '';
  922. }
  923. hasChars = false;
  924. currentAttrs = attrs;
  925. var optional = options.removeOptionalTags;
  926. if (optional) {
  927. var htmlTag = htmlTags(tag);
  928. // <html> may be omitted if first thing inside is not comment
  929. // <head> may be omitted if first thing inside is an element
  930. // <body> may be omitted if first thing inside is not space, comment, <meta>, <link>, <script>, <style> or <template>
  931. // <colgroup> may be omitted if first thing inside is <col>
  932. // <tbody> may be omitted if first thing inside is <tr>
  933. if (htmlTag && canRemoveParentTag(optionalStartTag, tag)) {
  934. removeStartTag();
  935. }
  936. optionalStartTag = '';
  937. // end-tag-followed-by-start-tag omission rules
  938. if (htmlTag && canRemovePrecedingTag(optionalEndTag, tag)) {
  939. removeEndTag();
  940. // <colgroup> cannot be omitted if preceding </colgroup> is omitted
  941. // <tbody> cannot be omitted if preceding </tbody>, </thead> or </tfoot> is omitted
  942. optional = !isStartTagMandatory(optionalEndTag, tag);
  943. }
  944. optionalEndTag = '';
  945. }
  946. // set whitespace flags for nested tags (eg. <code> within a <pre>)
  947. if (options.collapseWhitespace) {
  948. if (!stackNoTrimWhitespace.length) {
  949. squashTrailingWhitespace(tag);
  950. }
  951. if (!unary) {
  952. if (!_canTrimWhitespace(tag, attrs) || stackNoTrimWhitespace.length) {
  953. stackNoTrimWhitespace.push(tag);
  954. }
  955. if (!_canCollapseWhitespace(tag, attrs) || stackNoCollapseWhitespace.length) {
  956. stackNoCollapseWhitespace.push(tag);
  957. }
  958. }
  959. }
  960. var openTag = '<' + tag;
  961. var hasUnarySlash = unarySlash && options.keepClosingSlash;
  962. buffer.push(openTag);
  963. if (options.sortAttributes) {
  964. options.sortAttributes(tag, attrs);
  965. }
  966. var parts = [];
  967. for (var i = attrs.length, isLast = true; --i >= 0;) {
  968. var normalized = normalizeAttr(attrs[i], attrs, tag, options);
  969. if (normalized) {
  970. parts.unshift(buildAttr(normalized, hasUnarySlash, options, isLast, uidAttr));
  971. isLast = false;
  972. }
  973. }
  974. if (parts.length > 0) {
  975. buffer.push(' ');
  976. buffer.push.apply(buffer, parts);
  977. }
  978. // start tag must never be omitted if it has any attributes
  979. else if (optional && optionalStartTags(tag)) {
  980. optionalStartTag = tag;
  981. }
  982. buffer.push(buffer.pop() + (hasUnarySlash ? '/' : '') + '>');
  983. if (autoGenerated && !options.includeAutoGeneratedTags) {
  984. removeStartTag();
  985. optionalStartTag = '';
  986. }
  987. },
  988. end: function(tag, attrs, autoGenerated) {
  989. if (tag.toLowerCase() === 'svg') {
  990. options = Object.getPrototypeOf(options);
  991. }
  992. tag = options.name(tag);
  993. // check if current tag is in a whitespace stack
  994. if (options.collapseWhitespace) {
  995. if (stackNoTrimWhitespace.length) {
  996. if (tag === stackNoTrimWhitespace[stackNoTrimWhitespace.length - 1]) {
  997. stackNoTrimWhitespace.pop();
  998. }
  999. }
  1000. else {
  1001. squashTrailingWhitespace('/' + tag);
  1002. }
  1003. if (stackNoCollapseWhitespace.length &&
  1004. tag === stackNoCollapseWhitespace[stackNoCollapseWhitespace.length - 1]) {
  1005. stackNoCollapseWhitespace.pop();
  1006. }
  1007. }
  1008. var isElementEmpty = false;
  1009. if (tag === currentTag) {
  1010. currentTag = '';
  1011. isElementEmpty = !hasChars;
  1012. }
  1013. if (options.removeOptionalTags) {
  1014. // <html>, <head> or <body> may be omitted if the element is empty
  1015. if (isElementEmpty && topLevelTags(optionalStartTag)) {
  1016. removeStartTag();
  1017. }
  1018. optionalStartTag = '';
  1019. // </html> or </body> may be omitted if not followed by comment
  1020. // </head> may be omitted if not followed by space or comment
  1021. // </p> may be omitted if no more content in non-</a> parent
  1022. // except for </dt> or </thead>, end tags may be omitted if no more content in parent element
  1023. if (htmlTags(tag) && optionalEndTag && !trailingTags(optionalEndTag) && (optionalEndTag !== 'p' || !pInlineTags(tag))) {
  1024. removeEndTag();
  1025. }
  1026. optionalEndTag = optionalEndTags(tag) ? tag : '';
  1027. }
  1028. if (options.removeEmptyElements && isElementEmpty && canRemoveElement(tag, attrs)) {
  1029. // remove last "element" from buffer
  1030. removeStartTag();
  1031. optionalStartTag = '';
  1032. optionalEndTag = '';
  1033. }
  1034. else {
  1035. if (autoGenerated && !options.includeAutoGeneratedTags) {
  1036. optionalEndTag = '';
  1037. }
  1038. else {
  1039. buffer.push('</' + tag + '>');
  1040. }
  1041. charsPrevTag = '/' + tag;
  1042. if (!inlineTags(tag)) {
  1043. currentChars = '';
  1044. }
  1045. else if (isElementEmpty) {
  1046. currentChars += '|';
  1047. }
  1048. }
  1049. },
  1050. chars: function(text, prevTag, nextTag) {
  1051. prevTag = prevTag === '' ? 'comment' : prevTag;
  1052. nextTag = nextTag === '' ? 'comment' : nextTag;
  1053. if (options.decodeEntities && text && !specialContentTags(currentTag)) {
  1054. text = decode(text);
  1055. }
  1056. if (options.collapseWhitespace) {
  1057. if (!stackNoTrimWhitespace.length) {
  1058. if (prevTag === 'comment') {
  1059. var prevComment = buffer[buffer.length - 1];
  1060. if (prevComment.indexOf(uidIgnore) === -1) {
  1061. if (!prevComment) {
  1062. prevTag = charsPrevTag;
  1063. }
  1064. if (buffer.length > 1 && (!prevComment || !options.conservativeCollapse && / $/.test(currentChars))) {
  1065. var charsIndex = buffer.length - 2;
  1066. buffer[charsIndex] = buffer[charsIndex].replace(/\s+$/, function(trailingSpaces) {
  1067. text = trailingSpaces + text;
  1068. return '';
  1069. });
  1070. }
  1071. }
  1072. }
  1073. if (prevTag) {
  1074. if (prevTag === '/nobr' || prevTag === 'wbr') {
  1075. if (/^\s/.test(text)) {
  1076. var tagIndex = buffer.length - 1;
  1077. while (tagIndex > 0 && buffer[tagIndex].lastIndexOf('<' + prevTag) !== 0) {
  1078. tagIndex--;
  1079. }
  1080. trimTrailingWhitespace(tagIndex - 1, 'br');
  1081. }
  1082. }
  1083. else if (inlineTextTags(prevTag.charAt(0) === '/' ? prevTag.slice(1) : prevTag)) {
  1084. text = collapseWhitespace(text, options, /(?:^|\s)$/.test(currentChars));
  1085. }
  1086. }
  1087. if (prevTag || nextTag) {
  1088. text = collapseWhitespaceSmart(text, prevTag, nextTag, options);
  1089. }
  1090. else {
  1091. text = collapseWhitespace(text, options, true, true);
  1092. }
  1093. if (!text && /\s$/.test(currentChars) && prevTag && prevTag.charAt(0) === '/') {
  1094. trimTrailingWhitespace(buffer.length - 1, nextTag);
  1095. }
  1096. }
  1097. if (!stackNoCollapseWhitespace.length && nextTag !== 'html' && !(prevTag && nextTag)) {
  1098. text = collapseWhitespace(text, options, false, false, true);
  1099. }
  1100. }
  1101. if (options.processScripts && specialContentTags(currentTag)) {
  1102. text = processScript(text, options, currentAttrs);
  1103. }
  1104. if (isExecutableScript(currentTag, currentAttrs)) {
  1105. text = options.minifyJS(text);
  1106. }
  1107. if (isStyleSheet(currentTag, currentAttrs)) {
  1108. text = options.minifyCSS(text);
  1109. }
  1110. if (options.removeOptionalTags && text) {
  1111. // <html> may be omitted if first thing inside is not comment
  1112. // <body> may be omitted if first thing inside is not space, comment, <meta>, <link>, <script>, <style> or <template>
  1113. if (optionalStartTag === 'html' || optionalStartTag === 'body' && !/^\s/.test(text)) {
  1114. removeStartTag();
  1115. }
  1116. optionalStartTag = '';
  1117. // </html> or </body> may be omitted if not followed by comment
  1118. // </head>, </colgroup> or </caption> may be omitted if not followed by space or comment
  1119. if (compactTags(optionalEndTag) || looseTags(optionalEndTag) && !/^\s/.test(text)) {
  1120. removeEndTag();
  1121. }
  1122. optionalEndTag = '';
  1123. }
  1124. charsPrevTag = /^\s*$/.test(text) ? prevTag : 'comment';
  1125. if (options.decodeEntities && text && !specialContentTags(currentTag)) {
  1126. // Escape any `&` symbols that start either:
  1127. // 1) a legacy named character reference (i.e. one that doesn't end with `;`)
  1128. // 2) or any other character reference (i.e. one that does end with `;`)
  1129. // Note that `&` can be escaped as `&amp`, without the semi-colon.
  1130. // https://mathiasbynens.be/notes/ambiguous-ampersands
  1131. text = text.replace(/&((?:Iacute|aacute|uacute|plusmn|Otilde|otilde|agrave|Agrave|Yacute|yacute|Oslash|oslash|atilde|Atilde|brvbar|ccedil|Ccedil|Ograve|curren|divide|eacute|Eacute|ograve|Oacute|egrave|Egrave|Ugrave|frac12|frac14|frac34|ugrave|oacute|iacute|Ntilde|ntilde|Uacute|middot|igrave|Igrave|iquest|Aacute|cedil|laquo|micro|iexcl|Icirc|icirc|acirc|Ucirc|Ecirc|ocirc|Ocirc|ecirc|ucirc|Aring|aring|AElig|aelig|acute|pound|raquo|Acirc|times|THORN|szlig|thorn|COPY|auml|ordf|ordm|Uuml|macr|uuml|Auml|ouml|Ouml|para|nbsp|euml|quot|QUOT|Euml|yuml|cent|sect|copy|sup1|sup2|sup3|iuml|Iuml|ETH|shy|reg|not|yen|amp|AMP|REG|uml|eth|deg|gt|GT|LT|lt)(?!;)|(?:#?[0-9a-zA-Z]+;))/g, '&amp$1').replace(/</g, '&lt;');
  1132. }
  1133. if (uidPattern && options.collapseWhitespace && stackNoTrimWhitespace.length) {
  1134. text = text.replace(uidPattern, function(match, prefix, index) {
  1135. return ignoredCustomMarkupChunks[+index][0];
  1136. });
  1137. }
  1138. currentChars += text;
  1139. if (text) {
  1140. hasChars = true;
  1141. }
  1142. buffer.push(text);
  1143. },
  1144. comment: function(text, nonStandard) {
  1145. var prefix = nonStandard ? '<!' : '<!--';
  1146. var suffix = nonStandard ? '>' : '-->';
  1147. if (isConditionalComment(text)) {
  1148. text = prefix + cleanConditionalComment(text, options) + suffix;
  1149. }
  1150. else if (options.removeComments) {
  1151. if (isIgnoredComment(text, options)) {
  1152. text = '<!--' + text + '-->';
  1153. }
  1154. else {
  1155. text = '';
  1156. }
  1157. }
  1158. else {
  1159. text = prefix + text + suffix;
  1160. }
  1161. if (options.removeOptionalTags && text) {
  1162. // preceding comments suppress tag omissions
  1163. optionalStartTag = '';
  1164. optionalEndTag = '';
  1165. }
  1166. buffer.push(text);
  1167. },
  1168. doctype: function(doctype) {
  1169. buffer.push(options.useShortDoctype ? '<!doctype' +
  1170. (options.removeTagWhitespace ? '' : ' ') + 'html>' :
  1171. collapseWhitespaceAll(doctype));
  1172. }
  1173. });
  1174. if (options.removeOptionalTags) {
  1175. // <html> may be omitted if first thing inside is not comment
  1176. // <head> or <body> may be omitted if empty
  1177. if (topLevelTags(optionalStartTag)) {
  1178. removeStartTag();
  1179. }
  1180. // except for </dt> or </thead>, end tags may be omitted if no more content in parent element
  1181. if (optionalEndTag && !trailingTags(optionalEndTag)) {
  1182. removeEndTag();
  1183. }
  1184. }
  1185. if (options.collapseWhitespace) {
  1186. squashTrailingWhitespace('br');
  1187. }
  1188. return joinResultSegments(buffer, options, uidPattern ? function(str) {
  1189. return str.replace(uidPattern, function(match, prefix, index, suffix) {
  1190. var chunk = ignoredCustomMarkupChunks[+index][0];
  1191. if (options.collapseWhitespace) {
  1192. if (prefix !== '\t') {
  1193. chunk = prefix + chunk;
  1194. }
  1195. if (suffix !== '\t') {
  1196. chunk += suffix;
  1197. }
  1198. return collapseWhitespace(chunk, {
  1199. preserveLineBreaks: options.preserveLineBreaks,
  1200. conservativeCollapse: !options.trimCustomFragments
  1201. }, /^[ \n\r\t\f]/.test(chunk), /[ \n\r\t\f]$/.test(chunk));
  1202. }
  1203. return chunk;
  1204. });
  1205. } : identity, uidIgnore ? function(str) {
  1206. return str.replace(new RegExp('<!--' + uidIgnore + '([0-9]+)-->', 'g'), function(match, index) {
  1207. return ignoredMarkupChunks[+index];
  1208. });
  1209. } : identity);
  1210. }
  1211. function joinResultSegments(results, options, restoreCustom, restoreIgnore) {
  1212. var str;
  1213. var maxLineLength = options.maxLineLength;
  1214. if (maxLineLength) {
  1215. var line = '', lines = [];
  1216. while (results.length) {
  1217. var len = line.length;
  1218. var end = results[0].indexOf('\n');
  1219. if (end < 0) {
  1220. line += restoreIgnore(restoreCustom(results.shift()));
  1221. }
  1222. else {
  1223. line += restoreIgnore(restoreCustom(results[0].slice(0, end)));
  1224. results[0] = results[0].slice(end + 1);
  1225. }
  1226. if (len > 0 && line.length > maxLineLength) {
  1227. lines.push(line.slice(0, len));
  1228. line = line.slice(len);
  1229. }
  1230. else if (end >= 0) {
  1231. lines.push(line);
  1232. line = '';
  1233. }
  1234. }
  1235. if (line) {
  1236. lines.push(line);
  1237. }
  1238. str = lines.join('\n');
  1239. }
  1240. else {
  1241. str = restoreIgnore(restoreCustom(results.join('')));
  1242. }
  1243. return options.collapseWhitespace ? collapseWhitespace(str, options, true, true) : str;
  1244. }
  1245. exports.minify = function(value, options) {
  1246. var start = Date.now();
  1247. options = processOptions(options || {});
  1248. var result = minify(value, options);
  1249. options.log('minified in: ' + (Date.now() - start) + 'ms');
  1250. return result;
  1251. };