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

dg / texy / 22262497275

21 Feb 2026 07:01PM UTC coverage: 93.057% (+0.7%) from 92.367%
22262497275

push

github

dg
added CLAUDE.md

2426 of 2607 relevant lines covered (93.06%)

0.93 hits per line

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

90.63
/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
        /** @deprecated */
24
        public ?string $oddClass = null;
25

26
        /** @deprecated */
27
        public ?string $evenClass = null;
28
        private ?bool $disableTables = null;
29

30

31
        public function __construct(Texy\Texy $texy)
1✔
32
        {
33
                $this->texy = $texy;
1✔
34

35
                $texy->registerBlockPattern(
1✔
36
                        $this->patternTable(...),
1✔
37
                        '#^(?:' . Patterns::MODIFIER_HV . '\n)?' // .{color: red}
38
                        . '\|.*()$#mU', // | ....
1✔
39
                        'table',
1✔
40
                );
41
        }
1✔
42

43

44
        /**
45
         * Callback for:.
46
         *
47
         * .(title)[class]{style}>
48
         * |------------------
49
         * | xxx | xxx | xxx | .(..){..}[..]
50
         * |------------------
51
         * | aa | bb | cc |
52
         * @param  string[]  $matches
53
         */
54
        public function patternTable(Texy\BlockParser $parser, array $matches): ?HtmlElement
1✔
55
        {
56
                if ($this->disableTables) {
1✔
57
                        return null;
1✔
58
                }
59

60
                [, $mMod] = $matches;
1✔
61
                // [1] => .(title)[class]{style}<>_
62

63
                $texy = $this->texy;
1✔
64

65
                $el = new HtmlElement('table');
1✔
66
                $mod = new Modifier($mMod);
1✔
67
                $mod->decorate($texy, $el);
1✔
68

69
                $parser->moveBackward();
1✔
70

71
                if ($parser->next('#^\|(\#|\=){2,}(?![|\#=+])(.+)\1*\|?\ *' . Patterns::MODIFIER_H . '?()$#Um', $matches)) {
1✔
72
                        [, , $mContent, $mMod] = $matches;
×
73
                        // [1] => # / =
74
                        // [2] => ....
75
                        // [3] => .(title)[class]{style}<>
76

77
                        $caption = $el->create('caption');
×
78
                        $mod = new Modifier($mMod);
×
79
                        $mod->decorate($texy, $caption);
×
80
                        $caption->parseLine($texy, $mContent);
×
81
                }
82

83
                $isHead = false;
1✔
84
                $colModifier = [];
1✔
85
                $prevRow = []; // rowSpan building helper
1✔
86
                $rowCounter = 0;
1✔
87
                $colCounter = 0;
1✔
88
                $elPart = null;
1✔
89

90
                while (true) {
1✔
91
                        if ($parser->next('#^\|([=-])[+|=-]{2,}$#Um', $matches)) { // line
1✔
92
                                $isHead = !$isHead;
1✔
93
                                $prevRow = [];
1✔
94
                                continue;
1✔
95
                        }
96

97
                        if ($parser->next('#^\|(.*)(?:|\|[\ \t]*' . Patterns::MODIFIER_HV . '?)()$#U', $matches)) {
1✔
98
                                // smarter head detection
99
                                if ($rowCounter === 0 && !$isHead && $parser->next('#^\|[=-][+|=-]{2,}$#Um', $foo)) {
1✔
100
                                        $isHead = true;
1✔
101
                                        $parser->moveBackward();
1✔
102
                                }
103

104
                                if ($elPart === null) {
1✔
105
                                        $elPart = $el->create($isHead ? 'thead' : 'tbody');
1✔
106

107
                                } elseif (!$isHead && $elPart->getName() === 'thead') {
1✔
108
                                        $this->finishPart($elPart);
1✔
109
                                        $elPart = $el->create('tbody');
1✔
110
                                }
111

112
                                [, $mContent, $mMod] = $matches;
1✔
113
                                // [1] => ....
114
                                // [2] => .(title)[class]{style}<>_
115

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

118
                                if ($elRow->count()) {
1✔
119
                                        $elPart->add($elRow);
1✔
120
                                        $rowCounter++;
1✔
121
                                } else { // redundant row
122
                                        foreach ($prevRow as $elCell) {
1✔
123
                                                $elCell->rowSpan--;
1✔
124
                                        }
125
                                }
126

127
                                continue;
1✔
128
                        }
129

130
                        break;
1✔
131
                }
132

133
                if ($elPart === null) { // invalid table
1✔
134
                        return null;
×
135
                }
136

137
                if ($elPart->getName() === 'thead') {
1✔
138
                        // thead is optional, tbody is required
139
                        $elPart->setName('tbody');
×
140
                }
141

142
                $this->finishPart($elPart);
1✔
143

144
                // event listener
145
                $texy->invokeHandlers('afterTable', [$parser, $el, $mod]);
1✔
146

147
                return $el;
1✔
148
        }
