Просмотр исходного кода

handles link[rel=import] elements

Gildas 7 лет назад
Родитель
Сommit
99df4db665
1 измененных файлов с 46 добавлено и 1 удалено
  1. 46 1
      lib/single-file/single-file-core.js

+ 46 - 1
lib/single-file/single-file-core.js

@@ -110,7 +110,7 @@ const SingleFileCore = (() => {
 			if (this.options.removeUnusedCSSRules) {
 				this.processor.removeUnusedCSSRules();
 			}
-			const initializationPromises = [this.processor.inlineStylesheets(true), this.processor.linkStylesheets(), this.processor.attributeStyles(true)];
+			const initializationPromises = [this.processor.inlineStylesheets(true), this.processor.linkStylesheets(), this.processor.attributeStyles(true), this.processor.linkRelImport(true)];
 			if (!this.options.removeFrames) {
 				initializationPromises.push(this.processor.frames(true));
 			}
@@ -145,6 +145,7 @@ const SingleFileCore = (() => {
 			if (!this.options.removeFrames) {
 				await this.processor.frames();
 			}
+			await this.processor.linkRelImport();
 			if (this.onprogress) {
 				this.onprogress(new ProgressEvent(PAGE_ENDED, { pageURL: this.options.url }));
 			}
@@ -485,6 +486,50 @@ const SingleFileCore = (() => {
 			}));
 		}
 
+		async linkRelImport(initialization) {
+			let linkElements = this.doc.querySelectorAll("link[rel=import][href]");
+			linkElements = DomUtil.removeNoScriptFrames(linkElements);
+			if (!this.relImportProcessors) {
+				this.relImportProcessors = new Map();
+			}
+			await Promise.all(linkElements.map(async linkElement => {
+				if (initialization) {
+					const resourceURL = linkElement.href;
+					const options = {
+						insertSingleFileComment: false,
+						insertFaviconLink: false,
+						url: resourceURL,
+						removeHiddenElements: this.options.removeHiddenElements,
+						removeUnusedCSSRules: this.options.removeUnusedCSSRules,
+						jsEnabled: this.options.jsEnabled,
+						removeScripts: this.options.removeScripts,
+						saveRawPage: this.options.saveRawPage,
+						compressHTML: this.options.compressHTML,
+						compressCSS: this.options.compressCSS,
+						lazyLoadImages: this.options.lazyLoadImages,
+						framesData: this.options.framesData
+					};
+					if (resourceURL) {
+						if (resourceURL && resourceURL != this.baseURI && DomUtil.testValidPath(resourceURL)) {
+							const processor = new PageProcessor(options);
+							this.relImportProcessors.set(linkElement, processor);
+							await processor.loadPage();
+							return processor.initialize();
+						}
+					}
+				} else {
+					const processor = this.relImportProcessors.get(linkElement);
+					if (processor) {
+						this.relImportProcessors.delete(linkElement);
+						const pageData = await processor.getPageData();
+						linkElement.setAttribute("href", "data:text/html," + pageData.content);
+					} else {
+						linkElement.setAttribute("href", "about:blank");
+					}
+				}
+			}));
+		}
+
 		async attributeStyles(initialization) {
 			await Promise.all(Array.from(this.doc.querySelectorAll("[style]")).map(async element => {
 				const stylesheetContent = initialization ? await DomProcessorHelper.resolveImportURLs(element.getAttribute("style"), this.baseURI) : await DomProcessorHelper.processStylesheet(element.getAttribute("style"), this.baseURI);