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

dg / texy / 12879605443

21 Jan 2025 03:31AM UTC coverage: 92.224% (+0.03%) from 92.197%
12879605443

push

github

dg
regexp: uses unmatched as null (BC break)

14 of 14 new or added lines in 6 files covered. (100.0%)

101 existing lines in 14 files now uncovered.

2372 of 2572 relevant lines covered (92.22%)

0.92 hits per line

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

91.1
/src/Texy/Modules/BlockModule.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\Helpers;
14
use Texy\HtmlElement;
15

16

17
/**
18
 * Special blocks module.
19
 */
20
final class BlockModule extends Texy\Module
21
{
22
        public function __construct(Texy\Texy $texy)
1✔
23
        {
24
                $this->texy = $texy;
1✔
25

26
                //$texy->allowed['blocks'] = true;
27
                $texy->allowed['block/default'] = true;
1✔
28
                $texy->allowed['block/pre'] = true;
1✔
29
                $texy->allowed['block/code'] = true;
1✔
30
                $texy->allowed['block/html'] = true;
1✔
31
                $texy->allowed['block/text'] = true;
1✔
32
                $texy->allowed['block/texysource'] = true;
1✔
33
                $texy->allowed['block/comment'] = true;
1✔
34
                $texy->allowed['block/div'] = true;
1✔
35

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

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

59

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

81

82
        /**
83
         * Callback for:.
84
         * /-----code html .(title)[class]{style}
85
         * ....
86
         * ....
87
         * \----
88
         */
89
        public function pattern(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 = new Texy\Modifier($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
                $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✔
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

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, $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, $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
                $el->attrs['class'][] = $param; // lang
1✔
186
                $el->create('code', $s);
1✔
187
                return $el;
1✔
188
        }
189

190

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

198
                $el = new HtmlElement('pre');
1✔
199
                $mod->decorate($texy, $el);
1✔
200
                $el->attrs['class'][] = $param; // lang
1✔
201
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
202
                $s = $texy->protect($s, $texy::CONTENT_BLOCK);
1✔
203
                $el->setText($s);
1✔
204
                return $el;
1✔
205
        }
206

207

208
        private function blockPre(string $s, Texy\Texy $texy, Texy\Modifier $mod): string|HtmlElement
1✔
209
        {
210
                $s = Helpers::outdent($s);
1✔
211
                if ($s === '') {
1✔
UNCOV
212
                        return "\n";
×
213
                }
214

215
                $el = new HtmlElement('pre');
1✔
216
                $mod->decorate($texy, $el);
1✔
217
                $lineParser = new Texy\LineParser($texy, $el);
1✔
218
                // special mode - parse only html tags
219
                $tmp = $lineParser->patterns;
1✔
220
                $lineParser->patterns = [];
1✔
221
                if (isset($tmp['html/tag'])) {
1✔
222
                        $lineParser->patterns['html/tag'] = $tmp['html/tag'];
1✔
223
                }
224

225
                if (isset($tmp['html/comment'])) {
1✔
226
                        $lineParser->patterns['html/comment'] = $tmp['html/comment'];
1✔
227
                }
228

229
                unset($tmp);
1✔
230

231
                $lineParser->parse($s);
1✔
232
                $s = $el->getText();
1✔
233
                $s = Helpers::unescapeHtml($s);
1✔
234
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
235
                $s = $texy->unprotect($s);
1✔
236
                $s = $texy->protect($s, $texy::CONTENT_BLOCK);
1✔
237
                $el->setText($s);
1✔
238
                return $el;
1✔
239
        }
240

241

242
        private function blockHtml(string $s, Texy\Texy $texy): string
1✔
243
        {
244
                $s = trim($s, "\n");
1✔
245
                if ($s === '') {
1✔
UNCOV
246
                        return "\n";
×
247
                }
248

249
                $el = new HtmlElement;
1✔
250
                $lineParser = new Texy\LineParser($texy, $el);
1✔
251
                // special mode - parse only html tags
252
                $tmp = $lineParser->patterns;
1✔
253
                $lineParser->patterns = [];
1✔
254
                if (isset($tmp['html/tag'])) {
1✔
255
                        $lineParser->patterns['html/tag'] = $tmp['html/tag'];
1✔
256
                }
257

258
                if (isset($tmp['html/comment'])) {
1✔
259
                        $lineParser->patterns['html/comment'] = $tmp['html/comment'];
1✔
260
                }
261

262
                unset($tmp);
1✔
263

264
                $lineParser->parse($s);
1✔
265
                $s = $el->getText();
1✔
266
                $s = Helpers::unescapeHtml($s);
1✔
267
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
268
                $s = $texy->unprotect($s);
1✔
269
                return $texy->protect($s, $texy::CONTENT_BLOCK) . "\n";
1✔
270
        }
271

272

273
        private function blockText(string $s, Texy\Texy $texy): string
1✔
274
        {
275
                $s = trim($s, "\n");
1✔
276
                if ($s === '') {
1✔
UNCOV
277
                        return "\n";
×
278
                }
279

280
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
281
                $s = str_replace("\n", (new HtmlElement('br'))->startTag(), $s); // nl2br
1✔
282
                return $texy->protect($s, $texy::CONTENT_BLOCK) . "\n";
1✔
283
        }
284

285

286
        private function blockComment(): string
287
        {
288
                return "\n";
1✔
289
        }
290

291

292
        private function blockDiv(string $s, Texy\Texy $texy, Texy\Modifier $mod, Texy\BlockParser $parser)
1✔
293
        {
294
                $s = Helpers::outdent($s, true);
1✔
295
                if ($s === '') {
1✔
UNCOV
296
                        return "\n";
×
297
                }
298

299
                $el = new HtmlElement('div');
1✔
300
                $mod->decorate($texy, $el);
1✔
301
                $el->parseBlock($texy, $s, $parser->isIndented()); // TODO: INDENT or NORMAL ?
1✔
302
                return $el;
1✔
303
        }
304
}
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