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

dg / texy / 21345344909

26 Jan 2026 03:32AM UTC coverage: 92.382% (-0.4%) from 92.744%
21345344909

push

github

dg
HtmlElement: removed toHtml() & toText()

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

149 existing lines in 21 files now uncovered.

2401 of 2599 relevant lines covered (92.38%)

0.92 hits per line

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

82.2
/src/Texy/Modules/ImageModule.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\Helpers;
14
use Texy\Image;
15
use Texy\Patterns;
16
use function explode, getimagesize, is_file, is_int, min, round, rtrim, str_contains, trim;
17

18

19
/**
20
 * Processes image syntax and detects image dimensions.
21
 */
22
final class ImageModule extends Texy\Module
23
{
24
        /** root of relative images (http) */
25
        public ?string $root = 'images/';
26

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

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

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

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

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

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

45

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

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

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

71
        }
1✔
72

73

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

98

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

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

115

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

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

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

142
                return $this->texy->invokeAroundHandlers('image', $parser, [$image, $link]);
1✔
143
        }
144

145

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

155

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

166
                return null;
1✔
167
        }
168

169

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

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

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

193
                        if (!$texy->checkURL($image->URL, $texy::FILTER_IMAGE)) {
1✔
UNCOV
194
                                $image->URL = null;
×
195
                        }
196

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

206
                $image->modifier->setProperties($mod);
1✔
207
                return $image;
1✔
208
        }
209

210

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

221
                $texy = $this->texy;
1✔
222

223
                $mod = $image->modifier;
1✔
224
                $alt = $mod->title;
1✔
225
                $mod->title = null;
1✔
226
                $hAlign = $mod->hAlign;
1✔
227
                $mod->hAlign = null;
1✔
228

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

239
                if ($hAlign) {
1✔
240
                        $var = $hAlign . 'Class'; // leftClass, rightClass
1✔
241
                        if (!empty($this->$var)) {
1✔
242
                                $el->attrs['class'] = (array) ($el->attrs['class'] ?? []);
1✔
243
                                $el->attrs['class'][] = $this->$var;
1✔
244

245
                        } elseif (empty($texy->alignClasses[$hAlign])) {
1✔
246
                                $el->attrs['style'] = (array) ($el->attrs['style'] ?? []);
1✔
247
                                $el->attrs['style']['float'] = $hAlign;
1✔
248

249
                        } else {
UNCOV
250
                                $el->attrs['class'] = (array) ($el->attrs['class'] ?? []);
×
UNCOV
251
                                $el->attrs['class'][] = $texy->alignClasses[$hAlign];
×
252
                        }
253
                }
254

255
                if (!is_int($image->width) || !is_int($image->height) || $image->asMax) {
1✔
256
                        $this->detectDimensions($image);
1✔
257
                }
258

259
                $el->attrs['width'] = $image->width;
1✔
260
                $el->attrs['height'] = $image->height;
1✔
261

262
                $texy->summary['images'][] = (string) $el->attrs['src'];
1✔
263

264
                if ($link) {
1✔
265
                        return $texy->linkModule->linkToElement(null, $link, $el);
1✔
266
                }
267

268
                return $el;
1✔
269
        }
270

271

272
        private function detectDimensions(Image $image): void
1✔
273
        {
274
                // absolute URL & security check for double dot
275
                if ($image->URL === null || !Helpers::isRelative($image->URL) || str_contains($image->URL, '..')) {
1✔
276
                        return;
1✔
277
                }
278

279
                $file = rtrim((string) $this->fileRoot, '/\\') . '/' . $image->URL;
1✔
280
                if (!@is_file($file) || !($size = @getimagesize($file))) { // intentionally @
1✔
281
                        return;
1✔
282
                }
283

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

290
                        if (is_int($image->height)) {
×
UNCOV
291
                                $ratio = min($ratio, $image->height / $size[1]);
×
292
                        }
293

UNCOV
294
                        $image->width = (int) round($ratio * $size[0]);
×
295
                        $image->height = (int) round($ratio * $size[1]);
×
296

UNCOV
297
                } elseif (is_int($image->width)) {
×
UNCOV
298
                        $image->height = (int) round($size[1] / $size[0] * $image->width);
×
299

300
                } elseif (is_int($image->height)) {
×
UNCOV
301
                        $image->width = (int) round($size[0] / $size[1] * $image->height);
×
302

303
                } else {
UNCOV
304
                        $image->width = $size[0];
×
UNCOV
305
                        $image->height = $size[1];
×
306
                }
307
        }
308
}
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