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

prevent resolution of URLs pointing to document anchors

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

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

@@ -149,6 +149,7 @@ this.SingleFileCore = (() => {
 				await this.processor.htmlImports();
 				await this.processor.htmlImports();
 			}
 			}
 			this.processor.removeEmptyInlineElements();
 			this.processor.removeEmptyInlineElements();
+			this.processor.removeBase();
 		}
 		}
 
 
 		getPageData() {
 		getPageData() {
@@ -313,6 +314,10 @@ this.SingleFileCore = (() => {
 			});
 			});
 		}
 		}
 
 
+		removeBase() {
+			this.doc.querySelectorAll("base").forEach(element => element.remove());
+		}
+
 		removeScripts() {
 		removeScripts() {
 			this.doc.querySelectorAll("script:not([type=\"application/ld+json\"])").forEach(element => element.remove());
 			this.doc.querySelectorAll("script:not([type=\"application/ld+json\"])").forEach(element => element.remove());
 		}
 		}
@@ -344,7 +349,12 @@ this.SingleFileCore = (() => {
 		}
 		}
 
 
 		resolveHrefs() {
 		resolveHrefs() {
-			this.doc.querySelectorAll("[href]").forEach(element => element.setAttribute("href", element.href));
+			this.doc.querySelectorAll("[href]").forEach(element => {
+				const match = element.href && element.href.match(/(.*)#.*$/);
+				if (!match || match[1] != this.baseURI) {
+					element.setAttribute("href", element.href);
+				}
+			});
 		}
 		}
 
 
 		removeUnusedCSSRules() {
 		removeUnusedCSSRules() {