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