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

dg / texy / 22262381750

25 Jan 2026 11:44PM UTC coverage: 92.367% (-0.7%) from 93.057%
22262381750

push

github

dg
cs

9 of 11 new or added lines in 8 files covered. (81.82%)

161 existing lines in 23 files now uncovered.

2384 of 2581 relevant lines covered (92.37%)

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.nette.org)
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
use function array_intersect, array_keys, array_unshift, max, reset, rtrim, str_repeat, str_replace, strtr, wordwrap;
16

17

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

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

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

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

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

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

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

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

47

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

54

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

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

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

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

81
                // right trim
82
                $s = Regexp::replace($s, "#[\t ]+(\n|\r|$)#", '$1'); // right trim
1✔
83

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

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

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

103

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

118
                $s = '';
1✔
119

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

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

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

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

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

149

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

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

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

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

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

185
                        $indent = $this->indent && !array_intersect(array_keys($this->tagUsed, filter_value: true, strict: false), $this->preserveSpaces);
1✔
186

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

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

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

199
                $open = null;
1✔
200
                $close = null;
1✔
201
                $indent = 0;
1✔
202

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

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

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

226
                        // TODO: problematic formatting of select / options, object / params
227
                }
228

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

241
                return $s;
1✔
242
        }
243

244

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

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

267
                        array_unshift($tmp, $item);
1✔
268
                }
269

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

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

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

289
                return $s;
1✔
290
        }
291

292

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

303
                        $itemTag = $item['tag'];
1✔
304

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

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

324
                return $s;
1✔
325
        }
326

327

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