• 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

95.8
/src/Texy/Modules/HtmlOutputModule.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\Regexp;
15

16

17
/**
18
 * HTML output
19
 */
20
final class HtmlOutputModule extends Texy\Module
21
{
22
        /** indent HTML code? */
23
        public bool $indent = true;
24

25
        /** @var string[] */
26
        public array $preserveSpaces = ['textarea', 'pre', 'script', 'code', 'samp', 'kbd'];
27

28
        /** base indent level */
29
        public int $baseIndent = 0;
30

31
        /** wrap width, doesn't include indent space */
32
        public int $lineWrap = 80;
33

34
        /** indent space counter */
35
        private int $space = 0;
36

37
        /** @var array<string, int> */
38
        private array $tagUsed = [];
39

40
        /** @var array<int, array{tag: string, open: string, close: string, dtdContent: array<string, int>, indent: int}> */
41
        private array $tagStack = [];
42

43
        /** @var array<string, int>  content DTD used, when context is not defined */
44
        private array $baseDTD = [];
45

46

47
        public function __construct(Texy\Texy $texy)
1✔
48
        {
49
                $this->texy = $texy;
1✔
50
                $texy->addHandler('postProcess', $this->postProcess(...));
1✔
51
        }
1✔
52

53

54
        /**
55
         * Converts <strong><em> ... </strong> ... </em>.
56
         * into <strong><em> ... </em></strong><em> ... </em>
57
         */
58
        private function postProcess(Texy\Texy $texy, string &$s): void
1✔
59
        {
60
                $this->space = $this->baseIndent;
1✔
61
                $this->tagStack = [];
1✔
62
                $this->tagUsed = [];
1✔
63

64
                // special "base content"
65
                $dtd = $texy->getDTD();
1✔
66
                $this->baseDTD = $dtd['div'][1] + $dtd['html'][1] /*+ $dtd['head'][1]*/ + $dtd['body'][1] + ['html' => 1];
1✔
67

68
                // wellform and reformat
69
                $s = Regexp::replace(
1✔
70
                        $s . '</end/>',
1✔
71
                        '~
1✔
72
                                ( [^<]*+ )
73
                                < (?: (!--.*--) | (/?) ([a-z][a-z0-9._:-]*) (|[ \n].*) \s* (/?) ) >
74
                        ~Uis',
75
                        $this->cb(...),
1✔
76
                );
77

78
                // empty out stack
79
                foreach ($this->tagStack as $item) {
1✔
80
                        $s .= $item['close'];
1✔
81
                }
82

83
                // right trim
84
                $s = Regexp::replace($s, '~[\t ]+(\n|\r|$)~', '$1'); // right trim
1✔
85

86
                // join double \r to single \n
87
                $s = str_replace("\r\r", "\n", $s);
1✔
88
                $s = strtr($s, "\r", "\n");
1✔
89

90
                // greedy chars
91
                $s = Regexp::replace($s, '~\x07\ *~', '');
1✔
92
                // back-tabs
93
                $s = Regexp::replace($s, '~\t?\ *\x08~', '');
1✔
94

95
                // line wrap
96
                if ($this->lineWrap > 0) {
1✔
97
                        $s = Regexp::replace(
1✔
98
                                $s,
1✔
99
                                '~^(\t*)(.*)$~m',
1✔
100
                                $this->wrap(...),
1✔
101
                        );
102
                }
103
        }
1✔
104

105

106
        /**
107
         * Callback function: <tag> | </tag> | ....
108
         */
109
        private function cb(array $matches): string
1✔
110
        {
111
                // html tag
112
                [, $mText, $mComment, $mEnd, $mTag, $mAttr, $mEmpty] = $matches;
1✔
113
                // [1] => text
114
                // [1] => !-- comment --
115
                // [2] => /
116
                // [3] => TAG
117
                // [4] => ... (attributes)
118
                // [5] => / (empty)
119

120
                $s = '';
1✔
121

122
                // phase #1 - stuff between tags
123
                if ($mText !== '') {
1✔
124
                        $item = reset($this->tagStack);
1✔
125
                        if ($item && !isset($item['dtdContent'][HtmlElement::InnerText])) {  // text not allowed?
1✔
126

127
                        } elseif (array_intersect(array_keys($this->tagUsed, true, false), $this->preserveSpaces)) { // inside pre & textarea preserve spaces
1✔
128
                                $s = Texy\Helpers::freezeSpaces($mText);
1✔
129

130
                        } else {
131
                                $s = Regexp::replace($mText, '~[ \n]+~', ' '); // otherwise shrink multiple spaces
1✔
132
                        }
133
                }
134

135
                // phase #2 - HTML comment
136
                if ($mComment) {
1✔
137
                        return $s . '<' . Texy\Helpers::freezeSpaces($mComment) . '>';
1✔
138
                }
139

140
                // phase #3 - HTML tag
141
                $mEmpty = $mEmpty || isset(HtmlElement::$emptyElements[$mTag]);
1✔
142
                if ($mEmpty && $mEnd) { // bad tag; /end/
1✔
143
                        return $s;
1✔
144
                } elseif ($mEnd) {
1✔
145
                        return $s . $this->processEndTag($mTag);
1✔
146
                } else {
147
                        return $this->processStartTag($mTag, $mEmpty, $mAttr, $s);
1✔
148
                }
149
        }
150

151

152
        private function processStartTag(string $tag, bool $empty, string $attr, string $s): string
1✔
153
        {
154
                $dtdContent = $this->baseDTD;
1✔
155
                $dtd = $this->texy->getDTD();
1✔
156

157
                if (!isset($dtd[$tag])) {
1✔
158
                        // unknown (non-html) tag
159
                        $allowed = true;
×
UNCOV
160
                        $item = reset($this->tagStack);
×
UNCOV
161
                        if ($item) {
×
UNCOV
162
                                $dtdContent = $item['dtdContent'];
×
163
                        }
164
                } else {
165
                        $s .= $this->closeOptionalTags($tag, $dtdContent);
1✔
166

167
                        // is tag allowed in this content?
168
                        $allowed = isset($dtdContent[$tag]);
1✔
169

170
                        // check deep element prohibitions
171
                        if ($allowed && isset(HtmlElement::$prohibits[$tag])) {
1✔
172
                                foreach (HtmlElement::$prohibits[$tag] as $pTag) {
1✔
173
                                        if (!empty($this->tagUsed[$pTag])) {
1✔
174
                                                $allowed = false;
1✔
175
                                                break;
1✔
176
                                        }
177
                                }
178
                        }
179
                }
180

181
                // empty elements se neukladaji do zasobniku
182
                if ($empty) {
1✔
183
                        if (!$allowed) {
1✔
UNCOV
184
                                return $s;
×
185
                        }
186

187
                        $indent = $this->indent && !array_intersect(array_keys($this->tagUsed, true, false), $this->preserveSpaces);
1✔
188

189
                        if ($indent && $tag === 'br') { // formatting exception
1✔
190
                                return rtrim($s) . '<' . $tag . $attr . ">\n" . str_repeat("\t", max(0, $this->space - 1)) . "\x07";
1✔
191

192
                        } elseif ($indent && !isset(HtmlElement::$inlineElements[$tag])) {
1✔
193
                                $space = "\r" . str_repeat("\t", $this->space);
1✔
194
                                return $s . $space . '<' . $tag . $attr . '>' . $space;
1✔
195

196
                        } else {
197
                                return $s . '<' . $tag . $attr . '>';
1✔
198
                        }
199
                }
200

201
                $open = null;
1✔
202
                $close = null;
1✔
203
                $indent = 0;
1✔
204

205
                if ($allowed) {
1✔
206
                        $open = '<' . $tag . $attr . '>';
1✔
207

208
                        // receive new content
209
                        if ($tagDTD = $dtd[$tag] ?? null) {
1✔
210
                                if (isset($tagDTD[1][HtmlElement::InnerTransparent])) {
1✔
211
                                        $dtdContent += $tagDTD[1];
1✔
212
                                        unset($dtdContent[HtmlElement::InnerTransparent]);
1✔
213
                                } else {
214
                                        $dtdContent = $tagDTD[1];
1✔
215
                                }
216
                        }
217

218
                        // format output
219
                        if ($this->indent && !isset(HtmlElement::$inlineElements[$tag])) {
1✔
220
                                $close = "\x08" . '</' . $tag . '>' . "\n" . str_repeat("\t", $this->space);
1✔
221
                                $s .= "\n" . str_repeat("\t", $this->space++) . $open . "\x07";
1✔
222
                                $indent = 1;
1✔
223
                        } else {
224
                                $close = '</' . $tag . '>';
1✔
225
                                $s .= $open;
1✔
226
                        }
227

228
                        // TODO: problematic formatting of select / options, object / params
229
                }
230

231
                // open tag, put to stack, increase counter
232
                $item = [
1✔
233
                        'tag' => $tag,
1✔
234
                        'open' => $open,
1✔
235
                        'close' => $close,
1✔
236
                        'dtdContent' => $dtdContent,
1✔
237
                        'indent' => $indent,
1✔
238
                ];
239
                array_unshift($this->tagStack, $item);
1✔
240
                $tmp = &$this->tagUsed[$tag];
1✔
241
                $tmp++;
1✔
242

243
                return $s;
1✔
244
        }
245

246

247
        private function processEndTag(string $tag): string
1✔
248
        {
249
                // has start tag?
250
                if (empty($this->tagUsed[$tag])) {
1✔
251
                        return '';
1✔
252
                }
253

254
                // autoclose tags
255
                $tmp = [];
1✔
256
                $back = true;
1✔
257
                $s = '';
1✔
258
                foreach ($this->tagStack as $i => $item) {
1✔
259
                        $itemTag = $item['tag'];
1✔
260
                        $s .= $item['close'];
1✔
261
                        $this->space -= $item['indent'];
1✔
262
                        $this->tagUsed[$itemTag]--;
1✔
263
                        $back = $back && isset(HtmlElement::$inlineElements[$itemTag]);
1✔
264
                        unset($this->tagStack[$i]);
1✔
265
                        if ($itemTag === $tag) {
1✔
266
                                break;
1✔
267
                        }
268

269
                        array_unshift($tmp, $item);
1✔
270
                }
271

272
                if (!$back || !$tmp) {
1✔
273
                        return $s;
1✔
274
                }
275

276
                // allowed-check (nejspis neni ani potreba)
277
                $item = reset($this->tagStack);
1✔
278
                $dtdContent = $item ? $item['dtdContent'] : $this->baseDTD;
1✔
279
                if (!isset($dtdContent[$tmp[0]['tag']])) {
1✔
UNCOV
280
                        return $s;
×
281
                }
282

283
                // autoopen tags
284
                foreach ($tmp as $item) {
1✔
285
                        $s .= $item['open'];
1✔
286
                        $this->space += $item['indent'];
1✔
287
                        $this->tagUsed[$item['tag']]++;
1✔
288
                        array_unshift($this->tagStack, $item);
1✔
289
                }
290

291
                return $s;
1✔
292
        }
293

294

295
        private function closeOptionalTags(string $tag, array &$dtdContent): string
1✔
296
        {
297
                $s = '';
1✔
298
                foreach ($this->tagStack as $i => $item) {
1✔
299
                        // is tag allowed here?
300
                        $dtdContent = $item['dtdContent'];
1✔
301
                        if (isset($dtdContent[$tag])) {
1✔
302
                                break;
1✔
303
                        }
304

305
                        $itemTag = $item['tag'];
1✔
306

307
                        // auto-close hidden, optional and inline tags
308
                        if (
309
                                $item['close']
1✔
310
                                && (
311
                                        !isset(HtmlElement::$optionalEnds[$itemTag])
1✔
312
                                        && !isset(HtmlElement::$inlineElements[$itemTag])
1✔
313
                                )
314
                        ) {
315
                                break;
1✔
316
                        }
317

318
                        // close it
319
                        $s .= $item['close'];
1✔
320
                        $this->space -= $item['indent'];
1✔
321
                        $this->tagUsed[$itemTag]--;
1✔
322
                        unset($this->tagStack[$i]);
1✔
323
                        $dtdContent = $this->baseDTD;
1✔
324
                }
325

326
                return $s;
1✔
327
        }
328

329

330
        /**
331
         * Callback function: wrap lines.
332
         */
333
        private function wrap(array $m): string
1✔
334
        {
335
                [, $space, $s] = $m;
1✔
336
                return $space . wordwrap($s, $this->lineWrap, "\n" . $space);
1✔
337
        }
338
}
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