-
-
Notifications
You must be signed in to change notification settings - Fork 469
/
Copy pathTextNode.php
155 lines (135 loc) · 3.49 KB
/
TextNode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace PHPHtmlParser\Dom\Node;
use PHPHtmlParser\Dom\Tag;
use PHPHtmlParser\Exceptions\LogicalException;
/**
* Class TextNode.
*
* @property-read string $outerhtml
* @property-read string $innerhtml
* @property-read string $innerText
* @property-read string $text
* @property-read Tag $tag
* @property-read InnerNode $parent
*/
class TextNode extends LeafNode
{
/**
* This is a text node.
*
* @var Tag
*/
protected $tag;
/**
* This is the text in this node.
*
* @var string
*/
protected $text;
/**
* This is the converted version of the text.
*
* @var ?string
*/
protected $convertedText;
/**
* Sets the text for this node.
*
* @param bool $removeDoubleSpace
*/
public function __construct(string $text, $removeDoubleSpace = true)
{
if ($removeDoubleSpace) {
// remove double spaces
$replacedText = \mb_ereg_replace('\s+', ' ', $text);
if ($replacedText === false) {
throw new LogicalException('mb_ereg_replace returns false when attempting to clean white space from "' . $text . '".');
}
$text = $replacedText;
}
// restore line breaks
$text = \str_replace(' ', "\n", $text);
$this->text = $text;
$this->tag = new Tag('text');
parent::__construct();
}
/**
* @param bool $htmlSpecialCharsDecode
*/
public function setHtmlSpecialCharsDecode($htmlSpecialCharsDecode = false): void
{
parent::setHtmlSpecialCharsDecode($htmlSpecialCharsDecode);
$this->tag->setHtmlSpecialCharsDecode($htmlSpecialCharsDecode);
}
/**
* Returns the text of this node.
*/
public function text(): string
{
if ($this->htmlSpecialCharsDecode) {
$text = \htmlspecialchars_decode($this->text);
} else {
$text = $this->text;
}
// convert charset
if (!\is_null($this->encode)) {
if (!\is_null($this->convertedText)) {
// we already know the converted value
return $this->convertedText;
}
$text = $this->encode->convert($text);
// remember the conversion
$this->convertedText = $text;
return (string)$text;
}
return (string)$text;
}
/**
* Sets the text for this node.
*
* @var string
*/
public function setText(string $text): void
{
$this->text = $text;
if (!\is_null($this->encode)) {
$text = $this->encode->convert($text);
// remember the conversion
$this->convertedText = $text;
}
}
/**
* This node has no html, just return the text.
*
* @uses $this->text()
*/
public function innerHtml(): string
{
return $this->text();
}
/**
* This node has no html, just return the text.
*
* @uses $this->text()
*/
public function outerHtml(): string
{
return $this->text();
}
/**
* Checks if the current node is a text node.
*/
public function isTextNode(): bool
{
return true;
}
/**
* Call this when something in the node tree has changed. Like a child has been added
* or a parent has been changed.
*/
protected function clear(): void
{
$this->convertedText = null;
}
}