1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\SweetDOM |
5
|
|
|
* @copyright Copyright (c) The s9e authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\SweetDOM\NodeTraits; |
9
|
|
|
|
10
|
|
|
use BadMethodCallException; |
11
|
|
|
use DOMElement; |
12
|
|
|
use DOMException; |
13
|
|
|
use DOMNode; |
14
|
|
|
use DOMNodeList; |
15
|
|
|
use DOMText; |
16
|
|
|
use const DOM_SYNTAX_ERR, ENT_NOQUOTES, ENT_XML1; |
17
|
|
|
use function array_flip, call_user_func_array, htmlspecialchars, is_callable, preg_match, preg_match_all, preg_replace_callback, strpos, strtolower, substr, ucfirst; |
18
|
|
|
|
19
|
|
|
trait DeprecatedMethods |
20
|
|
|
{ |
21
|
|
|
use MagicMethods |
22
|
|
|
{ |
23
|
|
|
MagicMethods::__call as magicMethodsCall; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function __call(string $name, array $arguments) |
27
|
|
|
{ |
28
|
|
|
if (preg_match('(^insertAdjacent(?:Element|Text)$)i', $name)) |
29
|
|
|
{ |
30
|
|
|
$methodName = '_' . $name; |
31
|
|
|
|
32
|
|
|
return $this->$methodName(...$arguments); |
33
|
|
|
} |
34
|
|
|
if (preg_match('(^(ap|pre)pendText(Sibling|)$)i', $name, $m)) |
35
|
|
|
{ |
36
|
|
|
$methodName = [ |
37
|
|
|
'ap' => 'append', |
38
|
|
|
'pre' => 'prepend', |
39
|
|
|
'apsibling' => 'after', |
40
|
|
|
'presibling' => 'before' |
41
|
|
|
][strtolower($m[1] . $m[2])]; |
42
|
|
|
|
43
|
|
|
return $this->$methodName(...$arguments); |
44
|
|
|
} |
45
|
|
|
if (preg_match('(^(ap|pre)pend(\\w+)Sibling$)i', $name, $m)) |
46
|
|
|
{ |
47
|
|
|
$name = ['ap' => 'after', 'pre' => 'before'][strtolower($m[1])] . $m[2]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->magicMethodsCall($name, $arguments); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @deprecated |
55
|
|
|
*/ |
56
|
|
|
public function insertAdjacentXML(string $where, string $xml): void |
57
|
|
|
{ |
58
|
|
|
$fragment = $this->ownerDocument->createDocumentFragment(); |
59
|
|
|
$fragment->appendXML($this->addMissingNamespaceDeclarations($xml)); |
60
|
|
|
|
61
|
|
|
$this->insertAdjacentNode($where, $fragment); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Add namespace declarations that may be missing in given XML |
66
|
|
|
* |
67
|
|
|
* @param string $xml Original XML |
68
|
|
|
* @return string Modified XML |
69
|
|
|
*/ |
70
|
|
|
protected function addMissingNamespaceDeclarations(string $xml): string |
71
|
|
|
{ |
72
|
|
|
preg_match_all('(xmlns:\\K[-\\w]++(?==))', $xml, $m); |
73
|
|
|
$prefixes = array_flip($m[0]); |
74
|
|
|
|
75
|
|
|
return preg_replace_callback( |
76
|
|
|
'(<([-\\w]++):[^>]*?\\K\\s*/?>)', |
77
|
|
|
function ($m) use ($prefixes) |
78
|
|
|
{ |
79
|
|
|
$return = $m[0]; |
80
|
|
|
$prefix = $m[1]; |
81
|
|
|
if (!isset($prefixes[$prefix])) |
82
|
|
|
{ |
83
|
|
|
$nsURI = $this->lookupNamespaceURI($prefix); |
|
|
|
|
84
|
|
|
$return = ' xmlns:' . $prefix . '="' . htmlspecialchars($nsURI, ENT_XML1) . '"' . $return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $return; |
88
|
|
|
}, |
89
|
|
|
$xml |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Insert given node relative to this element's position |
95
|
|
|
* |
96
|
|
|
* @param string $where One of 'beforebegin', 'afterbegin', 'beforeend', 'afterend' |
97
|
|
|
* @param DOMNode $node |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function insertAdjacentNode(string $where, DOMNode $node): void |
101
|
|
|
{ |
102
|
|
|
match (strtolower($where)) |
103
|
|
|
{ |
104
|
|
|
'beforebegin' => $this->parentNode?->insertBefore($node, $this), |
105
|
|
|
'beforeend' => $this->appendChild($node), |
|
|
|
|
106
|
|
|
'afterend' => $this->parentNode?->insertBefore($node, $this->nextSibling), |
107
|
|
|
'afterbegin' => $this->insertBefore($node, $this->firstChild), |
|
|
|
|
108
|
|
|
default => throw new DOMException("'$where' is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'", DOM_SYNTAX_ERR) |
109
|
|
|
}; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function _insertAdjacentElement(string $where, self $element): self |
113
|
|
|
{ |
114
|
|
|
$this->insertAdjacentNode($where, $element); |
|
|
|
|
115
|
|
|
|
116
|
|
|
return $element; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function _insertAdjacentText(string $where, string $text): void |
120
|
|
|
{ |
121
|
|
|
$node = $this->ownerDocument->createTextNode($text); |
122
|
|
|
$this->insertAdjacentNode($where, $node); |
123
|
|
|
} |
124
|
|
|
} |