single-file-helper.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file 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 GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global CustomEvent, addEventListener, dispatchEvent */
  24. this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
  25. const singlefile = this.singlefile;
  26. const ON_BEFORE_CAPTURE_EVENT_NAME = "single-file-on-before-capture";
  27. const ON_AFTER_CAPTURE_EVENT_NAME = "single-file-on-after-capture";
  28. const REMOVED_CONTENT_ATTRIBUTE_NAME = "data-single-file-removed-content";
  29. const HIDDEN_CONTENT_ATTRIBUTE_NAME = "data-single-file-hidden-content";
  30. const HIDDEN_FRAME_ATTRIBUTE_NAME = "data-single-file-hidden-frame";
  31. const PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME = "data-single-file-preserved-space-element";
  32. const SHADOW_ROOT_ATTRIBUTE_NAME = "data-single-file-shadow-root-element";
  33. const WIN_ID_ATTRIBUTE_NAME = "data-single-file-win-id";
  34. const IMAGE_ATTRIBUTE_NAME = "data-single-file-image";
  35. const POSTER_ATTRIBUTE_NAME = "data-single-file-poster";
  36. const CANVAS_ATTRIBUTE_NAME = "data-single-file-canvas";
  37. const HTML_IMPORT_ATTRIBUTE_NAME = "data-single-file-import";
  38. const INPUT_VALUE_ATTRIBUTE_NAME = "data-single-file-input-value";
  39. const LAZY_SRC_ATTRIBUTE_NAME = "data-single-file-lazy-loaded-src";
  40. const STYLESHEET_ATTRIBUTE_NAME = "data-single-file-stylesheet";
  41. const DISABLED_NOSCRIPT_ATTRIBUTE_NAME = "data-single-file-disabled-noscript";
  42. const SELECTED_CONTENT_ATTRIBUTE_NAME = "data-single-file-selected-content";
  43. const ASYNC_SCRIPT_ATTRIBUTE_NAME = "data-single-file-async-script";
  44. const FLOW_ELEMENTS_SELECTOR = "*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)";
  45. const IGNORED_REMOVED_TAG_NAMES = ["NOSCRIPT", "DISABLED-NOSCRIPT", "META", "LINK", "STYLE", "TITLE", "TEMPLATE", "SOURCE", "OBJECT", "SCRIPT", "HEAD"];
  46. const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
  47. const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
  48. const FONT_WEIGHTS = {
  49. normal: "400",
  50. bold: "700"
  51. };
  52. addEventListener("single-file-user-script-init", () => singlefile.lib.helper.waitForUserScript = async eventPrefixName => {
  53. const event = new CustomEvent(eventPrefixName + "-request", { cancelable: true });
  54. const promiseResponse = new Promise(resolve => addEventListener(eventPrefixName + "-response", resolve));
  55. dispatchEvent(event);
  56. if (event.defaultPrevented) {
  57. await promiseResponse;
  58. }
  59. });
  60. return {
  61. initDoc,
  62. preProcessDoc,
  63. postProcessDoc,
  64. serialize,
  65. removeQuotes,
  66. flatten,
  67. ON_BEFORE_CAPTURE_EVENT_NAME,
  68. ON_AFTER_CAPTURE_EVENT_NAME,
  69. WIN_ID_ATTRIBUTE_NAME,
  70. PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME,
  71. REMOVED_CONTENT_ATTRIBUTE_NAME,
  72. HIDDEN_CONTENT_ATTRIBUTE_NAME,
  73. HIDDEN_FRAME_ATTRIBUTE_NAME,
  74. IMAGE_ATTRIBUTE_NAME,
  75. POSTER_ATTRIBUTE_NAME,
  76. CANVAS_ATTRIBUTE_NAME,
  77. INPUT_VALUE_ATTRIBUTE_NAME,
  78. SHADOW_ROOT_ATTRIBUTE_NAME,
  79. HTML_IMPORT_ATTRIBUTE_NAME,
  80. LAZY_SRC_ATTRIBUTE_NAME,
  81. STYLESHEET_ATTRIBUTE_NAME,
  82. SELECTED_CONTENT_ATTRIBUTE_NAME,
  83. ASYNC_SCRIPT_ATTRIBUTE_NAME
  84. };
  85. function initDoc(doc) {
  86. doc.querySelectorAll("meta[http-equiv=refresh]").forEach(element => {
  87. element.removeAttribute("http-equiv");
  88. element.setAttribute("disabled-http-equiv", "refresh");
  89. });
  90. }
  91. function preProcessDoc(doc, win, options) {
  92. doc.querySelectorAll("noscript").forEach(element => {
  93. element.setAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME, element.textContent);
  94. element.textContent = "";
  95. });
  96. initDoc(doc);
  97. if (doc.head) {
  98. doc.head.querySelectorAll(FLOW_ELEMENTS_SELECTOR).forEach(element => element.hidden = true);
  99. }
  100. doc.querySelectorAll("svg foreignObject").forEach(element => {
  101. const flowElements = element.querySelectorAll("html > head > " + FLOW_ELEMENTS_SELECTOR + ", html > body > " + FLOW_ELEMENTS_SELECTOR);
  102. if (flowElements.length) {
  103. Array.from(element.childNodes).forEach(node => node.remove());
  104. flowElements.forEach(flowElement => element.appendChild(flowElement));
  105. }
  106. });
  107. let elementsInfo;
  108. if (win && doc.documentElement) {
  109. elementsInfo = getElementsInfo(win, doc, doc.documentElement, options);
  110. } else {
  111. elementsInfo = {
  112. canvases: [],
  113. images: [],
  114. posters: [],
  115. usedFonts: [],
  116. shadowRoots: [],
  117. imports: [],
  118. markedElements: []
  119. };
  120. }
  121. return {
  122. canvases: elementsInfo.canvases,
  123. fonts: getFontsData(doc),
  124. stylesheets: getStylesheetsData(doc),
  125. images: elementsInfo.images,
  126. posters: elementsInfo.posters,
  127. usedFonts: Array.from(elementsInfo.usedFonts.values()),
  128. shadowRoots: elementsInfo.shadowRoots,
  129. imports: elementsInfo.imports,
  130. referrer: doc.referrer,
  131. markedElements: elementsInfo.markedElements
  132. };
  133. }
  134. function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map(), canvases: [], images: [], posters: [], shadowRoots: [], imports: [], markedElements: [] }, ascendantHidden) {
  135. const elements = Array.from(element.childNodes).filter(node => node instanceof win.HTMLElement);
  136. elements.forEach(element => {
  137. let elementHidden, computedStyle;
  138. if (options.removeHiddenElements || options.removeUnusedFonts || options.compressHTML) {
  139. computedStyle = win.getComputedStyle(element);
  140. if (options.removeHiddenElements) {
  141. if (ascendantHidden) {
  142. Array.from(element.childNodes).filter(node => node instanceof win.HTMLElement).forEach(element => {
  143. if (!IGNORED_REMOVED_TAG_NAMES.includes(element.tagName)) {
  144. element.setAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
  145. data.markedElements.push(element);
  146. }
  147. });
  148. }
  149. elementHidden = ascendantHidden || testHiddenElement(element, computedStyle, data.markedElements);
  150. }
  151. if (!elementHidden) {
  152. if (options.compressHTML && computedStyle) {
  153. const whiteSpace = computedStyle.getPropertyValue("white-space");
  154. if (whiteSpace && whiteSpace.startsWith("pre")) {
  155. element.setAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, "");
  156. data.markedElements.push(element);
  157. }
  158. }
  159. if (options.removeUnusedFonts) {
  160. getUsedFont(computedStyle, options, data.usedFonts);
  161. getUsedFont(win.getComputedStyle(element, ":first-letter"), options, data.usedFonts);
  162. getUsedFont(win.getComputedStyle(element, ":before"), options, data.usedFonts);
  163. getUsedFont(win.getComputedStyle(element, ":after"), options, data.usedFonts);
  164. }
  165. }
  166. }
  167. getResourcesInfo(win, doc, element, options, data, elementHidden, computedStyle);
  168. if (element.shadowRoot) {
  169. const shadowRootInfo = {};
  170. element.setAttribute(SHADOW_ROOT_ATTRIBUTE_NAME, data.shadowRoots.length);
  171. data.markedElements.push(element);
  172. data.shadowRoots.push(shadowRootInfo);
  173. getElementsInfo(win, doc, element.shadowRoot, options, data, elementHidden);
  174. shadowRootInfo.content = element.shadowRoot.innerHTML;
  175. shadowRootInfo.delegatesFocus = element.shadowRoot.delegatesFocus;
  176. shadowRootInfo.mode = element.shadowRoot.mode;
  177. if (element.shadowRoot.adoptedStyleSheets && element.shadowRoot.adoptedStyleSheets.length) {
  178. shadowRootInfo.adoptedStyleSheets = Array.from(element.shadowRoot.adoptedStyleSheets).map(stylesheet => Array.from(stylesheet.cssRules).map(cssRule => cssRule.cssText).join("\n"));
  179. }
  180. }
  181. getElementsInfo(win, doc, element, options, data, elementHidden);
  182. });
  183. return data;
  184. }
  185. function getResourcesInfo(win, doc, element, options, data, elementHidden, computedStyle) {
  186. if (element.tagName == "CANVAS") {
  187. try {
  188. const size = getSize(win, element, computedStyle);
  189. data.canvases.push({ dataURI: element.toDataURL("image/png", ""), width: size.width, height: size.height });
  190. element.setAttribute(CANVAS_ATTRIBUTE_NAME, data.canvases.length - 1);
  191. data.markedElements.push(element);
  192. } catch (error) {
  193. // ignored
  194. }
  195. }
  196. if (element.tagName == "IMG") {
  197. const imageData = {
  198. currentSrc: elementHidden ?
  199. "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" :
  200. (options.loadDeferredImages && element.getAttribute(LAZY_SRC_ATTRIBUTE_NAME)) || element.currentSrc
  201. };
  202. data.images.push(imageData);
  203. element.setAttribute(IMAGE_ATTRIBUTE_NAME, data.images.length - 1);
  204. data.markedElements.push(element);
  205. element.removeAttribute(LAZY_SRC_ATTRIBUTE_NAME);
  206. computedStyle = computedStyle || win.getComputedStyle(element);
  207. if (computedStyle) {
  208. imageData.size = getSize(win, element, computedStyle);
  209. const boxShadow = computedStyle.getPropertyValue("box-shadow");
  210. const backgroundImage = computedStyle.getPropertyValue("background-image");
  211. if ((!boxShadow || boxShadow == "none") &&
  212. (!backgroundImage || backgroundImage == "none") &&
  213. (imageData.size.pxWidth > 1 || imageData.size.pxHeight > 1)) {
  214. imageData.replaceable = true;
  215. imageData.backgroundColor = computedStyle.getPropertyValue("background-color");
  216. imageData.objectFit = computedStyle.getPropertyValue("object-fit");
  217. imageData.boxSizing = computedStyle.getPropertyValue("box-sizing");
  218. imageData.objectPosition = computedStyle.getPropertyValue("object-position");
  219. }
  220. }
  221. }
  222. if (element.tagName == "VIDEO") {
  223. if (!element.poster) {
  224. const canvasElement = doc.createElement("canvas");
  225. const context = canvasElement.getContext("2d");
  226. canvasElement.width = element.clientWidth;
  227. canvasElement.height = element.clientHeight;
  228. try {
  229. context.drawImage(element, 0, 0, canvasElement.width, canvasElement.height);
  230. data.posters.push(canvasElement.toDataURL("image/png", ""));
  231. element.setAttribute(POSTER_ATTRIBUTE_NAME, data.posters.length - 1);
  232. data.markedElements.push(element);
  233. } catch (error) {
  234. // ignored
  235. }
  236. }
  237. }
  238. if (element.tagName == "IFRAME") {
  239. if (elementHidden && options.removeHiddenElements) {
  240. element.setAttribute(HIDDEN_FRAME_ATTRIBUTE_NAME, "");
  241. data.markedElements.push(element);
  242. }
  243. }
  244. if (element.tagName == "LINK") {
  245. if (element.import) {
  246. data.imports.push({ content: serialize(element.import) });
  247. element.setAttribute(HTML_IMPORT_ATTRIBUTE_NAME, data.imports.length - 1);
  248. data.markedElements.push(element);
  249. }
  250. }
  251. if (element.tagName == "INPUT") {
  252. if (element.type != "password") {
  253. element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.value);
  254. data.markedElements.push(element);
  255. }
  256. if (element.type == "radio" || element.type == "checkbox") {
  257. element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.checked);
  258. data.markedElements.push(element);
  259. }
  260. }
  261. if (element.tagName == "TEXTAREA") {
  262. element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.value);
  263. data.markedElements.push(element);
  264. }
  265. if (element.tagName == "SELECT") {
  266. element.querySelectorAll("option").forEach(option => {
  267. if (option.selected) {
  268. option.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, "");
  269. data.markedElements.push(option);
  270. }
  271. });
  272. }
  273. if (element.tagName == "SCRIPT") {
  274. if (element.async && element.getAttribute("async") != "" && element.getAttribute("async") != "async") {
  275. element.setAttribute(ASYNC_SCRIPT_ATTRIBUTE_NAME, "");
  276. data.markedElements.push(element);
  277. }
  278. element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>");
  279. }
  280. }
  281. function getUsedFont(computedStyle, options, usedFonts) {
  282. if (computedStyle) {
  283. const fontStyle = computedStyle.getPropertyValue("font-style") || "normal";
  284. computedStyle.getPropertyValue("font-family").split(",").forEach(fontFamilyName => {
  285. fontFamilyName = normalizeFontFamily(fontFamilyName);
  286. if (!options.loadedFonts || options.loadedFonts.find(font => normalizeFontFamily(font.family) == fontFamilyName && font.style == fontStyle)) {
  287. const fontWeight = getFontWeight(computedStyle.getPropertyValue("font-weight"));
  288. const fontVariant = computedStyle.getPropertyValue("font-variant") || "normal";
  289. const value = [fontFamilyName, fontWeight, fontStyle, fontVariant];
  290. usedFonts.set(JSON.stringify(value), [fontFamilyName, fontWeight, fontStyle, fontVariant]);
  291. }
  292. });
  293. }
  294. }
  295. function normalizeFontFamily(fontFamilyName) {
  296. return removeQuotes(singlefile.lib.vendor.cssUnescape.process(fontFamilyName.trim())).toLowerCase();
  297. }
  298. function testHiddenElement(element, computedStyle, markedElements) {
  299. let hidden = false;
  300. if (computedStyle) {
  301. const display = computedStyle.getPropertyValue("display");
  302. const opacity = computedStyle.getPropertyValue("opacity");
  303. const visibility = computedStyle.getPropertyValue("visibility");
  304. hidden = display == "none";
  305. if (hidden) {
  306. if (element.style.getPropertyValue("display") != "none" && !IGNORED_REMOVED_TAG_NAMES.includes(element.tagName)) {
  307. element.setAttribute(HIDDEN_CONTENT_ATTRIBUTE_NAME, "");
  308. markedElements.push(element);
  309. }
  310. } else if ((opacity == "0" || visibility == "hidden") && element.getBoundingClientRect) {
  311. const boundingRect = element.getBoundingClientRect();
  312. hidden = !boundingRect.width && !boundingRect.height;
  313. }
  314. }
  315. return Boolean(hidden);
  316. }
  317. function postProcessDoc(doc, markedElements) {
  318. doc.querySelectorAll("[" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "]").forEach(element => {
  319. element.textContent = element.getAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  320. element.removeAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  321. });
  322. doc.querySelectorAll("meta[disabled-http-equiv]").forEach(element => {
  323. element.setAttribute("http-equiv", element.getAttribute("disabled-http-equiv"));
  324. element.removeAttribute("disabled-http-equiv");
  325. });
  326. if (doc.head) {
  327. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
  328. }
  329. if (!markedElements) {
  330. const singleFileAttributes = [REMOVED_CONTENT_ATTRIBUTE_NAME, HIDDEN_FRAME_ATTRIBUTE_NAME, HIDDEN_CONTENT_ATTRIBUTE_NAME, PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, IMAGE_ATTRIBUTE_NAME, POSTER_ATTRIBUTE_NAME, CANVAS_ATTRIBUTE_NAME, INPUT_VALUE_ATTRIBUTE_NAME, SHADOW_ROOT_ATTRIBUTE_NAME, HTML_IMPORT_ATTRIBUTE_NAME, STYLESHEET_ATTRIBUTE_NAME, ASYNC_SCRIPT_ATTRIBUTE_NAME];
  331. markedElements = doc.querySelectorAll(singleFileAttributes.map(name => "[" + name + "]").join(","));
  332. }
  333. markedElements.forEach(element => {
  334. element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME);
  335. element.removeAttribute(HIDDEN_CONTENT_ATTRIBUTE_NAME);
  336. element.removeAttribute(HIDDEN_FRAME_ATTRIBUTE_NAME);
  337. element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME);
  338. element.removeAttribute(IMAGE_ATTRIBUTE_NAME);
  339. element.removeAttribute(POSTER_ATTRIBUTE_NAME);
  340. element.removeAttribute(CANVAS_ATTRIBUTE_NAME);
  341. element.removeAttribute(INPUT_VALUE_ATTRIBUTE_NAME);
  342. element.removeAttribute(SHADOW_ROOT_ATTRIBUTE_NAME);
  343. element.removeAttribute(HTML_IMPORT_ATTRIBUTE_NAME);
  344. element.removeAttribute(STYLESHEET_ATTRIBUTE_NAME);
  345. element.removeAttribute(ASYNC_SCRIPT_ATTRIBUTE_NAME);
  346. });
  347. }
  348. function getStylesheetsData(doc) {
  349. if (doc) {
  350. const contents = [];
  351. doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
  352. try {
  353. const tempStyleElement = doc.createElement("style");
  354. tempStyleElement.textContent = styleElement.textContent;
  355. doc.body.appendChild(tempStyleElement);
  356. const stylesheet = tempStyleElement.sheet;
  357. tempStyleElement.remove();
  358. if (!stylesheet || stylesheet.cssRules.length != styleElement.sheet.cssRules.length) {
  359. styleElement.setAttribute(STYLESHEET_ATTRIBUTE_NAME, styleIndex);
  360. contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(cssRule => cssRule.cssText).join("\n");
  361. }
  362. } catch (error) {
  363. // ignored
  364. }
  365. });
  366. return contents;
  367. }
  368. }
  369. function getSize(win, imageElement, computedStyle) {
  370. let pxWidth = imageElement.naturalWidth;
  371. let pxHeight = imageElement.naturalHeight;
  372. if (!pxWidth && !pxHeight) {
  373. computedStyle = computedStyle || win.getComputedStyle(imageElement);
  374. let removeBorderWidth = false;
  375. if (computedStyle.getPropertyValue("box-sizing") == "content-box") {
  376. const boxSizingValue = imageElement.style.getPropertyValue("box-sizing");
  377. const boxSizingPriority = imageElement.style.getPropertyPriority("box-sizing");
  378. const clientWidth = imageElement.clientWidth;
  379. imageElement.style.setProperty("box-sizing", "border-box", "important");
  380. removeBorderWidth = imageElement.clientWidth != clientWidth;
  381. if (boxSizingValue) {
  382. imageElement.style.setProperty("box-sizing", boxSizingValue, boxSizingPriority);
  383. } else {
  384. imageElement.style.removeProperty("box-sizing");
  385. }
  386. }
  387. let paddingLeft, paddingRight, paddingTop, paddingBottom, borderLeft, borderRight, borderTop, borderBottom;
  388. paddingLeft = getWidth("padding-left", computedStyle);
  389. paddingRight = getWidth("padding-right", computedStyle);
  390. paddingTop = getWidth("padding-top", computedStyle);
  391. paddingBottom = getWidth("padding-bottom", computedStyle);
  392. if (removeBorderWidth) {
  393. borderLeft = getWidth("border-left-width", computedStyle);
  394. borderRight = getWidth("border-right-width", computedStyle);
  395. borderTop = getWidth("border-top-width", computedStyle);
  396. borderBottom = getWidth("border-bottom-width", computedStyle);
  397. } else {
  398. borderLeft = borderRight = borderTop = borderBottom = 0;
  399. }
  400. pxWidth = Math.max(0, imageElement.clientWidth - paddingLeft - paddingRight - borderLeft - borderRight);
  401. pxHeight = Math.max(0, imageElement.clientHeight - paddingTop - paddingBottom - borderTop - borderBottom);
  402. }
  403. return { pxWidth, pxHeight };
  404. }
  405. function getWidth(styleName, computedStyle) {
  406. if (computedStyle.getPropertyValue(styleName).endsWith("px")) {
  407. return parseFloat(computedStyle.getPropertyValue(styleName));
  408. }
  409. }
  410. function getFontsData() {
  411. if (singlefile.lib.processors.hooks.content.frames) {
  412. return singlefile.lib.processors.hooks.content.frames.getFontsData();
  413. }
  414. }
  415. function serialize(doc) {
  416. const docType = doc.doctype;
  417. let docTypeString = "";
  418. if (docType) {
  419. docTypeString = "<!DOCTYPE " + docType.nodeName;
  420. if (docType.publicId) {
  421. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  422. if (docType.systemId) {
  423. docTypeString += " \"" + docType.systemId + "\"";
  424. }
  425. } else if (docType.systemId) {
  426. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  427. } if (docType.internalSubset) {
  428. docTypeString += " [" + docType.internalSubset + "]";
  429. }
  430. docTypeString += "> ";
  431. }
  432. return docTypeString + doc.documentElement.outerHTML;
  433. }
  434. function removeQuotes(string) {
  435. if (string.match(REGEXP_SIMPLE_QUOTES_STRING)) {
  436. string = string.replace(REGEXP_SIMPLE_QUOTES_STRING, "$1");
  437. } else {
  438. string = string.replace(REGEXP_DOUBLE_QUOTES_STRING, "$1");
  439. }
  440. return string.trim();
  441. }
  442. function getFontWeight(weight) {
  443. return FONT_WEIGHTS[weight] || weight;
  444. }
  445. function flatten(array) {
  446. return array.flat ? array.flat() : array.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
  447. }
  448. })();