• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 11734033828

07 Nov 2024 02:10PM UTC coverage: 82.498% (-0.1%) from 82.608%
11734033828

push

github

web-flow
feat(view): add boolean attributes (#700)

43 of 61 new or added lines in 4 files covered. (70.49%)

7240 of 8776 relevant lines covered (82.5%)

49.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.14
/src/Tempest/View/src/Attributes/BooleanAttribute.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\View\Attributes;
6

7
use Exception;
8
use Tempest\View\Attribute;
9
use Tempest\View\Element;
10
use Tempest\View\Elements\GenericElement;
11

12
final readonly class BooleanAttribute implements Attribute
13
{
14
    public function __construct(
1✔
15
        private string $attributeName,
16
    ) {
17
    }
1✔
18

19
    public function apply(Element $element): ?Element
1✔
20
    {
21
        if (! $element instanceof GenericElement) {
1✔
NEW
22
            throw new Exception('This cannot happen');
×
23
        }
24

25
        $element
1✔
26
            ->addRawAttribute(sprintf(
1✔
27
                '<?php if(%s) { ?>%s<?php } ?>',
1✔
28
                $element->getAttribute($this->attributeName),
1✔
29
                ltrim($this->attributeName, ':'),
1✔
30
            ))
1✔
31
            ->unsetAttribute($this->attributeName);
1✔
32

33
        return $element;
1✔
34
    }
35

36
    public static function matches(Element $element, string $attributeName): bool
41✔
37
    {
38
        if (! $element instanceof GenericElement) {
41✔
39
            return false;
20✔
40
        }
41

42
        $attributeName = ltrim($attributeName, ':');
33✔
43

44
        $allowedElements = match ($attributeName) {
33✔
NEW
45
            'autofocus' => true,
×
NEW
46
            'allowfullscreen' => ['iframe'],
×
NEW
47
            'alpha', 'checked' => ['input'],
×
NEW
48
            'async', 'nomodule', 'defer' => ['script'],
×
NEW
49
            'autoplay', 'controls', 'loop', 'muted' => ['audio', 'video'],
×
NEW
50
            'default' => ['track'],
×
NEW
51
            'disabled' => ['link', 'fieldset', 'button', 'input', 'optgroup', 'option', 'select', 'textarea;'],
×
NEW
52
            'formnovalidate' => ['button', 'input'],
×
NEW
53
            'inert', 'itemscope' => ['HTML elements'],
×
NEW
54
            'ismap' => ['img'],
×
NEW
55
            'multiple' => ['input', 'select'],
×
NEW
56
            'open' => ['dialog', 'details'],
×
NEW
57
            'playsinline' => ['video'],
×
NEW
58
            'readonly' => ['input', 'textarea'],
×
NEW
59
            'required' => ['input', 'select', 'textarea'],
×
NEW
60
            'reversed' => ['ol'],
×
61
            'selected' => ['option'],
1✔
NEW
62
            'shadowrootclonable', 'shadowrootserializable', 'shadowrootdelegatesfocus' => ['template'],
×
63
            default => null,
33✔
64
        };
33✔
65

66
        return match (true) {
33✔
67
            $allowedElements === null => false,
33✔
68
            $allowedElements === true => true,
1✔
69
            default => in_array($element->getTag(), $allowedElements),
33✔
70
        };
33✔
71
    }
72
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc