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

dg / texy / 22262589061

21 Feb 2026 07:04PM UTC coverage: 92.991% (+1.8%) from 91.178%
22262589061

push

github

dg
LinkModule: deprecated label and modifiers in link definitions

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

126 existing lines in 22 files now uncovered.

2083 of 2240 relevant lines covered (92.99%)

0.93 hits per line

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

97.14
/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\BlockParser;
12
use Texy\HtmlElement;
13
use Texy\Modifier;
14
use Texy\Patterns;
15
use Texy\Regexp;
16
use function implode, ord, strlen;
17

18

19
/**
20
 * Processes ordered, unordered, and definition lists with nesting.
21
 */
22
final class ListModule extends Texy\Module
23
{
24
        /** @var array<string, array{string, int, string, 3?: string}> [regex, ordered?, list-style-type, next-regex?] */
25
        public array $bullets = [
26
                // first-rexexp ordered? list-style-type next-regexp
27
                '*' => ['\* [ \t]', 0, ''],
28
                '-' => ['[\x{2013}-] (?! [>-] )', 0, ''],
29
                '+' => ['\+ [ \t]', 0, ''],
30
                '1.' => ['1 \. [ \t]', /* not \d !*/ 1, '', '\d{1,3} \. [ \t]'],
31
                '1)' => ['\d{1,3} \) [ \t]', 1, ''],
32
                'I.' => ['I \. [ \t]', 1, 'upper-roman', '[IVX]{1,4} \. [ \t]'],
33
                'I)' => ['[IVX]+ \) [ \t]', 1, 'upper-roman'], // before A) !
34
                'a)' => ['[a-z] \) [ \t]', 1, 'lower-alpha'],
35
                'A)' => ['[A-Z] \) [ \t]', 1, 'upper-alpha'],
36
        ];
37

38

39
        public function __construct(
1✔
40
                private Texy\Texy $texy,
41
        ) {
42
                $texy->allowed['list'] = true;
1✔
43
                $texy->allowed['list/definition'] = true;
1✔
44
        }
1✔
45

46

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

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

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

85

86
        /**
87
         * Parses list.
88
         * @param  array<?string>  $matches
89
         */
90
        public function parseList(BlockParser $parser, array $matches): ?HtmlElement
1✔
91
        {
92
                [, $mMod, $mBullet] = $matches;
1✔
93
                // [1] => .(title)[class]{style}<>
94
                // [2] => bullet * + - 1) a) A) IV)
95

96
                $el = new HtmlElement;
1✔
97

98
                $bullet = $min = null;
1✔
99
                foreach ($this->bullets as $type => $desc) {
1✔
100
                        if (Regexp::match($mBullet, '~' . $desc[0] . '~A')) {
1✔
101
                                $bullet = $desc[3] ?? $desc[0];
1✔
102
                                $min = isset($desc[3]) ? 2 : 1;
1✔
103
                                $el->setName($desc[1] ? 'ol' : 'ul');
1✔
104
                                $el->attrs['style'] = (array) ($el->attrs['style'] ?? []);
1✔
105
                                $el->attrs['style']['list-style-type'] = $desc[2];
1✔
106
                                if ($desc[1]) { // ol
1✔
107
                                        if ($type[0] === '1' && (int) $mBullet > 1) {
1✔
UNCOV
108
                                                $el->attrs['start'] = (int) $mBullet;
×
109
                                        } elseif ($type[0] === 'a' && $mBullet[0] > 'a') {
1✔
UNCOV
110
                                                $el->attrs['start'] = ord($mBullet[0]) - 96;
×
111
                                        } elseif ($type[0] === 'A' && $mBullet[0] > 'A') {
1✔
UNCOV
112
                                                $el->attrs['start'] = ord($mBullet[0]) - 64;
×
113
                                        }
114
                                }
115

116
                                break;
1✔
117
                        }
118
                }
119
                assert($bullet !== null);
120

121
                $mod = Modifier::parse($mMod);
1✔
122
                $mod->decorate($this->texy, $el);
1✔
123

124
                $parser->moveBackward(1);
1✔
125

126
                while ($elItem = $this->parseItem($parser, $bullet, false, 'li')) {
1✔
127
                        $el->add($elItem);
1✔
128
                }
129

130
                if ($el->count() < $min) {
1✔
131
                        return null;
1✔
132
                }
133

134
                // event listener
135
                $this->texy->invokeHandlers('afterList', [$parser, $el, $mod]);
1✔
136

137
                return $el;
1✔
138
        }
139

140

141
        /**
142
         * Parses definition list.
143
         * @param  array<?string>  $matches
144
         */
145
        public function parseDefList(BlockParser $parser, array $matches): HtmlElement
1✔
146
        {
147
                [, $mMod, , , , $mBullet] = $matches;
1✔
148
                // [1] => .(title)[class]{style}<>
149
                // [2] => ...
150
                // [3] => .(title)[class]{style}<>
151
                // [4] => space
152
                // [5] => - * +
153

154
                $texy = $this->texy;
1✔
155

156
                $bullet = null;
1✔
157
                foreach ($this->bullets as $desc) {
1✔
158
                        if (Regexp::match($mBullet, '~' . $desc[0] . '~A')) {
1✔
159
                                $bullet = $desc[3] ?? $desc[0];
1✔
160
                                break;
1✔
161
                        }
162
                }
163
                assert($bullet !== null);
164

165
                $el = new HtmlElement('dl');
1✔
166
                $mod = Modifier::parse($mMod);
1✔
167
                $mod->decorate($texy, $el);
1✔
168
                $parser->moveBackward(2);
1✔
169

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

177
                while (true) {
1✔
178
                        if ($elItem = $this->parseItem($parser, $bullet, true, 'dd')) {
1✔
179
                                $el->add($elItem);
1✔
180
                                continue;
1✔
181
                        }
182

183
                        if ($parser->next($patternTerm, $matches)) {
1✔
184
                                [, $mContent, $mMod] = $matches;
1✔
185
                                // [1] => ...
186
                                // [2] => .(title)[class]{style}<>
187

188
                                $elItem = new HtmlElement('dt');
1✔
189
                                $modItem = Modifier::parse($mMod);
1✔
190
                                $modItem->decorate($texy, $elItem);
1✔
191

192
                                $elItem->parseLine($texy, $mContent);
1✔
193
                                $el->add($elItem);
1✔
194
                                continue;
1✔
195
                        }
196

197
                        break;
1✔
198
                }
199

200
                // event listener
201
                $texy->invokeHandlers('afterDefinitionList', [$parser, $el, $mod]);
1✔
202

203
                return $el;
1✔
204
        }
205

206

207
        /**
208
         * Parses single list item.
209
         */
210
        private function parseItem(BlockParser $parser, string $bullet, bool $indented, string $tag): ?HtmlElement
1✔
211
        {
212
                $spacesBase = $indented ? ('[\ \t]{1,}') : '';
1✔
213
                $patternItem = "~^
1✔
214
                        \\n?
215
                        ($spacesBase)                            # base indentation
1✔
216
                        {$bullet}                                # bullet character
1✔
217
                        [ \\t]*
218
                        ( \\S .* )?                              # content
219
                        " . Patterns::MODIFIER_H . '?
1✔
220
                $~mAU';
221

222
                // first line with bullet
223
                $matches = null;
1✔
224
                if (!$parser->next($patternItem, $matches)) {
1✔
225
                        return null;
1✔
226
                }
227

228
                [, $mIndent, $mContent, $mMod] = $matches;
1✔
229
                // [1] => indent
230
                // [2] => ...
231
                // [3] => .(title)[class]{style}<>
232

233
                $elItem = new HtmlElement($tag);
1✔
234
                $mod = Modifier::parse($mMod);
1✔
235
                $mod->decorate($this->texy, $elItem);
1✔
236

237
                // next lines
238
                $spaces = '';
1✔
239
                $content = ' ' . $mContent; // trick
1✔
240
                while ($parser->next('~^
1✔
241
                        (\n*)
242
                        ' . Regexp::quote($mIndent) . '
1✔
243
                        ([ \t]{1,' . $spaces . '})
1✔
244
                        (.*)
245
                $~Am', $matches)) {
246
                        [, $mBlank, $mSpaces, $mContent] = $matches;
1✔
247
                        // [1] => blank line?
248
                        // [2] => spaces
249
                        // [3] => ...
250

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

255
                        $content .= "\n" . $mBlank . $mContent;
1✔
256
                }
257

258
                // parse content
259
                $elItem->parseBlock($this->texy, $content, true);
1✔
260

261
                if (isset($elItem[0]) && $elItem[0] instanceof HtmlElement) {
1✔
262
                        $elItem[0]->setName(null);
1✔
263
                }
264

265
                return $elItem;
1✔
266
        }
267
}
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