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