• 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

92.25
/src/Texy/Modules/TableModule.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\HtmlElement;
12
use Texy\Modifier;
13
use Texy\Patterns;
14
use Texy\Regexp;
15
use function explode, ltrim, rtrim, str_contains, str_replace, strtr;
16

17

18
/**
19
 * Processes table syntax with headers, colspan, and rowspan support.
20
 */
21
final class TableModule extends Texy\Module
22
{
23
        private ?bool $disableTables = null;
24

25

26
        public function __construct(
1✔
27
                private Texy\Texy $texy,
28
        ) {
29
        }
1✔
30

31

32
        public function beforeParse(string &$text): void
1✔
33
        {
34
                $this->texy->registerBlockPattern(
1✔
35
                        $this->parseTable(...),
1✔
36
                        '~^
37
                                (?:' . Patterns::MODIFIER_HV . '\n)? # modifier (1)
1✔
38
                                \|                                   # table start
39
                                .*                                   # content
40
                        $~mU',
41
                        'table',
1✔
42
                );
43
        }
1✔
44

45

46
        /**
47
         * Parses tables.
48
         * @param  array<?string>  $matches
49
         */
50
        public function parseTable(Texy\BlockParser $parser, array $matches): ?HtmlElement
1✔
51
        {
52
                if ($this->disableTables) {
1✔
53
                        return null;
1✔
54
                }
55

56
                [, $mMod] = $matches;
1✔
57
                // [1] => .(title)[class]{style}<>_
58

59
                $texy = $this->texy;
1✔
60

61
                $el = new HtmlElement('table');
1✔
62
                $mod = Modifier::parse($mMod);
1✔
63
                $mod->decorate($texy, $el);
1✔
64

65
                $parser->moveBackward();
1✔
66

67
                if ($parser->next(
1✔
68
                        '~^
1✔
69
                                \|                               # opening pipe
70
                                ( [\#=] ){2,}  (?! [|#=+] )      # opening chars (1)
71
                                (.+)                             # caption (2)
72
                                \1*                              # matching closing chars
73
                                \|? \ *                              # optional closing pipe and spaces
74
                                ' . Patterns::MODIFIER_H . '?    # modifier (3)
1✔
75
                        $~Um',
76
                        $matches,
77
                )) {
UNCOV
78
                        [, , $mContent, $mMod] = $matches;
×
79
                        // [1] => # / =
80
                        // [2] => ....
81
                        // [3] => .(title)[class]{style}<>
82

UNCOV
83
                        $caption = $el->create('caption');
×
UNCOV
84
                        $mod = Modifier::parse($mMod);
×
UNCOV
85
                        $mod->decorate($texy, $caption);
×
UNCOV
86
                        $caption->parseLine($texy, $mContent);
×
87
                }
88

89
                $isHead = false;
1✔
90
                $colModifier = [];
1✔
91
                $prevRow = []; // rowSpan building helper
1✔
92
                $rowCounter = 0;
1✔
93
                $colCounter = 0;
1✔
94
                $elPart = null;
1✔
95

96
                while (true) {
1✔
97
                        if ($parser->next('~^ \| ([=-]) [+|=-]{2,} $~Um', $matches)) { // line
1✔
98
                                $isHead = !$isHead;
1✔
99
                                $prevRow = [];
1✔
100
                                continue;
1✔
101
                        }
102

103
                        if ($parser->next('~^ \| (.*) (?: | \| [ \t]* ' . Patterns::MODIFIER_HV . '?)$~U', $matches)) {
1✔
104
                                // smarter head detection
105
                                if ($rowCounter === 0 && !$isHead && $parser->next('~^ \| [=-] [+|=-]{2,} $~Um', $foo)) {
1✔
106
                                        $isHead = true;
1✔
107
                                        $parser->moveBackward();
1✔
108
                                }
109

110
                                if ($elPart === null) {
1✔
111
                                        $elPart = $el->create($isHead ? 'thead' : 'tbody');
1✔
112

113
                                } elseif (!$isHead && $elPart->getName() === 'thead') {
1✔
114
                                        $this->finishPart($elPart);
1✔
115
                                        $elPart = $el->create('tbody');
1✔
116
                                }
117

118
                                [, $mContent, $mMod] = $matches;
1✔
119
                                // [1] => ....
120
                                // [2] => .(title)[class]{style}<>_
121

122
                                $elRow = $this->processRow($mContent, $mMod, $isHead, $texy, $prevRow, $colModifier, $colCounter);
1✔
123

124
                                if ($elRow->count()) {
1✔
125
                                        $elPart->add($elRow);
1✔
126
                                        $rowCounter++;
1✔
127
                                } else { // redundant row
128
                                        foreach ($prevRow as $elCell) {
1✔
129
                                                $elCell->rowSpan--;
1✔
130
                                        }
131
                                }
132

133
                                continue;
1✔
134
                        }
135

136
                        break;
1✔
137
                }
138

139
                if ($elPart === null) { // invalid table
1✔
UNCOV
140
                        return null;
×
141
                }
142

143
                if ($elPart->getName() === 'thead') {
1✔
144
                        // thead is optional, tbody is required
UNCOV
145
                        $elPart->setName('tbody');
×
146
                }
147

148
                $this->finishPart($elPart);
1✔
149

150
                // event listener
151
                $texy->invokeHandlers('afterTable', [$parser, $el, $mod]);
1✔
152

153
                return $el;
1✔
154
        }
155

156

157
        /**
158
         * @param  array<int, TableCellElement>  $prevRow
159
         * @param  array<int, Modifier|null>  $colModifier
160
         */
161
        private function processRow(
1✔
162
                string $content,
163
                ?string $mMod,
164
                bool $isHead,
165
                Texy\Texy $texy,
166
                array &$prevRow,
167
                array &$colModifier,
168
                int &$colCounter,
169
        ): HtmlElement
170
        {
171
                $elRow = new HtmlElement('tr');
1✔
172
                $mod = Modifier::parse($mMod);
1✔
173
                $mod->decorate($texy, $elRow);
1✔
174

175
                $col = 0;
1✔
176
                $elCell = null;
1✔
177

178
                // special escape sequence \|
179
                $content = str_replace('\|', "\x13", $content);
1✔
180
                $content = Regexp::replace($content, '~(\[[^]]*)\|~', "$1\x13"); // HACK: support for [..|..]
1✔
181

182
                foreach (explode('|', $content) as $cell) {
1✔
183
                        $cell = strtr($cell, "\x13", '|');
1✔
184
                        // rowSpan
185
                        if (isset($prevRow[$col]) && ($matches = Regexp::match($cell, '~\^[ \t]*$|\*??(.*)[ \t]+\^$~AU'))) {
1✔
186
                                $prevRow[$col]->rowSpan++;
1✔
187
                                $cell = $matches[1] ?? '';
1✔
188
                                $prevRow[$col]->text .= "\n" . $cell;
1✔
189
                                $col += $prevRow[$col]->colSpan;
1✔
190
                                $elCell = null;
1✔
191
                                continue;
1✔
192
                        }
193

194
                        // colSpan
195
                        if ($cell === '' && $elCell) {
1✔
196
                                $elCell->colSpan++;
1✔
197
                                unset($prevRow[$col]);
1✔
198
                                $col++;
1✔
199
                                continue;
1✔
200
                        }
201

202
                        // common cell
203
                        if ($elCell = $this->processCell($cell, $colModifier[$col], $isHead, $texy)) {
1✔
204
                                $elRow->add($elCell);
1✔
205
                                $prevRow[$col] = $elCell;
1✔
206
                                $col++;
1✔
207
                        }
208
                }
209

210
                // even up with empty cells
211
                while ($col < $colCounter) {
1✔
212
                        $elCell = new TableCellElement;
1✔
213
                        $elCell->setName($isHead ? 'th' : 'td');
1✔
214
                        if (isset($colModifier[$col])) {
1✔
UNCOV
215
                                $colModifier[$col]->decorate($texy, $elCell);
×
216
                        }
217

218
                        $elRow->add($elCell);
1✔
219
                        $prevRow[$col] = $elCell;
1✔
220
                        $col++;
1✔
221
                }
222

223
                $colCounter = $col;
1✔
224
                return $elRow;
1✔
225
        }
226

227

228
        private function processCell(
1✔
229
                string $cell,
230
                ?Modifier &$cellModifier,
231
                bool $isHead,
232
                Texy\Texy $texy,
233
        ): ?TableCellElement
234
        {
235
                $matches = Regexp::match($cell, '~
1✔
236
                        ( \*?? )                          # head mark (1)
237
                        [ \t]*
238
                        ' . Patterns::MODIFIER_HV . '??   # modifier (2)
1✔
239
                        (.*)                              # content (3)
240
                        ' . Patterns::MODIFIER_HV . '?    # modifier (4)
1✔
241
                        [ \t]*
242
                $~AU');
243
                if (!$matches) {
1✔
UNCOV
244
                        return null;
×
245
                }
246

247
                [, $mHead, $mModCol, $mContent, $mMod] = $matches;
1✔
248
                // [1] => * ^
249
                // [2] => .(title)[class]{style}<>_
250
                // [3] => ....
251
                // [4] => .(title)[class]{style}<>_
252

253
                if ($mModCol) {
1✔
UNCOV
254
                        $cellModifier = Modifier::parse($mModCol);
×
255
                }
256

257
                $mod = $cellModifier ? clone $cellModifier : new Modifier;
1✔
258
                $mod->setProperties($mMod);
1✔
259

260
                $elCell = new TableCellElement;
1✔
261
                $elCell->setName($isHead || ($mHead === '*') ? 'th' : 'td');
1✔
262
                $mod->decorate($texy, $elCell);
1✔
263
                $elCell->text = $mContent;
1✔
264
                return $elCell;
1✔
265
        }
266

267

268
        /**
269
         * Parse text in all cells.
270
         */
271
        private function finishPart(HtmlElement $elPart): void
1✔
272
        {
273
                foreach ($elPart->getChildren() as $elRow) {
1✔
274
                        assert($elRow instanceof HtmlElement);
275
                        foreach ($elRow->getChildren() as $elCell) {
1✔
276
                                assert($elCell instanceof TableCellElement);
277
                                if ($elCell->colSpan > 1) {
1✔
278
                                        $elCell->attrs['colspan'] = $elCell->colSpan;
1✔
279
                                }
280

281
                                if ($elCell->rowSpan > 1) {
1✔
282
                                        $elCell->attrs['rowspan'] = $elCell->rowSpan;
1✔
283
                                }
284

285
                                $text = rtrim((string) $elCell->text);
1✔
286
                                if (str_contains($text, "\n")) {
1✔
287
                                        // multiline parse as block
288
                                        // HACK: disable tables
289
                                        $this->disableTables = true;
1✔
290
                                        $elCell->parseBlock($this->texy, Texy\Helpers::outdent($text));
1✔
291
                                        $this->disableTables = false;
1✔
292
                                } else {
293
                                        $elCell->parseLine($this->texy, ltrim($text));
1✔
294
                                }
295

296
                                if ($elCell->getText() === '') {
1✔
297
                                        $elCell->setText("\u{A0}"); // &nbsp;
1✔
298
                                }
299
                        }
300
                }
301
        }
1✔
302
}
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