• 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

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

15

16
/**
17
 * The captioned figures.
18
 */
19
final class FigureModule extends Texy\Module
20
{
21
        public string $tagName = 'div';
22

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

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

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

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

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

38

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

46

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

73

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

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

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

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

107

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

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

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

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

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

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

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

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

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

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

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

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