Explorar o código

optimized code for speed

Gildas %!s(int64=7) %!d(string=hai) anos
pai
achega
f8a183946a
Modificáronse 1 ficheiros con 10 adicións e 17 borrados
  1. 10 17
      lib/single-file/doc-helper.js

+ 10 - 17
lib/single-file/doc-helper.js

@@ -23,7 +23,7 @@
 this.docHelper = this.docHelper || (() => {
 
 	const REMOVED_CONTENT_ATTRIBUTE_NAME = "data-single-file-removed-content";
-	const REMOVED_CANDIDATE_ATTRIBUTE_NAME = "data-single-file-removed-candidate";
+	// const REMOVED_CANDIDATE_ATTRIBUTE_NAME = "data-single-file-removed-candidate";
 	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";
@@ -59,17 +59,15 @@ this.docHelper = this.docHelper || (() => {
 		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) {
 			const markerRemovedContent = removedContentAttributeName(options.sessionId);
-			const markerRemovedCandidate = removedCandidateAttributeName(options.sessionId);
 			let ignoredTags = JSON.parse(JSON.stringify(IGNORED_REMOVED_TAG_NAMES));
 			if (!options.removeScripts) {
 				ignoredTags = ignoredTags.concat("SCRIPT");
 			}
 			if (win) {
-				markHiddenCandidates(win, doc.body, markerRemovedContent, markerRemovedCandidate, ignoredTags);
+				markHiddenCandidates(win, doc.body, markerRemovedContent, new Set(), ignoredTags, new Map());
 				markHiddenElements(win, doc.body, markerRemovedContent);
 				markBackdropBackground(doc, win, markerRemovedContent);
 			}
-			doc.querySelectorAll(("[" + markerRemovedCandidate + "]")).forEach(element => element.removeAttribute(markerRemovedCandidate));
 		}
 		if (win && options.compressHTML) {
 			doc.querySelectorAll("*").forEach(element => {
@@ -109,14 +107,14 @@ this.docHelper = this.docHelper || (() => {
 		}
 	}
 
-	function markHiddenCandidates(win, element, markerRemovedContent, markerRemovedCandidate, ignoredTags) {
+	function markHiddenCandidates(win, element, markerRemovedContent, removedCandidates, ignoredTags, cacheElementsHidden) {
 		const elements = Array.from(element.childNodes).filter(node => node.nodeType == win.Node.ELEMENT_NODE);
-		elements.forEach(element => markHiddenCandidates(win, element, markerRemovedContent, markerRemovedCandidate, ignoredTags));
+		elements.forEach(element => markHiddenCandidates(win, element, markerRemovedContent, removedCandidates, ignoredTags, cacheElementsHidden));
 		if (elements.length) {
-			const hiddenCandidate = !elements.find(element => element.getAttribute(markerRemovedCandidate) !== "");
+			const hiddenCandidate = !elements.find(element => !removedCandidates.has(element));
 			if (hiddenCandidate) {
-				if (hiddenElement(win, element, ignoredTags) && element instanceof win.HTMLElement) {
-					element.setAttribute(markerRemovedCandidate, "");
+				if (hiddenElement(win, element, ignoredTags, cacheElementsHidden) && element instanceof win.HTMLElement) {
+					removedCandidates.add(element);
 					elements.forEach(element => {
 						if (element instanceof win.HTMLElement) {
 							element.setAttribute(markerRemovedContent, "");
@@ -124,8 +122,8 @@ this.docHelper = this.docHelper || (() => {
 					});
 				}
 			}
-		} else if (hiddenElement(win, element, ignoredTags)) {
-			element.setAttribute(markerRemovedCandidate, "");
+		} else if (hiddenElement(win, element, ignoredTags, cacheElementsHidden)) {
+			removedCandidates.add(element);
 		}
 	}
 
@@ -137,8 +135,7 @@ this.docHelper = this.docHelper || (() => {
 		}
 	}
 
-	function hiddenElement(win, element, ignoredTags) {
-		const cacheElementsHidden = new Map();
+	function hiddenElement(win, element, ignoredTags, cacheElementsHidden) {
 		const hidden = testHiddenElement(win, element, ignoredTags, cacheElementsHidden);
 		if (!hidden) {
 			let parentElement = element.parentElement;
@@ -212,10 +209,6 @@ this.docHelper = this.docHelper || (() => {
 		return REMOVED_CONTENT_ATTRIBUTE_NAME + (sessionId || "");
 	}
 
-	function removedCandidateAttributeName(sessionId) {
-		return REMOVED_CANDIDATE_ATTRIBUTE_NAME + (sessionId || "");
-	}
-
 	function windowIdAttributeName(sessionId) {
 		return WIN_ID_ATTRIBUTE_NAME + (sessionId || "");
 	}