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

dg / texy / 22262381750

25 Jan 2026 11:44PM UTC coverage: 92.367% (-0.7%) from 93.057%
22262381750

push

github

dg
cs

9 of 11 new or added lines in 8 files covered. (81.82%)

161 existing lines in 23 files now uncovered.

2384 of 2581 relevant lines covered (92.37%)

0.92 hits per line

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

96.88
/src/Texy/Modules/ListModule.php
1
<?php
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
declare(strict_types=1);
9

10
namespace Texy\Modules;
11

12
use Texy;
13
use Texy\BlockParser;
14
use Texy\HtmlElement;
15
use Texy\Modifier;
16
use Texy\Patterns;
17
use function implode, ord, preg_match, strlen;
18

19

20
/**
21
 * Ordered / unordered nested list module.
22
 */
23
final class ListModule extends Texy\Module
24
{
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(Texy\Texy $texy)
1✔
40
        {
41
                $this->texy = $texy;
1✔
42

43
                $texy->addHandler('beforeParse', $this->beforeParse(...));
1✔
44
                $texy->allowed['list'] = true;
1✔
45
                $texy->allowed['list/definition'] = true;
1✔
46
        }
1✔
47

48

49
        private function beforeParse(): void
50
        {
51
                $RE = $REul = [];
1✔
52
                foreach ($this->bullets as $desc) {
1✔
53
                        $RE[] = $desc[0];
1✔
54
                        if (!$desc[1]) {
1✔
55
                                $REul[] = $desc[0];
1✔
56
                        }
57
                }
58

59
                $this->texy->registerBlockPattern(
1✔
60
                        $this->patternList(...),
1✔
61
                        '#^(?:' . Patterns::MODIFIER_H . '\n)?' // .{color: red}
62
                        . '(' . implode('|', $RE) . ')[\ \t]*+\S.*$#mUu', // item (unmatched)
1✔
63
                        'list',
1✔
64
                );
65

66
                $this->texy->registerBlockPattern(
1✔
67
                        $this->patternDefList(...),
1✔
68
                        '#^(?:' . Patterns::MODIFIER_H . '\n)?' // .{color:red}
69
                        . '(\S.{0,2000})\:[\ \t]*' . Patterns::MODIFIER_H . '?\n' // Term:
70
                        . '([\ \t]++)(' . implode('|', $REul) . ')[\ \t]*+\S.*$#mUu', // - description
1✔
71
                        'list/definition',
1✔
72
                );
73
        }
1✔
74

75

76
        /**
77
         * Callback for:.
78
         *
79
         * 1) .... .(title)[class]{style}>
80
         * 2) ....
81
         *   + ...
82
         *   + ...
83
         * 3) ....
84
         */
85
        public function patternList(BlockParser $parser, array $matches): ?HtmlElement
1✔
86
        {
87
                [, $mMod, $mBullet] = $matches;
1✔
88
                // [1] => .(title)[class]{style}<>
89
                // [2] => bullet * + - 1) a) A) IV)
90

91
                $el = new HtmlElement;
1✔
92

93
                $bullet = $min = null;
1✔
94
                foreach ($this->bullets as $type => $desc) {
1✔
95
                        if (preg_match('#' . $desc[0] . '#Au', $mBullet)) {
1✔
96
                                $bullet = $desc[3] ?? $desc[0];
1✔
97
                                $min = isset($desc[3]) ? 2 : 1;
1✔
98
                                $el->setName($desc[1] ? 'ol' : 'ul');
1✔
99
                                $el->attrs['style']['list-style-type'] = $desc[2];
1✔
100
                                if ($desc[1]) { // ol
1✔
101
                                        if ($type[0] === '1' && (int) $mBullet > 1) {
1✔
UNCOV
102
                                                $el->attrs['start'] = (int) $mBullet;
×
103
                                        } elseif ($type[0] === 'a' && $mBullet[0] > 'a') {
1✔
UNCOV
104
                                                $el->attrs['start'] = ord($mBullet[0]) - 96;
×
105
                                        } elseif ($type[0] === 'A' && $mBullet[0] > 'A') {
1✔
UNCOV
106
                                                $el->attrs['start'] = ord($mBullet[0]) - 64;
×
107
                                        }
108
                                }
109

110
                                break;
1✔
111
                        }
112
                }
113

114
                $mod = new Modifier($mMod);
1✔
115
                $mod->decorate($this->texy, $el);
1✔
116

117
                $parser->moveBackward(1);
1✔
118

119
                while ($elItem = $this->patternItem($parser, $bullet, false, 'li')) {
1✔
120
                        $el->add($elItem);
1✔
121
                }
122

123
                if ($el->count() < $min) {
1✔
124
                        return null;
1✔
125
                }
126

127
                // event listener
128
                $this->texy->invokeHandlers('afterList', [$parser, $el, $mod]);
1✔
129

130
                return $el;
1✔
131
        }
132

133

134
        /**
135
         * Callback for:.
136
         *
137
         * Term: .(title)[class]{style}>
138
         * - description 1
139
         * - description 2
140
         * - description 3
141
         */
142
        public function patternDefList(BlockParser $parser, array $matches): HtmlElement
1✔
143
        {
144
                [, $mMod, , , , $mBullet] = $matches;
1✔
145
                // [1] => .(title)[class]{style}<>
146
                // [2] => ...
147
                // [3] => .(title)[class]{style}<>
148
                // [4] => space
149
                // [5] => - * +
150

151
                $texy = $this->texy;
1✔
152

153
                $bullet = null;
1✔
154
                foreach ($this->bullets as $desc) {
1✔
155
                        if (preg_match('#' . $desc[0] . '#Au', $mBullet)) {
1✔
156
                                $bullet = $desc[3] ?? $desc[0];
1✔
157
                                break;
1✔
158
                        }
159
                }
160

161
                $el = new HtmlElement('dl');
1✔
162
                $mod = new Modifier($mMod);
1✔
163
                $mod->decorate($texy, $el);
1✔
164
                $parser->moveBackward(2);
1✔
165

166
                $patternTerm = '#^\n?(\S.*)\:[\ \t]*' . Patterns::MODIFIER_H . '?()$#mUA';
1✔
167

168
                while (true) {
1✔
169
                        if ($elItem = $this->patternItem($parser, $bullet, true, 'dd')) {
1✔
170
                                $el->add($elItem);
1✔
171
                                continue;
1✔
172
                        }
173

174
                        if ($parser->next($patternTerm, $matches)) {
1✔
175
                                [, $mContent, $mMod] = $matches;
1✔
176
                                // [1] => ...
177
                                // [2] => .(title)[class]{style}<>
178

179
                                $elItem = new HtmlElement('dt');
1✔
180
                                $modItem = new Modifier($mMod);
1✔
181
                                $modItem->decorate($texy, $elItem);
1✔
182

183
                                $elItem->parseLine($texy, $mContent);
1✔
184
                                $el->add($elItem);
1✔
185
                                continue;
1✔
186
                        }
187

188
                        break;
1✔
189
                }
190

191
                // event listener
192
                $texy->invokeHandlers('afterDefinitionList', [$parser, $el, $mod]);
1✔
193

194
                return $el;
1✔
195
        }
196

197

198
        /**
199
         * Callback for single list item.
200
         */
201
        private function patternItem(BlockParser $parser, string $bullet, bool $indented, string $tag): ?HtmlElement
1✔
202
        {
203
                $spacesBase = $indented ? ('[\ \t]{1,}') : '';
1✔
204
                $patternItem = "#^\n?($spacesBase){$bullet}[ \\t]*(\\S.*)?" . Patterns::MODIFIER_H . '?()$#mAUu';
1✔
205

206
                // first line with bullet
207
                $matches = null;
1✔
208
                if (!$parser->next($patternItem, $matches)) {
1✔
209
                        return null;
1✔
210
                }
211

212
                [, $mIndent, $mContent, $mMod] = $matches;
1✔
213
                // [1] => indent
214
                // [2] => ...
215
                // [3] => .(title)[class]{style}<>
216

217
                $elItem = new HtmlElement($tag);
1✔
218
                $mod = new Modifier($mMod);
1✔
219
                $mod->decorate($this->texy, $elItem);
1✔
220

221
                // next lines
222
                $spaces = '';
1✔
223
                $content = ' ' . $mContent; // trick
1✔
224
                while ($parser->next('#^(\n*)' . $mIndent . '([\ \t]{1,' . $spaces . '})(.*)()$#Am', $matches)) {
1✔
225
                        [, $mBlank, $mSpaces, $mContent] = $matches;
1✔
226
                        // [1] => blank line?
227
                        // [2] => spaces
228
                        // [3] => ...
229

230
                        if ($spaces === '') {
1✔
231
                                $spaces = strlen($mSpaces);
1✔
232
                        }
233

234
                        $content .= "\n" . $mBlank . $mContent;
1✔
235
                }
236

237
                // parse content
238
                $elItem->parseBlock($this->texy, $content, true);
1✔
239

240
                if (isset($elItem[0]) && $elItem[0] instanceof HtmlElement) {
1✔
241
                        $elItem[0]->setName(null);
1✔
242
                }
243

244
                return $elItem;
1✔
245
        }
246
}
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