|
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 DOMException; |
|
11
|
|
|
use const DOM_SYNTAX_ERR, ENT_COMPAT, ENT_XML1, E_USER_DEPRECATED; |
|
12
|
|
|
use function array_flip, htmlspecialchars, preg_match, preg_match_all, preg_replace_callback, strtolower, trigger_error; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @method mixed magicMethodsCall(string $name, array $arguments) |
|
16
|
|
|
*/ |
|
17
|
|
|
trait DeprecatedMethods |
|
18
|
|
|
{ |
|
19
|
|
|
use MagicMethods |
|
20
|
|
|
{ |
|
21
|
|
|
MagicMethods::__call as magicMethodsCall; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function __call(string $name, array $arguments) |
|
25
|
|
|
{ |
|
26
|
|
|
if (preg_match('(^(ap|pre)pendText(Sibling|)$)i', $name, $m)) |
|
27
|
|
|
{ |
|
28
|
|
|
$methodName = [ |
|
29
|
|
|
'ap' => 'append', |
|
30
|
|
|
'pre' => 'prepend', |
|
31
|
|
|
'apsibling' => 'after', |
|
32
|
|
|
'presibling' => 'before' |
|
33
|
|
|
][strtolower($m[1] . $m[2])]; |
|
34
|
|
|
|
|
35
|
|
|
trigger_error('Deprecated: ' . $name . '() calls should be replaced with ' . $methodName . '(). See https://github.com/s9e/SweetDOM/blob/master/UPGRADING.md#from-2x-to-30', E_USER_DEPRECATED); |
|
36
|
|
|
|
|
37
|
|
|
return $this->$methodName(...$arguments); |
|
38
|
|
|
} |
|
39
|
|
|
if (preg_match('(^(ap|pre)pend(\\w+)Sibling$)i', $name, $m)) |
|
40
|
|
|
{ |
|
41
|
|
|
$methodName = ['ap' => 'after', 'pre' => 'before'][strtolower($m[1])] . $m[2]; |
|
42
|
|
|
|
|
43
|
|
|
trigger_error('Deprecated: ' . $name . '() calls should be replaced with ' . $methodName . '(). See https://github.com/s9e/SweetDOM/blob/master/UPGRADING.md#from-2x-to-30', E_USER_DEPRECATED); |
|
44
|
|
|
|
|
45
|
|
|
$name = $methodName; |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->magicMethodsCall($name, $arguments); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @deprecated |
|
54
|
|
|
*/ |
|
55
|
|
|
public function insertAdjacentXML(string $where, string $xml): void |
|
56
|
|
|
{ |
|
57
|
|
|
trigger_error('Deprecated: insertAdjacentXML() is deprecated. See https://github.com/s9e/SweetDOM/blob/master/UPGRADING.md#from-2x-to-30', E_USER_DEPRECATED); |
|
58
|
|
|
|
|
59
|
|
|
$fragment = $this->ownerDocument->createDocumentFragment(); |
|
60
|
|
|
$fragment->appendXML($this->addMissingNamespaceDeclarations($xml)); |
|
61
|
|
|
|
|
62
|
|
|
match (strtolower($where)) |
|
63
|
|
|
{ |
|
64
|
|
|
'afterbegin' => $this->prepend($fragment), |
|
65
|
|
|
'afterend' => $this->after($fragment), |
|
66
|
|
|
'beforebegin' => $this->before($fragment), |
|
67
|
|
|
'beforeend' => $this->appendChild($fragment), |
|
68
|
|
|
default => throw new DOMException("'$where' is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'", DOM_SYNTAX_ERR) |
|
69
|
|
|
}; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add namespace declarations that may be missing in given XML |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $xml Original XML |
|
76
|
|
|
* @return string Modified XML |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function addMissingNamespaceDeclarations(string $xml): string |
|
79
|
|
|
{ |
|
80
|
|
|
preg_match_all('(xmlns:\\K[-\\w]++(?==))', $xml, $m); |
|
81
|
|
|
$prefixes = array_flip($m[0]); |
|
82
|
|
|
|
|
83
|
|
|
return preg_replace_callback( |
|
84
|
|
|
'(<([-\\w]++):[^>]*?\\K\\s*/?>)', |
|
85
|
|
|
function ($m) use ($prefixes) |
|
86
|
|
|
{ |
|
87
|
|
|
$return = $m[0]; |
|
88
|
|
|
$prefix = $m[1]; |
|
89
|
|
|
if (!isset($prefixes[$prefix])) |
|
90
|
|
|
{ |
|
91
|
|
|
$nsURI = $this->lookupNamespaceURI($prefix); |
|
92
|
|
|
$return = ' xmlns:' . $prefix . '="' . htmlspecialchars($nsURI, ENT_COMPAT | ENT_XML1) . '"' . $return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $return; |
|
96
|
|
|
}, |
|
97
|
|
|
$xml |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
} |