-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclass.dom-document.php
More file actions
412 lines (333 loc) · 10.8 KB
/
class.dom-document.php
File metadata and controls
412 lines (333 loc) · 10.8 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
namespace WPGMZA;
if(!defined('ABSPATH'))
return;
/**
* We don't actually need to be callin require once here at all
*
* The autoloader works fine, but because it was rebuilt in some ways for PHP8
*
* We will leave it as it, with a conditional PHP8 brach for now
*
* Expect this to be removed more fully soon
*/
if(version_compare(phpversion(), '8.0', '>=')){
require_once(plugin_dir_path(__FILE__) . 'php8/class.dom-element.php');
} else {
require_once(plugin_dir_path(__FILE__) . 'class.dom-element.php');
}
#[\AllowDynamicProperties]
class DOMDocument extends \DOMDocument
{
private $src_file;
/**
* Constructor
* @see http://php.net/manual/en/class.domdocument.php
*/
public function __construct($version='1.0', $encoding='UTF-8')
{
\DOMDocument::__construct($version, $encoding);
$this->registerNodeClass('DOMElement', 'WPGMZA\DOMElement');
$this->onReady();
}
public static function convertUTF8ToHTMLEntities($html)
{
try{
if(version_compare(phpversion(), '8.2', '>=') && function_exists('mb_encode_numericentity')){
/* Deprecations in PHP require us to rework the way we do conversions */
$converted = htmlspecialchars_decode(mb_encode_numericentity(htmlentities($html, ENT_QUOTES, 'UTF-8'), [0x80, 0x10FFFF, 0, ~0], 'UTF-8'));
return $converted;
} else {
if(function_exists('mb_convert_encoding')){
return mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
} else{
trigger_error('Using fallback UTF to HTML entity conversion', E_USER_NOTICE);
return htmlspecialchars_decode(utf8_decode(htmlentities($html, ENT_COMPAT, 'utf-8', false)));
}
}
} catch (\Exception $ex){
if(function_exists('mb_convert_encoding')){
return mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
} else{
trigger_error('Using fallback UTF to HTML entity conversion', E_USER_NOTICE);
return htmlspecialchars_decode(utf8_decode(htmlentities($html, ENT_COMPAT, 'utf-8', false)));
}
}
}
/**
* Fired after construction when the Document is initialized
* @return void
*/
protected function onReady()
{
}
/**
* Fired after content has been loaded
* @return void
*/
protected function onLoaded()
{
}
/**
* Loads the specified file and evaulates nodes
* @see http://php.net/manual/en/domdocument.load.php
* @param string $src The file you want to load
* @param int $options See http://php.net/manual/en/domdocument.load.php
* @return boolean TRUE on success, FALSE otherwise
*/
#[\ReturnTypeWillChange]
public function load($src, $options=null)
{
if(!is_string($src))
throw new \Exception('Input must be a string');
$result = \DOMDocument::load($src, $options);
$this->src_file = $src;
$this->onEvaluateNodes();
$this->onLoaded();
return $result;
}
private function translateLineNumber($htmlLineNumber, $src)
{
}
public function onError($severity, $message, $file, $unused)
{
if(!preg_match('/DOMDocument::loadHTML.+line: (\d+)/', $message, $m)){
trigger_error($message, E_USER_WARNING);
return;
}
$htmlLineNumber = $m[1];
$lines = file($this->src_file);
$totalPhpLines = count($lines);
$lineCounter = 1;
$allowShortTags = ini_get('short_open_tag') == "1";
$regexOpenTag = ($allowShortTags ? '/<\?(php)?/' : '/<\?php/');
$regexCloseTag = "/\?>/";
$inPhp = false;
for($phpLineNumber = 1; $phpLineNumber <= $totalPhpLines; $phpLineNumber++)
{
if($lineCounter == $htmlLineNumber)
{
$message = preg_replace(
array('/loadHTML/', '/line: \d+/'),
array('loadPHPFile', "line: $phpLineNumber"),
$message
);
/* Supress error because MO files cause issues which can be ignored */
/* Update 2022-05-12 -> We don't need to log these at all,
* even surpression results in php-error class being added to WP admin area
*
* So we simply don't track the notice as it doesn't serve a real purpose on account of MO files
*/
/*
@trigger_error($message, E_USER_WARNING);
*/
return;
}
$line = $lines[$phpLineNumber - 1];
$numOpenTags = preg_match_all($regexOpenTag, $line);
$numCloseTags = preg_match_all($regexCloseTag, $line);
if($numOpenTags > $numCloseTags)
{
$inPhp = true;
}
else if($numCloseTags > 0)
{
$inPhp = false;
$lineCounter--; // NB: I don't understand why a close tag swallows the newline, but it does appear to
}
if(!$inPhp)
$lineCounter++;
}
$safeEntities = array('progress');
foreach($safeEntities as $entity){
if(preg_match("/DOMDocument::loadHTML.+{$entity} invalid in Entity/", $message, $m)){
// HTML 5 safe entity, doesn't need to be logged
return;
}
}
trigger_error("Failed to translate line number", E_USER_WARNING);
trigger_error($message, E_USER_WARNING);
}
/**
* Loads the specified file and parses any PHP
* @param string $src The file you want to load
* @param int $options See http://php.net/manual/en/domdocument.load.php
* @return boolean TRUE on success, FALSE otherwise
*/
public function loadPHPFile($src, $options=0)
{
if(!file_exists($src))
throw new \Exception("File does not exist: $src");
ob_start();
include $src;
$html = ob_get_clean();
if(empty($html))
throw new \Exception("$src is empty");
$this->src_file = $src;
$html = DOMDocument::convertUTF8ToHTMLEntities($html);
$suppress_warnings = !(defined('WP_DEBUG') && WP_DEBUG);
if(!$suppress_warnings){
$error_handler = set_error_handler(array($this, 'onError'), E_WARNING);
}
// From PHP 5.4.0 onwards, loadHTML takes 2 arguments
if(version_compare(PHP_VERSION, '5.4.0', '>='))
{
if($suppress_warnings)
$result = @$this->loadHTML($html, !empty($options) ? $options : LIBXML_NOERROR);
else
$result = $this->loadHTML($html, $options);
}
else
{
if($suppress_warnings)
$result = @$this->loadHTML($html, !empty($options) ? $options : LIBXML_NOERROR);
else
$result = $this->loadHTML($html);
}
if(!$suppress_warnings)
set_error_handler($error_handler);
$this->onLoaded();
return $result;
}
public function getDocumentElementSafe()
{
// Workaround for some installations of PHP missing documentElement property
if(property_exists($this, 'documentElement'))
return $this->documentElement;
$xpath = new \DOMXPath($this);
$result = $xpath->query('/html/body');
$item = $result->item(0);
return $item;
}
/**
* @internal Handles imports based on the content
*/
protected function handleImport($subject, $node, $forcePHP=false)
{
if(preg_match('/\.html(\.php)?$/i', $subject))
{
if(!file_exists($subject))
throw new \Exception("File '$subject' not found in {$this->src_file} line " . $node->getLineNo());
$node->import($subject, $forcePHP);
return;
}
if(preg_match('/\.php$/i', $subject))
{
if(!file_exists($subject))
throw new \Exception("File '$subject' not found");
$before = get_declared_classes();
require_once($subject);
$after = get_declared_classes();
$diff = array_diff($after, $before);
$classes_in_file = array();
foreach($diff as $class) {
$reflection = new \ReflectionClass($class);
$filename = $reflection->getFileName();
if(realpath($filename) == realpath($subject))
array_push($classes_in_file, $class);
}
if(empty($classes_in_file))
throw new \Exception('No classes found in file');
$class_name = $classes_in_file[ count($classes_in_file) - 1 ];
$instance = new $class_name();
if(!($instance instanceof DOMDocument))
throw new \Exception('Class must be an instance of WPGMZA\\DOMDocument');
$node->import($instance);
return;
}
throw new \Exception("Failed to import page \"$subject\" in {$this->src_file} on line " . $node->getLineNo());
}
/**
* Runs a CSS selector on the element. This is equivilant to Javascripts querySelector
* @param string $query a CSS selector
* @return mixed The first descendant \Smart\Element that matches the selector, or NULL if there is no match
*/
public function querySelector($query)
{
if(!$this->getDocumentElementSafe())
throw new \Exception('Document is empty');
return $this->getDocumentElementSafe()->querySelector($query);
}
/**
* Runs a CSS selector on the element. This is equivilant to Javascripts querySelectorAll
* @return mixed Any descendant \Smart\Element's that match the selector, or NULL if there is no match
*/
public function querySelectorAll($query)
{
if(!$this->getDocumentElementSafe())
throw new \Exception('Document is empty');
return $this->getDocumentElementSafe()->querySelectorAll($query);
}
/**
* Takes an associative array or object, and populates this element with it
* @param mixed $src Object or associative array
* @return void
*/
public function populate($src, $formatters=null)
{
if(!$this->getDocumentElementSafe())
throw new \Exception('Document is empty');
return $this->getDocumentElementSafe()->populate($src, $formatters);
}
public function serializeFormData()
{
if(!$this->getDocumentElementSafe())
throw new \Exception('Document is empty');
return $this->getDocumentElementSafe()->serializeFormData();
}
/**
* Utility function to create an error element
* @param string $message The error message
* @return \Smart\Element
*/
public function createErrorElement($message)
{
$span = $this->createElement('span');
$span->appendText($message);
$span->addClass('notice notice-error');
return $span;
}
/**
* This function saves only the inside of the <body> element of this document. This is useful when you want to import a HTML document into another, but you don't want to end up with nested <html> elements
* @return string The HTML string
*/
public function saveInnerBody()
{
$result = '';
$this->applyAria();
if(property_exists($this, 'documentElement'))
$body = $this->querySelector('body');
else
$body = $this->getDocumentElementSafe();
if(!$body)
return null;
for($node = $body->firstChild; $node != null; $node = $node->nextSibling)
$result .= $this->saveHTML($node);
return $result;
}
public function __get($name)
{
if($name == 'html')
return $this->saveInnerBody();
return null;
}
public function applyAria(){
$fields = $this->querySelectorAll('label + input,label + select');
if(!empty($fields)){
foreach($fields as $field){
try{
$label = $field->previousElementSibling;
if($label instanceof DomElement && $label->tagName === 'label'){
if($label->hasAttribute('id')){
$field->setAttribute('aria-labelledby', $label->getAttribute('id'));
} else {
$field->setAttribute('aria-label', trim($label->nodeValue));
}
}
} catch (\Exception $ex){
} catch (\Error $err){
}
}
}
}
}