single-file-helper.js 20 KB

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