• 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

92.13
/src/Texy/Modules/TableModule.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\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
        private ?bool $disableTables = null;
26

27

28
        public function __construct(Texy\Texy $texy)
1✔
29
        {
30
                $this->texy = $texy;
1✔
31

32
                $texy->registerBlockPattern(
1✔
33
                        $this->parseTable(...),
1✔
34
                        '~^
35
                                (?:' . Patterns::MODIFIER_HV . '\n)? # modifier (1)
1✔
36
                                \|                                   # table start
37
                                .*                                   # content
38
                        $~mU',
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
         */
53
        public function parseTable(Texy\BlockParser $parser, array $matches): HtmlElement|string|null
1✔
54
        {
55
                if ($this->disableTables) {
1✔
56
                        return null;
1✔
57
                }
58

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

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

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

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

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

86
                        $caption = $el->create('caption');
×
87
                        $mod = new Modifier($mMod);
×
88
                        $mod->decorate($texy, $caption);
×
UNCOV
89
                        $caption->inject($texy->parseLine($mContent));
×
90
                }
91

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

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

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

113
                                if ($elPart === null) {
1✔
114
                                        $elPart = $el->create($isHead ? 'thead' : 'tbody');
1✔
115

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

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

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

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

136
                                continue;
1✔
137
                        }
138

139
                        break;
1✔
140
                }
141

142
                if ($elPart === null) { // invalid table
1✔
UNCOV
143
                        return null;
×
144
                }
145

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

151
                $this->finishPart($elPart);
1✔
152

153
                // event listener
154
                $texy->invokeHandlers('afterTable', [$parser, $el, $mod]);
1✔
155

156
                return $el;
1✔
157
        }
158

159

160
        private function processRow(
1✔
161
                string $content,
162
                ?string $mMod,
163
                bool $isHead,
164
                Texy\Texy $texy,
165
                array &$prevRow,
166
                array &$colModifier,
167
                int &$colCounter,
168
                int $rowCounter,
169
        ): HtmlElement
170
        {
171
                $elRow = new HtmlElement('tr');
1✔
172
                $mod = new Modifier($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)
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 = new Modifier($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
                        foreach ($elRow->getChildren() as $elCell) {
1✔
275
                                if ($elCell->colSpan > 1) {
1✔
276
                                        $elCell->attrs['colspan'] = $elCell->colSpan;
1✔
277
                                }
278

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

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

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