• 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

98.65
/src/Modules/DelimitedBlockModule.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\{Helpers, Range, Syntax};
12
use Texy\Nodes\{
13
        BlockNode, CodeBlockNode, CommentNode, ContentNode, DivNode, HtmlBlockNode, PreformattedNode, TextBlockNode, TexySourceNode, UnknownDelimitedNode,
14
};
15
use Texy\Parsing\{OffsetMap, ParseContext};
16
use function strlen;
17

18

19
/**
20
 * Processes delimited blocks (/-- code, html, text, div, etc.).
21
 */
22
final class DelimitedBlockModule extends Texy\Module
23
{
24
        /** @internal built-in type => its syntax name */
25
        public const Types = [
26
                '' => Syntax::DelimitedBare,
27
                'pre' => Syntax::DelimitedPre,
28
                'code' => Syntax::DelimitedCode,
29
                'html' => Syntax::DelimitedHtml,
30
                'text' => Syntax::DelimitedText,
31
                'texysource' => Syntax::DelimitedTexySource,
32
                'comment' => Syntax::DelimitedComment,
33
                'div' => Syntax::DelimitedDiv,
34
        ];
35

36

37
        public function __construct(
1 ✔
38
                private Texy\Config $config,
39
        ) {
40
        }
1 ✔
41

42

43
        public function register(Texy\Parsing\Engine $engine): void
44
        {
45
                foreach (self::Types as $name) {
1 ✔
46
                        $engine->declare($name, enabled: $name !== Syntax::DelimitedTexySource);
1 ✔
47
                }
48

49
                $engine->addBlock(
1 ✔
50
                        Syntax::Delimited,
1 ✔
51
                        '~^
52
                                /--++ [ \t]*+                # opening tag /--
53
                                (.*)                         # content type (1)
54
                                ' . Texy\Patterns::ModifierHAlign . '? # modifier (2)
55
                                $
56
                                (' . self::buildContentPattern(ParseContext::MaxDepth) . ') # content (3)
1 ✔
57
                                (?:
58
                                        \n \\\--.* $ |           # closing tag
59
                                        \z                       # or end of input
60
                                )
61
                        ~mUix',
62
                        $this->parse(...),
1 ✔
63
                );
64
        }
1 ✔
65

66

67
        /**
68
         * Builds the pattern of the lines of a block, where a nested block down to $depth levels takes its lines as one.
69
         * Deeper, /-- is a line of content.
70
         */
71
        private static function buildContentPattern(int $depth): string
72
        {
73
                return $depth
1 ✔
74
                        ? '(?: \n (?!/--|\\\--) .*+ | \n /-- .*+ ' . self::buildContentPattern($depth - 1) . ' (?: \n \\\-- .*+ | \z ) )*+'
1 ✔
75
                        : '(?: \n (?!\\\--) .*+ )*+';
1 ✔
76
        }
77

78

79
        /**
80
         * Parses blocks /--foo
81
         * @param  array{string, string, ?string, string}  $matches
82
         * @param  array{int, int, ?int, int}  $offsets
83
         */
84
        public function parse(ParseContext $context, array $matches, array $offsets): ?BlockNode
85
        {
86
                $parts = Texy\Regexp::split($matches[1], '~\s+~', limit: 2);
1 ✔
87
                // the opening pattern matches case-insensitively, so /--PRE must mean the same as /--pre
88
                $type = Helpers::toLower($parts[0]);
1 ✔
89
                if (isset(self::Types[$type]) && empty($context->allowedSyntaxes[self::Types[$type]])) {
1 ✔
90
                        return null;
1 ✔
91
                }
92

93
                $param = empty($parts[1]) ? null : $parts[1];
1 ✔
94
                $mod = Texy\Modifier::parse($matches[2], $offsets[2] ?? null);
1 ✔
95
                $built = null;
1 ✔
96
                $next = function () use ($context, $type, $param, $mod, $matches, $offsets, &$built): ?BlockNode {
1 ✔
97
                        return $built = $this->createNode($context, $type, $param, $mod, $matches, $offsets);
1 ✔
98
                };
99

100
                $handler = $this->config->delimitedBlockHandler;
1 ✔
101
                if ($handler === null) {
1 ✔
102
                        return $next();
1 ✔
103
                }
104

105
                [$content, $map] = OffsetMap::outdentLines(OffsetMap::linesOf($matches[3], $offsets[3]));
1 ✔
106
                $node = $handler($context, $type, $content, $param, $mod, $next);
1 ✔
107
                if ($node !== null && $node !== $built) {
1 ✔
108
                        foreach ($node->getChildren() as $child) {
1 ✔
109
                                $map->applyTo($child, except: $built); // the handler read the outdented content
1 ✔
110
                        }
111
                        $node->range ??= new Range($offsets[0], strlen($matches[0]));
1 ✔
112
                }
113
                return $node;
1 ✔
114
        }
115

116

117
        /**
118
         * Creates the node of a built-in type, or the unknown node of any other type.
119
         * @param  array{string, string, ?string, string}  $matches
120
         * @param  array{int, int, ?int, int}  $offsets
121
         */
122
        private function createNode(
123
                ParseContext $context,
124
                string $type,
125
                ?string $param,
126
                ?Texy\Modifier $mod,
127
                array $matches,
128
                array $offsets,
129
        ): ?BlockNode
130
        {
131
                $mContent = $matches[3];
1 ✔
132
                [$content, $map] = OffsetMap::outdentLines(OffsetMap::linesOf($mContent, $offsets[3]));
1 ✔
133
                $range = new Range($offsets[0], strlen($matches[0]));
1 ✔
134

135
                switch ($type) {
136
                        case 'code':
1 ✔
137
                                return new CodeBlockNode($content, $param, $mod, $range, $map);
1 ✔
138

139
                        case 'pre':
1 ✔
140
                                return new PreformattedNode(
1 ✔
141
                                        $content,
142
                                        $this->parseVerbatim($context, $content, $map),
1 ✔
143
                                        $mod,
144
                                        $range,
145
                                        $map,
146
                                        Syntax::DelimitedPre,
1 ✔
147
                                );
148

149
                        case '':
1 ✔
150
                                $text = $this->parseVerbatim($context, $content, $map, []);
1 ✔
151
                                return new PreformattedNode($content, $text, $mod, $range, $map, Syntax::DelimitedBare);
1 ✔
152

153
                        case 'comment':
1 ✔
154
                                return new CommentNode($content, $range);
1 ✔
155

156
                        case 'html':
1 ✔
157
                        case 'text':
1 ✔
158
                                // html/text blocks don't use outdent - preserve original indentation
159
                                $content = trim($mContent, "\n");
1 ✔
160
                                $map = OffsetMap::fromLines(OffsetMap::linesOf($content, $offsets[3] + strspn($mContent, "\n")));
1 ✔
161
                                return $type === 'html'
1 ✔
162
                                        ? new HtmlBlockNode($content, $this->parseVerbatim($context, $content, $map), $mod, $range, $map)
1 ✔
163
                                        : new TextBlockNode($content, $mod, $range, $map);
1 ✔
164

165
                        case 'div':
1 ✔
166
                                [$content, $map] = OffsetMap::outdentLines(OffsetMap::linesOf($mContent, $offsets[3]), firstLine: true);
1 ✔
167
                                if ($content === '') {
1 ✔
168
                                        return null;
1 ✔
169
                                }
170
                                $parsed = $context->parseBlock($content);
1 ✔
171
                                $map->applyTo($parsed);
1 ✔
172
                                return new DivNode($parsed, $mod, $range);
1 ✔
173

174
                        case 'texysource':
1 ✔
175
                                // parsed as Texy, its HTML is displayed as source code
176
                                if ($content === '') {
1 ✔
UNCOV
177
                                        return null;
×
178
                                }
179
                                $parsed = $context->parseBlock($content);
1 ✔
180
                                $map->applyTo($parsed);
1 ✔
181
                                return new TexySourceNode($content, $parsed, $mod, $range, $map);
1 ✔
182

183
                        default:
184
                                return new UnknownDelimitedNode(
1 ✔
185
                                        $matches[0],
1 ✔
186
                                        $type,
187
                                        $range,
188
                                        OffsetMap::fromLines(OffsetMap::linesOf($matches[0], $offsets[0])),
1 ✔
189
                                );
190
                }
191
        }
192

193

194
        /**
195
         * Parses the markup of a verbatim block, which knows the given syntaxes only: HTML tags,
196
         * comments and entities in html and pre blocks, none in a bare /--.
197
         * @param  list<string>  $syntaxes
198
         */
199
        private function parseVerbatim(
200
                ParseContext $context,
201
                string $text,
202
                OffsetMap $map,
203
                array $syntaxes = [Syntax::HtmlTag, Syntax::HtmlComment, Syntax::HtmlEntity],
204
        ): ContentNode
205
        {
206
                $parsed = $context->getInlineParser()
1 ✔
207
                        ->withPatterns($syntaxes)
1 ✔
208
                        ->parse($context, $text);
1 ✔
209
                $map->applyTo($parsed);
1 ✔
210
                return $parsed;
1 ✔
211
        }
212
}
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