• 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

88.37
/src/Texy/Modules/EmoticonModule.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 function getimagesize, implode, is_array, is_file, krsort, preg_quote, rtrim, str_contains;
14

15

16
/**
17
 * Replaces emoticons with images or Unicode characters.
18
 */
19
final class EmoticonModule extends Texy\Module
20
{
21
        /** @var array<string, string>  supported emoticons and image files / chars */
22
        public array $icons = [
23
                ':-)' => '🙂',
24
                ':-(' => '☹',
25
                ';-)' => '😉',
26
                ':-D' => '😁',
27
                '8-O' => '😮',
28
                '8-)' => '😄',
29
                ':-?' => '😕',
30
                ':-x' => '😶',
31
                ':-P' => '😛',
32
                ':-|' => '😐',
33
        ];
34

35
        /** @deprecated */
36
        public ?string $class = null;
37

38
        /** @deprecated */
39
        public ?string $root = null;
40

41
        /** @deprecated */
42
        public ?string $fileRoot = null;
43

44

45
        public function __construct(Texy\Texy $texy)
1✔
46
        {
47
                $this->texy = $texy;
1✔
48
                $texy->allowed['emoticon'] = false;
1✔
49
                $texy->addHandler('emoticon', $this->toElement(...));
1✔
50
                $texy->addHandler('beforeParse', $this->beforeParse(...));
1✔
51
        }
1✔
52

53

54
        private function beforeParse(): void
55
        {
56
                if (empty($this->texy->allowed['emoticon'])) {
1✔
57
                        return;
1✔
58
                }
59

60
                krsort($this->icons);
1✔
61

62
                $pattern = [];
1✔
63
                foreach ($this->icons as $key => $foo) {
1✔
64
                        $pattern[] = Texy\Regexp::quote($key) . '+'; // last char can be repeated
1✔
65
                }
66

67
                $this->texy->registerLinePattern(
1✔
68
                        $this->parse(...),
1✔
69
                        '~
70
                                (?<= ^ | [\x00-\x20] )
71
                                (' . implode('|', $pattern) . ')
1✔
72
                        ~',
73
                        'emoticon',
1✔
74
                        '~' . implode('|', $pattern) . '~',
1✔
75
                );
76
        }
1✔
77

78

79
        /**
80
         * Callback for: :-))).
81
         * @param  string[]  $matches
82
         */
83
        public function parse(Texy\LineParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
84
        {
85
                $match = $matches[0];
1✔
86

87
                // find the closest match
88
                foreach ($this->icons as $emoticon => $foo) {
1✔
89
                        if (str_starts_with($match, $emoticon)) {
1✔
90
                                return $this->texy->invokeAroundHandlers('emoticon', $parser, [$emoticon, $match]);
1✔
91
                        }
92
                }
93

UNCOV
94
                return null;
×
95
        }
96

97

98
        public function toElement(
1✔
99
                Texy\HandlerInvocation $invocation,
100
                string $emoticon,
101
                string $raw,
102
        ): Texy\HtmlElement|string
103
        {
104
                $texy = $this->texy;
1✔
105
                $file = $this->icons[$emoticon];
1✔
106
                if (!str_contains($file, '.')) {
1✔
107
                        return $file;
1✔
108
                }
109

110
                $el = new Texy\HtmlElement('img');
1✔
111
                $el->attrs['src'] = Texy\Helpers::prependRoot($file, $this->root ?? $texy->imageModule->root);
1✔
112
                $el->attrs['alt'] = $raw;
1✔
113
                if ($this->class !== null) {
1✔
114
                        $el->attrs['class'] = (array) ($el->attrs['class'] ?? []);
1✔
115
                        $el->attrs['class'][] = $this->class;
1✔
116
                }
117

118
                // file path
119
                $file = rtrim($this->fileRoot ?? (string) $texy->imageModule->fileRoot, '/\\') . '/' . $file;
1✔
120
                if (@is_file($file)) { // intentionally @
1✔
UNCOV
121
                        $size = @getimagesize($file); // intentionally @
×
UNCOV
122
                        if (is_array($size)) {
×
UNCOV
123
                                $el->attrs['width'] = $size[0];
×
UNCOV
124
                                $el->attrs['height'] = $size[1];
×
125
                        }
126
                }
127

128
                $texy->summary['images'][] = $el->attrs['src'];
1✔
129
                return $el;
1✔
130
        }
131
}
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