single-file-helper.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 = window.addEventListener;
  59. const dispatchEvent = window.dispatchEvent;
  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. if (element.shadowRoot) {
  180. const shadowRootInfo = {};
  181. element.setAttribute(SHADOW_ROOT_ATTRIBUTE_NAME, data.shadowRoots.length);
  182. data.markedElements.push(element);
  183. data.shadowRoots.push(shadowRootInfo);
  184. getElementsInfo(win, doc, element.shadowRoot, options, data, elementHidden);
  185. shadowRootInfo.content = element.shadowRoot.innerHTML;
  186. shadowRootInfo.delegatesFocus = element.shadowRoot.delegatesFocus;
  187. shadowRootInfo.mode = element.shadowRoot.mode;
  188. if (element.shadowRoot.adoptedStyleSheets && element.shadowRoot.adoptedStyleSheets.length) {
  189. shadowRootInfo.adoptedStyleSheets = Array.from(element.shadowRoot.adoptedStyleSheets).map(stylesheet => Array.from(stylesheet.cssRules).map(cssRule => cssRule.cssText).join("\n"));
  190. }
  191. }
  192. getElementsInfo(win, doc, element, options, data, elementHidden);
  193. if (!options.autoSaveExternalSave && options.removeHiddenElements && ascendantHidden) {
  194. if (elementKept || element.getAttribute(KEPT_CONTENT_ATTRIBUTE_NAME) == "") {
  195. if (element.parentElement) {
  196. element.parentElement.setAttribute(KEPT_CONTENT_ATTRIBUTE_NAME, "");
  197. data.markedElements.push(element.parentElement);
  198. }
  199. } else if (elementHidden) {
  200. element.setAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
  201. data.markedElements.push(element);
  202. }
  203. }
  204. });
  205. return data;
  206. }
  207. function getResourcesInfo(win, doc, element, options, data, elementHidden, computedStyle) {
  208. if (element.tagName == "CANVAS") {
  209. try {
  210. const size = getSize(win, element, computedStyle);
  211. data.canvases.push({ dataURI: element.toDataURL("image/png", ""), width: size.width, height: size.height });
  212. element.setAttribute(CANVAS_ATTRIBUTE_NAME, data.canvases.length - 1);
  213. data.markedElements.push(element);
  214. } catch (error) {
  215. // ignored
  216. }
  217. }
  218. if (element.tagName == "IMG") {
  219. const imageData = {
  220. currentSrc: elementHidden ?
  221. "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" :
  222. (options.loadDeferredImages && element.getAttribute(LAZY_SRC_ATTRIBUTE_NAME)) || element.currentSrc
  223. };
  224. data.images.push(imageData);
  225. element.setAttribute(IMAGE_ATTRIBUTE_NAME, data.images.length - 1);
  226. data.markedElements.push(element);
  227. element.removeAttribute(LAZY_SRC_ATTRIBUTE_NAME);
  228. computedStyle = computedStyle || win.getComputedStyle(element);
  229. if (computedStyle) {
  230. imageData.size = getSize(win, element, computedStyle);
  231. const boxShadow = computedStyle.getPropertyValue("box-shadow");
  232. const backgroundImage = computedStyle.getPropertyValue("background-image");
  233. if ((!boxShadow || boxShadow == "none") &&
  234. (!backgroundImage || backgroundImage == "none") &&
  235. (imageData.size.pxWidth > 1 || imageData.size.pxHeight > 1)) {
  236. imageData.replaceable = true;
  237. imageData.backgroundColor = computedStyle.getPropertyValue("background-color");
  238. imageData.objectFit = computedStyle.getPropertyValue("object-fit");
  239. imageData.boxSizing = computedStyle.getPropertyValue("box-sizing");
  240. imageData.objectPosition = computedStyle.getPropertyValue("object-position");
  241. }
  242. }
  243. }
  244. if (element.tagName == "VIDEO") {
  245. if (!element.poster) {
  246. const canvasElement = doc.createElement("canvas");
  247. const context = canvasElement.getContext("2d");
  248. canvasElement.width = element.clientWidth;
  249. canvasElement.height = element.clientHeight;
  250. try {
  251. context.drawImage(element, 0, 0, canvasElement.width, canvasElement.height);
  252. data.posters.push(canvasElement.toDataURL("image/png", ""));
  253. element.setAttribute(POSTER_ATTRIBUTE_NAME, data.posters.length - 1);
  254. data.markedElements.push(element);
  255. } catch (error) {
  256. // ignored
  257. }
  258. }
  259. }
  260. if (element.tagName == "IFRAME") {
  261. if (elementHidden && options.removeHiddenElements) {
  262. element.setAttribute(HIDDEN_FRAME_ATTRIBUTE_NAME, "");
  263. data.markedElements.push(element);
  264. }
  265. }
  266. if (element.tagName == "LINK") {
  267. if (element.import) {
  268. data.imports.push({ content: serialize(element.import) });
  269. element.setAttribute(HTML_IMPORT_ATTRIBUTE_NAME, data.imports.length - 1);
  270. data.markedElements.push(element);
  271. }
  272. }
  273. if (element.tagName == "INPUT") {
  274. if (element.type != "password") {
  275. element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.value);
  276. data.markedElements.push(element);
  277. }
  278. if (element.type == "radio" || element.type == "checkbox") {
  279. element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.checked);
  280. data.markedElements.push(element);
  281. }
  282. }
  283. if (element.tagName == "TEXTAREA") {
  284. element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.value);
  285. data.markedElements.push(element);
  286. }
  287. if (element.tagName == "SELECT") {
  288. element.querySelectorAll("option").forEach(option => {
  289. if (option.selected) {
  290. option.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, "");
  291. data.markedElements.push(option);
  292. }
  293. });
  294. }
  295. if (element.tagName == "SCRIPT") {
  296. if (element.async && element.getAttribute("async") != "" && element.getAttribute("async") != "async") {
  297. element.setAttribute(ASYNC_SCRIPT_ATTRIBUTE_NAME, "");
  298. data.markedElements.push(element);
  299. }
  300. element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>");
  301. }
  302. }
  303. function getUsedFont(computedStyle, options, usedFonts) {
  304. if (computedStyle) {
  305. const fontStyle = computedStyle.getPropertyValue("font-style") || "normal";
  306. computedStyle.getPropertyValue("font-family").split(",").forEach(fontFamilyName => {
  307. fontFamilyName = normalizeFontFamily(fontFamilyName);
  308. if (!options.loadedFonts || options.loadedFonts.find(font => normalizeFontFamily(font.family) == fontFamilyName && font.style == fontStyle)) {
  309. const fontWeight = getFontWeight(computedStyle.getPropertyValue("font-weight"));
  310. const fontVariant = computedStyle.getPropertyValue("font-variant") || "normal";
  311. const value = [fontFamilyName, fontWeight, fontStyle, fontVariant];
  312. usedFonts.set(JSON.stringify(value), [fontFamilyName, fontWeight, fontStyle, fontVariant]);
  313. }
  314. });
  315. }
  316. }
  317. function normalizeFontFamily(fontFamilyName = "") {
  318. return removeQuotes(singlefile.lib.vendor.cssUnescape.process(fontFamilyName.trim())).toLowerCase();
  319. }
  320. function testHiddenElement(element, computedStyle) {
  321. let hidden = false;
  322. if (computedStyle) {
  323. const display = computedStyle.getPropertyValue("display");
  324. const opacity = computedStyle.getPropertyValue("opacity");
  325. const visibility = computedStyle.getPropertyValue("visibility");
  326. hidden = display == "none";
  327. if (!hidden && (opacity == "0" || visibility == "hidden") && element.getBoundingClientRect) {
  328. const boundingRect = element.getBoundingClientRect();
  329. hidden = !boundingRect.width && !boundingRect.height;
  330. }
  331. }
  332. return Boolean(hidden);
  333. }
  334. function postProcessDoc(doc, markedElements) {
  335. doc.querySelectorAll("[" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "]").forEach(element => {
  336. element.textContent = element.getAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  337. element.removeAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  338. });
  339. doc.querySelectorAll("meta[disabled-http-equiv]").forEach(element => {
  340. element.setAttribute("http-equiv", element.getAttribute("disabled-http-equiv"));
  341. element.removeAttribute("disabled-http-equiv");
  342. });
  343. if (doc.head) {
  344. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
  345. }
  346. if (!markedElements) {
  347. 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];
  348. markedElements = doc.querySelectorAll(singleFileAttributes.map(name => "[" + name + "]").join(","));
  349. }
  350. markedElements.forEach(element => {
  351. element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME);
  352. element.removeAttribute(HIDDEN_CONTENT_ATTRIBUTE_NAME);
  353. element.removeAttribute(KEPT_CONTENT_ATTRIBUTE_NAME);
  354. element.removeAttribute(HIDDEN_FRAME_ATTRIBUTE_NAME);
  355. element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME);
  356. element.removeAttribute(IMAGE_ATTRIBUTE_NAME);
  357. element.removeAttribute(POSTER_ATTRIBUTE_NAME);
  358. element.removeAttribute(CANVAS_ATTRIBUTE_NAME);
  359. element.removeAttribute(INPUT_VALUE_ATTRIBUTE_NAME);
  360. element.removeAttribute(SHADOW_ROOT_ATTRIBUTE_NAME);
  361. element.removeAttribute(HTML_IMPORT_ATTRIBUTE_NAME);
  362. element.removeAttribute(STYLESHEET_ATTRIBUTE_NAME);
  363. element.removeAttribute(ASYNC_SCRIPT_ATTRIBUTE_NAME);
  364. });
  365. }
  366. function getStylesheetsData(doc) {
  367. if (doc) {
  368. const contents = [];
  369. doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
  370. try {
  371. const tempStyleElement = doc.createElement("style");
  372. tempStyleElement.textContent = styleElement.textContent;
  373. doc.body.appendChild(tempStyleElement);
  374. const stylesheet = tempStyleElement.sheet;
  375. tempStyleElement.remove();
  376. if (!stylesheet || stylesheet.cssRules.length != styleElement.sheet.cssRules.length) {
  377. styleElement.setAttribute(STYLESHEET_ATTRIBUTE_NAME, styleIndex);
  378. contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(cssRule => cssRule.cssText).join("\n");
  379. }
  380. } catch (error) {
  381. // ignored
  382. }
  383. });
  384. return contents;
  385. }
  386. }
  387. function getSize(win, imageElement, computedStyle) {
  388. let pxWidth = imageElement.naturalWidth;
  389. let pxHeight = imageElement.naturalHeight;
  390. if (!pxWidth && !pxHeight) {
  391. computedStyle = computedStyle || win.getComputedStyle(imageElement);
  392. let removeBorderWidth = false;
  393. if (computedStyle.getPropertyValue("box-sizing") == "content-box") {
  394. const boxSizingValue = imageElement.style.getPropertyValue("box-sizing");
  395. const boxSizingPriority = imageElement.style.getPropertyPriority("box-sizing");
  396. const clientWidth = imageElement.clientWidth;
  397. imageElement.style.setProperty("box-sizing", "border-box", "important");
  398. removeBorderWidth = imageElement.clientWidth != clientWidth;
  399. if (boxSizingValue) {
  400. imageElement.style.setProperty("box-sizing", boxSizingValue, boxSizingPriority);
  401. } else {
  402. imageElement.style.removeProperty("box-sizing");
  403. }
  404. }
  405. let paddingLeft, paddingRight, paddingTop, paddingBottom, borderLeft, borderRight, borderTop, borderBottom;
  406. paddingLeft = getWidth("padding-left", computedStyle);
  407. paddingRight = getWidth("padding-right", computedStyle);
  408. paddingTop = getWidth("padding-top", computedStyle);
  409. paddingBottom = getWidth("padding-bottom", computedStyle);
  410. if (removeBorderWidth) {
  411. borderLeft = getWidth("border-left-width", computedStyle);
  412. borderRight = getWidth("border-right-width", computedStyle);
  413. borderTop = getWidth("border-top-width", computedStyle);
  414. borderBottom = getWidth("border-bottom-width", computedStyle);
  415. } else {
  416. borderLeft = borderRight = borderTop = borderBottom = 0;
  417. }
  418. pxWidth = Math.max(0, imageElement.clientWidth - paddingLeft - paddingRight - borderLeft - borderRight);
  419. pxHeight = Math.max(0, imageElement.clientHeight - paddingTop - paddingBottom - borderTop - borderBottom);
  420. }
  421. return { pxWidth, pxHeight };
  422. }
  423. function getWidth(styleName, computedStyle) {
  424. if (computedStyle.getPropertyValue(styleName).endsWith("px")) {
  425. return parseFloat(computedStyle.getPropertyValue(styleName));
  426. }
  427. }
  428. function getFontsData() {
  429. if (singlefile.lib.processors.hooks.content.frames) {
  430. return singlefile.lib.processors.hooks.content.frames.getFontsData();
  431. }
  432. }
  433. function serialize(doc) {
  434. const docType = doc.doctype;
  435. let docTypeString = "";
  436. if (docType) {
  437. docTypeString = "<!DOCTYPE " + docType.nodeName;
  438. if (docType.publicId) {
  439. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  440. if (docType.systemId) {
  441. docTypeString += " \"" + docType.systemId + "\"";
  442. }
  443. } else if (docType.systemId) {
  444. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  445. } if (docType.internalSubset) {
  446. docTypeString += " [" + docType.internalSubset + "]";
  447. }
  448. docTypeString += "> ";
  449. }
  450. return docTypeString + doc.documentElement.outerHTML;
  451. }
  452. function removeQuotes(string) {
  453. if (string.match(REGEXP_SIMPLE_QUOTES_STRING)) {
  454. string = string.replace(REGEXP_SIMPLE_QUOTES_STRING, "$1");
  455. } else {
  456. string = string.replace(REGEXP_DOUBLE_QUOTES_STRING, "$1");
  457. }
  458. return string.trim();
  459. }
  460. function getFontWeight(weight) {
  461. return FONT_WEIGHTS[weight.toLowerCase().trim()] || weight;
  462. }
  463. function flatten(array) {
  464. return array.flat ? array.flat() : array.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
  465. }
  466. })();