• 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

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

19

20
/**
21
 * Table module.
22
 */
23
final class TableModule extends Texy\Module
24
{
25
        /** @deprecated */
26
        public ?string $oddClass = null;
27

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

32

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

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

45

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

128
                                continue;
1✔
129
                        }
130

131
                        break;
1✔
132
                }
133

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

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

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

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

148
                return $el;
1✔
149
        }
150

151

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

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

172
                $col = 0;
1✔
173
                $elCell = null;
1✔
174

175
                // special escape sequence \|
176
                $content = str_replace('\|', "\x13", $content);
1✔
177
                $content = Regexp::replace($content, '#(\[[^\]]*)\|#', "$1\x13"); // HACK: support for [..|..]
1✔
178

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

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

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

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

215
                        $elRow->add($elCell);
1✔
216
                        $prevRow[$col] = $elCell;
1✔
217
                        $col++;
1✔
218
                }
219

220
                $colCounter = $col;
1✔
221
                return $elRow;
1✔
222
        }
223

224

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

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

243
                if ($mModCol) {
1✔
UNCOV
244
                        $cellModifier = new Modifier($mModCol);
×
245
                }
246

247
                $mod = $cellModifier ? clone $cellModifier : new Modifier;
1✔
248
                $mod->setProperties($mMod);
1✔
249

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

257

258
        /**
259
         * Parse text in all cells.
260
         */
261
        private function finishPart(HtmlElement $elPart): void
1✔
262
        {
263
                foreach ($elPart->getChildren() as $elRow) {
1✔
264
                        foreach ($elRow->getChildren() as $elCell) {
1✔
265
                                if ($elCell->colSpan > 1) {
1✔
266
                                        $elCell->attrs['colspan'] = $elCell->colSpan;
1✔
267
                                }
268

269
                                if ($elCell->rowSpan > 1) {
1✔
270
                                        $elCell->attrs['rowspan'] = $elCell->rowSpan;
1✔
271
                                }
272

273
                                $text = rtrim((string) $elCell->text);
1✔
274
                                if (str_contains($text, "\n")) {
1✔
275
                                        // multiline parse as block
276
                                        // HACK: disable tables
277
                                        $this->disableTables = true;
1✔
278
                                        $elCell->parseBlock($this->texy, Texy\Helpers::outdent($text));
1✔
279
                                        $this->disableTables = false;
1✔
280
                                } else {
281
                                        $elCell->parseLine($this->texy, ltrim($text));
1✔
282
                                }
283

284
                                if ($elCell->getText() === '') {
1✔
285
                                        $elCell->setText("\u{A0}"); // &nbsp;
1✔
286
                                }
287
                        }
288
                }
289
        }
1✔
290
}
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