Skip to content

Commit 41e6986

Browse files
committed
fix: use proper tree walker when detaching invisible elements
1 parent e7a1c4c commit 41e6986

1 file changed

Lines changed: 21 additions & 21 deletions

File tree

src/services/puppeteer.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -414,28 +414,28 @@ function detachDisplayNoneElements(root) {
414414
return detached;
415415
}
416416
const skipTags = new Set(['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE', 'LINK', 'META', 'HEAD']);
417-
const stack = [startRoot];
418-
while (stack.length) {
419-
const elem = stack.pop();
420-
const children = Array.from(elem.children);
421-
for (const child of children) {
422-
if (skipTags.has(child.tagName)) {
423-
continue;
424-
}
425-
let cs;
426-
try {
427-
cs = window.getComputedStyle(child);
428-
} catch (err) {
429-
cs = null;
430-
}
431-
if (cs && cs.display === 'none') {
432-
const marker = document.createComment('jina-detached-invisible');
433-
child.parentNode.replaceChild(marker, child);
434-
detached.push({ marker, elem: child });
435-
} else {
436-
stack.push(child);
437-
}
417+
const toDetach = [];
418+
const walker = document.createTreeWalker(startRoot, NodeFilter.SHOW_ELEMENT, (node) => {
419+
if (skipTags.has(node.tagName)) {
420+
return NodeFilter.FILTER_REJECT;
421+
}
422+
let cs;
423+
try {
424+
cs = window.getComputedStyle(node);
425+
} catch (e) {
426+
cs = null;
438427
}
428+
if (cs && cs.display === 'none') {
429+
toDetach.push(node);
430+
return NodeFilter.FILTER_REJECT;
431+
}
432+
return NodeFilter.FILTER_ACCEPT;
433+
});
434+
while (walker.nextNode()) { /* traversal drives the filter */ }
435+
for (const elem of toDetach) {
436+
const marker = document.createComment('jina-detached-invisible');
437+
elem.parentNode.replaceChild(marker, elem);
438+
detached.push({ marker, elem });
439439
}
440440
return detached;
441441
}

0 commit comments

Comments
 (0)