single-file-helper.js 20 KB

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