• 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

92.45
/src/Texy/Modules/FigureModule.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\Patterns;
14
use function trim;
15

16

17
/**
18
 * Processes images with captions.
19
 */
20
final class FigureModule extends Texy\Module
21
{
22
        public string $tagName = 'div';
23

24
        /** non-floated box CSS class */
25
        public ?string $class = 'figure';
26

27
        /** left-floated box CSS class */
28
        public ?string $leftClass = null;
29

30
        /** right-floated box CSS class */
31
        public ?string $rightClass = null;
32

33
        /** how calculate div's width */
34
        public int|false $widthDelta = 10;
35

36
        /** caption after *** is required */
37
        public bool $requireCaption = true;
38

39

40
        public function __construct(Texy\Texy $texy)
1✔
41
        {
42
                $this->texy = $texy;
1✔
43
                $texy->addHandler('figure', $this->toElement(...));
1✔
44
                $texy->addHandler('beforeParse', $this->beforeParse(...));
1✔
45
        }
1✔
46

47

48
        private function beforeParse(): void
49
        {
50
                $this->texy->registerBlockPattern(
1✔
51
                        $this->parse(...),
1✔
52
                        '~^
53
                                (?>
54
                                        \[\*\ *+                      # opening bracket with asterisk
55
                                        ([^\n' . Patterns::MARK . ']{1,1000}) # URLs (1)
56
                                        ' . Patterns::MODIFIER . '?   # modifier (2)
57
                                        \ *+
58
                                        ( \* | (?<! < ) > | < )       # alignment (3)
59
                                        ]
60
                                )
61
                                (?:
62
                                        :(' . Patterns::LINK_URL . ' | : ) # link or colon (4)
63
                                )??
64
                                (?:
65
                                        \ ++ \*\*\* \ ++              # separator
66
                                        (.{0,2000})                   # caption (5)
67
                                )' . ($this->requireCaption ? '' : '?') . ' 
1✔
68
                                ' . Patterns::MODIFIER_H . '?     # modifier (6)
1✔
69
                        $~mU',
70
                        'figure',
1✔
71
                );
72
        }
1✔
73

74

75
        /**
76
         * Callback for [*image*]:link *** .... .(title)[class]{style}>.
77
         * @param  string[]  $matches
78
         */
79
        public function parse(Texy\BlockParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
80
        {
81
                [, $mURLs, $mImgMod, $mAlign, $mLink, $mContent, $mMod] = $matches;
1✔
82
                // [1] => URLs
83
                // [2] => .(title)[class]{style}<>
84
                // [3] => * < >
85
                // [4] => url | [ref] | [*image*]
86
                // [5] => ...
87
                // [6] => .(title)[class]{style}<>
88

89
                $texy = $this->texy;
1✔
90
                $image = $texy->imageModule->factoryImage($mURLs, $mImgMod . $mAlign);
1✔
91
                $mod = new Texy\Modifier($mMod);
1✔
92
                $mContent = trim($mContent ?? '');
1✔
93

94
                if ($mLink) {
1✔
95
                        if ($mLink === ':') {
1✔
96
                                $link = new Texy\Link($image->linkedURL ?? $image->URL);
1✔
97
                                $link->raw = ':';
1✔
98
                                $link->type = $link::IMAGE;
1✔
99
                        } else {
100
                                $link = $texy->linkModule->factoryLink($mLink, null, null);
1✔
101
                        }
102
                } else {
103
                        $link = null;
1✔
104
                }
105

106
                return $texy->invokeAroundHandlers('figure', $parser, [$image, $link, $mContent, $mod]);
1✔
107
        }
108

109

110
        public function toElement(
1✔
111
                Texy\HandlerInvocation $invocation,
112
                Texy\Image $image,
113
                ?Texy\Link $link,
114
                string $content,
115
                Texy\Modifier $mod,
116
        ): ?Texy\HtmlElement
117
        {
118
                $texy = $this->texy;
1✔
119

120
                $hAlign = $image->modifier->hAlign;
1✔
121
                $image->modifier->hAlign = null;
1✔
122

123
                $elImg = $texy->imageModule->toElement(null, $image, $link); // returns Texy\HtmlElement or null!
1✔
124
                if (!$elImg) {
1✔
UNCOV
125
                        return null;
×
126
                }
127

128
                $el = new Texy\HtmlElement($this->tagName);
1✔
129
                if (!empty($image->width) && $this->widthDelta !== false) {
1✔
UNCOV
130
                        $el->attrs['style'] = (array) ($el->attrs['style'] ?? []);
×
UNCOV
131
                        $el->attrs['style']['max-width'] = ($image->width + $this->widthDelta) . 'px';
×
132
                }
133

134
                $mod->decorate($texy, $el);
1✔
135

136
                $el->add($elImg);
1✔
137

138
                if ($content !== '') {
1✔
139
                        $el[1] = new Texy\HtmlElement($this->tagName === 'figure' ? 'figcaption' : 'p');
1✔
140
                        $el[1]->inject($texy->parseLine($content));
1✔
141
                }
142

143
                $class = $this->class;
1✔
144
                if ($hAlign) {
1✔
145
                        $var = $hAlign . 'Class'; // leftClass, rightClass
1✔
146
                        if (!empty($this->$var)) {
1✔
147
                                $class = $this->$var;
1✔
148

149
                        } elseif (empty($texy->alignClasses[$hAlign])) {
1✔
150
                                $el->attrs['style'] = (array) ($el->attrs['style'] ?? []);
1✔
151
                                $el->attrs['style']['float'] = $hAlign;
1✔
152

153
                        } else {
UNCOV
154
                                $class .= '-' . $texy->alignClasses[$hAlign];
×
155
                        }
156
                }
157

158
                $el->attrs['class'] = (array) ($el->attrs['class'] ?? []);
1✔
159
                $el->attrs['class'][] = $class;
1✔
160

161
                return $el;
1✔
162
        }
163
}
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