html-images-minifier.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global CSSRule, docHelper, cssWhat, lazyLoader */
  21. this.imagesMinifier = this.imagesMinifier || (() => {
  22. const DEBUG = false;
  23. const SVG_NS = "http://www.w3.org/2000/svg";
  24. return {
  25. process: (doc, mediaAllInfo, options) => {
  26. const imageGroups = getImageGroups(doc);
  27. let duplicates = new Set();
  28. const duplicateURLs = [];
  29. imageGroups.forEach((elements, src) => {
  30. if (elements.length > 1 && src && src != options.baseURI) {
  31. elements.forEach(element => duplicates.add(element));
  32. duplicateURLs.push(src);
  33. }
  34. });
  35. if (duplicateURLs.length) {
  36. processStyleSheets(doc, duplicates, mediaAllInfo);
  37. processImages(doc, duplicates, duplicateURLs, options);
  38. }
  39. }
  40. };
  41. function getImageGroups(doc) {
  42. const imageGroups = new Map();
  43. doc.querySelectorAll("img[src]:not([srcset])").forEach(imageElement => {
  44. if (imageElement.src) {
  45. let imageInfo = imageGroups.get(imageElement.src);
  46. if (!imageInfo) {
  47. imageInfo = [];
  48. imageGroups.set(imageElement.src, imageInfo);
  49. }
  50. imageInfo.push(imageElement);
  51. }
  52. });
  53. return imageGroups;
  54. }
  55. function processStyleSheets(doc, duplicates, mediaAllInfo) {
  56. const matchedSelectors = getMatchedSelectors(duplicates, mediaAllInfo);
  57. doc.querySelectorAll("style").forEach((styleElement, sheetIndex) => {
  58. if (styleElement.sheet) {
  59. const cssRules = styleElement.sheet.cssRules;
  60. let mediaInfo;
  61. if (styleElement.media && styleElement.media != "all") {
  62. mediaInfo = mediaAllInfo.medias.get(sheetIndex + "-" + styleElement.media);
  63. } else {
  64. mediaInfo = mediaAllInfo;
  65. }
  66. styleElement.textContent = processRules(doc, cssRules, sheetIndex, mediaInfo, matchedSelectors);
  67. }
  68. });
  69. }
  70. function processImages(doc, duplicates, duplicateURLs, options) {
  71. const svgElement = doc.createElementNS(SVG_NS, "svg");
  72. const defsElement = doc.createElementNS(SVG_NS, "defs");
  73. svgElement.setAttributeNS(SVG_NS, "width", 0);
  74. svgElement.setAttributeNS(SVG_NS, "height", 0);
  75. svgElement.setAttributeNS(SVG_NS, "style", "display:none!important");
  76. svgElement.appendChild(defsElement);
  77. duplicateURLs.forEach((src, srcIndex) => {
  78. const imageElement = doc.createElementNS(SVG_NS, "image");
  79. imageElement.setAttribute("xlink:href", src);
  80. imageElement.id = "single-file-" + srcIndex;
  81. defsElement.appendChild(imageElement);
  82. });
  83. doc.body.appendChild(svgElement);
  84. const ignoredAttributeNames = [];
  85. if (options.lazyLoadImages) {
  86. const imageSelectors = lazyLoader.imageSelectors;
  87. Object.keys(imageSelectors.src).forEach(selector => ignoredAttributeNames.push(imageSelectors.src[selector]));
  88. Object.keys(imageSelectors.srcset).forEach(selector => ignoredAttributeNames.push(imageSelectors.srcset[selector]));
  89. }
  90. doc.querySelectorAll("img").forEach(imgElement => {
  91. let replaceImage = !options.lazyLoadImages;
  92. if (!replaceImage) {
  93. replaceImage = !Object.keys(ignoredAttributeNames).map(key => ignoredAttributeNames[key]).find(attributeName => imgElement.getAttribute(attributeName));
  94. }
  95. if (replaceImage && duplicates.has(imgElement)) {
  96. const urlIndex = duplicateURLs.indexOf(imgElement.src);
  97. if (urlIndex != -1) {
  98. const dataAttributeName = docHelper.imagesAttributeName(options.sessionId);
  99. const imageData = options.imageData[Number(imgElement.getAttribute(dataAttributeName))];
  100. const svgElement = doc.createElementNS(SVG_NS, "svg");
  101. const useElement = doc.createElementNS(SVG_NS, "use");
  102. svgElement.appendChild(useElement);
  103. imgElement.getAttributeNames().forEach(attributeName => attributeName != "src" && svgElement.setAttribute(attributeName, imgElement.getAttribute(attributeName)));
  104. svgElement.setAttributeNS(SVG_NS, "viewBox", "0 0 " + imageData.naturalWidth + " " + imageData.naturalHeight);
  105. svgElement.setAttributeNS(SVG_NS, "width", imageData.clientWidth);
  106. svgElement.setAttributeNS(SVG_NS, "height", imageData.clientHeight);
  107. svgElement.setAttributeNS(SVG_NS, "preserveAspectRatio", "none");
  108. useElement.setAttributeNS(SVG_NS, "xlink:href", "#single-file-" + urlIndex);
  109. const imageElement = doc.getElementById("single-file-" + urlIndex);
  110. if (!imageElement.getAttributeNS(SVG_NS, "width") && !imageElement.getAttributeNS(SVG_NS, "height") && imageData.naturalWidth && imageData.naturalHeight) {
  111. imageElement.setAttributeNS(SVG_NS, "viewBox", "0 0 " + imageData.naturalWidth + " " + imageData.naturalHeight);
  112. imageElement.setAttributeNS(SVG_NS, "width", imageData.naturalWidth);
  113. imageElement.setAttributeNS(SVG_NS, "height", imageData.naturalHeight);
  114. }
  115. svgElement.style.border = "1px solid red";
  116. imgElement.parentElement.replaceChild(svgElement, imgElement);
  117. }
  118. }
  119. });
  120. }
  121. function getMatchedSelectors(duplicates, parentMediaInfo, matchedRules = new Map()) {
  122. duplicates.forEach(imageElement => {
  123. let elementInfos = parentMediaInfo.elements.get(imageElement);
  124. if (!elementInfos) {
  125. elementInfos = parentMediaInfo.pseudos.get(imageElement);
  126. }
  127. if (elementInfos) {
  128. elementInfos.forEach(elementInfo => {
  129. if (elementInfo.cssRule) {
  130. let selectorInfo = matchedRules.get(elementInfo.cssRule.selectorText);
  131. if (!selectorInfo) {
  132. matchedRules.set(elementInfo.cssRule.selectorText, elementInfo.selectors);
  133. }
  134. }
  135. });
  136. }
  137. });
  138. parentMediaInfo.medias.forEach(mediaInfo => getMatchedSelectors(duplicates, mediaInfo, matchedRules));
  139. return matchedRules;
  140. }
  141. function processRules(doc, cssRules, sheetIndex, mediaInfo, matchedSelectors) {
  142. let sheetContent = "", mediaRuleIndex = 0;
  143. let startTime;
  144. if (DEBUG && cssRules.length > 1) {
  145. startTime = Date.now();
  146. log(" -- STARTED processRules", "rules.length =", cssRules.length);
  147. }
  148. Array.from(cssRules).forEach(cssRule => {
  149. if (cssRule.type == CSSRule.MEDIA_RULE) {
  150. sheetContent += "@media " + Array.from(cssRule.media).join(",") + "{";
  151. sheetContent += processRules(doc, cssRule.cssRules, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + cssRule.media.mediaText), matchedSelectors);
  152. mediaRuleIndex++;
  153. sheetContent += "}";
  154. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  155. const selectors = matchedSelectors.get(cssRule.selectorText);
  156. if (selectors) {
  157. selectors.forEach(selector => {
  158. const newSelector = transformSelector(selector);
  159. if (newSelector) {
  160. selectors.push(newSelector);
  161. }
  162. });
  163. const selectorText = selectors.map(selector => cssWhat.stringify([selector])).join(",");
  164. cssRule.selectorText = selectorText;
  165. }
  166. sheetContent += cssRule.cssText;
  167. } else {
  168. sheetContent += cssRule.cssText;
  169. }
  170. });
  171. if (DEBUG && cssRules.length > 1) {
  172. log(" -- ENDED processRules delay =", Date.now() - startTime);
  173. }
  174. return sheetContent;
  175. }
  176. function transformSelector(selector) {
  177. selector = JSON.parse(JSON.stringify(selector));
  178. let simpleSelector, selectorIndex = selector.length - 1, imageTagFound;
  179. while (selectorIndex >= 0 && !imageTagFound) {
  180. simpleSelector = selector[selectorIndex];
  181. imageTagFound = simpleSelector.type == "tag" && simpleSelector.name == "img";
  182. if (!imageTagFound) {
  183. selectorIndex--;
  184. }
  185. }
  186. if (imageTagFound) {
  187. simpleSelector.name = "svg";
  188. return selector;
  189. }
  190. }
  191. function log(...args) {
  192. console.log("S-File <img-min>", ...args); // eslint-disable-line no-console
  193. }
  194. })();