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

dg / texy / 29654833201

18 Jul 2026 04:09PM UTC coverage: 93.039% (+0.04%) from 93.004%
29654833201

push

github

dg
Restored test coverage dropped during the AST migration

The migration removed five test files coupled to v3 contracts (block,
content-model, formatter-indent, html, latte) with the intent to bring
them back once the output layer settled. Restored here, ported to the
AST-era API; latte and most of content-model pass unchanged.

Expected outputs were re-derived and every difference against the v3
files reviewed case by case. The differences fall into the intentional
categories: list items merge continuation lines, whitespace-sensitive
typography follows visible length instead of protection-mark key length,
rejected tags are escaped consistently (both halves of a pair) with
typography applied to the escaped text, paragraphs of escaped markup are
<p>-wrapped, and <p> inside <form> is preserved. Inner texysource
fragments are parsed without the transform phase, so their passthrough
tags stay live - a documented wrinkle.

content-model.phpt again guards the WellFormer content model, nesting
prohibitions, transparent and unknown elements and auto-closing in the
public suite, independently of the private regression corpus.

3248 of 3491 relevant lines covered (93.04%)

0.93 hits per line

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

95.73
/src/Texy/Modules/ListModule.php
1
<?php declare(strict_types=1);
2

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

8
namespace Texy\Modules;
9

10
use Texy;
11
use Texy\Modifier;
12
use Texy\Nodes\DefinitionListNode;
13
use Texy\Nodes\ListItemNode;
14
use Texy\Nodes\ListNode;
15
use Texy\Nodes\ListType;
16
use Texy\ParseContext;
17
use Texy\Patterns;
18
use Texy\Position;
19
use Texy\Regexp;
20
use Texy\Syntax;
21
use function count, implode, ord, strlen;
22

23

24
/**
25
 * Processes ordered, unordered, and definition lists with nesting.
26
 */
