-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLandmarkRule.php
More file actions
50 lines (38 loc) · 1.52 KB
/
LandmarkRule.php
File metadata and controls
50 lines (38 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace TwigA11y\Rules\Structure;
use TwigA11y\Rules\AbstractA11yRule;
use TwigCsFixer\Token\Token;
use TwigCsFixer\Token\Tokens;
final class LandmarkRule extends AbstractA11yRule
{
public function evaluate(Tokens $tokens, int $tokenIndex, callable $emit): void
{
$token = $tokens->get($tokenIndex);
if (!$token->isMatching(Token::TEXT_TYPE)) {
return;
}
$value = $token->getValue();
// Detect presence of main landmark or role="main"
if (str_contains($value, '<main') || str_contains($value, 'role="main"') || str_contains($value, "role='main'")) {
return;
}
// This rule is page-level. Only evaluate once per file (at tokenIndex 0)
// and only if the content looks like a full HTML page (contains
// a <body> or a <!DOCTYPE). This avoids flagging fragments/partials.
if (0 !== $tokenIndex) {
return;
}
$full = $this->getFullContent($tokens);
// If this looks like a fragment (no body/doctype), skip evaluation
if (!str_contains($full, '<body') && !str_contains(strtoupper($full), '<!DOCTYPE')) {
return;
}
// Scan the full content for a main landmark or role="main"
if (str_contains($full, '<main') || preg_match('/role\s*=\s*["\']main["\']/i', $full)) {
return;
}
$first = $tokens->get(0);
$emit('Page should include a main landmark', $first, 'Landmark.MissingMain');
}
}