single-file-helper.js 19 KB

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