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

dg / texy / 43069517462

11 Nov 2026 05:15PM UTC coverage: 96.934% (+0.3%) from 96.641%
43069517462

push

github

dg
PhraseNode: $type is what the phrase means, $syntax how it is written

PhraseNode::$type is the enum PhraseType, PhraseNode::$syntax the name of the
syntax, as LinkNode has it; Syntax::PhraseTypes says what the phrase syntaxes
mean. Html\Config::$phraseTags is keyed by the values of PhraseType plus 'code',
so one tag serves every notation of a meaning and the PhraseVariants table is
gone. The compatibility layer converts the keys of Texy 3, the syntax names,
with a deprecation, when parsing and when creating the HTML renderer;
Compat\Texy3::moveSwitches() is moveSettings() for it. The Texy printer writes
a phrase in its own notation while that means its type, otherwise, as for one
from Markdown, in the default notation of the meaning.

3a3a923d-ad0b-441d-8e6c-67a0d34075bf

30 of 30 new or added lines in 5 files covered. (100.0%)

24 existing lines in 12 files now uncovered.

3826 of 3947 relevant lines covered (96.93%)

0.97 hits per line

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

96.55
/src/Modifier.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;
9

10
use function strlen;
11

12

13
/**
14
 * Modifier processor.
15
 *
16
 * Modifiers are texts like .(title)[class1 class2 #id]{color: red}>^
17
 *   .         starts with dot
18
 *   (...)     title or alt modifier
19
 *   [...]     classes or ID modifier
20
 *   {...}     inner style modifier
21
 *   < > <> =  horizontal align modifier
22
 *   ^ - _     vertical align modifier
23
 */
