single-file-helper.js 20 KB

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