|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Logic adapted from html-validate (MIT), Copyright 2017 David Sveningsson. |
| 4 | +// |
| 5 | +// Scope caveat: this rule sees ONE template file at a time. Ember apps |
| 6 | +// compose pages from many templates; the true heading outline spans |
| 7 | +// route/component boundaries we can't cross. So this rule validates only |
| 8 | +// the heading order *within* this file and cannot catch cross-file jumps |
| 9 | +// (e.g. a route template starts at <h2> because its layout supplies <h1>). |
| 10 | +// To support layout-provided <h1>, set `minInitialRank` to a lower rank. |
| 11 | + |
| 12 | +const { roles: ariaRoles } = require('aria-query'); |
| 13 | + |
| 14 | +const HEADING_RE = /^h([1-6])$/; |
| 15 | + |
| 16 | +// Valid ARIA role tokens — used to find the first recognised token in a |
| 17 | +// space-separated role-fallback list (WAI-ARIA §4.1). `presentation`/`none` |
| 18 | +// are included in aria-query's role map. Abstract roles (e.g. `widget`, |
| 19 | +// `structure`, `landmark`) are excluded: WAI-ARIA forbids authors from using |
| 20 | +// abstract roles as `role` attribute values. |
| 21 | +const VALID_ROLE_TOKENS = new Set( |
| 22 | + [...ariaRoles.keys()].filter((name) => !ariaRoles.get(name).abstract) |
| 23 | +); |
| 24 | + |
| 25 | +function extractLevel(tag) { |
| 26 | + const match = HEADING_RE.exec(tag); |
| 27 | + return match ? Number.parseInt(match[1], 10) : null; |
| 28 | +} |
| 29 | + |
| 30 | +function findAttr(node, name) { |
| 31 | + return node.attributes?.find((attr) => attr.name === name); |
| 32 | +} |
| 33 | + |
| 34 | +function getStaticAttrString(node, name) { |
| 35 | + const attr = findAttr(node, name); |
| 36 | + if (!attr || !attr.value || attr.value.type !== 'GlimmerTextNode') { |
| 37 | + return null; |
| 38 | + } |
| 39 | + return attr.value.chars; |
| 40 | +} |
| 41 | + |
| 42 | +function isSectioningRoot(node) { |
| 43 | + if (node.tag === 'dialog') { |
| 44 | + return true; |
| 45 | + } |
| 46 | + const role = getStaticAttrString(node, 'role'); |
| 47 | + if (!role) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + // ARIA role values are case-insensitive per HTML spec, and the attribute |
| 51 | + // holds a space-separated token list (role-fallback). Per WAI-ARIA §4.1, |
| 52 | + // the effective role is the first token recognised by the user agent; any |
| 53 | + // invalid-token prefix is skipped. Walk tokens in order and return true |
| 54 | + // only when the first recognised token is dialog/alertdialog. |
| 55 | + const tokens = role.trim().toLowerCase().split(/\s+/u); |
| 56 | + const firstValid = tokens.find((token) => VALID_ROLE_TOKENS.has(token)); |
| 57 | + return firstValid === 'dialog' || firstValid === 'alertdialog'; |
| 58 | +} |
| 59 | + |
| 60 | +function parseMinInitialRank(value) { |
| 61 | + if (value === 'any') { |
| 62 | + return 6; |
| 63 | + } |
| 64 | + const match = /^h([1-6])$/.exec(value); |
| 65 | + return match ? Number.parseInt(match[1], 10) : 1; |
| 66 | +} |
| 67 | + |
| 68 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 69 | +module.exports = { |
| 70 | + meta: { |
| 71 | + type: 'problem', |
| 72 | + docs: { |
| 73 | + description: 'enforce heading hierarchy within a template', |
| 74 | + category: 'Accessibility', |
| 75 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-heading-level.md', |
| 76 | + templateMode: 'both', |
| 77 | + }, |
| 78 | + schema: [ |
| 79 | + { |
| 80 | + type: 'object', |
| 81 | + properties: { |
| 82 | + allowMultipleH1: { type: 'boolean' }, |
| 83 | + allowSkippedLevels: { type: 'boolean' }, |
| 84 | + minInitialRank: { enum: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'any'] }, |
| 85 | + }, |
| 86 | + additionalProperties: false, |
| 87 | + }, |
| 88 | + ], |
| 89 | + messages: { |
| 90 | + multipleH1: 'Multiple `<h1>` are not allowed', |
| 91 | + skipped: |
| 92 | + 'Heading level can only increase by one: expected `<h{{expected}}>` but got `<h{{actual}}>`', |
| 93 | + initial: |
| 94 | + 'Initial heading level must be `<h{{expected}}>` or higher rank but got `<h{{actual}}>`', |
| 95 | + }, |
| 96 | + }, |
| 97 | + |
| 98 | + create(context) { |
| 99 | + const options = context.options[0] || {}; |
| 100 | + // Only the multiple-h1 check is reliable within a single template file — |
| 101 | + // finding two <h1> in one file is a within-file signal regardless of |
| 102 | + // what ancestors render. The other two checks (initial rank, skipped |
| 103 | + // levels) defeat themselves in a component-based app: layouts and child |
| 104 | + // components supply headings the lint can't see. So both default to off. |
| 105 | + const allowMultipleH1 = Boolean(options.allowMultipleH1); |
| 106 | + const allowSkippedLevels = |
| 107 | + options.allowSkippedLevels === undefined ? true : Boolean(options.allowSkippedLevels); |
| 108 | + const minInitialRank = parseMinInitialRank(options.minInitialRank || 'any'); |
| 109 | + |
| 110 | + const stack = [{ node: null, current: 0, h1Count: 0 }]; |
| 111 | + |
| 112 | + function currentRoot() { |
| 113 | + return stack.at(-1); |
| 114 | + } |
| 115 | + |
| 116 | + return { |
| 117 | + GlimmerElementNode(node) { |
| 118 | + const level = extractLevel(node.tag); |
| 119 | + if (level !== null) { |
| 120 | + const root = currentRoot(); |
| 121 | + if (level === 1 && !allowMultipleH1) { |
| 122 | + if (root.h1Count >= 1) { |
| 123 | + context.report({ node, messageId: 'multipleH1' }); |
| 124 | + } else { |
| 125 | + root.h1Count += 1; |
| 126 | + } |
| 127 | + } |
| 128 | + if (level <= root.current) { |
| 129 | + root.current = level; |
| 130 | + } else { |
| 131 | + const expected = root.current + 1; |
| 132 | + if (root.current === 0) { |
| 133 | + if (level > minInitialRank) { |
| 134 | + context.report({ |
| 135 | + node, |
| 136 | + messageId: 'initial', |
| 137 | + data: { expected: String(minInitialRank), actual: String(level) }, |
| 138 | + }); |
| 139 | + } |
| 140 | + } else if (level !== expected && !allowSkippedLevels) { |
| 141 | + context.report({ |
| 142 | + node, |
| 143 | + messageId: 'skipped', |
| 144 | + data: { expected: String(expected), actual: String(level) }, |
| 145 | + }); |
| 146 | + } |
| 147 | + root.current = level; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if (isSectioningRoot(node)) { |
| 152 | + stack.push({ node, current: 0, h1Count: 0 }); |
| 153 | + } |
| 154 | + }, |
| 155 | + |
| 156 | + 'GlimmerElementNode:exit'(node) { |
| 157 | + const root = currentRoot(); |
| 158 | + if (root.node === node) { |
| 159 | + stack.pop(); |
| 160 | + } |
| 161 | + }, |
| 162 | + }; |
| 163 | + }, |
| 164 | +}; |
0 commit comments