optimize element context parse (#1323)

This commit is contained in:
LawyZheng
2024-12-05 14:10:30 +08:00
committed by GitHub
parent 0099e78a2c
commit 247ee7d3dd

View File

@@ -889,10 +889,15 @@ function checkDisabledFromStyle(element) {
return false; return false;
} }
function getElementContext(element) { // element should always be the parent of stopped_element
function getElementContext(element, stopped_element) {
// dfs to collect the non unique_id context // dfs to collect the non unique_id context
let fullContext = new Array(); let fullContext = new Array();
if (element === stopped_element) {
return fullContext;
}
// sometimes '*' shows as an after custom style // sometimes '*' shows as an after custom style
const afterCustom = getElementComputedStyle(element, "::after") const afterCustom = getElementComputedStyle(element, "::after")
.getPropertyValue("content") .getPropertyValue("content")
@@ -915,7 +920,7 @@ function getElementContext(element) {
} }
} else if (child.nodeType === Node.ELEMENT_NODE) { } else if (child.nodeType === Node.ELEMENT_NODE) {
if (!child.hasAttribute("unique_id") && isElementVisible(child)) { if (!child.hasAttribute("unique_id") && isElementVisible(child)) {
childContext = getElementContext(child); childContext = getElementContext(child, stopped_element);
} }
} }
if (childContext.length > 0) { if (childContext.length > 0) {
@@ -1320,18 +1325,18 @@ function buildElementTree(starter = document.body, frame, full_tree = false) {
} }
const getContextByParent = (element, ctx) => { const getContextByParent = (element, ctx) => {
// for most elements, we're going 10 layers up to see if we can find "label" as a parent // for most elements, we're going 5 layers up to see if we can find "label" as a parent
// if found, most likely the context under label is relevant to this element // if found, most likely the context under label is relevant to this element
let targetParentElements = new Set(["label", "fieldset"]); let targetParentElements = new Set(["label", "fieldset"]);
// look up for 10 levels to find the most contextual parent element // look up for 5 levels to find the most contextual parent element
let targetContextualParent = null; let targetContextualParent = null;
let currentEle = getDOMElementBySkyvenElement(element); let currentEle = getDOMElementBySkyvenElement(element);
if (!currentEle) { if (!currentEle) {
return ctx; return ctx;
} }
let parentEle = currentEle; let parentEle = currentEle;
for (var i = 0; i < 10; i++) { for (var i = 0; i < 5; i++) {
parentEle = parentEle.parentElement; parentEle = parentEle.parentElement;
if (parentEle) { if (parentEle) {
if ( if (
@@ -1355,10 +1360,10 @@ function buildElementTree(starter = document.body, frame, full_tree = false) {
// fieldset is usually within a form or another element that contains the whole context // fieldset is usually within a form or another element that contains the whole context
targetContextualParent = targetContextualParent.parentElement; targetContextualParent = targetContextualParent.parentElement;
if (targetContextualParent) { if (targetContextualParent) {
context = getElementContext(targetContextualParent); context = getElementContext(targetContextualParent, currentEle);
} }
} else { } else {
context = getElementContext(targetContextualParent); context = getElementContext(targetContextualParent, currentEle);
} }
if (context.length > 0) { if (context.length > 0) {
ctx.push(context); ctx.push(context);
@@ -1440,13 +1445,13 @@ function buildElementTree(starter = document.body, frame, full_tree = false) {
) { ) {
let grandParentElement = parentElement.parentElement; let grandParentElement = parentElement.parentElement;
if (grandParentElement) { if (grandParentElement) {
let context = getElementContext(grandParentElement); let context = getElementContext(grandParentElement, curElement);
if (context.length > 0) { if (context.length > 0) {
ctx.push(context); ctx.push(context);
} }
} }
} }
let context = getElementContext(parentElement); let context = getElementContext(parentElement, curElement);
if (context.length > 0) { if (context.length > 0) {
ctx.push(context); ctx.push(context);
} }