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