| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\NodeData; |
| 5 |
|
| 6 |
use Wikimedia\JsonCodec\Hint; |
| 7 |
use Wikimedia\JsonCodec\JsonCodecable; |
| 8 |
use Wikimedia\JsonCodec\JsonCodecableTrait; |
| 9 |
use Wikimedia\Parsoid\Core\DomSourceRange; |
| 10 |
use Wikimedia\Parsoid\Tokens\SourceRange; |
| 11 |
use Wikimedia\Parsoid\Tokens\Token; |
| 12 |
use Wikimedia\Parsoid\Utils\Utils; |
| 13 |
|
| 14 |
/** |
| 15 |
* Parsoid data for a DOM node. Managed by DOMDataUtils::get/setDataParsoid(). |
| 16 |
* |
| 17 |
* To reduce memory usage, most the properties need to be undeclared, but we can |
| 18 |
* use the property declarations below to satisfy phan and to provide type |
| 19 |
* information to IDEs. |
| 20 |
* |
| 21 |
* TODO: Declaring common properties would be beneficial for memory usage, but |
| 22 |
* changes the JSON serialized output and breaks tests. |
| 23 |
* |
| 24 |
* == Miscellaneous / General properties == |
| 25 |
* |
| 26 |
* Used to emit original wikitext in some scenarios (entities, placeholder spans) |
| 27 |
* Porting note: this can be '0', handle emptiness checks with care |
| 28 |
* @property string|null $src |
| 29 |
* |
| 30 |
* Tag widths for all tokens. |
| 31 |
* Temporarily present in data-parsoid, but not in final DOM output. |
| 32 |
* @see ComputeDSR::computeNodeDSR() |
| 33 |
* @property SourceRange|null $tsr |
| 34 |
* |
| 35 |
* Wikitext source ranges that generated this DOM node. |
| 36 |
* In the form [ start-offset, end-offset ] or |
| 37 |
* [ start-offset, end-offset, start-tag-width, end-tag-width ]. |
| 38 |
* |
| 39 |
* Consider input wikitext: `abcdef ''foo'' something else`. Let us look at the `''foo''` |
| 40 |
* part of the input. It generates `<i data-parsoid='{"dsr":[7,14,2,2]}'>foo</i>` . The dsr |
| 41 |
* property of the data-parsoid attribute of this i-tag tells us the following. This HTML node |
| 42 |
* maps to input wikitext substring 7..14. The opening tag <i> was 2 characters wide in wikitext |
| 43 |
* and the closing tag </i> was also 2 characters wide in wikitext. |
| 44 |
* @property DomSourceRange|null $dsr |
| 45 |
* |
| 46 |
* Denotes special syntax. Possible values: |
| 47 |
* - 'html' for html tags. Ex: `<div>foo</div>` |
| 48 |
* - 'row' for dt/dd that show on the same line. Ex: `;a:b` (but not `;a\n:b`) |
| 49 |
* - 'piped' for piped wikilinks with explicit content Ex: `[[Foo|bar]]` (but not `[[Foo]]`) |
| 50 |
* - 'magiclink', 'url' - legacy, not used anymore |
| 51 |
* @property string|null $stx |
| 52 |
* |
| 53 |
* Template parameter infos produced by TemplateHandler. After unserialization, |
| 54 |
* the objects are not fully populated. |
| 55 |
* @property ParamInfo[][]|null $pi |
| 56 |
* |
| 57 |
* Expanded template HTML (native preprocessor only). |
| 58 |
* @property string|null $html |
| 59 |
* |
| 60 |
* On mw:Entity spans this is set to the decoded entity value. |
| 61 |
* @property string|null $srcContent |
| 62 |
* |
| 63 |
* An array of associative arrays describing image rendering options, attached |
| 64 |
* to the image container (span or figure). |
| 65 |
* - ck: Canonical key for the image option. |
| 66 |
* - ak: Aliased key. |
| 67 |
* @property array|null $optList |
| 68 |
* |
| 69 |
* Rendered attributes (shadow info). The key is the attribute name. The value |
| 70 |
* is documented as "mixed" but seems to be coerced to string in |
| 71 |
* Sanitizer::sanitizeTagAttrs(). |
| 72 |
* @property array|null $a Rendered attributes |
| 73 |
* |
| 74 |
* Source attributes (shadow info). The key is the attribute name. The value |
| 75 |
* is documented as "mixed" but may possibly be a nullable string. |
| 76 |
* @property array|null $sa Source attributes |
| 77 |
* |
| 78 |
* FIXME never written |
| 79 |
* @property bool|null $strippedNL |
| 80 |
* |
| 81 |
* The number of extra dashes in the source of an hr |
| 82 |
* @property int|null $extra_dashes |
| 83 |
* |
| 84 |
* The complete text of a double-underscore behavior switch |
| 85 |
* @property string|null $magicSrc |
| 86 |
* |
| 87 |
* True if the input heading element had an id attribute, preventing automatic |
| 88 |
* assignment of a new id attribute. |
| 89 |
* @property bool|null $reusedId |
| 90 |
* |
| 91 |
* FIXME: Get rid of this property and the code that reads it after content |
| 92 |
* version 2.2.0 has expired from caches. |
| 93 |
* @property mixed $liHackSrc |
| 94 |
* |
| 95 |
* The link token associated with a redirect |
| 96 |
* @property Token|null $linkTk |
| 97 |
* |
| 98 |
* On a meta mw:EmptyLine, the associated comment and whitespace tokens. Used |
| 99 |
* in this sense by both the tokenizer and TokenStreamPatcher. |
| 100 |
* @property array $tokens |
| 101 |
* |
| 102 |
* This is set to "extlink" on auto URL (external hotlink) image links. |
| 103 |
* @property string|null $type |
| 104 |
* |
| 105 |
* On a meta mw:Placeholder/StrippedTag, this is the name of the stripped tag. |
| 106 |
* @property string|null $name |
| 107 |
* |
| 108 |
* This is set on image containers in which a template expands to multiple |
| 109 |
* image parameters. It is converted to a typeof attribute later in the same |
| 110 |
* function, so it's unclear why it needs to persist in data-parsoid. |
| 111 |
* @property bool|null $uneditable |
| 112 |
* |
| 113 |
* == WrapTemplates == |
| 114 |
* |
| 115 |
* The wikitext source which was not included in a template wrapper. |
| 116 |
* @property string|null $unwrappedWT |
| 117 |
* |
| 118 |
* The token or DOM node name, optionally suffixed with the syntax name from |
| 119 |
* $this->stx, of the first node within the encapsulated content. |
| 120 |
* @property string|null $firstWikitextNode |
| 121 |
* |
| 122 |
* == Extensions == |
| 123 |
* |
| 124 |
* Offsets of opening and closing tags for extension tags, in the form |
| 125 |
* [ opening tag start , closing tag end, opening tag width, closing tag width ] |
| 126 |
* Temporarily present in data-parsoid, but not in final DOM output. |
| 127 |
* @property DomSourceRange|null $extTagOffsets |
| 128 |
* |
| 129 |
* This is true on the extension output wrapper if the extension input wikitext |
| 130 |
* was an empty string. Consumed by <references/>. |
| 131 |
* @property bool $empty |
| 132 |
* |
| 133 |
* The reference group. This is attached to the <ol> or its wrapper <div>, |
| 134 |
* redundantly with the data-mw-group attribute on the <ol>. It is produced by |
| 135 |
* the extension's sourceToDom() and consumed by wtPostprocess(). |
| 136 |
* @property string $group |
| 137 |
* |
| 138 |
* == Annotations == |
| 139 |
* This is used on annotation meta tags to indicate that the corresponding |
| 140 |
* tag has been moved compared to it's initial location defined by wikitext. |
| 141 |
* An annotation tag can be moved either as the result of fostering or as |
| 142 |
* the result of annotation range extension to enclose a contiguous DOM |
| 143 |
* forest. |
| 144 |
* @property bool|null $wasMoved |
| 145 |
* |
| 146 |
* == HTML tags == |
| 147 |
* |
| 148 |
* Are void tags self-closed? (Ex: `<br>` vs `<br />`) |
| 149 |
* @property bool|null $selfClose |
| 150 |
* |
| 151 |
* Void tags that are not self-closed (Ex: `<br>`) |
| 152 |
* @property bool|null $noClose |
| 153 |
* |
| 154 |
* Whether this start HTML tag has no corresponding wikitext and was auto-inserted by a token |
| 155 |
* handler to generate well-formed html. Usually happens when a token handler fixes up misnesting. |
| 156 |
* @property bool|null $autoInsertedStartToken |
| 157 |
* |
| 158 |
* Whether this end HTML tag has no corresponding wikitext and was auto-inserted by a token |
| 159 |
* handler to generate well-formed html. Usually happens when a token handler fixes up misnesting. |
| 160 |
* @property bool|null $autoInsertedEndToken |
| 161 |
* |
| 162 |
* Whether this start HTML tag has no corresponding wikitext and was auto-inserted to generate |
| 163 |
* well-formed html. Usually happens when treebuilder fixes up badly nested HTML. |
| 164 |
* @property bool|null $autoInsertedStart |
| 165 |
* |
| 166 |
* Whether this end HTML tag has no corresponding wikitext and was auto-inserted to generate |
| 167 |
* well-formed html. Ex: `<tr>`, `<th>`, `<td>`, `<li>`, etc. that have no explicit closing |
| 168 |
* markup. Or, html tags that aren't closed. |
| 169 |
* @property bool|null $autoInsertedEnd |
| 170 |
* |
| 171 |
* Source tag name for HTML tags. Records case variations (`<div>` vs `<DiV>` vs `<DIV>`). |
| 172 |
* @property string|null $srcTagName |
| 173 |
* |
| 174 |
* UnpackDomFragments sets this on misnested elements |
| 175 |
* @property bool|null $misnested |
| 176 |
* |
| 177 |
* This is set by MarkFosteredContent to indicate fostered content and content |
| 178 |
* wrappers. |
| 179 |
* @property bool|null $fostered |
| 180 |
* |
| 181 |
* == Links == |
| 182 |
* |
| 183 |
* Link trail source (Ex: the "l" in `[[Foo]]l`) |
| 184 |
* Porting note: this can be '0', handle emptiness checks with care |
| 185 |
* @property string|null $tail |
| 186 |
* |
| 187 |
* Link prefix source |
| 188 |
* Porting note: this can be '0', handle emptiness checks with care |
| 189 |
* @property string|null $prefix |
| 190 |
* |
| 191 |
* True if the link was a pipetrick (`[[Foo|]]`). |
| 192 |
* @note This will likely be removed soon since this should not show up in saved wikitext since |
| 193 |
* this is a pre-save transformation trick. |
| 194 |
* @property bool|null $pipeTrick |
| 195 |
* |
| 196 |
* Did the link use interwiki syntax? |
| 197 |
* Probably redundant with the rel=mw:WikiLink/Interwiki |
| 198 |
* @property bool|null $isIW |
| 199 |
* |
| 200 |
* == Tables == |
| 201 |
* |
| 202 |
* Source for start-text separators in table wikitext. |
| 203 |
* @property string|null $startTagSrc |
| 204 |
* |
| 205 |
* Source for end-text separators in table wikitext. |
| 206 |
* @property string|null $endTagSrc |
| 207 |
* |
| 208 |
* Source for attribute-text separators in table wikitext. |
| 209 |
* @property string|null $attrSepSrc |
| 210 |
* |
| 211 |
* 'row' for td/th cells that show up on the same line, null otherwise |
| 212 |
* @property string|null $stx_v |
| 213 |
* |
| 214 |
* == Language variant token properties == |
| 215 |
* |
| 216 |
* @property array|null $flags Flags with their human-readable names |
| 217 |
* @property array|null $variants The variant names |
| 218 |
* @property array|null $original Original flags |
| 219 |
* @property array|null $flagSp Spaces around flags, uncompressed |
| 220 |
* |
| 221 |
* An array of associative arrays describing the parts of the variant rule. |
| 222 |
* - text: (string) The text |
| 223 |
* - semi: (bool) A semicolon marker |
| 224 |
* - sp: (array|string) An array of strings containing spaces |
| 225 |
* - oneway: (bool) A one-way rule definition |
| 226 |
* - twoway: (bool) A two-way rule definition |
| 227 |
* - from: (array) An associative array: |
| 228 |
* - tokens: (array) A token array |
| 229 |
* - srcOffsets: SourceRange |
| 230 |
* - to: (array) An associative array same as "from" |
| 231 |
* - lang: (string) |
| 232 |
* @property array|null $texts |
| 233 |
* |
| 234 |
* == Language variant data-parsoid properties == |
| 235 |
* |
| 236 |
* @property array|null $flSp Spaces around flags, compressed with compressSpArray(). |
| 237 |
* @property array|null $tSp Spaces around texts, compressed with compressSpArray(). |
| 238 |
* @property array|null $fl Original flags, copied from $this->original on the token. |
| 239 |
*/ |
| 240 |
#[\AllowDynamicProperties] |
| 241 |
class DataParsoid implements JsonCodecable { |
| 242 |
use JsonCodecableTrait; |
| 243 |
|
| 244 |
/** |
| 245 |
* Holds a number of transient properties in the wt->html pipeline to pass information between |
| 246 |
* stages. Dropped before serialization. |
| 247 |
* @var TempData|null |
| 248 |
*/ |
| 249 |
public $tmp; |
| 250 |
|
| 251 |
/** |
| 252 |
* Deeply clone this object |
| 253 |
* |
| 254 |
* @return DataParsoid |
| 255 |
*/ |
| 256 |
public function clone(): self { |
| 257 |
$dp = clone $this; |
| 258 |
// Properties that need deep cloning |
| 259 |
if ( isset( $dp->tmp ) ) { |
| 260 |
$dp->tmp = Utils::clone( $dp->tmp ); |
| 261 |
} |
| 262 |
if ( isset( $dp->linkTk ) ) { |
| 263 |
$dp->linkTk = Utils::clone( $dp->linkTk ); |
| 264 |
} |
| 265 |
if ( isset( $dp->tokens ) ) { |
| 266 |
$dp->tokens = Utils::clone( $dp->tokens ); |
| 267 |
} |
| 268 |
|
| 269 |
// Properties that need shallow cloning |
| 270 |
if ( isset( $dp->tsr ) ) { |
| 271 |
$dp->tsr = clone $dp->tsr; |
| 272 |
} |
| 273 |
if ( isset( $dp->dsr ) ) { |
| 274 |
$dp->dsr = clone $dp->dsr; |
| 275 |
} |
| 276 |
if ( isset( $dp->extTagOffsets ) ) { |
| 277 |
$dp->extTagOffsets = clone $dp->extTagOffsets; |
| 278 |
} |
| 279 |
|
| 280 |
// The remaining properties were sufficiently handled by the clone operator |
| 281 |
return $dp; |
| 282 |
} |
| 283 |
|
| 284 |
public function isModified(): bool { |
| 285 |
// NOTE: strict equality will not work in this comparison |
| 286 |
// @phan-suppress-next-line PhanPluginComparisonObjectEqualityNotStrict |
| 287 |
return $this != new self; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Get a lazy-initialized object to which temporary properties can be written. |
| 292 |
* @return TempData |
| 293 |
*/ |
| 294 |
public function getTemp(): TempData { |
| 295 |
// tmp can be unset despite being declared |
| 296 |
$this->tmp ??= new TempData(); |
| 297 |
return $this->tmp; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Check whether a bit is set in $this->tmp->bits |
| 302 |
* |
| 303 |
* @param int $flag |
| 304 |
* @return bool |
| 305 |
*/ |
| 306 |
public function getTempFlag( $flag ): bool { |
| 307 |
return isset( $this->tmp ) && ( $this->tmp->bits & $flag ); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Set a bit in $this->tmp->bits |
| 312 |
* |
| 313 |
* @param int $flag |
| 314 |
* @param bool $value |
| 315 |
*/ |
| 316 |
public function setTempFlag( $flag, $value = true ): void { |
| 317 |
if ( $value ) { |
| 318 |
if ( !isset( $this->tmp ) ) { |
| 319 |
$tmp = new TempData; |
| 320 |
$tmp->bits = $flag; |
| 321 |
$this->tmp = $tmp; |
| 322 |
} else { |
| 323 |
$this->tmp->bits |= $flag; |
| 324 |
} |
| 325 |
} elseif ( isset( $this->tmp ) ) { |
| 326 |
$this->tmp->bits &= ~$flag; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/** @inheritDoc */ |
| 331 |
public function toJsonArray(): array { |
| 332 |
static $clearNullsFrom = [ |
| 333 |
'dsr', 'tsr', 'extTagOffsets', |
| 334 |
]; |
| 335 |
$result = (array)$this; |
| 336 |
unset( $result['tmp'] ); |
| 337 |
// Conciseness: don't include `null` values from certain properties. |
| 338 |
foreach ( $clearNullsFrom as $prop ) { |
| 339 |
if ( !isset( $result[$prop] ) ) { |
| 340 |
unset( $result[$prop] ); |
| 341 |
} |
| 342 |
} |
| 343 |
return $result; |
| 344 |
} |
| 345 |
|
| 346 |
/** @inheritDoc */ |
| 347 |
public static function jsonClassHintFor( string $keyname ) { |
| 348 |
static $hints = null; |
| 349 |
if ( $hints === null ) { |
| 350 |
$dsr = Hint::build( DomSourceRange::class, Hint::USE_SQUARE ); |
| 351 |
$sr = Hint::build( SourceRange::class, Hint::USE_SQUARE ); |
| 352 |
$hints = [ |
| 353 |
'dsr' => $dsr, |
| 354 |
'extTagOffsets' => $dsr, |
| 355 |
'tsr' => $sr, |
| 356 |
'pi' => Hint::build( ParamInfo::class, Hint::LIST, Hint::LIST ), |
| 357 |
'linkTk' => Token::class, |
| 358 |
]; |
| 359 |
} |
| 360 |
return $hints[$keyname] ?? null; |
| 361 |
} |
| 362 |
|
| 363 |
/** @inheritDoc */ |
| 364 |
public static function newFromJsonArray( array $json ): DataParsoid { |
| 365 |
$dp = new DataParsoid; |
| 366 |
foreach ( $json as $key => $value ) { |
| 367 |
switch ( $key ) { |
| 368 |
case 'dsr': |
| 369 |
case 'extTagOffsets': |
| 370 |
case 'tsr': |
| 371 |
// For backward compatibility, leave these unset if null. |
| 372 |
if ( $value !== null ) { |
| 373 |
$dp->$key = $value; |
| 374 |
} |
| 375 |
break; |
| 376 |
case 'tmp': |
| 377 |
// This isn't serialized, but we can deserialize it |
| 378 |
// for tests. |
| 379 |
$tmp = new TempData; |
| 380 |
foreach ( $value as $key2 => $value2 ) { |
| 381 |
$tmp->$key2 = $value2; |
| 382 |
} |
| 383 |
$dp->$key = $tmp; |
| 384 |
break; |
| 385 |
default: |
| 386 |
$dp->$key = $value; |
| 387 |
break; |
| 388 |
} |
| 389 |
} |
| 390 |
return $dp; |
| 391 |
} |
| 392 |
} |