Skip to content

bug: closest('[data-link-for]') in scoping hint can match the anchor itself #5257

@marcoscaceres

Description

@marcoscaceres

Description

In src/core/link-to-dfn.js:326, the scoping hint uses elem.closest("[data-link-for]") to detect if a broken link is inside a data-link-for section.

However, closest() matches the element itself first. When using inline shorthand like [=bar/foo=], the <a> element gets data-link-for="bar" directly on it. The hint then falsely says:

This link is inside a data-link-for="bar" section

...when the attribute is on the link itself, not a parent section.

Additionally, when data-link-for="" (intentionally unscoped), the hint still fires and tells the author to add data-link-for="" — which is already present.

Suggested fix

Use elem.parentElement?.closest("[data-link-for]") to restrict to ancestors only, and guard against the empty-string case:

const scopedSection = elem.parentElement?.closest("[data-link-for]");
const scopingNote = scopedSection?.dataset.linkFor
  ? ` This link is inside a ...`
  : "";

Found by

Adversarial gate holdout test (blind review of PR #5170).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions