• 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

87.8
/src/Texy/Modules/EmoticonModule.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

14

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

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

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

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

43

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

52

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

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

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

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

77

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

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

92
                return null;
×
93
        }
94

95

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

108
                $el = new Texy\HtmlElement('img');
1✔
109
                $el->attrs['src'] = Texy\Helpers::prependRoot($file, $this->root ?? $texy->imageModule->root);
1✔
110
                $el->attrs['alt'] = $raw;
1✔
111
                $el->attrs['class'][] = $this->class;
1✔
112

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

123
                $texy->summary['images'][] = $el->attrs['src'];
1✔
124
                return $el;
1✔
125
        }
126
}
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