html-images-minifier.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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) {
  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 dataAttributeName = docHelper.imagesAttributeName(options.sessionId);
  97. const imageData = options.imageData[Number(imgElement.getAttribute(dataAttributeName))];
  98. const svgElement = doc.createElementNS(SVG_NS, "svg");
  99. const useElement = doc.createElementNS(SVG_NS, "use");
  100. svgElement.appendChild(useElement);
  101. imgElement.getAttributeNames().forEach(attributeName => attributeName != "src" && svgElement.setAttribute(attributeName, imgElement.getAttribute(attributeName)));
  102. svgElement.setAttributeNS(SVG_NS, "viewBox", "0 0 " + imageData.naturalWidth + " " + imageData.naturalHeight);
  103. svgElement.setAttributeNS(SVG_NS, "width", imageData.clientWidth);
  104. svgElement.setAttributeNS(SVG_NS, "height", imageData.clientHeight);
  105. svgElement.setAttributeNS(SVG_NS, "preserveAspectRatio", "none");
  106. const urlIndex = duplicateURLs.indexOf(imgElement.src);
  107. useElement.setAttributeNS(SVG_NS, "xlink:href", "#single-file-" + urlIndex);
  108. const imageElement = doc.getElementById("single-file-" + urlIndex);
  109. if (!imageElement.getAttributeNS(SVG_NS, "width") && !imageElement.getAttributeNS(SVG_NS, "height") && imageData.naturalWidth && imageData.naturalHeight) {
  110. imageElement.setAttributeNS(SVG_NS, "viewBox", "0 0 " + imageData.naturalWidth + " " + imageData.naturalHeight);
  111. }
  112. svgElement.style.border = "1px solid red";
  113. imgElement.parentElement.replaceChild(svgElement, imgElement);
  114. }
  115. });
  116. }
  117. function getMatchedSelectors(duplicates, parentMediaInfo, matchedRules = new Map()) {
  118. duplicates.forEach(imageElement => {
  119. let elementInfos = parentMediaInfo.elements.get(imageElement);
  120. if (!elementInfos) {
  121. elementInfos = parentMediaInfo.pseudos.get(imageElement);
  122. }
  123. if (elementInfos) {
  124. elementInfos.forEach(elementInfo => {
  125. if (elementInfo.cssRule) {
  126. let selectorInfo = matchedRules.get(elementInfo.cssRule.selectorText);
  127. if (!selectorInfo) {
  128. matchedRules.set(elementInfo.cssRule.selectorText, elementInfo.selectors);
  129. }
  130. }
  131. });
  132. }
  133. });
  134. parentMediaInfo.medias.forEach(mediaInfo => getMatchedSelectors(duplicates, mediaInfo, matchedRules));
  135. return matchedRules;
  136. }
  137. function processRules(doc, cssRules, sheetIndex, mediaInfo, matchedSelectors) {
  138. let sheetContent = "", mediaRuleIndex = 0;
  139. let startTime;
  140. if (DEBUG && cssRules.length > 1) {
  141. startTime = Date.now();
  142. log(" -- STARTED processRules", "rules.length =", cssRules.length);
  143. }
  144. Array.from(cssRules).forEach(cssRule => {
  145. if (cssRule.type == CSSRule.MEDIA_RULE) {
  146. sheetContent += "@media " + Array.from(cssRule.media).join(",") + "{";
  147. sheetContent += processRules(doc, cssRule.cssRules, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + cssRule.media.mediaText), matchedSelectors);
  148. mediaRuleIndex++;
  149. sheetContent += "}";
  150. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  151. const selectors = matchedSelectors.get(cssRule.selectorText);
  152. if (selectors) {
  153. selectors.forEach(selector => {
  154. const newSelector = transformSelector(selector);
  155. if (newSelector) {
  156. selectors.push(newSelector);
  157. }
  158. });
  159. const selectorText = selectors.map(selector => cssWhat.stringify([selector])).join(",");
  160. cssRule.selectorText = selectorText;
  161. }
  162. sheetContent += cssRule.cssText;
  163. } else {
  164. sheetContent += cssRule.cssText;
  165. }
  166. });
  167. if (DEBUG && cssRules.length > 1) {
  168. log(" -- ENDED processRules delay =", Date.now() - startTime);
  169. }
  170. return sheetContent;
  171. }
  172. function transformSelector(selector) {
  173. selector = JSON.parse(JSON.stringify(selector));
  174. let simpleSelector, selectorIndex = selector.length - 1, imageTagFound;
  175. while (selectorIndex >= 0 && !imageTagFound) {
  176. simpleSelector = selector[selectorIndex];
  177. imageTagFound = simpleSelector.type == "tag" && simpleSelector.name == "img";
  178. if (!imageTagFound) {
  179. selectorIndex--;
  180. }
  181. }
  182. if (imageTagFound) {
  183. simpleSelector.name = "svg";
  184. return selector;
  185. }
  186. }
  187. function log(...args) {
  188. console.log("S-File <img-min>", ...args); // eslint-disable-line no-console
  189. }
  190. })();