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

dg / texy / 15479423458

05 Jun 2025 11:37PM UTC coverage: 92.741% (+0.5%) from 92.224%
15479423458

push

github

dg
HtmlElement: removed toHtml() & toText()

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

78 existing lines in 11 files now uncovered.

2389 of 2576 relevant lines covered (92.74%)

0.93 hits per line

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

82.61
/src/Texy/Modules/ImageModule.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\Helpers;
14
use Texy\Image;
15
use Texy\Patterns;
16

17

18
/**
19
 * Images module.
20
 */
21
final class ImageModule extends Texy\Module
22
{
23
        /** root of relative images (http) */
24
        public ?string $root = 'images/';
25

26
        /** root of linked images (http) */
27
        public ?string $linkedRoot = 'images/';
28

29
        /** physical location of images on server */
30
        public ?string $fileRoot = null;
31

32
        /** left-floated images CSS class */
33
        public ?string $leftClass = null;
34

35
        /** right-floated images CSS class */
36
        public ?string $rightClass = null;
37

38
        /** default alternative text */
39
        public ?string $defaultAlt = '';
40

41
        /** @var array<string, Image> image references */
42
        private array $references = [];
43

44

45
        public function __construct(Texy\Texy $texy)
1✔
46
        {
47
                $this->texy = $texy;
1✔
48

49
                $texy->allowed['image/definition'] = true;
1✔
50
                $texy->addHandler('image', $this->toElement(...));
1✔
51
                $texy->addHandler('beforeParse', $this->beforeParse(...));
1✔
52

53
                // [*image*]:LINK
54
                $texy->registerLinePattern(
1✔
55
                        $this->parseImage(...),
1✔
56
                        '~
57
                                \[\* \ *+                         # opening bracket with asterisk
58
                                ([^\n' . Patterns::MARK . ']{1,1000}) # URLs (1)
59
                                ' . Patterns::MODIFIER . '?       # modifier (2)
60
                                \ *+
61
                                ( \* | (?<! < ) > | < )           # alignment (3)
62
                                ]
63
                                (?:
64
                                        :(' . Patterns::LINK_URL . ' | : ) # link or just colon (4)
1✔
65
                                )??
66
                        ~U',
67
                        'image',
1✔
68
                );
69

70
        }
1✔
71

72

73
        /**
74
         * Text pre-processing.
75
         */
76
        private function beforeParse(Texy\Texy $texy, &$text): void
1✔
77
        {
78
                if (!empty($texy->allowed['image/definition'])) {
1✔
79
                        // [*image*]: urls .(title)[class]{style}
80
                        $text = Texy\Regexp::replace(
1✔
81
                                $text,
1✔
82
                                '~^
83
                                        \[\*                              # opening [*
84
                                        ( [^\n]{1,100} )                  # reference (1)
85
                                        \*]                               # closing *]
86
                                        : [ \t]+
87
                                        (.{1,1000})                       # URL (2)
88
                                        [ \t]*
89
                                        ' . Patterns::MODIFIER . '?       # modifier (3)
1✔
90
                                        \s*
91
                                $~mU',
92
                                $this->parseReferenceDef(...),
1✔
93
                        );
94
                }
95
        }
1✔
96

97

98
        /**
99
         * Callback for: [*image*]: urls .(title)[class]{style}.
100
         */
101
        private function parseReferenceDef(array $matches): string
1✔
102
        {
103
                [, $mRef, $mURLs, $mMod] = $matches;
1✔
104
                // [1] => [* (reference) *]
105
                // [2] => urls
106
                // [3] => .(title)[class]{style}<>
107

108
                $image = $this->factoryImage($mURLs, $mMod, false);
1✔
109
                $this->addReference($mRef, $image);
1✔
110
                return '';
1✔
111
        }
112

113

114
        /**
115
         * Callback for [* small.jpg 80x13 || big.jpg .(alternative text)[class]{style}>]:LINK.
116
         */
117
        public function parseImage(Texy\LineParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
118
        {
119
                [, $mURLs, $mMod, $mAlign, $mLink] = $matches;
1✔
120
                // [1] => URLs
121
                // [2] => .(title)[class]{style}<>
122
                // [3] => * < >
123
                // [4] => url | [ref] | [*image*]
124

125
                $image = $this->factoryImage($mURLs, $mMod . $mAlign);
1✔
126

127
                if ($mLink) {
1✔
128
                        if ($mLink === ':') {
1✔
129
                                $link = new Texy\Link($image->linkedURL ?? $image->URL);
1✔
130
                                $link->raw = ':';
1✔
131
                                $link->type = $link::IMAGE;
1✔
132
                        } else {
133
                                $link = $this->texy->linkModule->factoryLink($mLink, null, null);
1✔
134
                        }
135
                } else {
136
                        $link = null;
1✔
137
                }
138

139
                return $this->texy->invokeAroundHandlers('image', $parser, [$image, $link]);
1✔
140
        }
141

142

143
        /**
144
         * Adds new named reference to image.
145
         */
146
        public function addReference(string $name, Image $image): void
1✔
147
        {
148
                $image->name = Helpers::toLower($name);
1✔
149
                $this->references[$image->name] = $image;
1✔
150
        }
1✔
151

152

153
        /**
154
         * Returns named reference.
155
         */
156
        public function getReference(string $name): ?Image
1✔
157
        {
158
                $name = Helpers::toLower($name);
1✔
159
                if (isset($this->references[$name])) {
1✔
160
                        return clone $this->references[$name];
1✔
161
                }
162

163
                return null;
1✔
164
        }
165

166

167
        /**
168
         * Parses image's syntax. Input: small.jpg 80x13 || linked.jpg
169
         */
170
        public function factoryImage(string $content, ?string $mod, bool $tryRef = true): Image
1✔
171
        {
172
                $image = $tryRef ? $this->getReference(trim($content)) : null;
1✔
173

174
                if (!$image) {
1✔
175
                        $texy = $this->texy;
1✔
176
                        $content = explode('|', $content);
1✔
177
                        $image = new Image;
1✔
178

179
                        // dimensions
180
                        $matches = null;
1✔
181
                        if ($matches = Texy\Regexp::match($content[0], '~^(.*)\ (\d+|\?)\ *([Xx])\ *(\d+|\?)\ *$~U')) {
1✔
182
                                $image->URL = trim($matches[1]);
1✔
183
                                $image->asMax = $matches[3] === 'X';
1✔
184
                                $image->width = $matches[2] === '?' ? null : (int) $matches[2];
1✔
185
                                $image->height = $matches[4] === '?' ? null : (int) $matches[4];
1✔
186
                        } else {
187
                                $image->URL = trim($content[0]);
1✔
188
                        }
189

190
                        if (!$texy->checkURL($image->URL, $texy::FILTER_IMAGE)) {
1✔
191
                                $image->URL = null;
×
192
                        }
193

194
                        // linked image
195
                        if (isset($content[2])) {
1✔
196
                                $tmp = trim($content[2]);
×
197
                                if ($tmp !== '' && $texy->checkURL($tmp, $texy::FILTER_ANCHOR)) {
×
198
                                        $image->linkedURL = $tmp;
×
199
                                }
200
                        }
201
                }
202

203
                $image->modifier->setProperties($mod);
1✔
204
                return $image;
1✔
205
        }
206

207

208
        public function toElement(
1✔
209
                ?Texy\HandlerInvocation $invocation,
210
                Image $image,
211
                ?Texy\Link $link = null,
212
        ): ?Texy\HtmlElement
213
        {
214
                if ($image->URL == null) {
1✔
UNCOV
215
                        return null;
×
216
                }
217

218
                $texy = $this->texy;
1✔
219

220
                $mod = $image->modifier;
1✔
221
                $alt = $mod->title;
1✔
222
                $mod->title = null;
1✔
223
                $hAlign = $mod->hAlign;
1✔
224
                $mod->hAlign = null;
1✔
225

226
                $el = new Texy\HtmlElement('img');
1✔
227
                $el->attrs['src'] = null; // trick - move to front
1✔
228
                $mod->decorate($texy, $el);
1✔
229
                $el->attrs['src'] = Helpers::prependRoot($image->URL, $this->root);
1✔
230
                if (!isset($el->attrs['alt'])) {
1✔
231
                        $el->attrs['alt'] = $alt === null
1✔
232
                                ? $this->defaultAlt
1✔
233
                                : $texy->typographyModule->postLine($alt);
1✔
234
                }
235

236
                if ($hAlign) {
1✔
237
                        $var = $hAlign . 'Class'; // leftClass, rightClass
1✔
238
                        if (!empty($this->$var)) {
1✔
239
                                $el->attrs['class'][] = $this->$var;
1✔
240

241
                        } elseif (empty($texy->alignClasses[$hAlign])) {
1✔
242
                                $el->attrs['style']['float'] = $hAlign;
1✔
243

244
                        } else {
UNCOV
245
                                $el->attrs['class'][] = $texy->alignClasses[$hAlign];
×
246
                        }
247
                }
248

249
                if (!is_int($image->width) || !is_int($image->height) || $image->asMax) {
1✔
250
                        $this->detectDimensions($image);
1✔
251
                }
252

253
                $el->attrs['width'] = $image->width;
1✔
254
                $el->attrs['height'] = $image->height;
1✔
255

256
                $texy->summary['images'][] = $el->attrs['src'];
1✔
257

258
                if ($link) {
1✔
259
                        return $texy->linkModule->linkToElement(null, $link, $el);
1✔
260
                }
261

262
                return $el;
1✔
263
        }
264

265

266
        private function detectDimensions(Image $image): void
1✔
267
        {
268
                // absolute URL & security check for double dot
269
                if (!Helpers::isRelative($image->URL) || str_contains($image->URL, '..')) {
1✔
270
                        return;
1✔
271
                }
272

273
                $file = rtrim((string) $this->fileRoot, '/\\') . '/' . $image->URL;
1✔
274
                if (!@is_file($file) || !($size = @getimagesize($file))) { // intentionally @
1✔
275
                        return;
1✔
276
                }
277

UNCOV
278
                if ($image->asMax) {
×
UNCOV
279
                        $ratio = 1;
×
UNCOV
280
                        if (is_int($image->width)) {
×
281
                                $ratio = min($ratio, $image->width / $size[0]);
×
282
                        }
283

284
                        if (is_int($image->height)) {
×
UNCOV
285
                                $ratio = min($ratio, $image->height / $size[1]);
×
286
                        }
287

288
                        $image->width = (int) round($ratio * $size[0]);
×
UNCOV
289
                        $image->height = (int) round($ratio * $size[1]);
×
290

291
                } elseif (is_int($image->width)) {
×
292
                        $image->height = (int) round($size[1] / $size[0] * $image->width);
×
293

294
                } elseif (is_int($image->height)) {
×
295
                        $image->width = (int) round($size[0] / $size[1] * $image->height);
×
296

297
                } else {
298
                        $image->width = $size[0];
×
UNCOV
299
                        $image->height = $size[1];
×
300
                }
301
        }
302
}
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