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

dg / texy / 15479423458

05 Jun 2025 11:37PM UTC coverage: 92.741% (+0.5%) from 92.224%
15479423458

push

github

dg
HtmlElement: removed toHtml() & toText()

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

78 existing lines in 11 files now uncovered.

2389 of 2576 relevant lines covered (92.74%)

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

18

19
/**
20
 * Table module.
21
 */
22
final class TableModule extends Texy\Module
23
{
24
        private ?bool $disableTables = null;
25

26

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

31
                $texy->registerBlockPattern(
1✔
32
                        $this->parseTable(...),
1✔
33
                        '~^
34
                                (?:' . Patterns::MODIFIER_HV . '\n)? # modifier (1)
1✔
35
                                \|                                   # table start
36
                                .*                                   # content
37
                        $~mU',
38
                        'table',
1✔
39
                );
40
        }
1✔
41

42

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

135
                                continue;
1✔
136
                        }
137

138
                        break;
1✔
139
                }
140

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

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

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

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

155
                return $el;
1✔
156
        }
157

158

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

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

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

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

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

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

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

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

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

226

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

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

252
                if ($mModCol) {
1✔
UNCOV
253
                        $cellModifier = new Modifier($mModCol);
×
254
                }
255

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

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

266

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

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

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

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