• 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

98.08
/src/Texy/Modules/ParagraphModule.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\Regexp;
14
use function preg_match, preg_split, str_contains, str_replace, strlen, strtr, substr_replace, trim;
15
use const PREG_SPLIT_NO_EMPTY;
16

17

18
/**
19
 * Paragraph module.
20
 */
21
final class ParagraphModule extends Texy\Module
22
{
23
        public function __construct(Texy\Texy $texy)
1✔
24
        {
25
                $this->texy = $texy;
1✔
26
                $texy->addHandler('paragraph', $this->toElement(...));
1✔
27
        }
1✔
28

29

30
        public function process(Texy\BlockParser $parser, string $content): array
1✔
31
        {
32
                $parts = $parser->isIndented()
1✔
33
                        ? Regexp::split($content, '~(\n (?! \ ) | \n{2,})~', skipEmpty: true)
1✔
34
                        : Regexp::split($content, '~(\n{2,})~', skipEmpty: true);
1✔
35

36
                $children = [];
1✔
37
                foreach ($parts as $s) {
1✔
38
                        $s = trim($s);
1✔
39
                        if ($s === '') {
1✔
40
                                continue;
1✔
41
                        }
42

43
                        // try to find modifier
44
                        $mod = null;
1✔
45
                        if ($mx = Regexp::match($s, '~' . Texy\Patterns::MODIFIER_H . '(?= \n | \z)~sUm', captureOffset: true)) {
1✔
46
                                [$mMod] = $mx[1];
1✔
47
                                $s = trim(substr_replace($s, '', $mx[0][1], strlen($mx[0][0])));
1✔
48
                                if ($s === '') {
1✔
49
                                        continue;
1✔
50
                                }
51

52
                                $mod = new Texy\Modifier;
1✔
53
                                $mod->setProperties($mMod);
1✔
54
                        }
55

56
                        $res = $this->texy->invokeAroundHandlers('paragraph', $parser, [$s, $mod]);
1✔
57
                        if ($res) {
1✔
58
                                $children[] = $res;
1✔
59
                        }
60
                }
61

62
                return $children;
1✔
63
        }
64

65

66
        public function toElement(
1✔
67
                Texy\HandlerInvocation $invocation,
68
                string $content,
69
                ?Texy\Modifier $mod = null,
70
        ): ?Texy\HtmlElement
71
        {
72
                $texy = $this->texy;
1✔
73

74
                // find hard linebreaks
75
                $content = $texy->mergeLines
1✔
76
                        // ....
77
                        // ... => \r means break line
78
                        ? Regexp::replace($content, '~\n\ +(?= \S)~', "\r")
1✔
UNCOV
79
                        : Regexp::replace($content, '~\n~', "\r");
×
80

81
                $el = new Texy\HtmlElement('p');
1✔
82
                $el->inject($texy->parseLine($content));
1✔
83
                $content = $el->getText(); // string
1✔
84

85
                // check content type
86
                // block contains block tag
87
                if (str_contains($content, $texy::CONTENT_BLOCK)) {
1✔
88
                        $el->setName(null); // ignores modifier!
1✔
89

90
                // block contains text (protected)
91
                } elseif (str_contains($content, $texy::CONTENT_TEXTUAL)) {
1✔
92
                        // leave element p
93

94
                // block contains text
95
                } elseif (Regexp::match($content, '~[^\s' . Texy\Patterns::MARK . ']~')) {
1✔
96
                        // leave element p
97

98
                // block contains only replaced element
99
                } elseif (str_contains($content, $texy::CONTENT_REPLACED)) {
1✔
100
                        if ($texy->nontextParagraph instanceof Texy\HtmlElement) {
1✔
101
                                $el = (clone $texy->nontextParagraph)->setText($content);
1✔
102
                        } else {
103
                                $el->setName($texy->nontextParagraph);
1✔
104
                        }
105

106
                // block contains only markup tags or spaces or nothing
107
                } else {
108
                        // if {ignoreEmptyStuff} return null;
109
                        if (!$mod) {
1✔
110
                                $el->setName(null);
1✔
111
                        }
112
                }
113

114
                if ($el->getName()) {
1✔
115
                        // apply modifier
116
                        if ($mod) {
1✔
117
                                $mod->decorate($texy, $el);
1✔
118
                        }
119

120
                        // add <br>
121
                        if (str_contains($content, "\r")) {
1✔
122
                                $key = $texy->protect('<br>', $texy::CONTENT_REPLACED);
1✔
123
                                $content = str_replace("\r", $key, $content);
1✔
124
                        }
125
                }
126

127
                $content = strtr($content, "\r\n", '  ');
1✔
128
                $el->setText($content);
1✔
129

130
                return $el;
1✔
131
        }
132
}
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