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

dg / texy / 16790351274

06 Aug 2025 10:42PM UTC coverage: 92.746% (+0.005%) from 92.741%
16790351274

push

github

dg
HtmlElement: removed toHtml() & toText()

18 of 19 new or added lines in 5 files covered. (94.74%)

136 existing lines in 23 files now uncovered.

2391 of 2578 relevant lines covered (92.75%)

0.93 hits per line

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

97.03
/src/Texy/Modules/ListModule.php
1
<?php
2

3
/**
4
 * This file is part of the Texy! (https://texy.info)
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 Texy\Regexp;
18
use function implode, ord, preg_match, strlen;
19

20

21
/**
22
 * Ordered / unordered nested list module.
23
 */
24
final class ListModule extends Texy\Module
25
{
26
        public array $bullets = [
27
                // first-rexexp ordered? list-style-type next-regexp
28
                '*' => ['\* [ \t]', 0, ''],
29
                '-' => ['[\x{2013}-] (?! [>-] )', 0, ''],
30
                '+' => ['\+ [ \t]', 0, ''],
31
                '1.' => ['1 \. [ \t]', /* not \d !*/ 1, '', '\d{1,3} \. [ \t]'],
32
                '1)' => ['\d{1,3} \) [ \t]', 1, ''],
33
                'I.' => ['I \. [ \t]', 1, 'upper-roman', '[IVX]{1,4} \. [ \t]'],
34
                'I)' => ['[IVX]+ \) [ \t]', 1, 'upper-roman'], // before A) !
35
                'a)' => ['[a-z] \) [ \t]', 1, 'lower-alpha'],
36
                'A)' => ['[A-Z] \) [ \t]', 1, 'upper-alpha'],
37
        ];
38

39

40
        public function __construct(Texy\Texy $texy)
1✔
41
        {
42
                $this->texy = $texy;
1✔
43

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

49

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

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

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

88

89
        /**
90
         * Callback for:.
91
         *
92
         * 1) .... .(title)[class]{style}>
93
         * 2) ....
94
         *   + ...
95
         *   + ...
96
         * 3) ....
97
         */
98
        public function parseList(BlockParser $parser, array $matches): ?HtmlElement
1✔
99
        {
100
                [, $mMod, $mBullet] = $matches;
1✔
101
                // [1] => .(title)[class]{style}<>
102
                // [2] => bullet * + - 1) a) A) IV)
103

104
                $el = new HtmlElement;
1✔
105

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

123
                                break;
1✔
124
                        }
125
                }
126

127
                $mod = new Modifier($mMod);
1✔
128
                $mod->decorate($this->texy, $el);
1✔
129

130
                $parser->moveBackward(1);
1✔
131

132
                while ($elItem = $this->parseItem($parser, $bullet, false, 'li')) {
1✔
133
                        $el->add($elItem);
1✔
134
                }
135

136
                if ($el->count() < $min) {
1✔
137
                        return null;
1✔
138
                }
139

140
                // event listener
141
                $this->texy->invokeHandlers('afterList', [$parser, $el, $mod]);
1✔
142

143
                return $el;
1✔
144
        }
145

146

147
        /**
148
         * Callback for:.
149
         *
150
         * Term: .(title)[class]{style}>
151
         * - description 1
152
         * - description 2
153
         * - description 3
154
         */
155
        public function parseDefList(BlockParser $parser, array $matches): HtmlElement
