| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- /*
- * Copyright 2018 Gildas Lormeau
- * contact : gildas.lormeau <at> gmail.com
- *
- * This file is part of SingleFile.
- *
- * SingleFile is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * SingleFile is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
- */
- /* global fontFaceProxy */
- this.docHelper = this.docHelper || (() => {
- const REMOVED_CONTENT_ATTRIBUTE_NAME = "data-single-file-removed-content";
- const PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME = "data-single-file-preserved-space-element";
- const WIN_ID_ATTRIBUTE_NAME = "data-frame-tree-win-id";
- const RESPONSIVE_IMAGE_ATTRIBUTE_NAME = "data-single-file-responsive-image";
- const IMAGE_ATTRIBUTE_NAME = "data-single-file-image";
- const INPUT_VALUE_ATTRIBUTE_NAME = "data-single-file-value";
- return {
- preProcessDoc,
- postProcessDoc,
- serialize,
- windowIdAttributeName,
- preservedSpaceAttributeName,
- removedContentAttributeName,
- responsiveImagesAttributeName,
- imagesAttributeName,
- inputValueAttributeName
- };
- function preProcessDoc(doc, win, options) {
- doc.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
- doc.querySelectorAll("noscript").forEach(element => {
- const disabledNoscriptElement = doc.createElement("disabled-noscript");
- Array.from(element.childNodes).forEach(node => disabledNoscriptElement.appendChild(node));
- disabledNoscriptElement.hidden = true;
- element.parentElement.replaceChild(disabledNoscriptElement, element);
- });
- doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.hidden = true);
- if (options.removeHiddenElements) {
- doc.querySelectorAll("html > body *:not(style):not(script):not(link):not(frame):not(iframe):not(object):not(meta):not(title):not(meta):not(noscript):not(template)").forEach(element => {
- const style = win.getComputedStyle(element);
- if (element instanceof win.HTMLElement && style && (element.hidden || style.display == "none" || ((style.opacity === 0 || style.visibility == "hidden") && !element.clientWidth && !element.clientHeight)) && !element.querySelector("iframe, frame, object[type=\"text/html\"][data]")) {
- element.setAttribute(removedContentAttributeName(options.sessionId), "");
- }
- });
- }
- if (options.compressHTML) {
- doc.querySelectorAll("*").forEach(element => {
- const style = win.getComputedStyle(element);
- if (style && style.whiteSpace.startsWith("pre")) {
- element.setAttribute(preservedSpaceAttributeName(options.sessionId), "");
- }
- });
- }
- retrieveInputValues(doc, options);
- return {
- canvasData: getCanvasData(doc),
- fontsData: getFontsData(doc),
- stylesheetContents: getStylesheetContents(doc),
- responsiveImageData: getResponsiveImageData(doc, options),
- imageData: getImageData(doc, options),
- postersData: getPostersData(doc)
- };
- }
- function postProcessDoc(doc, options) {
- doc.querySelectorAll("disabled-noscript").forEach(element => {
- const noscriptElement = doc.createElement("noscript");
- Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
- element.parentElement.replaceChild(noscriptElement, element);
- });
- doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
- if (options.removeHiddenElements) {
- doc.querySelectorAll("[" + removedContentAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(removedContentAttributeName(options.sessionId)));
- }
- if (options.compressHTML) {
- doc.querySelectorAll("[" + preservedSpaceAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(preservedSpaceAttributeName(options.sessionId)));
- }
- doc.querySelectorAll("[" + responsiveImagesAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(responsiveImagesAttributeName(options.sessionId)));
- doc.querySelectorAll("[" + imagesAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(imagesAttributeName(options.sessionId)));
- doc.querySelectorAll("[" + inputValueAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(inputValueAttributeName(options.sessionId)));
- }
- function responsiveImagesAttributeName(sessionId) {
- return RESPONSIVE_IMAGE_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
- }
- function imagesAttributeName(sessionId) {
- return IMAGE_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
- }
- function preservedSpaceAttributeName(sessionId) {
- return PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
- }
- function removedContentAttributeName(sessionId) {
- return REMOVED_CONTENT_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
- }
- function windowIdAttributeName(sessionId) {
- return WIN_ID_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
- }
- function inputValueAttributeName(sessionId) {
- return INPUT_VALUE_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
- }
- function getCanvasData(doc) {
- if (doc) {
- const canvasData = [];
- doc.querySelectorAll("canvas").forEach(canvasElement => {
- try {
- canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
- } catch (error) {
- canvasData.push(null);
- }
- });
- return canvasData;
- }
- }
- function getStylesheetContents(doc) {
- if (doc) {
- const contents = [];
- doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
- let stylesheet;
- try {
- const tempStyleElement = doc.createElement("style");
- tempStyleElement.textContent = styleElement.textContent;
- doc.body.appendChild(tempStyleElement);
- stylesheet = tempStyleElement.sheet;
- tempStyleElement.remove();
- if (!stylesheet || stylesheet.cssRules.length != styleElement.sheet.cssRules.length) {
- contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n");
- }
- } catch (error) {
- /* ignored */
- }
- });
- return contents;
- }
- }
- function getImageData(doc, options) {
- if (doc) {
- const data = [];
- doc.querySelectorAll("img[src]").forEach((imageElement, imageElementIndex) => {
- imageElement.setAttribute(imagesAttributeName(options.sessionId), imageElementIndex);
- let imageData;
- if (imageElement.src) {
- imageData = {
- width: imageElement.width,
- height: imageElement.height,
- clientWidth: imageElement.clientWidth,
- clientHeight: imageElement.clientHeight,
- naturalWidth: imageElement.naturalWidth,
- naturalHeight: imageElement.naturalHeight
- };
- }
- data.push(imageData);
- });
- return data;
- }
- }
- function getResponsiveImageData(doc, options) {
- if (doc) {
- const data = [];
- doc.querySelectorAll("picture, img[srcset]").forEach((element, elementIndex) => {
- const tagName = element.tagName.toLowerCase();
- let imageData = {}, imageElement;
- element.setAttribute(responsiveImagesAttributeName(options.sessionId), elementIndex);
- if (tagName == "picture") {
- const sources = Array.from(element.querySelectorAll("source")).map(sourceElement => (
- { src: sourceElement.src, srcset: sourceElement.srcset }
- ));
- imageElement = element.querySelector("img");
- imageData.sources = sources;
- }
- if (tagName == "img") {
- imageElement = element;
- }
- if (imageElement) {
- imageData.source = {
- clientWidth: imageElement.clientWidth,
- clientHeight: imageElement.clientHeight,
- naturalWidth: imageElement.naturalWidth,
- naturalHeight: imageElement.naturalHeight,
- width: imageElement.width,
- height: imageElement.height,
- src: (!imageElement.currentSrc.startsWith("data:") && imageElement.currentSrc) || (!imageElement.src.startsWith("data:") && imageElement.src)
- };
- }
- data.push(imageData);
- });
- return data;
- }
- }
- function getPostersData(doc) {
- if (doc) {
- const postersData = [];
- doc.querySelectorAll("video").forEach(videoElement => {
- if (videoElement.poster) {
- postersData.push(null);
- } else {
- const canvasElement = doc.createElement("canvas");
- const context = canvasElement.getContext("2d");
- canvasElement.width = videoElement.clientWidth;
- canvasElement.height = videoElement.clientHeight;
- try {
- context.drawImage(videoElement, 0, 0, canvasElement.width, canvasElement.height);
- postersData.push(canvasElement.toDataURL("image/png", ""));
- } catch (error) {
- postersData.push(null);
- }
- }
- });
- return postersData;
- }
- }
- function getFontsData() {
- if (typeof fontFaceProxy != "undefined") {
- return fontFaceProxy.getFontsData();
- }
- }
- function retrieveInputValues(doc, options) {
- doc.querySelectorAll("input").forEach(input => input.setAttribute(inputValueAttributeName(options.sessionId), input.value));
- doc.querySelectorAll("textarea").forEach(textarea => textarea.setAttribute(inputValueAttributeName(options.sessionId), textarea.value));
- doc.querySelectorAll("select").forEach(select => {
- select.querySelectorAll("option").forEach(option => {
- if (option.selected) {
- option.setAttribute(inputValueAttributeName(options.sessionId), "");
- }
- });
- });
- }
- function serialize(doc) {
- const docType = doc.doctype;
- let docTypeString = "";
- if (docType) {
- docTypeString = "<!DOCTYPE " + docType.nodeName;
- if (docType.publicId) {
- docTypeString += " PUBLIC \"" + docType.publicId + "\"";
- if (docType.systemId) {
- docTypeString += " \"" + docType.systemId + "\"";
- }
- } else if (docType.systemId) {
- docTypeString += " SYSTEM \"" + docType.systemId + "\"";
- } if (docType.internalSubset) {
- docTypeString += " [" + docType.internalSubset + "]";
- }
- docTypeString += "> ";
- }
- return docTypeString + doc.documentElement.outerHTML;
- }
- })();
|