|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Query selector. |
|
5
|
|
|
* |
|
6
|
|
|
* @method \DOMNodeList[]|\DOMNode[]|\DOMElement[]|DOMXpathTypehint[]|object query(string $expression, \DOMNode $contextnode, boolean $registerNodeNS) |
|
7
|
|
|
*/ |
|
8
|
|
|
class SmartDOMXpath extends DOMXpath |
|
9
|
|
|
{ |
|
10
|
|
|
public function __construct(\DOMDocument $dom) |
|
11
|
|
|
{ |
|
12
|
|
|
parent::__construct($dom); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Body Instance |
|
17
|
|
|
* ```html |
|
18
|
|
|
* <!--get--> <body></body> |
|
19
|
|
|
* ```. |
|
20
|
|
|
* |
|
21
|
|
|
* @return \DOMNode|null |
|
22
|
|
|
*/ |
|
23
|
|
|
public function body() |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->query('//body')->item(0); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Typehinting VSCode Support |
|
31
|
|
|
* @requires PHP Intelephense VSCode Extension |
|
32
|
|
|
* @method bool hasAttribute(string $attribute_name) check if dom element has Attribute |
|
33
|
|
|
* @method string|null getAttribute(string $attribute_name) get attribute from dom element |
|
34
|
|
|
* @method void setAttribute(string $attribute_name, string $attribute_value) set attribute from dom element |
|
35
|
|
|
* @method void removeAttribute(string $attribute_name) remove attribute from dom element |
|
36
|
|
|
*/ |
|
37
|
|
|
class DOMXpathTypehint |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* Inner html element. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
public $innerHTML; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Insert after opening body tag. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $content |
|
51
|
|
|
*/ |
|
52
|
|
|
function insertBodyFirst(\SmartDOMXpath $xpath, \DOMNode $content) |
|
53
|
|
|
{ |
|
54
|
|
|
$body = $xpath->body(); |
|
55
|
|
|
|
|
56
|
|
|
return $body->insertBefore($content, $body->firstChild); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Insert before closing body tag. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $content |
|
63
|
|
|
*/ |
|
64
|
|
|
function insertBodyLast(\SmartDOMXpath $xpath, \DOMNode $content) |
|
65
|
|
|
{ |
|
66
|
|
|
$body = $xpath->body(); |
|
67
|
|
|
|
|
68
|
|
|
return $body->appendChild($content); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* create node text. |
|
73
|
|
|
*/ |
|
74
|
|
|
function createText(string $string) |
|
75
|
|
|
{ |
|
76
|
|
|
return new DOMText($string); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* create script tag |
|
81
|
|
|
* ```php |
|
82
|
|
|
* $dom = new DOMDocument(); |
|
83
|
|
|
* $dom->loadHTMLFile('index.html'); |
|
84
|
|
|
* $script = createScript($dom, ['src'=>'/jquery.js', 'async'=>'true']); |
|
85
|
|
|
* $dom->find('body')->item(0)->appendChild($script); |
|
86
|
|
|
* ``` |
|
87
|
|
|
* return instanceof DOMElement. |
|
88
|
|
|
*/ |
|
89
|
|
|
function createScript(\DOMDocument $dom, array $opt) |
|
90
|
|
|
{ |
|
91
|
|
|
if (!isset($opt['innertext'])) { |
|
92
|
|
|
$opt['innertext'] = ''; |
|
93
|
|
|
} |
|
94
|
|
|
$script = $dom->createElement('script', $opt['innertext']); |
|
95
|
|
|
if (isset($opt['src'])) { |
|
96
|
|
|
$script->setAttribute('src', $opt['src']); |
|
97
|
|
|
if (isset($opt['cache'])) { |
|
98
|
|
|
if (defined('CONFIG')) { |
|
99
|
|
|
if (isset(CONFIG['cache']['key'])) { |
|
100
|
|
|
$script->setAttribute('src', $opt['src'] . '?' . CONFIG['cache']['key']); |
|
101
|
|
|
} else { |
|
102
|
|
|
$script->setAttribute('src', $opt['src'] . '?' . md5(serialize(CONFIG))); |
|
103
|
|
|
} |
|
104
|
|
|
} else { |
|
105
|
|
|
$script->setAttribute('src', $opt['src'] . '?' . md5(serialize(latestFile([ROOT])))); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
if (isset($opt['async'])) { |
|
110
|
|
|
$script->setAttribute('async', $opt['async']); |
|
111
|
|
|
} |
|
112
|
|
|
if (isset($opt['defer'])) { |
|
113
|
|
|
$script->setAttribute('defer', $opt['defer']); |
|
114
|
|
|
} |
|
115
|
|
|
if (isset($opt['type'])) { |
|
116
|
|
|
$script->setAttribute('type', $opt['type']); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $script; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* create Element Node. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $opt |
|
126
|
|
|
* ```php |
|
127
|
|
|
* $opt = array(array('span' => |
|
128
|
|
|
* array('class'=>'badge badge-default','id'=>'label') |
|
129
|
|
|
* )) |
|
130
|
|
|
* ``` |
|
131
|
|
|
*/ |
|
132
|
|
|
function createElement(\DOMDocument $dom, array $opt) |
|
133
|
|
|
{ |
|
134
|
|
|
$node = null; |
|
135
|
|
|
foreach ($opt as $key => $attributes) { |
|
136
|
|
|
$node = $dom->createElement($key); |
|
137
|
|
|
foreach ($attributes as $key => $value) { |
|
|
|
|
|
|
138
|
|
|
$node->setAttribute($key, $value); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $node; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
function createMeta(\DOMDocument $doc) |
|
146
|
|
|
{ |
|
147
|
|
|
$meta = [ |
|
148
|
|
|
['charset' => 'utf-8'], |
|
149
|
|
|
['name' => 'dc.creator', 'content' => 'Foo Bar'], |
|
150
|
|
|
]; |
|
151
|
|
|
|
|
152
|
|
|
foreach ($meta as $attributes) { |
|
153
|
|
|
$node = $doc->createElement('meta'); |
|
154
|
|
|
foreach ($attributes as $key => $value) { |
|
155
|
|
|
$node->setAttribute($key, $value); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.