149

150

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

170
                $rowClass = $rowCounter % 2 === 0 ? $this->oddClass : $this->evenClass;
1✔
171
                if ($rowClass && !isset($mod->classes[$this->oddClass ?? '']) && !isset($mod->classes[$this->evenClass ?? ''])) {
1✔
172
                        $elRow->attrs['class'] = (array) ($elRow->attrs['class'] ?? []);
×
173
                        $elRow->attrs['class'][] = $rowClass;
×
174
                }
175

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

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

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

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

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

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

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

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

228

229
        private function processCell(
1✔
230
                string $cell,
231
                ?Modifier &$cellModifier,
232
                bool $isHead,
233
                Texy\Texy $texy,
234
        ): ?TableCellElement
235
        {
236
                $matches = Regexp::match($cell, '#(\*??)[\ \t]*' . Patterns::MODIFIER_HV . '??(.*)' . Patterns::MODIFIER_HV . '?[\ \t]*()$#AU');
1✔
237
                if (!$matches) {
1✔
238
                        return null;
×
239
                }
240

241
                [, $mHead, $mModCol, $mContent, $mMod] = $matches;
1✔
242
                // [1] => * ^
243
                // [2] => .(title)[class]{style}<>_
244
                // [3] => ....
245
                // [4] => .(title)[class]{style}<>_
246

247
                if ($mModCol) {
1✔
248
                        $cellModifier = new Modifier($mModCol);
×
249
                }
250

251
                $mod = $cellModifier ? clone $cellModifier : new Modifier;
1✔
252
                $mod->setProperties($mMod);
1✔
253

254
                $elCell = new TableCellElement;
1✔
255
                $elCell->setName($isHead || ($mHead === '*') ? 'th' : 'td');
1✔
256
                $mod->decorate($texy, $elCell);
1✔
257
                $elCell->text = $mContent;
1✔
258
                return $elCell;
1✔
259
        }
260

261

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

275
                                if ($elCell->rowSpan > 1) {
1✔
276
                                        $elCell->attrs['rowspan'] = $elCell->rowSpan;
1✔
277
                                }
278

279
                                $text = rtrim((string) $elCell->text);
1✔
280
                                if (str_contains($text, "\n")) {
1✔
281
                                        // multiline parse as block
282
                                        // HACK: disable tables
283
                                        $this->disableTables = true;
1✔
284
                                        $elCell->parseBlock($this->texy, Texy\Helpers::outdent($text));
1✔
285
                                        $this->disableTables = false;
1✔
286
                                } else {
287
                                        $elCell->parseLine($this->texy, ltrim($text));
1✔
288
                                }
289

290
                                if ($elCell->getText() === '') {
1✔
291
                                        $elCell->setText("\u{A0}"); // &nbsp;
1✔
292
                                }
293
                        }
294
                }
295
        }
1✔
296
}
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