docHelper.js 4.7 KB

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