doc-helper.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. return {
  25. preProcessDoc,
  26. postProcessDoc,
  27. serialize,
  28. windowIdAttributeName,
  29. preservedSpaceAttributeName,
  30. removedContentAttributeName
  31. };
  32. function preProcessDoc(doc, win, options) {
  33. doc.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  34. doc.head.querySelectorAll("noscript").forEach(element => {
  35. const disabledNoscriptElement = doc.createElement("disabled-noscript");
  36. Array.from(element.childNodes).forEach(node => disabledNoscriptElement.appendChild(node));
  37. disabledNoscriptElement.hidden = true;
  38. element.parentElement.replaceChild(disabledNoscriptElement, element);
  39. });
  40. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.hidden = true);
  41. if (options.removeHiddenElements) {
  42. doc.querySelectorAll("html > body *:not(style):not(script):not(link):not(frame):not(iframe):not(object)").forEach(element => {
  43. const style = win.getComputedStyle(element);
  44. 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]")) {
  45. element.setAttribute(removedContentAttributeName(options.sessionId), "");
  46. }
  47. });
  48. }
  49. if (options.compressHTML) {
  50. doc.querySelectorAll("*").forEach(element => {
  51. const style = win.getComputedStyle(element);
  52. if (style && style.whiteSpace.startsWith("pre")) {
  53. element.setAttribute(preservedSpaceAttributeName(options.sessionId), "");
  54. }
  55. });
  56. }
  57. return {
  58. canvasData: getCanvasData(doc),
  59. emptyStyleRulesText: getEmptyStyleRulesText(doc)
  60. };
  61. }
  62. function postProcessDoc(doc, options) {
  63. doc.querySelectorAll("disabled-noscript").forEach(element => {
  64. const noscriptElement = doc.createElement("noscript");
  65. Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
  66. element.parentElement.replaceChild(noscriptElement, element);
  67. });
  68. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
  69. if (options.removeHiddenElements) {
  70. doc.querySelectorAll("[" + removedContentAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(removedContentAttributeName(options.sessionId)));
  71. }
  72. if (options.compressHTML) {
  73. doc.querySelectorAll("[" + preservedSpaceAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(preservedSpaceAttributeName(options.sessionId)));
  74. }
  75. doc.querySelectorAll("[" + windowIdAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(windowIdAttributeName(options.sessionId)));
  76. }
  77. function preservedSpaceAttributeName(sessionId) {
  78. return PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  79. }
  80. function removedContentAttributeName(sessionId) {
  81. return REMOVED_CONTENT_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  82. }
  83. function windowIdAttributeName(sessionId) {
  84. return WIN_ID_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
  85. }
  86. function getCanvasData(doc) {
  87. if (doc) {
  88. const canvasData = [];
  89. doc.querySelectorAll("canvas").forEach(canvasElement => {
  90. try {
  91. canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
  92. } catch (error) {
  93. canvasData.push(null);
  94. }
  95. });
  96. return canvasData;
  97. }
  98. }
  99. function getEmptyStyleRulesText(doc) {
  100. if (doc) {
  101. const textData = [];
  102. doc.querySelectorAll("style").forEach(styleElement => {
  103. if (!styleElement.textContent) {
  104. textData.push(Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n"));
  105. }
  106. });
  107. return textData;
  108. }
  109. }
  110. function serialize(doc) {
  111. const docType = doc.doctype;
  112. let docTypeString = "";
  113. if (docType) {
  114. docTypeString = "<!DOCTYPE " + docType.nodeName;
  115. if (docType.publicId) {
  116. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  117. if (docType.systemId) {
  118. docTypeString += " \"" + docType.systemId + "\"";
  119. }
  120. } else if (docType.systemId) {
  121. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  122. } if (docType.internalSubset) {
  123. docTypeString += " [" + docType.internalSubset + "]";
  124. }
  125. docTypeString += "> ";
  126. }
  127. return docTypeString + doc.documentElement.outerHTML;
  128. }
  129. })();