| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\TreeBuilder; |
| 4 |
|
| 5 |
/** |
| 6 |
* The parent class for the "stack of open elements". |
| 7 |
* @see CachingStack |
| 8 |
* @see SimpleStack |
| 9 |
*/ |
| 10 |
abstract class Stack { |
| 11 |
/** @var Element|null */ |
| 12 |
public $current; |
| 13 |
|
| 14 |
/** |
| 15 |
* Push an element |
| 16 |
* |
| 17 |
* @param Element $elt |
| 18 |
*/ |
| 19 |
abstract public function push( Element $elt ); |
| 20 |
|
| 21 |
/** |
| 22 |
* Pop an element from the stack |
| 23 |
* |
| 24 |
* @return Element|null |
| 25 |
*/ |
| 26 |
abstract public function pop(); |
| 27 |
|
| 28 |
/** |
| 29 |
* Replace an element which is on the stack with another element of the |
| 30 |
* same type (i.e. with the same tag name). |
| 31 |
* |
| 32 |
* @param Element $old The element to be removed |
| 33 |
* @param Element $new The element to be inserted |
| 34 |
*/ |
| 35 |
abstract public function replace( Element $old, Element $new ); |
| 36 |
|
| 37 |
/** |
| 38 |
* Remove an element, potentially from the middle of the stack. |
| 39 |
* |
| 40 |
* @param Element $elt |
| 41 |
*/ |
| 42 |
abstract public function remove( Element $elt ); |
| 43 |
|
| 44 |
/** |
| 45 |
* Is there an element in (default) scope which is in the HTML namespace |
| 46 |
* and has the given tag name? |
| 47 |
* |
| 48 |
* @param string $name |
| 49 |
* @return bool |
| 50 |
*/ |
| 51 |
abstract public function isInScope( $name ); |
| 52 |
|
| 53 |
/** |
| 54 |
* Is the given element in (default) scope? |
| 55 |
* |
| 56 |
* @param Element $elt |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
abstract public function isElementInScope( Element $elt ); |
| 60 |
|
| 61 |
/** |
| 62 |
* Is there any element in the (default) scope which is in the HTML |
| 63 |
* namespace and has one of the given tag names? |
| 64 |
* |
| 65 |
* @param array<string,mixed> $names An array with the tag names in the |
| 66 |
* keys, the value arbitrary |
| 67 |
* @return bool |
| 68 |
*/ |
| 69 |
abstract public function isOneOfSetInScope( $names ); |
| 70 |
|
| 71 |
/** |
| 72 |
* Is there an element in list scope which is an HTML element with the |
| 73 |
* given name? |
| 74 |
* |
| 75 |
* @param string $name |
| 76 |
* @return bool |
| 77 |
*/ |
| 78 |
abstract public function isInListScope( $name ); |
| 79 |
|
| 80 |
/** |
| 81 |
* Is there an element in button scope which is an HTML element with the |
| 82 |
* given name? |
| 83 |
* |
| 84 |
* @param string $name |
| 85 |
* @return bool |
| 86 |
*/ |
| 87 |
abstract public function isInButtonScope( $name ); |
| 88 |
|
| 89 |
/** |
| 90 |
* Is there an element in table scope which is an HTML element with the |
| 91 |
* given name? |
| 92 |
* |
| 93 |
* @param string $name |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
abstract public function isInTableScope( $name ); |
| 97 |
|
| 98 |
/** |
| 99 |
* Is there an element in select scope which is an HTML element with the |
| 100 |
* given name? |
| 101 |
* |
| 102 |
* @param string $name |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
abstract public function isInSelectScope( $name ); |
| 106 |
|
| 107 |
/** |
| 108 |
* Get an element from the stack, where 0 is the first element inserted, |
| 109 |
* and $this->length() - 1 is the most recently inserted element. This will |
| 110 |
* raise a PHP notice if the index is out of range. |
| 111 |
* |
| 112 |
* @param int $idx |
| 113 |
* @return Element|null |
| 114 |
*/ |
| 115 |
abstract public function item( $idx ); |
| 116 |
|
| 117 |
/** |
| 118 |
* Get the number of elements in the stack. |
| 119 |
* |
| 120 |
* @return int |
| 121 |
*/ |
| 122 |
abstract public function length(); |
| 123 |
|
| 124 |
/** |
| 125 |
* Is there a template element in the stack of open elements? |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
abstract public function hasTemplate(); |
| 130 |
|
| 131 |
/** |
| 132 |
* Get a string representation of the stack for debugging purposes. |
| 133 |
* |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
public function dump() { |
| 137 |
$s = ''; |
| 138 |
for ( $i = 0; $i < $this->length(); $i++ ) { |
| 139 |
$item = $this->item( $i ); |
| 140 |
$s .= "$i. " . $item->getDebugTag(); |
| 141 |
if ( $i === $this->length() - 1 && $item !== $this->current ) { |
| 142 |
$s .= " CURRENT POINTER INCORRECT"; |
| 143 |
} |
| 144 |
$s .= "\n"; |
| 145 |
} |
| 146 |
return $s; |
| 147 |
} |
| 148 |
} |