-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOMe.php
More file actions
738 lines (598 loc) · 24.4 KB
/
DOMe.php
File metadata and controls
738 lines (598 loc) · 24.4 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
<?php
####################################################
# Document Object Model - Eynon #
# ------------------------------------------------ #
# This class is a custom reworking of PHP's #
# built in document object model. The end goal #
# was to be able to use less code to accomplish #
# the same tasks. Plus, additional features may be #
# added to advance this class beyond the scope of #
# PHP's DOMDocument.
# #
# ------------------------------------------------ #
# CREATED BY: Thomas Eynon #
# www.thomaseynon.com #
# CREATED ON: 1/18/2012 #
####################################################
if (!interface_exists("Object_Array")) {
// Blend the interfaces.
interface Object_Array extends Iterator, Countable {
}
}
if (!class_exists("DOMe")) {
class DOMe implements Object_Array {
public $tag, $children, $attributes, $text, $allowHTML, $endTag, $id, $preValue;
private $styles, $pointer;
function __construct($tag, $value = "", $endTag = true, $allowHTML = false, $preValue = "") {
$this->tag = $tag;
$this->text = $value;
$this->preValue = $preValue;
$this->endTag = $endTag;
$this->allowHTML = $allowHTML;
$this->id = 0;
$this->children = array();
$this->childIndex = array();
$this->attributes = array();
$this->styles = array();
$this->pointer = 0;
}
###############################################################################################
# CHILDREN - ASSIGNMENT
###############################################################################################
function &newChild($tag, $value = "", $endTag = true, $allowHTML = false) {
// Build the new child.
$child = new DOMe($tag, $value, $endTag, $allowHTML);
// Get the index added of the array.
$index = count($this->children);
// Give the child the id.
$child->id = $index;
// Add to children.
$this->children[] = $child;
// Add the index information to the index array.
$this->childIndex[] = $index;
// Return the child.
return $this->children[$index];
}
function assign(&$object) {
// Get the index added of the array.
$index = count($this->children);
// Give the child the id.
$object->id = $index;
// Assign the object.
$this->children[] = &$object;
// Add the index for tracking.
$this->childIndex[] = $index;
}
function insertBefore($reference, &$object) {
// First assign the element.
// Get the index added of the array.
$index = count($this->children);
// Give the child the id.
$object->id = $index;
// Assign the object.
$this->children[] = &$object;
// Check if the reference object has an id.
if (isset($reference->id)) {
// The reference ID is the index.
$key = array_search($reference->id, $this->childIndex);
if ($key !== false) {
// Rebuilt the array.
$array_left = array_slice($this->childIndex, 0, $key);
$array_right = array_slice($this->childIndex, $key);
// Add the object key, then append the right side of the array.
array_push($array_left, $index);
// Merge the array back together.
$this->childIndex = array_merge($array_left, $array_right);
}
}
}
function insertAfter($reference, &$object) {
// First assign the element.
// Get the index added of the array.
$index = count($this->children);
// Give the child the id.
$object->id = $index;
// Assign the object.
$this->children[] = &$object;
// Check if the reference object has an id.
if (isset($reference->id)) {
// The reference ID is the index.
$key = array_search($reference->id, $this->childIndex);
if ($key !== false) {
// Rebuilt the array.
$array_left = array_slice($this->childIndex, 0, $key + 1);
$array_right = array_slice($this->childIndex, $key + 1);
// Add the object key, then append the right side of the array.
array_push($array_left, $index);
// Merge the array back together.
$this->childIndex = array_merge($array_left, $array_right);
}
}
}
function insertFirst(&$object) {
// First assign the element.
// Get the index added of the array.
$index = count($this->children);
// Give the child the id.
$object->id = $index;
// Assign the object.
$this->children[] = &$object;
// Check if the reference object has an id.
array_unshift($this->childIndex, $index);
}
function insertLast(&$object) {
$this->assign($object);
}
// $levels indicates how many levels down you want to clone. -1 means all.
function copy($levels = -1) {
$object = new DOMe($this->tag, $this->text, $this->endTag, $this->allowHTML);
$object->attributes = $this->attributes;
$object->styles = $this->styles;
if ($levels != 0) {
// Copy the children.
if ($levels > 0) --$levels;
$object->childIndex = $this->childIndex;
foreach ($object->childIndex as $key) {
$object->children[$key] = $this->children[$key]->copy($levels);
}
}
return $object;
}
function remove($reference) {
// Check if the reference object has an id.
if (isset($reference->id)) {
// The reference ID is the index.
$key = array_search($reference->id, $this->childIndex);
if ($key !== false) {
// Rebuilt the array.
$array_left = array_slice($this->childIndex, 0, $key);
$array_right = array_slice($this->childIndex, $key + 1);
// Merge the array back together.
$this->childIndex = array_merge($array_left, $array_right);
// Unset the value.
unset($this->children[$reference->id]);
}
}
}
###############################################################################################
# CHILDREN - RETRIEVAL
###############################################################################################
// Retrieve the first element to match the tag.
function &getElementByTagName($tag, $levels = -1) {
$result = false;
if ($this->tag == $tag) {
return $this;
}
// Search in order
foreach ($this->childIndex as $key) {
$result = &$this->children[$key]->getElementByTagName($tag);
if ($result !== false) {
return $result;
}
}
return $result;
}
// Get Nth child.
function &child($key) {
if (isset($this->childIndex[$key]))
return $this->children[$this->childIndex[$key]];
return false;
}
###############################################################################################
# ATTRIBUTES
###############################################################################################
function setAttribute($attributeName, $value = "", $caseSensitive = false) {
// If its case sensitive, don't set it to lower case. Unless it's the style tag. Then it should always be lower case, so we can access it easily within the class.
if (!$caseSensitive || strtolower($attributeName) == "style") {
$attributeName = strtolower($attributeName);
}
$this->attributes[$attributeName] = htmlspecialchars($value, ENT_COMPAT);
// If we set a style, get the current styles.
if (strtolower($attributeName) == "style") $this->buildStyles();
return true;
}
function setAttributes($string, $caseSensitive = false) {
// Parse the string. Regex doesn't seem to work well here if javascript is included in attributes.
$attribute = "";
$value = "";
$attributes = array();
$flag = 0;
$closeChar = null;
$ignoreNextChar = false;
// Loop through each character in the string.
for ($i = 0; $i < strlen($string); $i++) {
// When flag is zero, we are looking for an attribute.
if ($flag == 0) {
// Attributes can't have spaces between the equal.
if (!ctype_space($string[$i])) {
// If we find an = sign, we are moving from the attribute name to the value.
if ($string[$i] != "=") {
// Append character to attribute.
$attribute .= $string[$i];
}
else {
// Assign the attribute with an empty value, then move on to Encapsulation step.
$attributes[$attribute] = "";
$flag = 1;
}
}
else {
// In case someone is trying to pass an attribute like "checked"
if (!empty($attribute))
$attributes[$attribute] = "";
$attribute = "";
}
}
else if ($flag == 1) {
// Encapsulation
// We need to determine the opening character so we know what the end character is.
if ($string[$i] == "'")
$closeChar = "'"; // Open character is a single quote, so end character is a single quote.
else if ($string[$i] == '"')
$closeChar = '"'; // Open character is a double quote, so end character is a double quote.
else if (ctype_space($string[$i])) {
// If it's a space, this value lacks proper encapsulation, next space we find will close the value. Append character to value string.
$closeChar = null;
$value = $string[$i];
}
// Move on to value step.
$flag = 2;
}
else if ($flag == 2) {
// If a space closes the value and a space is found, close the value.
if ($closeChar == null && ctype_space($string[$i])) {
echo $value ." |1 ";
$attributes[$attribute] = $value;
$attribute = "";
$value = "";
$flag = 0;
$closeChar = null;
continue;
}
else if ($closeChar == $string[$i] && !$ignoreNextChar) {
// If the close character was found and it's not escaped, end the value.
$attributes[$attribute] = $value;
$attribute = "";
$value = "";
$flag = 0;
$closeChar = null;
continue;
}
// If the previous char was an escape character, set the flag back to false.
if ($ignoreNextChar) $ignoreNextChar = false;
// Append the character to the value.
$value .= $string[$i];
// If this is an escape character, set the escape flag to true.
if ($string[$i] == "\\") {
$ignoreNextChar = true;
}
}
}
if (!empty($attribute)) $attributes[$attribute] = $value;
// Now set each individual attribute
foreach ($attributes as $key => $value) {
$this->setAttribute($key, $value, $caseSensitive);
}
}
// Recursively assign attributes to specified tags.
// --------------------------------------------------
// USAGE:
// $attributeName - The attribute we are adding
// or changing.
// $newName - What we are changing the attribute to.
// $newValue - What we are setting the value to.
// 0 means leave the value as it is.
// $tagFilter - An array with tag names.
// If the array is empty, then it
// applies to everything.
// If it has a tag name, it
// will only apply values to elements
// that have that tag set.
// $attributeFilter - An array with attributes and values.
// If the attribute is found and has the value
// described, the action will apply.
//
// EXAMPLE:
// setAttribute_r("disabled", "disabled", "true", array("input"));
// - This will recursively search and set the attribute ("disabled")
// to any element that has an attribute of "input".
// IE: All input boxes will be disabled.
/////////////////////////////////////////////////////
function setAttribute_r($attributeName, $newName, $newValue = 0, $tagFilter = array(), $attributeFilter = array(), $matchAll = false) {
$replace = false;
if (count($tagFilter) > 0) { // We are applying it specifically.
// Loop through the filter array and match values
if (in_array($this->tag, $tagFilter)) {
// See if there are attribute filters.
if (count($attributeFilter) > 0) {
// If we require that all attributes exist, set replace to true and if we find that one doesn't exist, we will set it to false.
if ($matchAll) {
$replace = true;
}
// Make sure that the attribute exists.
foreach ($attributeFilter as $attr => $val) {
if (isset($this->attributes[$attr]) && $this->attributes[$attr] == $val) {
// An attribute has matched
if (!$matchAll) {
$replace = true;
break;
}
}
else {
// If matchall is on and we have one that doesn't match, then we don't process the replacement.
$replace = false;
}
}
}
else {
$replace = true;
}
}
}
else { // We are applying this no matter what the tag name is.
// See if there are attribute filters.
if (count($attributeFilter) > 0) {
// If we require that all attributes exist, set replace to true and if we find that one doesn't exist, we will set it to false.
if ($matchAll) {
$replace = true;
}
// Make sure that the attribute exists.
foreach ($attributeFilter as $attr => $val) {
if (isset($this->attributes[$attr]) && $this->attributes[$attr] == $val) {
// An attribute has matched
if (!$matchAll) {
$replace = true;
break;
}
}
else {
// If matchall is on and we have one that doesn't match, then we don't process the replacement.
$replace = false;
}
}
}
else {
$replace = true;
}
}
if ($replace) {
$this->swapAttribute($attributeName, $newName);
// If there is a new value, assign it.
if ($newValue !== 0) {
$this->setAttribute($newName, $newValue);
}
}
// Run this on all the child elements.
foreach ($this->childIndex as $key) {
$this->children[$key]->setAttribute_r($attributeName, $newName, $newValue, $tagFilter, $attributeFilter, $matchAll);
}
}
// Runs a function on all values in an object tree.
function valueWalk($function) {
// Run the function.
$this->value = $function($this->value);
// Run this on all the child elements.
foreach ($this->elements as $key => $element) {
$this->elements[$key]->valueWalk($function);
}
}
function swapAttribute($attributeName, $newName) {
$value = "";
if (isset($this->attributes[$attributeName])) {
// Create temporary storage
$value = $this->attributes[$attributeName];
// Remove this attribute.
unset($this->attributes[$attributeName]);
}
// Create the new one.
$this->attributes[$newName] = $value;
}
###############################################################################################
# STYLES
###############################################################################################
// Get a specified style's value.
function getStyle($style) {
if ($this->styleExists($style))
return $this->styles[$style];
return null;
}
// Check if a style exists.
function styleExists($style) {
// Always build the style again, because a style can be set through the attribute tag or manually.
//$this->buildStyles();
if (isset($this->styles[$styleName]))
return true;
return false;
}
// Set the specified style. If it does not exist, create it.
function setStyle($style, $value, $caseSensitive = false) {
//$this->buildStyles();
if (!$caseSensitive)
$style = strtolower($style);
$this->styles[$style] = $value;
$this->setAttribute("style", $this->generateStyle());
}
function removeStyle($style, $caseSensitive = false) {
//$this->buildStyles();
if (!$caseSensitive)
$style = strtolower($style);
if (isset($this->styles[$style]))
unset($this->styles[$style]);
$this->setAttribute("style", $this->generateStyle());
}
// Generate the style string.
function generateStyle() {
$styleString = "";
// Build the new styles tag.
foreach ($this->styles as $style => $value) {
$styleString .= "{$style}: {$value};";
}
return $styleString;
}
// Build the current styles.
function buildStyles() {
// Set the styles array to empty.
$this->styles = array();
// See if the style attribute exists.
if (isset($this->attributes['style'])) {
// Explode the styles into individuals
$style_attr = explode(";", $this->attributes['style']);
// Build an array of the styles.
foreach ($style_attr as $value) {
$style_info = explode(":", $value);
if (!empty($style_info[0])) {
// trim and lower the style name tags.
$style_info[0] = strtolower(trim($style_info[0]));
$style_info[1] = trim($style_info[1]);
$this->styles[$style_info[0]] = $style_info[1];
}
}
}
}
###############################################################################################
# Importing
###############################################################################################
function importHTML($html, $root = true) {
// Strip comments.
if ($root) $html = preg_replace("@<!--(.*?)-->@", "", $html);
// These tags dont have end tags and should not be incremented.
$selfClosing = array("img", "br", "input", "hr");
// First determine if there is a tag.
while (strlen($html) > 0) {
// Trim whitespace
$html = trim($html);
// If there is a tag.
if (substr_count($html, "<") > 0) {
// Go to the first < tag.
$text = substr($html, 0, strpos($html, "<"));
// If there is text, append it to the current elements value.
if (strlen($text) > 0) $this->text = $text;
// Clear everything before the tag.
$html = substr($html, strlen($text) + 1);
// If the next character is a slash, we are ending this element.
if (substr($html, 0, 1) == "/") {
$html = substr($html, strpos($html, ">") + 1);
break;
}
else {
// Get the tag name.
$tag = substr($html, 0, strpos($html, ">"));
$tagName = substr($tag, 0, strpos($tag, " "));
if (empty($tagName)) $tagName = $tag;
$endTag = true;
if (in_array($tagName, $selfClosing)) {
$endTag = false;
}
$element = &$this->newChild($tagName, "", $endTag);
$html = substr($html, strlen($tagName));
// Get any attributes
$attributes = substr($html, 0, strpos($html, ">"));
$element->setAttributes($attributes, false);
$html = substr($html, strpos($html, ">") + 1);
if ($endTag) {
// Run through the data again on the child.
$html = $element->importHTML($html, false, ++$count);
}
}
}
else {
if (strlen($html > 0)) {
$this->value = $html;
substr($html, strlen($this->value));
}
}
}
if ($root) {
return $this;
}
else {
return $html;
}
}
###############################################################################################
# OUTPUT
###############################################################################################
function generate($tidy = true, $indent = 0, $indentStr = " ") {
$indentText = "";
$tidyText = "";
$indentStr = "";
if ($tidy) {
// How far in should we indent this?
for ($x = 0; $x < $indent; $x++) {
// Add the indent character to the indent string.
$indentText .= $indentStr;
}
$tidyText = "\n";
}
else {
$indentText = "";
$tidyText = "";
$indentStr = "";
}
$data = "";
if (!empty($this->preValue)) $data = $indentText . $this->preValue . $tidyText;
$data .= "{$indentText}<{$this->tag}";
foreach ($this->attributes as $key=>$value) {
$data .= " {$key}=\"{$value}\"";
}
if ($this->endTag == false) {
$data .=" /";
}
$data .= ">";
if ($tidy) {
$data .= $tidyText;
}
$this->text = preg_replace("@([\n]+)@i", "{$indentText}{$indentStr}$1", $this->text);
if ($this->allowHTML) {
if (!empty($this->text))
$data .= $indentText.$indentStr.$this->text;
}
else {
if (!empty($this->text))
$data .= $indentText.$indentStr.htmlspecialchars($this->text, ENT_QUOTES);
}
if (!empty($this->text))
$data .= $tidyText;
// Loop through the children.
foreach ($this->childIndex as $index) {
if (is_object($this->children[$index]) && get_class($this->children[$index]) == "DOMe") {
$data .= $this->children[$index]->generate($tidy, $indent + 1, $indentStr);
}
}
// Draw the end tag?
if ($this->endTag) {
$data .= "{$indentText}</{$this->tag}>";
if ($tidy) {
$data .= $tidyText;
}
}
return $data;
}
// Iterator template methods
function current() {
return $this->children[$this->childIndex[$this->pointer]];
}
function next() {
$this->pointer++;
if (isset($this->childIndex[$this->pointer])) {
return $this->children[$this->childIndex[$this->pointer]];
}
return false;
}
function valid() {
if (isset($this->childIndex[$this->pointer])) {
return true;
}
return false;
}
function rewind() {
$this->pointer = 0;
}
function key() {
return $this->pointer;
}
function count() {
return count($this->childIndex);
}
}
}