doc-helper.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. this.docHelper = this.docHelper || (() => {
  21. const REMOVED_CONTENT_ATTRIBUTE_NAME = "data-single-file-removed-content";
  22. const PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME = "data-single-file-preserved-space-element";
  23. const WIN_ID_ATTRIBUTE_NAME = "data-frame-tree-win-id";
  24. const RESPONSIVE_IMAGE_ATTRIBUTE_NAME = "data-single-file-responsive-image";
  25. return {
  26. preProcessDoc,
  27. postProcessDoc,
  28. serialize,
  29. windowIdAttributeName,
  30. preservedSpaceAttributeName,
  31. removedContentAttributeName,
  32. responsiveImagesAttributeName
  33. };
  34. function preProcessDoc(doc, win, options) {
  35. doc.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  36. doc.querySelectorAll("noscript").forEach(element => {
  37. const disabledNoscriptElement = doc.createElement("disabled-noscript");
  38. Array.from(element.childNodes).forEach(node => disabledNoscriptElement.appendChild(node));
  39. disabledNoscriptElement.hidden = true;
  40. element.parentElement.replaceChild(disabledNoscriptElement, element);
  41. });
  42. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.hidden = true);
  43. if (options.removeHiddenElements) {
  44. doc.querySelectorAll("html > body *:not(style):not(script):not(link):not(frame):not(iframe):not(object):not(meta):not(title):not(meta):not(noscript):not(template)").forEach(element => {
  45. const style = win.getComputedStyle(element);
  46. if (element instanceof win.HTMLElement && style && (element.hidden || style.display == "none" || ((style.opacity === 0 || style.visibility == "hidden") && !element.clientWidth && !element.clientHeight)) && !element.querySelector("iframe, frame, object[type=\"text/html\"][data]")) {
  47. element.setAttribute(removedContentAttributeName(options.sessionId), "");
  48. }
  49. });
  50. }
  51. if (options.compressHTML) {
  52. doc.querySelectorAll("*").forEach(element => {
  53. const style = win.getComputedStyle(element);
  54. if (style && style.whiteSpace.startsWith("pre")) {
  55. element.setAttribute(preservedSpaceAttributeName(options.sessionId), "");
  56. }
  57. });
  58. }
  59. return {
  60. canvasData: getCanvasData(doc),
  61. stylesheetContents: getStylesheetContents(doc),
  62. imageData: getImageData(doc, options)
  63. };
  64. }
  65. function postProcessDoc(doc, options) {
  66. doc.querySelectorAll("disabled-noscript").forEach(element => {
  67. const noscriptElement = doc.createElement("noscript");
  68. Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
  69. element.parentElement.replaceChild(noscriptElement, element);
  70. });
  71. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
  72. if (options.removeHiddenElements) {
  73. doc.querySelectorAll("[" + removedContentAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(removedContentAttributeName(options.sessionId)));
  74. }
  75. if (options.compressHTML) {
  76. doc.querySelectorAll("[" + preservedSpaceAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(preservedSpaceAttributeName(options.sessionId)));
  77. }
  78. doc.querySelectorAll("[" + responsiveImagesAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(responsiveImagesAttributeName(options.sessionId)));
  79. }
  80. function responsiveImagesAttributeName(sessionId) {
  81. return RESPONSIVE_IMAGE_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  82. }
  83. function preservedSpaceAttributeName(sessionId) {
  84. return PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  85. }
  86. function removedContentAttributeName(sessionId) {
  87. return REMOVED_CONTENT_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  88. }
  89. function windowIdAttributeName(sessionId) {
  90. return WIN_ID_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  91. }
  92. function getCanvasData(doc) {
  93. if (doc) {
  94. const canvasData = [];
  95. doc.querySelectorAll("canvas").forEach(canvasElement => {
  96. try {
  97. canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
  98. } catch (error) {
  99. canvasData.push(null);
  100. }
  101. });
  102. return canvasData;
  103. }
  104. }
  105. function getStylesheetContents(doc) {
  106. if (doc) {
  107. const contents = [];
  108. doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
  109. let stylesheet;
  110. try {
  111. const tempStyleElement = doc.createElement("style");
  112. tempStyleElement.textContent = styleElement.textContent;
  113. doc.body.appendChild(tempStyleElement);
  114. stylesheet = tempStyleElement.sheet;
  115. tempStyleElement.remove();
  116. if (!stylesheet || stylesheet.cssRules.length != styleElement.sheet.cssRules.length) {
  117. contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n");
  118. }
  119. } catch (error) {
  120. /* ignored */
  121. }
  122. });
  123. return contents;
  124. }
  125. }
  126. function getImageData(doc, options) {
  127. if (doc) {
  128. const data = [];
  129. doc.querySelectorAll("picture, img[srcset]").forEach((element, elementIndex) => {
  130. const tagName = element.tagName.toLowerCase();
  131. let imageData = {}, imageElement;
  132. element.setAttribute(responsiveImagesAttributeName(options.sessionId), elementIndex);
  133. if (tagName == "picture") {
  134. const sources = Array.from(element.querySelectorAll("source")).map(sourceElement => (
  135. { src: sourceElement.src, srcset: sourceElement.srcset }
  136. ));
  137. imageElement = element.querySelector("img");
  138. imageData.sources = sources;
  139. }
  140. if (tagName == "img") {
  141. imageElement = element;
  142. }
  143. imageData.source = {
  144. clientWidth: imageElement.clientWidth,
  145. clientHeight: imageElement.clientHeight,
  146. naturalWidth: imageElement.naturalWidth,
  147. naturalHeight: imageElement.naturalHeight,
  148. width: imageElement.width,
  149. height: imageElement.height,
  150. src: (!imageElement.currentSrc.startsWith("data:") && imageElement.currentSrc) || (!imageElement.src.startsWith("data:") && imageElement.src)
  151. };
  152. data.push(imageData);
  153. });
  154. return data;
  155. }
  156. }
  157. function serialize(doc) {
  158. const docType = doc.doctype;
  159. let docTypeString = "";
  160. if (docType) {
  161. docTypeString = "<!DOCTYPE " + docType.nodeName;
  162. if (docType.publicId) {
  163. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  164. if (docType.systemId) {
  165. docTypeString += " \"" + docType.systemId + "\"";
  166. }
  167. } else if (docType.systemId) {
  168. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  169. } if (docType.internalSubset) {
  170. docTypeString += " [" + docType.internalSubset + "]";
  171. }
  172. docTypeString += "> ";
  173. }
  174. return docTypeString + doc.documentElement.outerHTML;
  175. }
  176. })();