Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Element | |
0.00% |
0 / 27 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| isMathmlTextIntegration | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| isHtmlIntegration | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| getNoahKey | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getDebugTag | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\RemexHtml\TreeBuilder; |
| 5 | |
| 6 | use Wikimedia\RemexHtml\HTMLData; |
| 7 | use Wikimedia\RemexHtml\PropGuard; |
| 8 | use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 9 | |
| 10 | /** |
| 11 | * Storage for all the state that TreeBuilder needs to associate with each |
| 12 | * element. These objects should be freed once they fall out of TreeBuilder's |
| 13 | * data structures (the stack etc.). |
| 14 | * |
| 15 | * These objects are also used to communicate information about elements with |
| 16 | * downstream clients. |
| 17 | */ |
| 18 | class Element implements FormattingElement { |
| 19 | use PropGuard; |
| 20 | |
| 21 | /** |
| 22 | * The namespace. This will be the HTML namespace for elements that are not |
| 23 | * in foreign content, even if there is a prefix. |
| 24 | * @var string |
| 25 | */ |
| 26 | public $namespace; |
| 27 | |
| 28 | /** |
| 29 | * The tag name, usually exactly as it appeared in the source document. |
| 30 | * This is not strictly a local name, since it still contains a colon for |
| 31 | * prefixed elements. In foreign content, it is effectively a local name. |
| 32 | * It is suitable for use as a serialized tag name. |
| 33 | * @var string |
| 34 | */ |
| 35 | public $name; |
| 36 | |
| 37 | /** |
| 38 | * This is an internal designation of the type of the element, which is |
| 39 | * equal to the tag name when the element is in the HTML namespace, and is |
| 40 | * some other string when the element is not in the HTML namespace. |
| 41 | * @var string |
| 42 | */ |
| 43 | public $htmlName; |
| 44 | |
| 45 | public Attributes $attrs; |
| 46 | |
| 47 | /** |
| 48 | * This is true if the element was created by the TreeBuilder either as a |
| 49 | * fragment context node, or as a synthetic <html> element to be used as |
| 50 | * the top-level element in fragment parsing. |
| 51 | * @var bool |
| 52 | */ |
| 53 | public $isVirtual; |
| 54 | |
| 55 | /** |
| 56 | * Internal to CachingStack. A link in the scope list. |
| 57 | * @var self|null |
| 58 | */ |
| 59 | public $nextEltInScope; |
| 60 | |
| 61 | /** |
| 62 | * Internal to CachingStack and SimpleStack. The current stack index, or |
| 63 | * null if the element is not in the stack. |
| 64 | * @var int|null |
| 65 | */ |
| 66 | public $stackIndex; |
| 67 | |
| 68 | /** |
| 69 | * Internal to ActiveFormattingElements. |
| 70 | * @var Element|Marker|null |
| 71 | */ |
| 72 | public $prevAFE; |
| 73 | |
| 74 | /** |
| 75 | * Internal to ActiveFormattingElements. |
| 76 | * @var Element|Marker|null |
| 77 | */ |
| 78 | public $nextAFE; |
| 79 | |
| 80 | /** |
| 81 | * Internal to ActiveFormattingElements. |
| 82 | * @var FormattingElement|null |
| 83 | */ |
| 84 | public $nextNoah; |
| 85 | |
| 86 | /** |
| 87 | * The cache for getNoahKey() |
| 88 | * @var string|null |
| 89 | */ |
| 90 | private $noahKey; |
| 91 | |
| 92 | /** |
| 93 | * This member variable can be written to by the TreeHandler, to store any |
| 94 | * state associated with the element (such as a DOM node). It is not used |
| 95 | * by TreeBuilder. |
| 96 | * @var mixed |
| 97 | */ |
| 98 | public $userData; |
| 99 | |
| 100 | /** |
| 101 | * A unique ID which identifies the element |
| 102 | * @var int |
| 103 | */ |
| 104 | public $uid; |
| 105 | |
| 106 | /** |
| 107 | * The next unique ID to be used |
| 108 | * |
| 109 | * @var int |
| 110 | */ |
| 111 | private static $nextUid = 1; |
| 112 | |
| 113 | /** |
| 114 | * The element types in the MathML namespace which are MathML text |
| 115 | * integration points. |
| 116 | */ |
| 117 | private const MATHML_INTEGRATION = [ |
| 118 | 'mi' => true, |
| 119 | 'mo' => true, |
| 120 | 'mn' => true, |
| 121 | 'ms' => true, |
| 122 | 'mtext' => true |
| 123 | ]; |
| 124 | |
| 125 | /** |
| 126 | * The element types in the SVG namespace which are SVG text integration |
| 127 | * points. |
| 128 | */ |
| 129 | private const SVG_HTML_INTEGRATION = [ |
| 130 | 'foreignObject' => true, |
| 131 | 'desc' => true, |
| 132 | 'title' => true |
| 133 | ]; |
| 134 | |
| 135 | /** |
| 136 | * Constructor. |
| 137 | * |
| 138 | * @param string $namespace |
| 139 | * @param string $name |
| 140 | * @param Attributes $attrs |
| 141 | */ |
| 142 | public function __construct( $namespace, $name, Attributes $attrs ) { |
| 143 | $this->namespace = $namespace; |
| 144 | $this->name = $name; |
| 145 | if ( $namespace === HTMLData::NS_HTML ) { |
| 146 | $this->htmlName = $name; |
| 147 | } elseif ( $namespace === HTMLData::NS_MATHML ) { |
| 148 | $this->htmlName = "mathml $name"; |
| 149 | } elseif ( $namespace === HTMLData::NS_SVG ) { |
| 150 | $this->htmlName = "svg $name"; |
| 151 | } else { |
| 152 | $this->htmlName = "$namespace $name"; |
| 153 | } |
| 154 | $this->attrs = $attrs; |
| 155 | $this->uid = self::$nextUid++; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Is the element a MathML text integration point? |
| 160 | * |
| 161 | * @return bool |
| 162 | */ |
| 163 | public function isMathmlTextIntegration() { |
| 164 | return $this->namespace === HTMLData::NS_MATHML |
| 165 | && isset( self::MATHML_INTEGRATION[$this->name] ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Is the element an HTML integration point? |
| 170 | * @return bool |
| 171 | */ |
| 172 | public function isHtmlIntegration() { |
| 173 | if ( $this->namespace === HTMLData::NS_MATHML ) { |
| 174 | if ( isset( $this->attrs['encoding'] ) ) { |
| 175 | $encoding = strtolower( $this->attrs['encoding'] ); |
| 176 | return $encoding === 'text/html' || $encoding === 'application/xhtml+xml'; |
| 177 | } else { |
| 178 | return false; |
| 179 | } |
| 180 | } elseif ( $this->namespace === HTMLData::NS_SVG ) { |
| 181 | return isset( self::SVG_HTML_INTEGRATION[$this->name] ); |
| 182 | } else { |
| 183 | return false; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get a string key for the Noah's Ark algorithm |
| 189 | * |
| 190 | * @return string |
| 191 | */ |
| 192 | public function getNoahKey() { |
| 193 | if ( $this->noahKey === null ) { |
| 194 | $attrs = $this->attrs->getValues(); |
| 195 | ksort( $attrs ); |
| 196 | $this->noahKey = serialize( [ $this->htmlName, $attrs ] ); |
| 197 | } |
| 198 | return $this->noahKey; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Get a string identifying the element, for use in debugging. |
| 203 | * @return string |
| 204 | */ |
| 205 | public function getDebugTag() { |
| 206 | return $this->htmlName . '#' . $this->uid; |
| 207 | } |
| 208 | } |