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