single-file-helper.js 20 KB

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