1
|
|
|
<?php |
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
|
258 |
|
public function __construct(string $text, $removeDoubleSpace = true) |
40
|
|
|
{ |
41
|
258 |
|
if ($removeDoubleSpace) { |
42
|
|
|
// remove double spaces |
43
|
255 |
|
$text = mb_ereg_replace('\s+', ' ', $text); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// restore line breaks |
47
|
258 |
|
$text = str_replace(' ', "\n", $text); |
48
|
|
|
|
49
|
258 |
|
$this->text = $text; |
50
|
258 |
|
$this->tag = new Tag('text'); |
51
|
258 |
|
parent::__construct(); |
52
|
258 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the text of this node. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
177 |
|
public function text(): string |
60
|
|
|
{ |
61
|
|
|
// convert charset |
62
|
177 |
|
if ( ! is_null($this->encode)) { |
63
|
123 |
|
if ( ! is_null($this->convertedText)) { |
64
|
|
|
// we already know the converted value |
65
|
6 |
|
return $this->convertedText; |
66
|
|
|
} |
67
|
120 |
|
$text = $this->encode->convert($this->text); |
68
|
|
|
|
69
|
|
|
// remember the conversion |
70
|
120 |
|
$this->convertedText = $text; |
71
|
|
|
|
72
|
120 |
|
return $text; |
73
|
|
|
} else { |
74
|
54 |
|
return $this->text; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sets the text for this node. |
80
|
|
|
* |
81
|
|
|
* @var string $text |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
9 |
|
public function setText(string $text): void |
85
|
|
|
{ |
86
|
9 |
|
$this->text = $text; |
87
|
|
|
|
88
|
9 |
|
if ( ! is_null($this->encode)) { |
89
|
6 |
|
$text = $this->encode->convert($text); |
90
|
|
|
|
91
|
|
|
// remember the conversion |
92
|
6 |
|
$this->convertedText = $text; |
93
|
|
|
} |
94
|
9 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* This node has no html, just return the text. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
* @uses $this->text() |
101
|
|
|
*/ |
102
|
6 |
|
public function innerHtml(): string |
103
|
|
|
{ |
104
|
6 |
|
return $this->text(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* This node has no html, just return the text. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
* @uses $this->text() |
112
|
|
|
*/ |
113
|
3 |
|
public function outerHtml(): string |
114
|
|
|
{ |
115
|
3 |
|
return $this->text(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Call this when something in the node tree has changed. Like a child has been added |
120
|
|
|
* or a parent has been changed. |
121
|
|
|
*/ |
122
|
|
|
protected function clear(): void |
123
|
|
|
{ |
124
|
|
|
$this->convertedText = null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Checks if the current node is a text node. |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
6 |
|
public function isTextNode(): bool |
133
|
|
|
{ |
134
|
6 |
|
return true; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|