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 ENT_COMPAT, ENT_XML1; |
12
|
|
|
use function array_flip, htmlspecialchars, preg_match, preg_match_all, preg_replace_callback, strtolower; |
13
|
|
|
|
14
|
|
|
trait DeprecatedMethods |
15
|
|
|
{ |
16
|
|
|
use PolyfillMethods |
17
|
|
|
{ |
18
|
|
|
PolyfillMethods::__call as polyfillMethodsCall; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function __call(string $name, array $arguments) |
22
|
|
|
{ |
23
|
|
|
if (preg_match('(^(ap|pre)pendText(Sibling|)$)i', $name, $m)) |
24
|
|
|
{ |
25
|
|
|
$methodName = [ |
26
|
|
|
'ap' => 'append', |
27
|
|
|
'pre' => 'prepend', |
28
|
|
|
'apsibling' => 'after', |
29
|
|
|
'presibling' => 'before' |
30
|
|
|
][strtolower($m[1] . $m[2])]; |
31
|
|
|
|
32
|
|
|
return $this->$methodName(...$arguments); |
33
|
|
|
} |
34
|
|
|
if (preg_match('(^(ap|pre)pend(\\w+)Sibling$)i', $name, $m)) |
35
|
|
|
{ |
36
|
|
|
$name = ['ap' => 'after', 'pre' => 'before'][strtolower($m[1])] . $m[2]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->polyfillMethodsCall($name, $arguments); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @deprecated |
44
|
|
|
*/ |
45
|
|
|
public function insertAdjacentXML(string $where, string $xml): void |
46
|
|
|
{ |
47
|
|
|
$fragment = $this->ownerDocument->createDocumentFragment(); |
48
|
|
|
$fragment->appendXML($this->addMissingNamespaceDeclarations($xml)); |
49
|
|
|
|
50
|
|
|
$this->insertAdjacentNode($where, $fragment); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add namespace declarations that may be missing in given XML |
55
|
|
|
* |
56
|
|
|
* @param string $xml Original XML |
57
|
|
|
* @return string Modified XML |
58
|
|
|
*/ |
59
|
|
|
protected function addMissingNamespaceDeclarations(string $xml): string |
60
|
|
|
{ |
61
|
|
|
preg_match_all('(xmlns:\\K[-\\w]++(?==))', $xml, $m); |
62
|
|
|
$prefixes = array_flip($m[0]); |
63
|
|
|
|
64
|
|
|
return preg_replace_callback( |
65
|
|
|
'(<([-\\w]++):[^>]*?\\K\\s*/?>)', |
66
|
|
|
function ($m) use ($prefixes) |
67
|
|
|
{ |
68
|
|
|
$return = $m[0]; |
69
|
|
|
$prefix = $m[1]; |
70
|
|
|
if (!isset($prefixes[$prefix])) |
71
|
|
|
{ |
72
|
|
|
$nsURI = $this->lookupNamespaceURI($prefix); |
|
|
|
|
73
|
|
|
$return = ' xmlns:' . $prefix . '="' . htmlspecialchars($nsURI, ENT_COMPAT | ENT_XML1) . '"' . $return; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $return; |
77
|
|
|
}, |
78
|
|
|
$xml |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |