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