24
final class Modifier
25
{
26
        /**
27
         * Names in {...} treated as HTML attributes; anything else is a CSS style. Names that are
28
         * also CSS properties (width, height, color, border, translate...) are omitted on purpose,
29
         * so that the far more common style meaning wins.
30
         */
31
        public const ElAttrs = [
32
                'abbr' => 1, 'accesskey' => 1, 'alt' => 1, 'cite' => 1, 'colspan' => 1, 'contenteditable' => 1, 'crossorigin' => 1,
33
                'datetime' => 1, 'decoding' => 1, 'dir' => 1, 'download' => 1, 'draggable' => 1, 'for' => 1, 'headers' => 1,
34
                'hidden' => 1, 'href' => 1, 'hreflang' => 1, 'id' => 1, 'inert' => 1, 'inputmode' => 1, 'itemid' => 1, 'itemprop' => 1,
35
                'itemref' => 1, 'itemscope' => 1, 'itemtype' => 1, 'lang' => 1, 'loading' => 1, 'name' => 1, 'ping' => 1, 'popover' => 1,
36
                'referrerpolicy' => 1, 'rel' => 1, 'reversed' => 1, 'role' => 1, 'rowspan' => 1, 'scope' => 1, 'slot' => 1,
37
                'spellcheck' => 1, 'src' => 1, 'srcset' => 1, 'start' => 1, 'tabindex' => 1, 'target' => 1, 'title' => 1,
38
                'type' => 1, 'value' => 1,
39
        ];
40

41
        /** @var array<string, true> of classes (as keys) */
42
        public array $classes = [];
43

44

45
        /** @param  array<string, true>|list<string>  $classes  names as keys, or a list of names */
46
        public function __construct(
1 ✔
47
                public ?string $id = null,
48
                array $classes = [],
49
                /** @var array<string, string> of CSS styles */
50
                public array $styles = [],
51
                /** @var array<string, string|string[]> of HTML element attributes */
52
                public array $attrs = [],
53
                public ?string $hAlign = null,
54
                public ?string $vAlign = null,
55
                public ?string $title = null,
56
                public ?Range $range = null,
57
        ) {
58
                foreach ($classes as $key => $value) {
1 ✔
59
                        $this->classes[$value === true ? (string) $key : $value] = true;
1 ✔
60
                }
61
        }
1 ✔
62

63

64
        /**
65
         * Parses modifier string and returns new instance.
66
         */
67
        public static function parse(?string $s, ?int $offset = null): ?self
68
        {
69
                if ($s === null) {
1 ✔
70
                        return null;
1 ✔
71
                }
72
                $modifier = new self;
1 ✔
73
                if ($offset !== null) {
1 ✔
74
                        $modifier->range = new Range($offset, strlen($s));
1 ✔
75
                }
76
                $modifier->setProperties($s);
1 ✔
77
                return $modifier;
1 ✔
78
        }
79

80

81
        /** @internal  adds the parts of a modifier string to this one */
82
        public function setProperties(?string $s): void
83
        {
84
                $p = 0;
1 ✔
85
                $len = $s ? strlen($s) : 0;
1 ✔
86

87
                while ($p < $len) {
1 ✔
88
                        $ch = $s[$p];
1 ✔
89

90
                        if ($ch === '(') { // title
1 ✔
91
                                $m = Regexp::match($s, '~(?: \\\\\) | [^)\n] )++\)~xA', offset: $p);
1 ✔
92
                                if (!isset($m[0])) {
1 ✔
93
                                        break;
1 ✔
94
                                }
95
                                $this->title = Helpers::unescapeHtml(str_replace('\)', ')', trim(substr($m[0], 1, -1))));
1 ✔
96
                                $p += strlen($m[0]);
1 ✔
97

98
                        } elseif ($ch === '{' && ($a = strpos($s, '}', $p)) !== false) { // style & attributes
1 ✔
99
                                $this->parseStyle(substr($s, $p + 1, $a - $p - 1));
1 ✔
100
                                $p = $a + 1;
1 ✔
101

102
                        } elseif ($ch === '[' && ($a = strpos($s, ']', $p)) !== false) { // classes & ID
1 ✔
103
                                $this->parseClasses(str_replace('#', ' #', substr($s, $p + 1, $a - $p - 1)));
1 ✔
104
                                $p = $a + 1;
1 ✔
105

106
                        } elseif ($val = ['^' => 'top', '-' => 'middle', '_' => 'bottom'][$ch] ?? null) { // alignment
1 ✔
UNCOV
107
                                $this->vAlign = $val;
×
108
                                $p++;
×
109

110
                        } elseif (substr($s, $p, 2) === '<>') {
1 ✔
111
                                $this->hAlign = 'center';
1 ✔
112
                                $p += 2;
1 ✔
113

114
                        } elseif ($val = ['=' => 'justify', '>' => 'right', '<' => 'left'][$ch] ?? null) {
1 ✔
115
                                $this->hAlign = $val;
1 ✔
116
                                $p++;
1 ✔
117
                        } else {
118
                                break;
1 ✔
119
                        }
120
                }
121
        }
1 ✔
122

123

124
        /**
125
         * CSS property names match regardless of case, except custom properties (--name).
126
         * @internal
127
         */
128
        public static function normalizeProperty(string $name): string
129
        {
130
                return str_starts_with($name, '--') ? $name : strtolower($name);
1 ✔
131
        }
132

133

134
        private function parseStyle(string $s): void
135
        {
136
                foreach (explode(';', $s) as $value) {
1 ✔
137
                        $pair = explode(':', $value, 2);
1 ✔
138
                        $prop = self::normalizeProperty(trim($pair[0]));
1 ✔
139
                        if ($prop === '' || !isset($pair[1])) {
1 ✔
140
                                continue;
1 ✔
141
                        }
142

143
                        $value = trim($pair[1]);
1 ✔
144

145
                        if (isset(self::ElAttrs[$prop]) || preg_match('~^(?:data|aria)-[a-z0-9_.-]+$~D', $prop)) { // attribute
1 ✔
146
                                $this->attrs[$prop] = $value;
1 ✔
147
                        } elseif ($value !== '') { // style
1 ✔
148
                                $this->styles[$prop] = $value;
1 ✔
149
                        }
150
                }
151
        }
1 ✔
152

153

154
        private function parseClasses(string $s): void
155
        {
156
                foreach (explode(' ', $s) as $value) {
1 ✔
157
                        if ($value === '') {
1 ✔
158
                                continue;
1 ✔
159
                        } elseif ($value[0] === '#') {
1 ✔
160
                                $this->id = substr($value, 1);
1 ✔
161
                        } else {
162
                                $this->classes[$value] = true;
1 ✔
163
                        }
164
                }
165
        }
1 ✔
166
}
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