1
0

single-file-helper.js 20 KB

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