single-file-helper.js 19 KB

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