Skip to content

Commit f70a0d2

Browse files
committed
refactor(dom-snapshot): update synthetic node handling and improve accessible name resolution for non-interactive elements
1 parent b3b5570 commit f70a0d2

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

packages/dom-snapshot/src/collector.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function createNodeFromElement(
209209
options: CollectorOptions,
210210
_idToNode: DomSnapshotFlatMap,
211211
rootDocument: Document,
212-
isSynthetic: boolean,
212+
_isSynthetic: boolean,
213213
): DomSnapshotNode {
214214
const nodeId = ensureElementUid(element);
215215
const role = resolveRole(element);
@@ -318,15 +318,9 @@ function createNodeFromElement(
318318
}
319319
}
320320

321-
// For synthetic nodes without a name, derive one from text content
322-
if (isSynthetic && !node.name) {
323-
const syntheticTextContent = normalizeTextContent(
324-
element.textContent || "",
325-
);
326-
if (syntheticTextContent) {
327-
node.name = syntheticTextContent.slice(0, options.maxTextLength);
328-
}
329-
}
321+
// For synthetic nodes without a name, we no longer derive from textContent
322+
// The text content is already captured via StaticText child nodes
323+
// Setting a massive name from all descendant text creates redundancy and noise
330324

331325
return node;
332326
}
@@ -471,8 +465,20 @@ function resolveAccessibleName(
471465
}
472466
}
473467

474-
const textContent = normalizeTextContent(element.textContent || "");
475-
return textContent || null;
468+
// For other elements, only use textContent as name if the element is interactive
469+
// Non-interactive containers should not derive name from their descendant text
470+
const tagName = element.tagName.toLowerCase();
471+
const role = element.getAttribute("role") || "";
472+
const isInteractive =
473+
INTERACTIVE_ROLES.has(role) || INTERACTIVE_TAGS.has(tagName);
474+
475+
if (isInteractive) {
476+
const textContent = extractVisibleTextContent(element);
477+
return textContent || null;
478+
}
479+
480+
// For non-interactive elements, don't derive name from textContent
481+
return null;
476482
}
477483

478484
function resolveElementValue(element: Element): string | undefined {
@@ -519,14 +525,13 @@ function extractTextNodes(
519525
return;
520526
}
521527
const uid = `${ensureElementUid(element)}::text-${index}`;
522-
// StaticText nodes preserve full text content without truncation
523-
// as they provide important context for understanding page content
528+
// StaticText nodes use 'name' for text content
529+
// No need for 'textContent' field as it would be redundant
524530
const textNode: DomSnapshotNode = {
525531
id: uid,
526532
role: STATIC_TEXT_ROLE,
527533
name: text,
528534
children: [],
529-
textContent: text,
530535
};
531536
idToNode[uid] = textNode; // Add to flat map for consistency
532537
results.push(textNode);

0 commit comments

Comments
 (0)