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