no-disallowed-ancestor
Warns when an element appears as a descendant of an ancestor element its content model explicitly forbids (forbiddenAncestors), or when one of its ancestors is not among those its content model limits it to (permittedAncestors), according to the HTML Living Standard. It has settings in @markuplint/html-spec.
Whether a child is allowed by the parent's own content model at all is permitted-contents's concern, not this rule's — some elements are additionally forbidden from appearing anywhere further down the tree under a specific ancestor, not just as its direct child.
The main element is limited to the ancestors the specification lists:
A hierarchically correct main element is one whose ancestor elements are limited to html, body, div, form without an accessible name, and autonomous custom elements. Each main element must be a hierarchically correct main element.
Cite: HTML Living Standard: The main element
A component counts as a custom element, and a component that a pretender stands for is judged by the element it stands for. The accessible name of a form is computed with the recommended ARIA version. Where the name is not known until it is rendered (a dynamic value of aria-label or title: an expression or a template tag, or an aria-labelledby that may refer to an element the markup does not tell: aria-labelledby={id}, a token that refers to no element in a tree that has <p id={id}>, or a token that refers to an element whose text the markup does not tell: an element that a pretender declares, or one whose content is not known), the form may have a name or not at runtime, so a main element in it is not reported.
The content of a template element belongs to a separate document fragment, so the elements in it have no ancestors: the check does not go beyond a template element. A template element that the template engine or the framework renders away (a directive of a template engine, or Vue's <template v-if>, v-else-if, v-else, v-for and v-slot) is not an HTML template element, and the check looks through it. In React and Vue, a template element that is not rendered away (one without a directive, or with Vue's v-show) is an ordinary element whose children are in the document, so the check goes through it as through any other element.
❌ Examples of incorrect code for this rule
<address>
<div>
<address>Nested address</address>
</div>
</address>
<section>
<main>Main content</main>
</section>
<form aria-label="Search">
<main>Main content</main>
</form>
✅ Examples of correct code for this rule
<address>Contact information</address>
<body>
<div>
<main>Main content</main>
</div>
</body>
<x-layout>
<main>Main content</main>
</x-layout>
Interface
{
"no-disallowed-ancestor": boolean
}
Default Severity
error