• 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

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->toElement(...));
1✔
37
                $texy->addHandler('beforeBlockParse', $this->beforeBlockParse(...));
1✔
38

39
                $texy->registerBlockPattern(
1✔
40
                        $this->parse(...),
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 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 = 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
        public function toElement(
1✔
107
                Texy\HandlerInvocation $invocation,
108
                string $blocktype,
109
                string $s,
110
                $param,
111
                Texy\Modifier $mod,
112
        ): HtmlElement|string|null
113
        {
114
                $texy = $this->texy;
1✔
115
                $parser = $invocation->getParser();
1✔
116
                \assert($parser instanceof Texy\BlockParser);
117

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

UNCOV
140
                return null;
×
141
        }
142

143

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

151

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

159
                $el = new HtmlElement;
1✔
160
                if ($param === 'line') {
1✔
UNCOV
161
                        $el->inject($texy->parseLine($s));
×
162
                } else {
163
                        $el->inject($texy->parseBlock($s));
1✔
164
                }
165

166
                $s = $texy->maskedStringToHtml($texy->elemToMaskedString($el));
1✔
167
                return $this->blockCode($s, $texy, $mod, 'html');
1✔
168
        }
169

170

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

178
                $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
1✔
179
                $s = $texy->protect($s, $texy::CONTENT_BLOCK);
1✔
180
                $el = new HtmlElement('pre');
1✔
181
                $mod->decorate($texy, $el);
1✔
182
                $el->attrs['class'][] = $param; // lang
1✔
183
                $el->create('code', $s);
1✔
184
                return $el;
1✔
185
        }
186

187

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

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

204

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

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

222
                if (isset($tmp['html/comment'])) {
1✔
223
                        $lineParser->patterns['html/comment'] = $tmp['html/comment'];
1✔
224
                }
225

226
                unset($tmp);
1✔
227

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

238

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

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

255
                if (isset($tmp['html/comment'])) {
1✔
256
                        $lineParser->patterns['html/comment'] = $tmp['html/comment'];
1✔
257
                }
258

259
                unset($tmp);
1✔
260

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

269

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

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

282

283
        private function blockComment(): string
284
        {
285
                return "\n";
1✔
286
        }
287

288

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

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