1✔
156
        {
157
                [, $mMod, , , , $mBullet] = $matches;
1✔
158
                // [1] => .(title)[class]{style}<>
159
                // [2] => ...
160
                // [3] => .(title)[class]{style}<>
161
                // [4] => space
162
                // [5] => - * +
163

164
                $texy = $this->texy;
1✔
165

166
                $bullet = null;
1✔
167
                foreach ($this->bullets as $desc) {
1✔
168
                        if (Regexp::match($mBullet, '~' . $desc[0] . '~A')) {
1✔
169
                                $bullet = $desc[3] ?? $desc[0];
1✔
170
                                break;
1✔
171
                        }
172
                }
173

174
                $el = new HtmlElement('dl');
1✔
175
                $mod = new Modifier($mMod);
1✔
176
                $mod->decorate($texy, $el);
1✔
177
                $parser->moveBackward(2);
1✔
178

179
                $patternTerm = '~^
1✔
180
                        \n?
181
                        ( \S .* )                       # term content
182
                        : [ \t]*                        # colon separator
183
                        ' . Patterns::MODIFIER_H . '?
184
                $~mUA';
185

186
                while (true) {
1✔
187
                        if ($elItem = $this->parseItem($parser, $bullet, true, 'dd')) {
1✔
188
                                $el->add($elItem);
1✔
189
                                continue;
1✔
190
                        }
191

192
                        if ($parser->next($patternTerm, $matches)) {
1✔
193
                                [, $mContent, $mMod] = $matches;
1✔
194
                                // [1] => ...
195
                                // [2] => .(title)[class]{style}<>
196

197
                                $elItem = new HtmlElement('dt');
1✔
198
                                $modItem = new Modifier($mMod);
1✔
199
                                $modItem->decorate($texy, $elItem);
1✔
200

201
                                $elItem->inject($texy->parseLine($mContent));
1✔
202
                                $el->add($elItem);
1✔
203
                                continue;
1✔
204
                        }
205

206
                        break;
1✔
207
                }
208

209
                // event listener
210
                $texy->invokeHandlers('afterDefinitionList', [$parser, $el, $mod]);
1✔
211

212
                return $el;
1✔
213
        }
214

215

216
        /**
217
         * Callback for single list item.
218
         */
219
        private function parseItem(BlockParser $parser, string $bullet, bool $indented, string $tag): ?HtmlElement
1✔
220
        {
221
                $spacesBase = $indented ? ('[\ \t]{1,}') : '';
1✔
222
                $patternItem = "~^
1✔
223
                        \\n?
224
                        ($spacesBase)                            # base indentation
1✔
225
                        {$bullet}                                # bullet character
1✔
226
                        [ \\t]*
227
                        ( \\S .* )?                              # content
228
                        " . Patterns::MODIFIER_H . '?
1✔
229
                $~mAU';
230

231
                // first line with bullet
232
                $matches = null;
1✔
233
                if (!$parser->next($patternItem, $matches)) {
1✔
234
                        return null;
1✔
235
                }
236

237
                [, $mIndent, $mContent, $mMod] = $matches;
1✔
238
                // [1] => indent
239
                // [2] => ...
240
                // [3] => .(title)[class]{style}<>
241

242
                $elItem = new HtmlElement($tag);
1✔
243
                $mod = new Modifier($mMod);
1✔
244
                $mod->decorate($this->texy, $elItem);
1✔
245

246
                // next lines
247
                $spaces = '';
1✔
248
                $content = ' ' . $mContent; // trick
1✔
249
                while ($parser->next('~^
1✔
250
                        (\n*)
251
                        ' . Regexp::quote($mIndent) . '
1✔
252
                        ([ \t]{1,' . $spaces . '})
1✔
253
                        (.*)
254
                $~Am', $matches)) {
255
                        [, $mBlank, $mSpaces, $mContent] = $matches;
1✔
256
                        // [1] => blank line?
257
                        // [2] => spaces
258
                        // [3] => ...
259

260
                        if ($spaces === '') {
1✔
261
                                $spaces = strlen($mSpaces);
1✔
262
                        }
263

264
                        $content .= "\n" . $mBlank . $mContent;
1✔
265
                }
266

267
                // parse content
268
                $elItem->inject($this->texy->parseBlock($content, true));
1✔
269

270
                if (isset($elItem[0]) && $elItem[0] instanceof HtmlElement) {
1✔
271
                        $elItem[0]->setName(null);
1✔
272
                }
273

274
                return $elItem;
1✔
275
        }
276
}
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