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

dg / texy / 42150856071

06 Nov 2026 01:24AM UTC coverage: 96.113% (+0.3%) from 95.817%
42150856071

push

github

dg
Output\Texy\Renderer: a text written nowhere is read by the inline parser

A text node without a range had only its & and < escaped, so the text
"*foo*" of a consumer printed as an emphasis. Such a text is now read by
the inline parser of the given Texy, and the first character of whatever
it finds there besides plain text and the addresses it links by itself
is escaped, until it finds nothing. A text alone cannot see its
neighbours, so a paragraph or heading holding one is read back as a
whole, and where its text differs, it is printed again with every
character escaped that Texy syntax could start with. The mechanism comes
from TexyConverter of texy/markdown, which can drop it.

Parsed documents print as before: the prints of the 16201 valid
documents of the reference corpus are byte-identical.

8f49b3e4-2f59-4596-83cb-a4ee32148a48

40 of 40 new or added lines in 1 file covered. (100.0%)

58 existing lines in 7 files now uncovered.

3610 of 3756 relevant lines covered (96.11%)

0.96 hits per line

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

92.86
/src/Texy/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 __construct(
1 ✔
23
                private Texy\Texy $texy,
24
        ) {
25
                $texy->addPass($this->processPassthrough(...));
1 ✔
26
        }
1 ✔
27

28

29
        /**
30
         * Pairs passthrough tags into HtmlElementNode trees and evaluates the
31
         * tag whitelist over them (transform phase): rejected tags become text.
32
         */
33
        public function processPassthrough(Texy\Nodes\DocumentNode $doc): void
34
        {
35
                if (empty($this->texy->allowed[Syntax::HtmlTag])) {
1 ✔
36
                        return;
×
37
                }
38

39
                (new Texy\Passes\HtmlPairingPass)->process($doc);
1 ✔
40
                (new Texy\Passes\HtmlSanitizePass($this->texy->htmlPolicy))
1 ✔
41
                        ->process($doc);
1 ✔
42
        }
1 ✔
43

44

45
        public function beforeParse(): void
46
        {
47
                $this->texy->addLineSyntax(
1 ✔
48
                        Syntax::HtmlTag,
1 ✔
49
                        '~
1 ✔
50
                                < (/?)                          # tag begin
51
                                ([a-z][a-z0-9_:-]{0,50})        # tag name
52
                                (?= [\s/>] )
53
                                (
54
                                        (?:
55
                                                \s++ [a-z0-9_:-]++ |          # attribute name
56
                                                = \s*+ " [^"]*+ " |           # attribute value in double quotes
57
                                                = \s*+ \' [^\']*+ \' |        # attribute value in single quotes
58
                                                = [^\s>]++                    # attribute value without quotes
59
                                        )*
60
                                )
61
                                \s*+
62
                                (/?)                             # self-closing slash
63
                                >
64
                        ~isx',
65
                        $this->parseTag(...),
1 ✔
66
                );
67

68
                $this->texy->addLineSyntax(
1 ✔
69
                        Syntax::HtmlComment,
1 ✔
70
                        '~
1 ✔
71
                                <!--
72
                                ( .*? )
73
                                -->
74
                        ~isx',
75
                        $this->parseComment(...),
1 ✔
76
                );
77

78
                $this->texy->addLineSyntax(
1 ✔
79
                        Syntax::HtmlEntity,
1 ✔
80
                        '~' . Texy\Helpers::EntityPattern . '~',
1 ✔
81
                        $this->parseEntity(...),
1 ✔
82
                );
83
        }
1 ✔
84

85

86
        /**
87
         * Parses &amp;, &#160;, &#xA0;
88
         * @param  array{string}  $matches
89
         * @param  array{int}  $offsets
90
         */
91
        public function parseEntity(ParseContext $context, array $matches, array $offsets): ?TextNode
92
        {
93
                $decoded = Texy\Helpers::decodeEntity($matches[0]);
1 ✔
94
                return $decoded === null
1 ✔
95
                        ? null
1 ✔
96
                        : new TextNode($decoded, new Range($offsets[0], strlen($matches[0])));
1 ✔
97
        }
98

99

100
        /**
101
         * Parses <!-- comment -->
102
         * @param  array{string, string}  $matches
103
         * @param  array{int, int}  $offsets
104
         */
105
        public function parseComment(ParseContext $context, array $matches, array $offsets): HtmlCommentNode
106
        {
107
                return new HtmlCommentNode($matches[1], new Range($offsets[0], strlen($matches[0])));
1 ✔
108
        }
109

110

111
        /**
112
         * Parses <tag attr="...">
113
         * @param  array{string, string, string, string, string}  $matches
114
         * @param  array{int, int, int, int, int}  $offsets
115
         */
116
        public function parseTag(ParseContext $context, array $matches, array $offsets): ?HtmlTagNode
117
        {
118
                [, $mEnd, $mTag, $mAttr, $mEmpty] = $matches;
1 ✔
119

120
                $isStart = $mEnd !== '/';
1 ✔
121
                $isEmpty = $mEmpty === '/';
1 ✔
122
                if (!$isEmpty && str_ends_with($mAttr, '/')) {
1 ✔
123
                        $mAttr = substr($mAttr, 0, -1);
×
UNCOV
124
                        $isEmpty = true;
×
125
                }
126

127
                // error - can't close empty element
128
                if ($isEmpty && !$isStart) {
1 ✔
UNCOV
129
                        return null;
×
130
                }
131

132
                // error - end element with attrs
133
                $mAttr = trim(strtr($mAttr, "\r\n", '  ')); // \r is the hard break mark of a paragraph
1 ✔
134
                if ($mAttr && !$isStart) {
1 ✔
135
                        return null;
1 ✔
136
                }
137

138
                return new HtmlTagNode(
1 ✔
139
                        $mTag,
140
                        $isStart ? $this->parseAttributes($mAttr) : [],
1 ✔
141
                        closing: !$isStart,
1 ✔
142
                        selfClosing: $isEmpty,
143
                        range: new Range($offsets[0], strlen($matches[0])),
1 ✔
144
                        source: $matches[0],
1 ✔
145
                );
146
        }
147

148

149
        /** @return array<string, string|bool> */
150
        private function parseAttributes(string $attrs): array
151
        {
152
                $res = [];
1 ✔
153
                $matches = Regexp::matchAll(
1 ✔
154
                        $attrs,
155
                        <<<'X'
156
                                ~
1 ✔
157
                                ([a-z0-9_:-]+)                 # attribute name
158
                                \s*
159
                                (?:
160
                                        = \s*                      # equals sign
161
                                        (
162
                                                ' [^']* ' |            # single quoted value
163
                                                " [^"]* " |            # double quoted value
164
                                                [^'"\s]+               # unquoted value
165
                                        )
166
                                )?
167
                                ~isx
168
                                X,
169
                );
170

171
                /** @var array{string, string, ?string} $m */
172
                foreach ($matches as $m) {
1 ✔
173
                        $key = strtolower($m[1]);
1 ✔
174
                        $value = $m[2];
1 ✔
175
                        if ($value === null) {
1 ✔
176
                                $res[$key] = true;
1 ✔
177
                        } elseif ($value[0] === '\'' || $value[0] === '"') {
1 ✔
178
                                $res[$key] = trim(Texy\Helpers::unescapeHtml(substr($value, 1, -1)));
1 ✔
179
                        } else {
180
                                $res[$key] = trim(Texy\Helpers::unescapeHtml($value));
1 ✔
181
                        }
182
                }
183

184
                return $res;
1 ✔
185
        }
186
}
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