html-images-minifier.js 8.2 KB

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