Ver Fonte

defined global regexps

Gildas há 7 anos atrás
pai
commit
0c705bdf4f
1 ficheiros alterados com 10 adições e 4 exclusões
  1. 10 4
      lib/single-file/single-file-core.js

+ 10 - 4
lib/single-file/single-file-core.js

@@ -440,8 +440,8 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			this.doc.querySelectorAll("[href]").forEach(element => {
 				if (element.href) {
 					const href = element.href.baseVal ? element.href.baseVal : element.href;
-					const match = href.match(/(.*)#.*$/);
-					if (!match || match[1] != this.baseURI) {
+					const normalizedHref = DomUtil.normalizeURL(href);
+					if (!normalizedHref || normalizedHref != this.baseURI) {
 						element.setAttribute("href", href);
 					}
 				}
@@ -759,6 +759,12 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 	// ---------
 	// DomHelper
 	// ---------
+	const REGEXP_AMP = /&/g;
+	const REGEXP_NBSP = /\u00a0/g;
+	const REGEXP_START_TAG = /</g;
+	const REGEXP_END_TAG = />/g;
+	const REGEXP_URL_HASH = /(#.+?)$/;
+
 	class DomProcessorHelper {
 		static setFrameEmptySrc(frameElement) {
 			if (frameElement.tagName == "OBJECT") {
@@ -840,7 +846,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 					}
 				} else {
 					if (resourceURL.startsWith(DATA_URI_PREFIX)) {
-						const escapedResourceURL = resourceURL.replace(/&/g, "&amp;").replace(/\u00a0/g, "&nbsp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
+						const escapedResourceURL = resourceURL.replace(REGEXP_AMP, "&amp;").replace(REGEXP_NBSP, "&nbsp;").replace(REGEXP_START_TAG, "&lt;").replace(REGEXP_END_TAG, "&gt;");
 						if (escapedResourceURL != resourceURL && stylesheetContent.includes(urlFunction)) {
 							stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(urlFunction), urlFunction.replace(originalResourceURL, escapedResourceURL));
 						}
@@ -914,7 +920,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 							const DOMParser = DOM.getParser();
 							if (DOMParser) {
 								const svgDoc = new DOMParser().parseFromString(content, "image/svg+xml");
-								const hashMatch = originalResourceURL.match(/(#.+?)$/);
+								const hashMatch = originalResourceURL.match(REGEXP_URL_HASH);
 								if (hashMatch && hashMatch[0]) {
 									const symbolElement = svgDoc.querySelector(hashMatch[0]);
 									if (symbolElement) {