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

dg / texy / 16790351274

06 Aug 2025 10:42PM UTC coverage: 92.746% (+0.005%) from 92.741%
16790351274

push

github

dg
HtmlElement: removed toHtml() & toText()

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

136 existing lines in 23 files now uncovered.

2391 of 2578 relevant lines covered (92.75%)

0.93 hits per line

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

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

16

17
/**
18
 * The captioned figures.
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
         */
78
        public function parse(Texy\BlockParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
79
        {
80
                [, $mURLs, $mImgMod, $mAlign, $mLink, $mContent, $mMod] = $matches;
1✔
81
                // [1] => URLs
82
                // [2] => .(title)[class]{style}<>
83
                // [3] => * < >
84
                // [4] => url | [ref] | [*image*]
85
                // [5] => ...
86
                // [6] => .(title)[class]{style}<>
87

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

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

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

108

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

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

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

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

132
                $mod->decorate($texy, $el);
1✔
133

134
                $el[0] = $elImg;
1✔
135

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

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

147
                        } elseif (empty($texy->alignClasses[$hAlign])) {
1✔
148
                                $el->attrs['style']['float'] = $hAlign;
1✔
149

150
                        } else {
UNCOV
151
                                $class .= '-' . $texy->alignClasses[$hAlign];
×
152
                        }
153
                }
154

155
                $el->attrs['class'][] = $class;
1✔
156

157
                return $el;
1✔
158
        }
159
}
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