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

dg / texy / 16790351274

06 Aug 2025 10:42PM UTC coverage: 92.746% (+0.005%) from 92.741%
16790351274

push

github

dg
HtmlElement: removed toHtml() & toText()

18 of 19 new or added lines in 5 files covered. (94.74%)

136 existing lines in 23 files now uncovered.

2391 of 2578 relevant lines covered (92.75%)

0.93 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
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
                        '~
1✔
73
                                ( [^<]*+ )
74
                                < (?: (!--.*--) | (/?) ([a-z][a-z0-9._:-]*) (|[ \n].*) \s* (/?) ) >
75
                        ~Uis',
76
                        $this->cb(...),
1✔
77
                );
78

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

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

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

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

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

106

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

121
                $s = '';
1✔
122

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

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

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

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

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

152

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

244
                return $s;
1✔
245
        }
246

247

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

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

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

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

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

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

292
                return $s;
1✔
293
        }
294

295

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

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

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

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

327
                return $s;
1✔
328
        }
329

330

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