27
final class ListModule extends Texy\Module
28
{
29
        /** @var array<string, array{string, ListType, 2?: string}> [regex, type, next-regex?] */
30
        public array $bullets = [
31
                '*' => ['\* [ \t]', ListType::Unordered],
32
                '-' => ['[\x{2013}-] (?! [>-] )', ListType::Unordered],
33
                '+' => ['\+ [ \t]', ListType::Unordered],
34
                '1.' => ['1 \. [ \t]' /* not \d! */, ListType::Decimal, '\d{1,3} \. [ \t]'],
35
                '1)' => ['\d{1,3} \) [ \t]', ListType::Decimal],
36
                'I.' => ['I \. [ \t]', ListType::UpperRoman, '[IVX]{1,4} \. [ \t]'],
37
                'I)' => ['[IVX]+ \) [ \t]', ListType::UpperRoman], // before A) !
38
                'a)' => ['[a-z] \) [ \t]', ListType::LowerAlpha],
39
                'A)' => ['[A-Z] \) [ \t]', ListType::UpperAlpha],
40
        ];
41

42

43
        public function __construct(
1✔
44
                private Texy\Texy $texy,
45
        ) {
46
                $texy->allowed[Syntax::List] = true;
1✔
47
                $texy->allowed[Syntax::DefinitionList] = true;
1✔
48
        }
1✔
49

50

51
        public function beforeParse(string &$text): void
1✔
52
        {
53
                $RE = $REul = [];
1✔
54
                foreach ($this->bullets as $desc) {
1✔
55
                        $RE[] = $desc[0];
1✔
56
                        if (!$desc[1]->isOrdered()) {
1✔
57
                                $REul[] = $desc[0];
1✔
58
                        }
59
                }
60

61
                $this->texy->registerBlockPattern(
1✔
62
                        $this->parseList(...),
1✔
63
                        '~^
64
                                (?:' . Patterns::MODIFIER_H . '\n)? # modifier (1)
1✔
65
                                (' . implode('|', $RE) . ')         # list marker (2)
1✔
66
                                [ \t]*+
67
                                \S .*                               # content
68
                        $~mU',
69
                        Syntax::List,
1✔
70
                );
71

72
                $this->texy->registerBlockPattern(
1✔
73
                        $this->parseDefList(...),
1✔
74
                        '~^
75
                                (?:' . Patterns::MODIFIER_H . '\n)?   # modifier (1)
1✔
76
                                ( \S .{0,2000} )                      # definition term (2)
77
                                : [ \t]*                              # colon separator
78
                                ' . Patterns::MODIFIER_H . '?         # modifier (3)
1✔
79
                                \n
80
                                ([ \t]++)                             # indentation (4)
81
                                (' . implode('|', $REul) . ')         # description marker (5)
1✔
82
                                [ \t]*+
83
                                \S .*                                 # content
84
                        $~mU',
85
                        Syntax::DefinitionList,
1✔
86
                );
87
        }
1✔
88

89

90
        /**
91
         * Parses list.
92
         * @param  array<?string>  $matches
93
         * @param  array<?int>  $offsets
94
         */
95
        public function parseList(ParseContext $context, array $matches, array $offsets): ?ListNode
1✔
96
        {
97
                /** @var array{string, ?string, string} $matches */
98
                [, $mMod, $mBullet] = $matches;
1✔
99

100
                $bullet = null;
1✔
101
                $min = 1;
1✔
102
                $start = null;
1✔
103
                $listType = ListType::Unordered;
1✔
104

105
                foreach ($this->bullets as $key => $desc) {
1✔
106
                        if (Regexp::match($mBullet, '~' . $desc[0] . '~A')) {
1✔
107
                                $bullet = $desc[2] ?? $desc[0];
1✔
108
                                $min = isset($desc[2]) ? 2 : 1;
1✔
109
                                $listType = $desc[1];
1✔
110
                                if ($listType->isOrdered()) {
1✔
111
                                        if ($key[0] === '1' && (int) $mBullet > 1) {
1✔
112
                                                $start = (int) $mBullet;
×
113
                                        } elseif ($key[0] === 'a' && $mBullet[0] > 'a') {
1✔
114
                                                $start = ord($mBullet[0]) - 96;
×
115
                                        } elseif ($key[0] === 'A' && $mBullet[0] > 'A') {
1✔
116
                                                $start = ord($mBullet[0]) - 64;
×
117
                                        }
118
                                }
119
                                break;
1✔
120
                        }
121
                }
122

123
                if ($bullet === null) {
1✔
124
                        return null;
×
125
                }
126

127
                $context->getBlockParser()->moveBackward();
1✔
128

129
                $items = [];
1✔
130
                while ($item = $this->parseItem($context, $bullet, false)) {
1✔
131
                        $items[] = $item;
1✔
132
                }
133

134
                if (count($items) < $min) {
1✔
135
                        return null;
1✔
136
                }
137

138
                return new ListNode(
1✔
139
                        $items,
1✔
140
                        $listType,
141
                        $start,
142
                        Modifier::parse($mMod),
1✔
143
                        new Position($offsets[0], strlen($matches[0])),
1✔
144
                );
145
        }
146

147

148
        /**
149
         * Parses definition list.
150
         * @param  array<?string>  $matches
151
         * @param  array<?int>  $offsets
152
         */
153
        public function parseDefList(ParseContext $context, array $matches, array $offsets): ?DefinitionListNode
1✔
154
        {
155
                /** @var array{string, ?string, string, ?string, string, string} $matches */
156
                [, $mMod, , , , $mBullet] = $matches;
1✔
157

158
                $bullet = null;
1✔
159

160
                foreach ($this->bullets as $desc) {
1✔
161
                        if (Regexp::match($mBullet, '~' . $desc[0] . '~A')) {
1✔
162
                                $bullet = $desc[2] ?? $desc[0];
1✔
163
                                break;
1✔
164
                        }
165
                }
166

167
                if ($bullet === null) {
1✔
168
                        return null;
×
169
                }
170

171
                $context->getBlockParser()->moveBackward(2);
1✔
172

173
                $items = [];
1✔
174
                $patternTerm = '~^
1✔
175
                        \n?
176
                        ( \S .* )                       # term content
177
                        : [ \t]*                        # colon separator
178
                        ' . Patterns::MODIFIER_H . '?
1✔
179
                $~mUA';
180

181
                while (true) {
1✔
182
                        if ($item = $this->parseItem($context, $bullet, true)) {
1✔
183
                                $items[] = $item;
1✔
184
                                continue;
1✔
185
                        }
186

187
                        $termMatches = null;
1✔
188
                        $termOffsets = null;
1✔
189
                        if ($context->getBlockParser()->next($patternTerm, $termMatches, $termOffsets)) {
1✔
190
                                /** @var array{string, string, ?string} $termMatches */
191
                                [, $mContent, $mTermMod] = $termMatches;
1✔
192
                                $termMod = Modifier::parse($mTermMod);
1✔
193
                                $contentOffset = $termOffsets[1] ?? 0;
1✔
194
                                $termContent = $context->parseInline($mContent, $contentOffset);
1✔
195
                                $items[] = new ListItemNode($termContent, true, $termMod);
1✔
196
                                continue;
1✔
197
                        }
198

199
                        break;
1✔
200
                }
201

202
                return new DefinitionListNode(
1✔
203
                        $items,
1✔
204
                        Modifier::parse($mMod),
1✔
205
                        new Position($offsets[0], strlen($matches[0])),
1✔
206
                );
207
        }
208

209

210
        /**
211
         * Parses single list item.
212
         */
213
        private function parseItem(ParseContext $context, string $bullet, bool $indented): ?ListItemNode
1✔
214
        {
215
                $spacesBase = $indented ? ('[\ \t]{1,}') : '';
1✔
216
                $patternItem = "~^
1✔
217
                        \\n?
218
                        ($spacesBase)                            # base indentation (1)
1✔
219
                        {$bullet}                                # bullet character
1✔
220
                        [ \\t]*
221
                        ( \\S .* )?                              # content (2)
222
                        " . Patterns::MODIFIER_H . '?           # modifier (3)
1✔
223
                $~mAU';
224

225
                $matches = null;
1✔
226
                $offsets = null;
1✔
227
                if (!$context->getBlockParser()->next($patternItem, $matches, $offsets)) {
1✔
228
                        return null;
1✔
229
                }
230

231
                /** @var array{string, string, ?string, ?string} $matches */
232
                [, $mIndent, $mContent, $mMod] = $matches;
1✔
233

234
                // Assemble content and map local line starts to absolute source offsets
235
                $map = [];
1✔
236
                $content = ' ' . ($mContent ?? '');
1✔
237
                if ($mContent !== null) {
1✔
238
                        $map[1] = $offsets[2] ?? $offsets[0]; // 1 = the leading space
1✔
239
                }
240

241
                // next lines
242
                $spaces = '';
1✔
243
                while ($context->getBlockParser()->next('~^
1✔
244
                        (\n*)
245
                        ' . Regexp::quote($mIndent) . '
1✔
246
                        ([ \t]{1,' . $spaces . '})
1✔
247
                        (.*)                                     # content (3)
248
                $~Am', $matches, $offsets)) {
249
                        /** @var array{string, string, string, string} $matches */
250
                        [, $mBlank, $mSpaces, $mContent] = $matches;
1✔
251

252
                        if ($spaces === '') {
1✔
253
                                $spaces = strlen($mSpaces);
1✔
254
                        }
255

256
                        if ($mContent !== '' && $offsets[3] !== null) {
1✔
257
                                $map[strlen($content) + 1 + strlen($mBlank)] = $offsets[3]; // 1 = "\n"
1✔
258
                        }
259

260
                        $content .= "\n" . $mBlank . $mContent;
1✔
261
                }
262

263
                // Parse content as blocks
264
                $parsed = $context->parseBlock(trim($content));
1✔
265

266
                // Fix positions in parsed content using offset mapping
267
                if ($map) {
1✔
268
                        $skipped = strlen($content) - strlen(ltrim($content));
1✔
269
                        (new Texy\OffsetMap($map, $skipped))->applyTo($parsed);
1✔
270
                }
271

272
                return new ListItemNode(
1✔
273
                        $parsed,
1✔
274
                        false,
1✔
275
                        Modifier::parse($mMod),
1✔
276
                );
277
        }
278
}
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