• 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

90.0
/src/Texy/Modules/BlockModule.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\Helpers;
12
use Texy\HtmlElement;
13
use function assert, htmlspecialchars, str_replace, trim;
14
use const ENT_NOQUOTES;
15

16

17
/**
18
 * Processes special blocks (/-- code, html, text, div, etc.).
19
 */
20
final class BlockModule extends Texy\Module
21
{
22
        public function __construct(
1✔
23
                private Texy\Texy $texy,
24
        ) {
25
                //$texy->allowed['blocks'] = true;
26
                $texy->allowed['block/default'] = true;
1✔
27
                $texy->allowed['block/pre'] = true;
1✔
28
                $texy->allowed['block/code'] = true;
1✔
29
                $texy->allowed['block/html'] = true;
1✔
30
                $texy->allowed['block/text'] = true;
1✔
31
                $texy->allowed['block/texysource'] = true;
1✔
32
                $texy->allowed['block/comment'] = true;
1✔
33
                $texy->allowed['block/div'] = true;
1✔
34

35
                $texy->addHandler('block', $this->solve(...));
1✔
36
                $texy->addHandler('beforeBlockParse', $this->beforeBlockParse(...));
1✔
37
        }
1✔
38

39

40
        public function beforeParse(string &$text): void
1✔
41
        {
42
                $this->texy->registerBlockPattern(
1✔
43
                        $this->parse(...),
1✔
44
                        '~^
45
                                /--++ \ *+                    # opening tag /--
46
                                (.*)                          # content type (1)
47
                                ' . Texy\Patterns::MODIFIER_H . '? # modifier (2)
1✔
48
                                $
49
                                ((?:                         # content (3)
50
                                        \n (?0) |                # recursive nested blocks
51
                                        \n.*+                    # or any content
52
                                )*)
53
                                (?:
54
                                        \n \\\--.* $ |           # closing tag
55
                                        \z                       # or end of input
56
                                )
57
                        ~mUi',
58
                        'blocks',
1✔
59
                );
60
        }
1✔
61

62

63
        /**
64
         * Single block pre-processing.
65
         */
66
        private function beforeBlockParse(Texy\BlockParser $parser, string &$text): void
1✔
67
        {
68
                // autoclose exclusive blocks
69
                $text = Texy\Regexp::replace(
1✔
70
                        $text,
1✔
71
                        '~^
1✔
72
                                ( /--++ \ *+ (?! div|texysource ) .* )  # opening tag except div/texysource (1)
73
                                $
74
                                ((?: \n.*+ )*?)                 # content (2)
75
                                (?:
76
                                        \n \\\--.* $ |              # closing tag
77
                                        (?= (\n /--.* $))           # or next block starts (3)
78
                                )
79
                        ~mi',
80
                        "\$1\$2\n\\--",                 // add closing tag
1✔
81
                );
82
        }
1✔
83

84

85
        /**
86
         * Parses blocks /--foo
87
         * @param  array<?string>  $matches
88
         */
89
        public function parse(Texy\BlockParser $parser, array $matches): HtmlElement|string|null
1✔
90
        {
91
                [, $mParam, $mMod, $mContent] = $matches;
1✔
92
                // [1] => code | text | ...
93
                // [2] => ... additional parameters
94
                // [3] => .(title)[class]{style}<>
95
                // [4] => ... content
96

97
                $mod = Texy\Modifier::parse($mMod);
1✔
98
                $parts = Texy\Regexp::split($mParam, '~\s+~', limit: 2);
1✔
99
                $blocktype = empty($parts[0]) ? 'block/default' : 'block/' . $parts[0];
1✔
100
                $param = empty($parts[1]) ? null : $parts[1];
1✔
101

102
                return $this->texy->invokeAroundHandlers('block', $parser, [$blocktype, $mContent, $param, $mod]);
1✔
103
        }
104

105

106
        /**
107
         * Finish invocation.
108
         */
109
        private function solve(
1✔
110
                Texy\HandlerInvocation $invocation,
111
                string $blocktype,
112
                string $s,
113
                ?string $param,
114
                Texy\Modifier $mod,
115
        ): HtmlElement|string|null
116
        {
117
                $texy = $this->texy;
1✔
118
                $parser = $invocation->getParser();
1✔
119
                assert($parser instanceof Texy\BlockParser);
120

121
                if ($blocktype === 'block/texy') {
1✔
UNCOV
122
                        return $this->blockTexy($s, $texy, $parser);
×
123
                } elseif (empty($texy->allowed[$blocktype])) {
1✔
124
                        return null;
1✔
125
                } elseif ($blocktype === 'block/texysource') {
1✔
126
                        return $this->blockTexySource($s, $texy, $mod, $param);
1✔
127
                } elseif ($blocktype === 'block/code') {
1✔
128
                        return $this->blockCode($s, $texy, $mod, $param);
1✔
129
                } elseif ($blocktype === 'block/default') {
1✔
130
                        return $this->blockDefault($s, $texy, $mod, $param);
1✔
131
                } elseif ($blocktype === 'block/pre') {
1✔
132
                        return $this->blockPre($s, $texy, $mod);
1✔
133
                } elseif ($blocktype === 'block/html') {
1✔
134
                        return $this->blockHtml($s, $texy);
1✔
135
                } elseif ($blocktype === 'block/text') {
1✔
136
                        return $this->blockText($s, $texy);
1✔
137
                } elseif ($blocktype === 'block/comment') {
1✔
138
                        return $this->blockComment();
1✔
139
                } elseif ($blocktype === 'block/div') {
1✔
140
                        return $this->blockDiv($s, $texy, $mod, $parser);
1✔
141
                }
142

UNCOV
143
                return null;
×
144
        }
145

146

147
        private function blockTexy(string $s, Texy\Texy $texy, Texy\BlockParser $parser): HtmlElement
148
        {
UNCOV
149
                $el = new HtmlElement;
×
UNCOV
150
                $el->parseBlock($texy, $s, $parser->isIndented());
×
UNCOV
151
                return $el;
×
152
        }
153

154

155
        private function blockTexySource(string $s, Texy\Texy $texy, Texy\Modifier $mod, ?string $param): string|HtmlElement
1✔
156
        {
157
                $s = Helpers::outdent($s);
1✔
158
                if ($s === '') {
1✔
UNCOV
159
                        return "\n";
×
160
                }
161

162
                $el = new HtmlElement;
1✔
163
                if ($param === 'line') {
1✔
UNCOV
164
                        $el->parseLine($texy, $s);
×
165
                } else {
166
                        $el->parseBlock($texy, $s);
1✔
167
                }
168

169
                $s = $el->toHtml($texy);
1✔
170
                return $this->blockCode($s, $texy, $mod, 'html');
1✔
171
        }
172

173

174
        private function blockCode(string $s, Texy\Texy $texy, Texy\Modifier $mod, ?string $param): string|HtmlElement
1✔
175
        {
176
                $s = Helpers::outdent($s);
1✔
177
                if ($s === '') {
1✔
UNCOV
178
                        return "\n";
×
179
                }
180

181
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
182
                $s = $texy->protect($s, $texy::CONTENT_BLOCK);
1✔
183
                $el = new HtmlElement('pre');
1✔
184
                $mod->decorate($texy, $el);
1✔
185
                if ($param !== null) {
1✔
186
                        $el->attrs['class'] = (array) ($el->attrs['class'] ?? []);
1✔
187
                        $el->attrs['class'][] = $param;
1✔
188
                }
189
                $el->create('code', $s);
1✔
190
                return $el;
1✔
191
        }
192

193

194
        private function blockDefault(string $s, Texy\Texy $texy, Texy\Modifier $mod, ?string $param): string|HtmlElement
1✔
195
        {
196
                $s = Helpers::outdent($s);
1✔
197
                if ($s === '') {
1✔
UNCOV
198
                        return "\n";
×
199
                }
200

201
                $el = new HtmlElement('pre');
1✔
202
                $mod->decorate($texy, $el);
1✔
203
                if ($param !== null) {
1✔
UNCOV
204
                        $el->attrs['class'] = (array) ($el->attrs['class'] ?? []);
×
UNCOV
205
                        $el->attrs['class'][] = $param;
×
206
                }
207
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
208
                $s = $texy->protect($s, $texy::CONTENT_BLOCK);
1✔
209
                $el->setText($s);
1✔
210
                return $el;
1✔
211
        }
212

213

214
        private function blockPre(string $s, Texy\Texy $texy, Texy\Modifier $mod): string|HtmlElement
1✔
215
        {
216
                $s = Helpers::outdent($s);
1✔
217
                if ($s === '') {
1✔
UNCOV
218
                        return "\n";
×
219
                }
220

221
                $el = new HtmlElement('pre');
1✔
222
                $mod->decorate($texy, $el);
1✔
223
                $lineParser = $texy->createInlineParser();
1✔
224
                // special mode - parse only html tags
225
                $tmp = $lineParser->patterns;
1✔
226
                $lineParser->patterns = [];
1✔
227
                if (isset($tmp['html/tag'])) {
1✔
228
                        $lineParser->patterns['html/tag'] = $tmp['html/tag'];
1✔
229
                }
230

231
                if (isset($tmp['html/comment'])) {
1✔
232
                        $lineParser->patterns['html/comment'] = $tmp['html/comment'];
1✔
233
                }
234

235
                unset($tmp);
1✔
236

237
                $el->setText($lineParser->parse($s));
1✔
238
                $s = $el->getText();
1✔
239
                assert($s !== null);
240
                $s = Helpers::unescapeHtml($s);
1✔
241
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
242
                $s = $texy->unprotect($s);
1✔
243
                $s = $texy->protect($s, $texy::CONTENT_BLOCK);
1✔
244
                $el->setText($s);
1✔
245
                return $el;
1✔
246
        }
247

248

249
        private function blockHtml(string $s, Texy\Texy $texy): string
1✔
250
        {
251
                $s = trim($s, "\n");
1✔
252
                if ($s === '') {
1✔
UNCOV
253
                        return "\n";
×
254
                }
255

256
                $lineParser = $texy->createInlineParser();
1✔
257
                // special mode - parse only html tags
258
                $tmp = $lineParser->patterns;
1✔
259
                $lineParser->patterns = [];
1✔
260
                if (isset($tmp['html/tag'])) {
1✔
261
                        $lineParser->patterns['html/tag'] = $tmp['html/tag'];
1✔
262
                }
263

264
                if (isset($tmp['html/comment'])) {
1✔
265
                        $lineParser->patterns['html/comment'] = $tmp['html/comment'];
1✔
266
                }
267

268
                unset($tmp);
1✔
269

270
                $el = new HtmlElement(null, $lineParser->parse($s));
1✔
271
                $s = $el->getText();
1✔
272
                assert($s !== null);
273
                $s = Helpers::unescapeHtml($s);
1✔
274
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
275
                $s = $texy->unprotect($s);
1✔
276
                return $texy->protect($s, $texy::CONTENT_BLOCK) . "\n";
1✔
277
        }
278

279

280
        private function blockText(string $s, Texy\Texy $texy): string
1✔
281
        {
282
                $s = trim($s, "\n");
1✔
283
                if ($s === '') {
1✔
UNCOV
284
                        return "\n";
×
285
                }
286

287
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
288
                $s = str_replace("\n", (new HtmlElement('br'))->startTag(), $s); // nl2br
1✔
289
                return $texy->protect($s, $texy::CONTENT_BLOCK) . "\n";
1✔
290
        }
291

292

293
        private function blockComment(): string
294
        {
295
                return "\n";
1✔
296
        }
297

298

299
        private function blockDiv(
1✔
300
                string $s,
301
                Texy\Texy $texy,
302
                Texy\Modifier $mod,
303
                Texy\BlockParser $parser,
304
        ): string|HtmlElement
305
        {
306
                $s = Helpers::outdent($s, firstLine: true);
1✔
307
                if ($s === '') {
1✔
UNCOV
308
                        return "\n";
×
309
                }
310

311
                $el = new HtmlElement('div');
1✔
312
                $mod->decorate($texy, $el);
1✔
313
                $el->parseBlock($texy, $s, $parser->isIndented()); // TODO: INDENT or NORMAL ?
1✔
314
                return $el;
1✔
315
        }
316
}
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