-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaViewportRule.php
More file actions
53 lines (43 loc) · 1.44 KB
/
MetaViewportRule.php
File metadata and controls
53 lines (43 loc) · 1.44 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
51
52
53
<?php
declare(strict_types=1);
namespace TwigA11y\Rules\Structure;
use TwigA11y\Rules\AbstractA11yRule;
use TwigA11y\Template\TemplateKind;
use TwigCsFixer\Token\Token;
use TwigCsFixer\Token\Tokens;
final class MetaViewportRule extends AbstractA11yRule
{
public function evaluate(Tokens $tokens, int $tokenIndex, callable $emit): void
{
// Guard so we only perform the full-file scan once by running on the
// first token index. Using an instance-scoped scanned flag caused the
// rule to silently skip later files when the same instance was reused.
if (0 !== $tokenIndex) {
return;
}
$token = $tokens->get($tokenIndex);
if (!$token->isMatching(Token::TEXT_TYPE)) {
return;
}
$full = $this->getFullContent($tokens);
$fullLower = strtolower($full);
if (!str_contains($fullLower, 'name="viewport"') && !str_contains($fullLower, "name='viewport'")) {
return;
}
if (preg_match('/user-scalable\s*=\s*no/i', $fullLower)) {
$token = $tokens->get(0);
$emit('Avoid using user-scalable=no in the viewport meta.', $token, 'MetaViewport.UserScalable');
}
}
/**
* @return TemplateKind[]
*/
protected function supportedKinds(): array
{
return [TemplateKind::FullPage];
}
protected function evaluateOncePerFile(): bool
{
return true;
}
}