html-minifier.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * The MIT License (MIT)
  6. *
  7. * Copyright (c) 2018
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. // Derived from the work of Kirill Maltsev - https://github.com/posthtml/htmlnano
  28. /* global Node, NodeFilter */
  29. this.htmlmini = this.htmlmini || (() => {
  30. // Source: https://github.com/kangax/html-minifier/issues/63
  31. const booleanAttributes = [
  32. "allowfullscreen",
  33. "async",
  34. "autofocus",
  35. "autoplay",
  36. "checked",
  37. "compact",
  38. "controls",
  39. "declare",
  40. "default",
  41. "defaultchecked",
  42. "defaultmuted",
  43. "defaultselected",
  44. "defer",
  45. "disabled",
  46. "enabled",
  47. "formnovalidate",
  48. "hidden",
  49. "indeterminate",
  50. "inert",
  51. "ismap",
  52. "itemscope",
  53. "loop",
  54. "multiple",
  55. "muted",
  56. "nohref",
  57. "noresize",
  58. "noshade",
  59. "novalidate",
  60. "nowrap",
  61. "open",
  62. "pauseonexit",
  63. "readonly",
  64. "required",
  65. "reversed",
  66. "scoped",
  67. "seamless",
  68. "selected",
  69. "sortable",
  70. "truespeed",
  71. "typemustmatch",
  72. "visible"
  73. ];
  74. const noWhitespaceCollapseElements = ["script", "style", "pre", "textarea"];
  75. // Source: https://www.w3.org/TR/html4/sgml/dtd.html#events (Generic Attributes)
  76. const safeToRemoveAttrs = [
  77. "id",
  78. "class",
  79. "style",
  80. "title",
  81. "lang",
  82. "dir",
  83. "onclick",
  84. "ondblclick",
  85. "onmousedown",
  86. "onmouseup",
  87. "onmouseover",
  88. "onmousemove",
  89. "onmouseout",
  90. "onkeypress",
  91. "onkeydown",
  92. "onkeyup"
  93. ];
  94. const redundantAttributes = {
  95. "form": {
  96. "method": "get"
  97. },
  98. "script": {
  99. "language": "javascript",
  100. "type": "text/javascript",
  101. // Remove attribute if the function returns false
  102. "charset": node => {
  103. // The charset attribute only really makes sense on “external” SCRIPT elements:
  104. // http://perfectionkills.com/optimizing-html/#8_script_charset
  105. return !node.getAttribute("src");
  106. }
  107. },
  108. "style": {
  109. "media": "all",
  110. "type": "text/css"
  111. },
  112. "link": {
  113. "media": "all"
  114. }
  115. };
  116. const REGEXP_WHITESPACE = /[ \t\f\r]+/g;
  117. const REGEXP_NEWLINE = /[\n]+/g;
  118. const REGEXP_ENDS_WHITESPACE = /^\s+$/;
  119. const modules = [
  120. collapseBooleanAttributes,
  121. mergeTextNodes,
  122. collapseWhitespace,
  123. removeComments,
  124. removeEmptyAttributes,
  125. removeRedundantAttributes,
  126. compressJSONLD,
  127. node => mergeElements(node, "style", (node, previousSibling) => node.parentElement && node.parentElement.tagName == "HEAD" && node.media == previousSibling.media),
  128. node => mergeElements(node, "script", (node, previousSibling) => node.parentElement && node.parentElement.tagName == "HEAD" && ((!node.type || node.nodeType == "text/javascript") && (!previousSibling.type || previousSibling.nodeType == "text/javascript")))
  129. ];
  130. return {
  131. process: (doc, options) => {
  132. const nodesWalker = doc.createTreeWalker(doc.documentElement, NodeFilter.SHOW_ALL, null, false);
  133. let node = nodesWalker.nextNode();
  134. while (node) {
  135. const deletedNode = modules.find(module => module(node, options));
  136. const previousNode = node;
  137. node = nodesWalker.nextNode();
  138. if (deletedNode) {
  139. previousNode.remove();
  140. }
  141. }
  142. },
  143. postProcess: doc => {
  144. removeEmptyInlineElements(doc);
  145. }
  146. };
  147. function collapseBooleanAttributes(node) {
  148. if (node.nodeType == Node.ELEMENT_NODE) {
  149. Array.from(node.attributes).forEach(attribute => {
  150. if (booleanAttributes.includes(attribute.name)) {
  151. node.setAttribute(attribute.name, "");
  152. }
  153. });
  154. }
  155. }
  156. function mergeTextNodes(node) {
  157. if (node.nodeType == Node.TEXT_NODE) {
  158. if (node.previousSibling && node.previousSibling.nodeType == Node.TEXT_NODE) {
  159. node.textContent = node.previousSibling.textContent + node.textContent;
  160. node.previousSibling.remove();
  161. }
  162. }
  163. }
  164. function mergeElements(node, tagName, acceptMerge) {
  165. if (node.nodeType == Node.ELEMENT_NODE && node.tagName.toLowerCase() == tagName.toLowerCase()) {
  166. let previousSibling = node.previousSibling;
  167. let previousSiblings = [];
  168. while (previousSibling && previousSibling.nodeType == Node.TEXT_NODE && !previousSibling.textContent.trim()) {
  169. previousSiblings.push(previousSibling);
  170. previousSibling = previousSibling.previousSibling;
  171. }
  172. if (previousSibling && previousSibling.nodeType == Node.ELEMENT_NODE && previousSibling.tagName == node.tagName && acceptMerge(node, previousSibling)) {
  173. node.textContent = previousSibling.textContent + node.textContent;
  174. previousSiblings.forEach(node => node.remove());
  175. previousSibling.remove();
  176. }
  177. }
  178. }
  179. function collapseWhitespace(node, options) {
  180. if (node.nodeType == Node.TEXT_NODE) {
  181. let element = node.parentElement;
  182. const spacePreserved = element.getAttribute(options.preservedSpaceAttributeName) == "";
  183. let textContent = node.textContent;
  184. let noWhitespace = !spacePreserved && noWhitespaceCollapse(element);
  185. while (noWhitespace) {
  186. element = element.parentElement;
  187. noWhitespace = element && noWhitespaceCollapse(element);
  188. }
  189. if ((!element || noWhitespace) && textContent.length > 1) {
  190. node.textContent = textContent.replace(REGEXP_WHITESPACE, getWhiteSpace(node)).replace(REGEXP_NEWLINE, getWhiteSpace(node));
  191. }
  192. }
  193. }
  194. function getWhiteSpace(node) {
  195. return node.parentElement && node.parentElement.tagName == "HEAD" ? "\n" : " ";
  196. }
  197. function noWhitespaceCollapse(element) {
  198. return element && !noWhitespaceCollapseElements.includes(element.tagName.toLowerCase());
  199. }
  200. function removeComments(node) {
  201. if (node.nodeType == Node.COMMENT_NODE) {
  202. return !node.textContent.toLowerCase().trim().startsWith("[if");
  203. }
  204. }
  205. function removeEmptyAttributes(node) {
  206. if (node.nodeType == Node.ELEMENT_NODE) {
  207. Array.from(node.attributes).forEach(attribute => {
  208. if (safeToRemoveAttrs.includes(attribute.name.toLowerCase())) {
  209. const attributeValue = node.getAttribute(attribute.name);
  210. if (attributeValue == "" || (attributeValue || "").match(REGEXP_ENDS_WHITESPACE)) {
  211. node.removeAttribute(attribute.name);
  212. }
  213. }
  214. });
  215. }
  216. }
  217. function removeRedundantAttributes(node) {
  218. if (node.nodeType == Node.ELEMENT_NODE) {
  219. const tagRedundantAttributes = redundantAttributes[node.tagName.toLowerCase()];
  220. if (tagRedundantAttributes) {
  221. Object.keys(tagRedundantAttributes).forEach(redundantAttributeName => {
  222. const tagRedundantAttributeValue = tagRedundantAttributes[redundantAttributeName];
  223. if (typeof tagRedundantAttributeValue == "function" ? tagRedundantAttributeValue(node) : node.getAttribute(redundantAttributeName) == tagRedundantAttributeValue) {
  224. node.removeAttribute(redundantAttributeName);
  225. }
  226. });
  227. }
  228. }
  229. }
  230. function compressJSONLD(node) {
  231. if (node.nodeType == Node.ELEMENT_NODE && node.tagName == "SCRIPT" && node.type == "application/ld+json" && node.textContent.trim()) {
  232. try {
  233. node.textContent = JSON.stringify(JSON.parse(node.textContent));
  234. } catch (error) {
  235. /* ignored */
  236. }
  237. }
  238. }
  239. function removeEmptyInlineElements(doc) {
  240. doc.querySelectorAll("style, script, noscript").forEach(element => {
  241. if (!element.textContent.trim()) {
  242. element.remove();
  243. }
  244. });
  245. }
  246. })();