Skip to content

bug(core): Context Packet fallback selectors can target the wrong element #377

Description

@vamgan

Summary

Fallback selectors emitted in Context Packets can fail to identify the selected element.

Root cause

packages/core/src/context.ts, buildElementSelector(), computes a document-global index among annotated elements of the same tag and emits :nth-of-type(index). CSS nth-of-type() is sibling-relative, not document-global.

For repeated tags under different parents, generated selectors can match nothing or the wrong element. The method also scans every same-tag element for each packet.

Proposed fix

  • Prefer unique id and data-askable-id.
  • Otherwise construct a parent-relative selector path using sibling-local indexes.
  • Validate that the selector resolves uniquely to the original element before returning it.
  • Avoid a document-wide same-tag scan.

Acceptance criteria

  • Repeated tags under different parents resolve correctly
  • Unannotated siblings are handled
  • IDs and attribute values are safely escaped
  • Duplicate data-askable-id values do not produce a falsely unique selector
  • Shadow DOM behavior is documented/tested
  • Packet-generation benchmark covers large documents

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreCore package and cross-package runtime behaviorbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions