doc-helper.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. getCanvasData,
  28. getEmptyStyleRulesText,
  29. getDoctype,
  30. WIN_ID_ATTRIBUTE_NAME,
  31. PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME,
  32. REMOVED_CONTENT_ATTRIBUTE_NAME
  33. };
  34. function preProcessDoc(doc, win, options) {
  35. doc.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  36. doc.head.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)").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(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
  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(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, "");
  56. }
  57. });
  58. }
  59. }
  60. function postProcessDoc(doc, options) {
  61. doc.querySelectorAll("disabled-noscript").forEach(element => {
  62. const noscriptElement = doc.createElement("noscript");
  63. Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
  64. element.parentElement.replaceChild(noscriptElement, element);
  65. });
  66. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
  67. if (options.removeHiddenElements) {
  68. doc.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME));
  69. }
  70. if (options.compressHTML) {
  71. doc.querySelectorAll("[" + PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME));
  72. }
  73. doc.querySelectorAll("[" + WIN_ID_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(WIN_ID_ATTRIBUTE_NAME));
  74. }
  75. function getCanvasData(doc) {
  76. if (doc) {
  77. const canvasData = [];
  78. doc.querySelectorAll("canvas").forEach(canvasElement => {
  79. try {
  80. canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
  81. } catch (error) {
  82. canvasData.push(null);
  83. }
  84. });
  85. return canvasData;
  86. }
  87. }
  88. function getEmptyStyleRulesText(doc) {
  89. if (doc) {
  90. const textData = [];
  91. doc.querySelectorAll("style").forEach(styleElement => {
  92. if (!styleElement.textContent) {
  93. textData.push(Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n"));
  94. }
  95. });
  96. return textData;
  97. }
  98. }
  99. function getDoctype(doc) {
  100. const docType = doc.doctype;
  101. let docTypeStr;
  102. if (docType) {
  103. docTypeStr = "<!DOCTYPE " + docType.nodeName;
  104. if (docType.publicId) {
  105. docTypeStr += " PUBLIC \"" + docType.publicId + "\"";
  106. if (docType.systemId) {
  107. docTypeStr += " \"" + docType.systemId + "\"";
  108. }
  109. } else if (docType.systemId) {
  110. docTypeStr += " SYSTEM \"" + docType.systemId + "\"";
  111. } if (docType.internalSubset) {
  112. docTypeStr += " [" + docType.internalSubset + "]";
  113. }
  114. return docTypeStr + ">\n";
  115. }
  116. return "";
  117. }
  118. })();