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

dg / texy / 42887648368

10 Nov 2026 01:51PM UTC coverage: 96.622% (-0.1%) from 96.735%
42887648368

push

github

dg
Config::$locales: each locale is a list of four quotes (BC break)

Opening and closing double quotes, then the single ones, instead of the pairs
singleQuotes and doubleQuotes. The compatibility layer converts the old table.

cabae8f0-9dec-45ab-a919-45e6dadc3ad6

8 of 8 new or added lines in 3 files covered. (100.0%)

73 existing lines in 18 files now uncovered.

3719 of 3849 relevant lines covered (96.62%)

0.97 hits per line

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

93.62
/src/Modules/HtmlModule.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Texy! (https://texy.deegee.dev)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Texy\Modules;
9

10
use Texy;
11
use Texy\Nodes\{HtmlCommentNode, HtmlTagNode, TextNode};
12
use Texy\Parsing\ParseContext;
13
use Texy\{Range, Regexp, Syntax};
14
use function strlen;
15

16

17
/**
18
 * Processes HTML tags, comments and entity references in input text.
19
 */
20
final class HtmlModule extends Texy\Module
21
{
22
        public function register(Texy\Parsing\Registry $registry): void
23
        {
24
                $registry->addInline(
1 ✔
25
                        Syntax::HtmlTag,
1 ✔
26
                        '~
1 ✔
27
                                < (/?)                          # tag begin
28
                                ([a-z][a-z0-9_:-]{0,50})        # tag name
29
                                (?= [\s/>] )
30
                                (
31
                                        (?:
32
                                                \s++ [a-z0-9_:-]++ |          # attribute name
33
                                                = \s*+ " [^"]*+ " |           # attribute value in double quotes
34
                                                = \s*+ \' [^\']*+ \' |        # attribute value in single quotes
35
                                                = [^\s>]++                    # attribute value without quotes
36
                                        )*
37
                                )
38
                                \s*+
39
                                (/?)                             # self-closing slash
40
                                >
41
                        ~isx',
42
                        $this->parseTag(...),
1 ✔
43
                );
44

45
                $registry->addInline(
1 ✔
46
                        Syntax::HtmlComment,
1 ✔
47
                        '~
1 ✔
48
                                <!--
49
                                ( .*? )
50
                                -->
51
                        ~isx',
52
                        $this->parseComment(...),
1 ✔
53
                );
54

55
                $registry->addInline(
1 ✔
56
                        Syntax::HtmlEntity,
1 ✔
57
                        '~' . Texy\Helpers::EntityPattern . '~',
1 ✔
58
                        $this->parseEntity(...),
1 ✔
59
                );
60
        }
1 ✔
61

62

63
        /**
64
         * Parses &amp;, &#160;, &#xA0;
65
         * @param  array{string}  $matches
66
         * @param  array{int}  $offsets
67
         */
68
        public function parseEntity(ParseContext $context, array $matches, array $offsets): ?TextNode
69
        {
70
                $decoded = Texy\Helpers::decodeEntity($matches[0]);
1 ✔
71
                return $decoded === null
1 ✔
72
                        ? null
1 ✔
73
                        : new TextNode($decoded, new Range($offsets[0], strlen($matches[0])));
1 ✔
74
        }
75

76

77
        /**
78
         * Parses <!-- comment -->
79
         * @param  array{string, string}  $matches
80
         * @param  array{int, int}  $offsets
81
         */
82
        public function parseComment(ParseContext $context, array $matches, array $offsets): HtmlCommentNode
83
        {
84
                return new HtmlCommentNode($matches[1], new Range($offsets[0], strlen($matches[0])));
1 ✔
85
        }
86

87

88
        /**
89
         * Parses <tag attr="...">
90
         * @param  array{string, string, string, string, string}  $matches
91
         * @param  array{int, int, int, int, int}  $offsets
92
         */
93
        public function parseTag(ParseContext $context, array $matches, array $offsets): ?HtmlTagNode
94
        {
95
                [, $mEnd, $mTag, $mAttr, $mEmpty] = $matches;
1 ✔
96

97
                $isStart = $mEnd !== '/';
1 ✔
98
                $isEmpty = $mEmpty === '/';
1 ✔
99
                if (!$isEmpty && str_ends_with($mAttr, '/')) {
1 ✔
UNCOV
100
                        $mAttr = substr($mAttr, 0, -1);
×
UNCOV
101
                        $isEmpty = true;
×
102
                }
103

104
                // error - can't close empty element
105
                if ($isEmpty && !$isStart) {
1 ✔
UNCOV
106
                        return null;
×
107
                }
108

109
                // error - end element with attrs
110
                $mAttr = trim(strtr($mAttr, "\r\n", '  ')); // \r is the hard break mark of a paragraph
1 ✔
111
                if ($mAttr && !$isStart) {
1 ✔
112
                        return null;
1 ✔
113
                }
114

115
                return new HtmlTagNode(
1 ✔
116
                        $mTag,
117
                        $isStart ? $this->parseAttributes($mAttr) : [],
1 ✔
118
                        closing: !$isStart,
1 ✔
119
                        selfClosing: $isEmpty,
120
                        range: new Range($offsets[0], strlen($matches[0])),
1 ✔
121
                        source: $matches[0],
1 ✔
122
                );
123
        }
124

125

126
        /** @return array<string, string|bool> */
127
        private function parseAttributes(string $attrs): array
128
        {
129
                $res = [];
1 ✔
130
                $matches = Regexp::matchAll(
1 ✔
131
                        $attrs,
132
                        <<<'X'
133
                                ~
1 ✔
134
                                ([a-z0-9_:-]+)                 # attribute name
135
                                \s*
136
                                (?:
137
                                        = \s*                      # equals sign
138
                                        (
139
                                                ' [^']* ' |            # single quoted value
140
                                                " [^"]* " |            # double quoted value
141
                                                [^'"\s]+               # unquoted value
142
                                        )
143
                                )?
144
                                ~isx
145
                                X,
146
                );
147

148
                /** @var array{string, string, ?string} $m */
149
                foreach ($matches as $m) {
1 ✔
150
                        $key = strtolower($m[1]);
1 ✔
151
                        $value = $m[2];
1 ✔
152
                        if ($value === null) {
1 ✔
153
                                $res[$key] = true;
1 ✔
154
                        } elseif ($value[0] === '\'' || $value[0] === '"') {
1 ✔
155
                                $res[$key] = trim(Texy\Helpers::unescapeHtml(substr($value, 1, -1)));
1 ✔
156
                        } else {
157
                                $res[$key] = trim(Texy\Helpers::unescapeHtml($value));
1 ✔
158
                        }
159
                }
160

161
                return $res;
1 ✔
162
        }
163
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc