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

dg / texy / 44949415842

21 Nov 2026 08:56PM UTC coverage: 97.174% (+0.01%) from 97.162%
44949415842

push

github

dg
ElementDecorator, HtmlSanitizePass: a numeric class or style name no longer throws TypeError

A class such as .[start 5] is stored as an array key, which PHP turns into an
int, and the policy checks that take a string threw on it.

681d1c05-f259-441d-877e-829884510010

7 of 7 new or added lines in 2 files covered. (100.0%)

34 existing lines in 6 files now uncovered.

3886 of 3999 relevant lines covered (97.17%)

0.97 hits per line

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

91.67
/src/Modules/HtmlModule.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Texy! (https://texy.deegee.dev)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Texy\Modules;
9

10
use Texy;
11
use Texy\Nodes\{HtmlCommentNode, HtmlTagNode, TextNode};
12
use Texy\Parsing\ParseContext;
13
use Texy\{Range, Regexp, Syntax};
14
use function strlen;
15

16

17
/**
18
 * Processes HTML tags, comments and entity references in input text.
19
 */
20
final class HtmlModule extends Texy\Module
21
{
22
        public function register(Texy\Parsing\Engine $engine): void
23
        {
24
                $engine->addInline(
1 ✔
25
                        Syntax::HtmlTag,
1 ✔
26
                        '~
1 ✔
27
                                < (/?)                                # tag begin
28
                                ([a-z][a-z0-9_:-]{0,50})              # tag name
29
                                (?= [\s/>] )
30
                                (
31
                                        (?:
32
                                                \s++ [a-z_:][a-z0-9_:-]*+ |   # attribute name
33
                                                = \s*+ (?: "[^"]*+" | \'[^\']*+\' ) (?=[\s=>]|/>) |  # attribute value in quotes
34
                                                = [^\s>]++                    # attribute value without quotes
35
                                        )*+
36
                                )
37
                                \s*+
38
                                (/?)                                  # self-closing slash
39
                                >
40
                        ~isx',
41
                        $this->parseTag(...),
1 ✔
42
                );
43

44
                $engine->addInline(
1 ✔
45
                        Syntax::HtmlComment,
1 ✔
46
                        '~
1 ✔
47
                                <!--
48
                                ( .*? )
49
                                -->
50
                        ~isx',
51
                        $this->parseComment(...),
1 ✔
52
                );
53

54
                $engine->addInline(
1 ✔
55
                        Syntax::HtmlEntity,
1 ✔
56
                        '~' . Texy\Helpers::EntityPattern . '~',
1 ✔
57
                        $this->parseEntity(...),
1 ✔
58
                );
59
        }
1 ✔
60

61

62
        /**
63
         * Parses &amp;, &#160;, &#xA0;
64
         * @param  array{string}  $matches
65
         * @param  array{int}  $offsets
66
         */
67
        public function parseEntity(ParseContext $context, array $matches, array $offsets): ?TextNode
68
        {
69
                $decoded = Texy\Helpers::decodeEntity($matches[0]);
1 ✔
70
                return $decoded === null
1 ✔
71
                        ? null
1 ✔
72
                        : new TextNode($decoded, new Range($offsets[0], strlen($matches[0])));
1 ✔
73
        }
74

75

76
        /**
77
         * Parses <!-- comment -->
78
         * @param  array{string, string}  $matches
79
         * @param  array{int, int}  $offsets
80
         */
81
        public function parseComment(ParseContext $context, array $matches, array $offsets): HtmlCommentNode
82
        {
83
                return new HtmlCommentNode($matches[1], new Range($offsets[0], strlen($matches[0])));
1 ✔
84
        }
85

86

87
        /**
88
         * Parses <tag attr="...">
89
         * @param  array{string, string, string, string, string}  $matches
90
         * @param  array{int, int, int, int, int}  $offsets
91
         */
92
        public function parseTag(ParseContext $context, array $matches, array $offsets): ?HtmlTagNode
93
        {
94
                [, $mEnd, $mTag, $mAttr, $mEmpty] = $matches;
1 ✔
95

96
                $isStart = $mEnd !== '/';
1 ✔
97
                $isEmpty = $mEmpty === '/';
1 ✔
98
                if (!$isEmpty && str_ends_with($mAttr, '/')) {
1 ✔
UNCOV
99
                        $mAttr = substr($mAttr, 0, -1);
×
100
                        $isEmpty = true;
×
101
                }
102

103
                // error - can't close empty element
104
                if ($isEmpty && !$isStart) {
1 ✔
UNCOV
105
                        return null;
×
106
                }
107

108
                // error - end element with attrs
109
                $mAttr = trim(strtr($mAttr, "\r\n", '  ')); // \r is the hard break mark of a paragraph
1 ✔
110
                if ($mAttr && !$isStart) {
1 ✔
111
                        return null;
1 ✔
112
                }
113

114
                return new HtmlTagNode(
1 ✔
115
                        $mTag,
116
                        $isStart ? $this->parseAttributes($mAttr) : [],
1 ✔
117
                        closing: !$isStart,
1 ✔
118
                        selfClosing: $isEmpty,
119
                        range: new Range($offsets[0], strlen($matches[0])),
1 ✔
120
                        source: $matches[0],
1 ✔
121
                );
122
        }
123

124

125
        /** @return array<string, string|bool> */
126
        private function parseAttributes(string $attrs): array
127
        {
128
                $res = [];
1 ✔
129
                $matches = Regexp::matchAll(
1 ✔
UNCOV
130
                        $attrs,
×
131
                        <<<'X'
132
                                ~
1 ✔
133
                                ([a-z_:][a-z0-9_:-]*)          # attribute name
134
                                \s*
135
                                (?:
136
                                        = \s*                      # equals sign
137
                                        (
138
                                                ' [^']* ' |            # single quoted value
139
                                                " [^"]* " |            # double quoted value
140
                                                [^'"\s]+               # unquoted value
141
                                        )
142
                                )?
143
                                ~isx
144
                                X,
145
                );
146

147
                /** @var array{string, string, ?string} $m */
148
                foreach ($matches as $m) {
1 ✔
149
                        $key = strtolower($m[1]);
1 ✔
150
                        $value = $m[2];
1 ✔
151
                        if ($value === null) {
1 ✔
152
                                $res[$key] = true;
1 ✔
153
                        } elseif ($value[0] === '\'' || $value[0] === '"') {
1 ✔
154
                                $res[$key] = trim(Texy\Helpers::unescapeHtml(substr($value, 1, -1)));
1 ✔
155
                        } else {
156
                                $res[$key] = trim(Texy\Helpers::unescapeHtml($value));
1 ✔
157
                        }
158
                }
159

160
                return $res;
1 ✔
161
        }
162
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc