forked from merk/StringParser_BBCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.php
More file actions
660 lines (602 loc) · 16.6 KB
/
Copy pathNode.php
File metadata and controls
660 lines (602 loc) · 16.6 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
<?php
/**
* BB code string parsing class
*
* Version: 0.3.3
*
* @author Christian Seiler <spam@christian-seiler.de>
* @copyright Christian Seiler 2004-2008
* @package stringparser
*
* The MIT License
*
* Copyright (c) 2004-2008 Christian Seiler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Node type: Unknown node
* @see StringParser_Node::_type
*/
define ('STRINGPARSER_NODE_UNKNOWN', 0);
/**
* Node type: Root node
* @see StringParser_Node::_type
*/
define ('STRINGPARSER_NODE_ROOT', 1);
/**
* Node type: Text node
* @see StringParser_Node::_type
*/
define ('STRINGPARSER_NODE_TEXT', 2);
/**
* Global value that is a counter of string parser node ids. Compare it to a
* sequence in databases.
* @var int
*/
$GLOBALS['__STRINGPARSER_NODE_ID'] = 0;
/**
* Generic string parser node class
*
* This is an abstract class for any type of node that is used within the
* string parser. General warning: This class contains code regarding references
* that is very tricky. Please do not touch this code unless you exactly know
* what you are doing. Incorrect handling of references may cause PHP to crash
* with a segmentation fault! You have been warned.
*
* @package stringparser
*/
class StringParser_Node {
/**
* The type of this node.
*
* There are three standard node types: root node, text node and unknown
* node. All node types are integer constants. Any node type of a
* subclass must be at least 32 to allow future developements.
*
* @access protected
* @var int
* @see STRINGPARSER_NODE_ROOT, STRINGPARSER_NODE_TEXT
* @see STRINGPARSER_NODE_UNKNOWN
*/
var $_type = STRINGPARSER_NODE_UNKNOWN;
/**
* The node ID
*
* This ID uniquely identifies this node. This is needed when searching
* for a specific node in the children array. Please note that this is
* only an internal variable and should never be used - not even in
* subclasses and especially not in external data structures. This ID
* has nothing to do with any type of ID in HTML oder XML.
*
* @access protected
* @var int
* @see StringParser_Node::_children
*/
var $_id = -1;
/**
* The parent of this node.
*
* It is either null (root node) or a reference to the parent object.
*
* @access protected
* @var mixed
* @see StringParser_Node::_children
*/
var $_parent = null;
/**
* The children of this node.
*
* It contains an array of references to all the children nodes of this
* node.
*
* @access protected
* @var array
* @see StringParser_Node::_parent
*/
var $_children = array ();
/**
* Occured at
*
* This defines the position in the parsed text where this node occurred
* at. If -1, this value was not possible to be determined.
*
* @access public
* @var int
*/
var $occurredAt = -1;
/**
* Constructor
*
* Currently, the constructor only allocates a new ID for the node and
* assigns it.
*
* @access public
* @param int $occurredAt The position in the text where this node
* occurred at. If not determinable, it is -1.
* @global __STRINGPARSER_NODE_ID
*/
function StringParser_Node ($occurredAt = -1) {
$this->_id = $GLOBALS['__STRINGPARSER_NODE_ID']++;
$this->occurredAt = $occurredAt;
}
/**
* Type of the node
*
* This function returns the type of the node
*
* @access public
* @return int
*/
function type () {
return $this->_type;
}
/**
* Prepend a node
*
* @access public
* @param object $node The node to be prepended.
* @return bool On success, the function returns true, else false.
*/
function prependChild (&$node) {
if (!is_object ($node)) {
return false;
}
// root nodes may not be children of other nodes!
if ($node->_type == STRINGPARSER_NODE_ROOT) {
return false;
}
// if node already has a parent
if ($node->_parent !== false) {
// remove node from there
$parent =& $node->_parent;
if (!$parent->removeChild ($node, false)) {
return false;
}
unset ($parent);
}
$index = count ($this->_children) - 1;
// move all nodes to a new index
while ($index >= 0) {
// save object
$object =& $this->_children[$index];
// we have to unset it because else it will be
// overridden in in the loop
unset ($this->_children[$index]);
// put object to new position
$this->_children[$index+1] =& $object;
$index--;
}
$this->_children[0] =& $node;
return true;
}
/**
* Append text to last text child
* @access public
* @param string $text The text to append
* @return bool On success, the function returns true, else false
*/
function appendToLastTextChild ($text) {
$ccount = count ($this->_children);
if ($ccount == 0 || $this->_children[$ccount-1]->_type != STRINGPARSER_NODE_TEXT) {
$ntextnode = new StringParser_Node_Text ($text);
return $this->appendChild ($ntextnode);
} else {
$this->_children[$ccount-1]->appendText ($text);
return true;
}
}
/**
* Append a node to the children
*
* This function appends a node to the children array(). It
* automatically sets the {@link StrinParser_Node::_parent _parent}
* property of the node that is to be appended.
*
* @access public
* @param object $node The node that is to be appended.
* @return bool On success, the function returns true, else false.
*/
function appendChild (&$node) {
if (!is_object ($node)) {
return false;
}
// root nodes may not be children of other nodes!
if ($node->_type == STRINGPARSER_NODE_ROOT) {
return false;
}
// if node already has a parent
if ($node->_parent !== null) {
// remove node from there
$parent =& $node->_parent;
if (!$parent->removeChild ($node, false)) {
return false;
}
unset ($parent);
}
// append it to current node
$new_index = count ($this->_children);
$this->_children[$new_index] =& $node;
$node->_parent =& $this;
return true;
}
/**
* Insert a node before another node
*
* @access public
* @param object $node The node to be inserted.
* @param object $reference The reference node where the new node is
* to be inserted before.
* @return bool On success, the function returns true, else false.
*/
function insertChildBefore (&$node, &$reference) {
if (!is_object ($node)) {
return false;
}
// root nodes may not be children of other nodes!
if ($node->_type == STRINGPARSER_NODE_ROOT) {
return false;
}
// is the reference node a child?
$child = $this->_findChild ($reference);
if ($child === false) {
return false;
}
// if node already has a parent
if ($node->_parent !== null) {
// remove node from there
$parent =& $node->_parent;
if (!$parent->removeChild ($node, false)) {
return false;
}
unset ($parent);
}
$index = count ($this->_children) - 1;
// move all nodes to a new index
while ($index >= $child) {
// save object
$object =& $this->_children[$index];
// we have to unset it because else it will be
// overridden in in the loop
unset ($this->_children[$index]);
// put object to new position
$this->_children[$index+1] =& $object;
$index--;
}
$this->_children[$child] =& $node;
return true;
}
/**
* Insert a node after another node
*
* @access public
* @param object $node The node to be inserted.
* @param object $reference The reference node where the new node is
* to be inserted after.
* @return bool On success, the function returns true, else false.
*/
function insertChildAfter (&$node, &$reference) {
if (!is_object ($node)) {
return false;
}
// root nodes may not be children of other nodes!
if ($node->_type == STRINGPARSER_NODE_ROOT) {
return false;
}
// is the reference node a child?
$child = $this->_findChild ($reference);
if ($child === false) {
return false;
}
// if node already has a parent
if ($node->_parent !== false) {
// remove node from there
$parent =& $node->_parent;
if (!$parent->removeChild ($node, false)) {
return false;
}
unset ($parent);
}
$index = count ($this->_children) - 1;
// move all nodes to a new index
while ($index >= $child + 1) {
// save object
$object =& $this->_children[$index];
// we have to unset it because else it will be
// overridden in in the loop
unset ($this->_children[$index]);
// put object to new position
$this->_children[$index+1] =& $object;
$index--;
}
$this->_children[$child + 1] =& $node;
return true;
}
/**
* Remove a child node
*
* This function removes a child from the children array. A parameter
* tells the function whether to destroy the child afterwards or not.
* If the specified node is not a child of this node, the function will
* return false.
*
* @access public
* @param mixed $child The child to destroy; either an integer
* specifying the index of the child or a reference
* to the child itself.
* @param bool $destroy Destroy the child afterwards.
* @return bool On success, the function returns true, else false.
*/
function removeChild (&$child, $destroy = false) {
if (is_object ($child)) {
// if object: get index
$object =& $child;
unset ($child);
$child = $this->_findChild ($object);
if ($child === false) {
return false;
}
} else {
// remove reference on $child
$save = $child;
unset($child);
$child = $save;
// else: get object
if (!isset($this->_children[$child])) {
return false;
}
$object =& $this->_children[$child];
}
// store count for later use
$ccount = count ($this->_children);
// index out of bounds
if (!is_int ($child) || $child < 0 || $child >= $ccount) {
return false;
}
// inkonsistency
if ($this->_children[$child]->_parent === null ||
$this->_children[$child]->_parent->_id != $this->_id) {
return false;
}
// $object->_parent = null would equal to $this = null
// as $object->_parent is a reference to $this!
// because of this, we have to unset the variable to remove
// the reference and then redeclare the variable
unset ($object->_parent); $object->_parent = null;
// we have to unset it because else it will be overridden in
// in the loop
unset ($this->_children[$child]);
// move all remaining objects one index higher
while ($child < $ccount - 1) {
// save object
$obj =& $this->_children[$child+1];
// we have to unset it because else it will be
// overridden in in the loop
unset ($this->_children[$child+1]);
// put object to new position
$this->_children[$child] =& $obj;
// UNSET THE OBJECT!
unset ($obj);
$child++;
}
if ($destroy) {
return StringParser_Node::destroyNode ($object);
unset ($object);
}
return true;
}
/**
* Get the first child of this node
*
* @access public
* @return mixed
*/
function &firstChild () {
$ret = null;
if (!count ($this->_children)) {
return $ret;
}
return $this->_children[0];
}
/**
* Get the last child of this node
*
* @access public
* @return mixed
*/
function &lastChild () {
$ret = null;
$c = count ($this->_children);
if (!$c) {
return $ret;
}
return $this->_children[$c-1];
}
/**
* Destroy a node
*
* @access public
* @static
* @param object $node The node to destroy
* @return bool True on success, else false.
*/
static function destroyNode (&$node) {
if ($node === null) {
return false;
}
// if parent exists: remove node from tree!
if ($node->_parent !== null) {
$parent =& $node->_parent;
// directly return that result because the removeChild
// method will call destroyNode again
return $parent->removeChild ($node, true);
}
// node has children
while (count ($node->_children)) {
$child = 0;
// remove first child until no more children remain
if (!$node->removeChild ($child, true)) {
return false;
}
unset($child);
}
// now call the nodes destructor
if (!$node->_destroy ()) {
return false;
}
// now just unset it and prey that there are no more references
// to this node
unset ($node);
return true;
}
/**
* Destroy this node
*
*
* @access protected
* @return bool True on success, else false.
*/
function _destroy () {
return true;
}
/**
* Find a child node
*
* This function searches for a node in the own children and returns
* the index of the node or false if the node is not a child of this
* node.
*
* @access protected
* @param mixed $child The node to look for.
* @return mixed The index of the child node on success, else false.
*/
function _findChild (&$child) {
if (!is_object ($child)) {
return false;
}
$ccount = count ($this->_children);
for ($i = 0; $i < $ccount; $i++) {
if ($this->_children[$i]->_id == $child->_id) {
return $i;
}
}
return false;
}
/**
* Checks equality of this node and another node
*
* @access public
* @param mixed $node The node to be compared with
* @return bool True if the other node equals to this node, else false.
*/
function equals (&$node) {
return ($this->_id == $node->_id);
}
/**
* Determines whether a criterium matches this node
*
* @access public
* @param string $criterium The criterium that is to be checked
* @param mixed $value The value that is to be compared
* @return bool True if this node matches that criterium
*/
function matchesCriterium ($criterium, $value) {
return false;
}
/**
* Search for nodes with a certain criterium
*
* This may be used to implement getElementsByTagName etc.
*
* @access public
* @param string $criterium The criterium that is to be checked
* @param mixed $value The value that is to be compared
* @return array All subnodes that match this criterium
*/
function &getNodesByCriterium ($criterium, $value) {
$nodes = array ();
$node_ctr = 0;
for ($i = 0; $i < count ($this->_children); $i++) {
if ($this->_children[$i]->matchesCriterium ($criterium, $value)) {
$nodes[$node_ctr++] =& $this->_children[$i];
}
$subnodes = $this->_children[$i]->getNodesByCriterium ($criterium, $value);
if (count ($subnodes)) {
$subnodes_count = count ($subnodes);
for ($j = 0; $j < $subnodes_count; $j++) {
$nodes[$node_ctr++] =& $subnodes[$j];
unset ($subnodes[$j]);
}
}
unset ($subnodes);
}
return $nodes;
}
/**
* Search for nodes with a certain criterium and return the count
*
* Similar to getNodesByCriterium
*
* @access public
* @param string $criterium The criterium that is to be checked
* @param mixed $value The value that is to be compared
* @return int The number of subnodes that match this criterium
*/
function getNodeCountByCriterium ($criterium, $value) {
$node_ctr = 0;
for ($i = 0; $i < count ($this->_children); $i++) {
if ($this->_children[$i]->matchesCriterium ($criterium, $value)) {
$node_ctr++;
}
$subnodes = $this->_children[$i]->getNodeCountByCriterium ($criterium, $value);
$node_ctr += $subnodes;
}
return $node_ctr;
}
/**
* Dump nodes
*
* This dumps a tree of nodes
*
* @access public
* @param string $prefix The prefix that is to be used for indentation
* @param string $linesep The line separator
* @param int $level The initial level of indentation
* @return string
*/
function dump ($prefix = " ", $linesep = "\n", $level = 0) {
$str = str_repeat ($prefix, $level) . $this->_id . ": " . $this->_dumpToString () . $linesep;
for ($i = 0; $i < count ($this->_children); $i++) {
$str .= $this->_children[$i]->dump ($prefix, $linesep, $level + 1);
}
return $str;
}
/**
* Dump this node to a string
*
* @access protected
* @return string
*/
function _dumpToString () {
if ($this->_type == STRINGPARSER_NODE_ROOT) {
return "root";
}
return (string)$this->_type;
}
}