doc-util-core.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2010-2019 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 URL */
  21. this.DocUtilCore = this.DocUtilCore || (() => {
  22. return {
  23. getClass: (modules, domUtil) => {
  24. if (modules.serializer === undefined) {
  25. modules.serializer = {
  26. process(doc) {
  27. const docType = doc.doctype;
  28. let docTypeString = "";
  29. if (docType) {
  30. docTypeString = "<!DOCTYPE " + docType.nodeName;
  31. if (docType.publicId) {
  32. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  33. if (docType.systemId)
  34. docTypeString += " \"" + docType.systemId + "\"";
  35. } else if (docType.systemId)
  36. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  37. if (docType.internalSubset)
  38. docTypeString += " [" + docType.internalSubset + "]";
  39. docTypeString += "> ";
  40. }
  41. return docTypeString + doc.documentElement.outerHTML;
  42. }
  43. };
  44. }
  45. return class DocUtil {
  46. static async getContent(resourceURL, options) {
  47. return domUtil.getContent(resourceURL, options);
  48. }
  49. static parseURL(resourceURL, baseURI) {
  50. return new URL(resourceURL, baseURI);
  51. }
  52. static resolveURL(resourceURL, baseURI) {
  53. return this.parseURL(resourceURL, baseURI).href;
  54. }
  55. static parseDocContent(content, baseURI) {
  56. return domUtil.parseDocContent(content, baseURI);
  57. }
  58. static parseSVGContent(content) {
  59. return domUtil.parseSVGContent(content);
  60. }
  61. static async digest(algo, text) {
  62. return domUtil.digestText(algo, text);
  63. }
  64. static getContentSize(content) {
  65. return domUtil.getContentSize(content);
  66. }
  67. static async validFont(urlFunction) {
  68. return domUtil.isValidFontUrl(urlFunction);
  69. }
  70. static minifyHTML(doc, options) {
  71. return modules.htmlMinifier.process(doc, options);
  72. }
  73. static postMinifyHTML(doc) {
  74. return modules.htmlMinifier.postProcess(doc);
  75. }
  76. static minifyCSSRules(stylesheets, styles, mediaAllInfo) {
  77. return modules.cssRulesMinifier.process(stylesheets, styles, mediaAllInfo);
  78. }
  79. static removeUnusedFonts(doc, stylesheets, styles, options) {
  80. return modules.fontsMinifier.process(doc, stylesheets, styles, options);
  81. }
  82. static removeAlternativeFonts(doc, stylesheets) {
  83. return modules.fontsAltMinifier.process(doc, stylesheets);
  84. }
  85. static getMediaAllInfo(doc, stylesheets, styles) {
  86. return modules.matchedRules.getMediaAllInfo(doc, stylesheets, styles);
  87. }
  88. static compressCSS(content, options) {
  89. return modules.cssMinifier.processString(content, options);
  90. }
  91. static minifyMedias(stylesheets) {
  92. return modules.mediasMinifier.process(stylesheets);
  93. }
  94. static removeAlternativeImages(doc, options) {
  95. return modules.imagesAltMinifier.process(doc, options);
  96. }
  97. static parseSrcset(srcset) {
  98. return modules.srcsetParser.process(srcset);
  99. }
  100. static preProcessDoc(doc, win, options) {
  101. return modules.docHelper.preProcessDoc(doc, win, options);
  102. }
  103. static postProcessDoc(doc, options) {
  104. modules.docHelper.postProcessDoc(doc, options);
  105. }
  106. static serialize(doc, compressHTML) {
  107. return modules.serializer.process(doc, compressHTML);
  108. }
  109. static removeQuotes(string) {
  110. return modules.docHelper.removeQuotes(string);
  111. }
  112. static windowIdAttributeName(sessionId) {
  113. return modules.docHelper.windowIdAttributeName(sessionId);
  114. }
  115. static preservedSpaceAttributeName(sessionId) {
  116. return modules.docHelper.preservedSpaceAttributeName(sessionId);
  117. }
  118. static removedContentAttributeName(sessionId) {
  119. return modules.docHelper.removedContentAttributeName(sessionId);
  120. }
  121. static imagesAttributeName(sessionId) {
  122. return modules.docHelper.imagesAttributeName(sessionId);
  123. }
  124. static inputValueAttributeName(sessionId) {
  125. return modules.docHelper.inputValueAttributeName(sessionId);
  126. }
  127. static shadowRootAttributeName(sessionId) {
  128. return modules.docHelper.shadowRootAttributeName(sessionId);
  129. }
  130. };
  131. }
  132. };
  133. })();