blob: 1d03e630fdbd473233bf458f1937156acad2fdd5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
<?php
namespace andreskrey\Readability;
use andreskrey\Readability\NodeClass\DOMDocument;
use andreskrey\Readability\NodeClass\DOMNode;
use andreskrey\Readability\NodeClass\DOMNodeList;
/**
* Class NodeUtility
* @package andreskrey\Readability
*/
class NodeUtility
{
/**
*
* Imported from the Element class on league\html-to-markdown
*
* @param $node
* @return mixed
*/
public static function nextElement($node)
{
$next = $node;
while ($next
&& $next->nodeName !== '#text'
&& trim($next->textContent)) {
$next = $next->nextSibling;
}
return $next;
}
/**
* Changes the node tag name. Since tagName on DOMElement is a read only value, this must be done creating a new
* element with the new tag name and importing it to the main DOMDocument.
*
* @param string $value
* @param bool $importAttributes
* @return DOMNode
*/
public static function setNodeTag($node, $value, $importAttributes = false)
{
$new = new DOMDocument();
$new->appendChild($new->createElement($value));
$children = $node->childNodes;
/** @var $children DOMNodeList $i */
for ($i = 0; $i < $children->length; $i++) {
$import = $new->importNode($children->item($i), true);
$new->firstChild->appendChild($import);
}
if ($importAttributes) {
// Import attributes from the original node.
foreach ($node->attributes as $attribute) {
$new->firstChild->setAttribute($attribute->nodeName, $attribute->nodeValue);
}
}
// The import must be done on the firstChild of $new, since $new is a DOMDocument and not a DOMElement.
$import = $node->ownerDocument->importNode($new->firstChild, true);
$node->parentNode->replaceChild($import, $node);
return $import;
}
}
|