|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\HTMLToMarkdown; |
|
4
|
|
|
|
|
5
|
|
|
class Element implements ElementInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var \DOMNode |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $node; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var ElementInterface|null |
|
14
|
|
|
*/ |
|
15
|
|
|
private $nextCached; |
|
16
|
|
|
|
|
17
|
90 |
|
public function __construct(\DOMNode $node) |
|
18
|
|
|
{ |
|
19
|
90 |
|
$this->node = $node; |
|
20
|
90 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return bool |
|
24
|
|
|
*/ |
|
25
|
12 |
|
public function isBlock() |
|
26
|
|
|
{ |
|
27
|
12 |
|
switch ($this->getTagName()) { |
|
28
|
12 |
|
case 'blockquote': |
|
29
|
12 |
|
case 'body': |
|
30
|
12 |
|
case 'div': |
|
31
|
12 |
|
case 'h1': |
|
32
|
12 |
|
case 'h2': |
|
33
|
12 |
|
case 'h3': |
|
34
|
12 |
|
case 'h4': |
|
35
|
12 |
|
case 'h5': |
|
36
|
12 |
|
case 'h6': |
|
37
|
12 |
|
case 'hr': |
|
38
|
12 |
|
case 'html': |
|
39
|
12 |
|
case 'li': |
|
40
|
9 |
|
case 'p': |
|
41
|
9 |
|
case 'ol': |
|
42
|
9 |
|
case 'ul': |
|
43
|
3 |
|
return true; |
|
44
|
|
|
default: |
|
45
|
9 |
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
public function isText() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->getTagName() === '#text'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
9 |
|
public function isWhitespace() |
|
61
|
|
|
{ |
|
62
|
9 |
|
return $this->getTagName() === '#text' && trim($this->getValue()) === ''; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
90 |
|
public function getTagName() |
|
69
|
|
|
{ |
|
70
|
90 |
|
return $this->node->nodeName; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
78 |
|
public function getValue() |
|
77
|
|
|
{ |
|
78
|
78 |
|
return $this->node->nodeValue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return ElementInterface|null |
|
83
|
|
|
*/ |
|
84
|
75 |
|
public function getParent() |
|
85
|
|
|
{ |
|
86
|
75 |
|
return new static($this->node->parentNode) ?: null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
90 |
|
public function hasChildren() |
|
93
|
|
|
{ |
|
94
|
90 |
|
return $this->node->hasChildNodes(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return ElementInterface[] |
|
99
|
|
|
*/ |
|
100
|
90 |
|
public function getChildren() |
|
101
|
|
|
{ |
|
102
|
90 |
|
$ret = array(); |
|
103
|
|
|
/** @var \DOMNode $node */ |
|
104
|
90 |
|
foreach ($this->node->childNodes as $node) { |
|
105
|
90 |
|
$ret[] = new static($node); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
90 |
|
return $ret; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return ElementInterface|null |
|
113
|
|
|
*/ |
|
114
|
18 |
|
public function getNext() |
|
115
|
|
|
{ |
|
116
|
18 |
|
if ($this->nextCached === null) { |
|
117
|
18 |
|
$nextNode = $this->getNextNode($this->node); |
|
118
|
18 |
|
if ($nextNode !== null) { |
|
119
|
18 |
|
$this->nextCached = new static($nextNode); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
18 |
|
return $this->nextCached; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param \DomNode $node |
|
128
|
|
|
* @param bool $checkChildren |
|
129
|
|
|
* |
|
130
|
|
|
* @return \DomNode|null |
|
131
|
|
|
*/ |
|
132
|
18 |
|
private function getNextNode($node, $checkChildren = true) |
|
133
|
|
|
{ |
|
134
|
18 |
|
if ($checkChildren && $node->firstChild) { |
|
135
|
|
|
return $node->firstChild; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
18 |
|
if ($node->nextSibling) { |
|
139
|
18 |
|
return $node->nextSibling; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
3 |
|
if ($node->parentNode) { |
|
143
|
3 |
|
return $this->getNextNode($node->parentNode, false); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string[]|string $tagNames |
|
149
|
|
|
* |
|
150
|
|
|
* @return bool |
|
151
|
|
|
*/ |
|
152
|
90 |
|
public function isDescendantOf($tagNames) |
|
153
|
|
|
{ |
|
154
|
90 |
|
if (!is_array($tagNames)) { |
|
155
|
6 |
|
$tagNames = array($tagNames); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
90 |
|
for ($p = $this->node->parentNode; $p !== false; $p = $p->parentNode) { |
|
159
|
90 |
|
if (is_null($p)) { |
|
160
|
90 |
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
90 |
|
if (in_array($p->nodeName, $tagNames)) { |
|
164
|
21 |
|
return true; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param string $markdown |
|
173
|
|
|
*/ |
|
174
|
90 |
|
public function setFinalMarkdown($markdown) |
|
175
|
|
|
{ |
|
176
|
90 |
|
$markdown_node = $this->node->ownerDocument->createTextNode($markdown); |
|
177
|
90 |
|
$this->node->parentNode->replaceChild($markdown_node, $this->node); |
|
178
|
90 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
81 |
|
public function getChildrenAsString() |
|
184
|
|
|
{ |
|
185
|
81 |
|
return $this->node->C14N(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return int |
|
190
|
|
|
*/ |
|
191
|
9 |
|
public function getSiblingPosition() |
|
192
|
|
|
{ |
|
193
|
9 |
|
$position = 0; |
|
194
|
|
|
|
|
195
|
|
|
// Loop through all nodes and find the given $node |
|
196
|
9 |
|
foreach ($this->getParent()->getChildren() as $current_node) { |
|
197
|
9 |
|
if (!$current_node->isWhitespace()) { |
|
198
|
9 |
|
$position++; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// TODO: Need a less-buggy way of comparing these |
|
202
|
|
|
// Perhaps we can somehow ensure that we always have the exact same object and use === instead? |
|
203
|
9 |
|
if ($this->equals($current_node)) { |
|
204
|
9 |
|
break; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
9 |
|
return $position; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return int |
|
213
|
|
|
*/ |
|
214
|
12 |
|
public function getListItemLevel() |
|
215
|
|
|
{ |
|
216
|
12 |
|
$level = 0; |
|
217
|
12 |
|
$parent = $this->getParent(); |
|
218
|
|
|
|
|
219
|
12 |
|
while ($parent !== null && $parent->node->parentNode) { |
|
220
|
12 |
|
if ($parent->getTagName() === 'li') { |
|
221
|
6 |
|
$level++; |
|
222
|
|
|
} |
|
223
|
12 |
|
$parent = $parent->getParent(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
12 |
|
return $level; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param string $name |
|
231
|
|
|
* |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
27 |
|
public function getAttribute($name) |
|
235
|
|
|
{ |
|
236
|
27 |
|
if ($this->node instanceof \DOMElement) { |
|
237
|
27 |
|
return $this->node->getAttribute($name); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return ''; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param ElementInterface $element |
|
245
|
|
|
* |
|
246
|
|
|
* @return bool |
|
247
|
|
|
*/ |
|
248
|
9 |
|
public function equals(ElementInterface $element) |
|
249
|
|
|
{ |
|
250
|
9 |
|
if ($element instanceof self) { |
|
251
|
9 |
|
return $element->node === $this->node; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return $element === $this; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|