| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Utils; |
| 5 |
|
| 6 |
use DOMException; |
| 7 |
use Wikimedia\Assert\UnreachableException; |
| 8 |
use Wikimedia\Bcp47Code\Bcp47Code; |
| 9 |
use Wikimedia\Parsoid\Config\Env; |
| 10 |
use Wikimedia\Parsoid\DOM\Comment; |
| 11 |
use Wikimedia\Parsoid\DOM\Document; |
| 12 |
use Wikimedia\Parsoid\DOM\DocumentFragment; |
| 13 |
use Wikimedia\Parsoid\DOM\Element; |
| 14 |
use Wikimedia\Parsoid\DOM\Node; |
| 15 |
use Wikimedia\Parsoid\DOM\Text; |
| 16 |
use Wikimedia\Parsoid\Ext\ExtensionTagHandler; |
| 17 |
use Wikimedia\Parsoid\NodeData\DataParsoid; |
| 18 |
use Wikimedia\Parsoid\NodeData\I18nInfo; |
| 19 |
use Wikimedia\Parsoid\NodeData\TempData; |
| 20 |
use Wikimedia\Parsoid\Tokens\CommentTk; |
| 21 |
use Wikimedia\Parsoid\Wikitext\Consts; |
| 22 |
use Wikimedia\Parsoid\Wt2Html\Frame; |
| 23 |
|
| 24 |
/** |
| 25 |
* These utilites pertain to querying / extracting / modifying wikitext information from the DOM. |
| 26 |
*/ |
| 27 |
class WTUtils { |
| 28 |
private const FIRST_ENCAP_REGEXP = |
| 29 |
'#(?:^|\s)(mw:(?:Transclusion|Param|LanguageVariant|Extension(/\S+)))(?=$|\s)#D'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Regex corresponding to FIRST_ENCAP_REGEXP, but excluding extensions. If FIRST_ENCAP_REGEXP is |
| 33 |
* updated, this one should be as well. |
| 34 |
*/ |
| 35 |
private const NON_EXTENSION_ENCAP_REGEXP = |
| 36 |
'#(?:^|\s)(mw:(?:Transclusion|Param|LanguageVariant))(?=$|\s)#D'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Regexp for checking marker metas typeofs representing |
| 40 |
* transclusion markup or template param markup. |
| 41 |
*/ |
| 42 |
private const TPL_META_TYPE_REGEXP = '#^mw:(?:Transclusion|Param)(?:/End)?$#D'; |
| 43 |
|
| 44 |
/** |
| 45 |
* Regexp for checking marker metas typeofs representing |
| 46 |
* annotation markup |
| 47 |
*/ |
| 48 |
public const ANNOTATION_META_TYPE_REGEXP = '#^mw:(?:Annotation/([\w\d]+))(?:/End)?$#uD'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Check whether a node's data-parsoid object includes |
| 52 |
* an indicator that the original wikitext was a literal |
| 53 |
* HTML element (like table or p) |
| 54 |
* |
| 55 |
* @param DataParsoid $dp |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public static function hasLiteralHTMLMarker( DataParsoid $dp ): bool { |
| 59 |
return isset( $dp->stx ) && $dp->stx === 'html'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Run a node through {@link #hasLiteralHTMLMarker}. |
| 64 |
* @param ?Node $node |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public static function isLiteralHTMLNode( ?Node $node ): bool { |
| 68 |
return $node instanceof Element && |
| 69 |
self::hasLiteralHTMLMarker( DOMDataUtils::getDataParsoid( $node ) ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @param Node $node |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
public static function isZeroWidthWikitextElt( Node $node ): bool { |
| 77 |
return isset( Consts::$ZeroWidthWikitextTags[DOMCompat::nodeName( $node )] ) && |
| 78 |
!self::isLiteralHTMLNode( $node ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Is `$node` a block node that is also visible in wikitext? |
| 83 |
* An example of an invisible block node is a `<p>`-tag that |
| 84 |
* Parsoid generated, or a `<ul>`, `<ol>` tag. |
| 85 |
* |
| 86 |
* @param Node $node |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
public static function isBlockNodeWithVisibleWT( Node $node ): bool { |
| 90 |
return DOMUtils::isWikitextBlockNode( $node ) && |
| 91 |
!self::isZeroWidthWikitextElt( $node ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Helper functions to detect when an A-$node uses [[..]]/[..]/... style |
| 96 |
* syntax (for wikilinks, ext links, url links). rel-type is not sufficient |
| 97 |
* anymore since mw:ExtLink is used for all the three link syntaxes. |
| 98 |
* |
| 99 |
* @param Element $node |
| 100 |
* @return bool |
| 101 |
*/ |
| 102 |
public static function isATagFromWikiLinkSyntax( Element $node ): bool { |
| 103 |
if ( DOMCompat::nodeName( $node ) !== 'a' ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
$dp = DOMDataUtils::getDataParsoid( $node ); |
| 108 |
return DOMUtils::hasRel( $node, 'mw:WikiLink' ) || |
| 109 |
( isset( $dp->stx ) && $dp->stx !== "url" && $dp->stx !== "magiclink" ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Helper function to detect when an A-node uses ext-link syntax. |
| 114 |
* rel attribute is not sufficient anymore since mw:ExtLink is used for |
| 115 |
* multiple link types |
| 116 |
* |
| 117 |
* @param Element $node |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
public static function isATagFromExtLinkSyntax( Element $node ): bool { |
| 121 |
if ( DOMCompat::nodeName( $node ) !== 'a' ) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
$dp = DOMDataUtils::getDataParsoid( $node ); |
| 126 |
return DOMUtils::hasRel( $node, 'mw:ExtLink' ) && |
| 127 |
( !isset( $dp->stx ) || ( $dp->stx !== "url" && $dp->stx !== "magiclink" ) ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Helper function to detect when an A-node uses url-link syntax. |
| 132 |
* rel attribute is not sufficient anymore since mw:ExtLink is used for |
| 133 |
* multiple link types |
| 134 |
* |
| 135 |
* @param Element $node |
| 136 |
* @return bool |
| 137 |
*/ |
| 138 |
public static function isATagFromURLLinkSyntax( Element $node ): bool { |
| 139 |
if ( DOMCompat::nodeName( $node ) !== 'a' ) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
$dp = DOMDataUtils::getDataParsoid( $node ); |
| 144 |
return DOMUtils::hasRel( $node, 'mw:ExtLink' ) && |
| 145 |
isset( $dp->stx ) && $dp->stx === "url"; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Helper function to detect when an A-node uses magic-link syntax. |
| 150 |
* rel attribute is not sufficient anymore since mw:ExtLink is used for |
| 151 |
* multiple link types |
| 152 |
* |
| 153 |
* @param Element $node |
| 154 |
* @return bool |
| 155 |
*/ |
| 156 |
public static function isATagFromMagicLinkSyntax( Element $node ): bool { |
| 157 |
if ( DOMCompat::nodeName( $node ) !== 'a' ) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
$dp = DOMDataUtils::getDataParsoid( $node ); |
| 162 |
return DOMUtils::hasRel( $node, 'mw:ExtLink' ) && |
| 163 |
isset( $dp->stx ) && $dp->stx === 'magiclink'; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Check whether a node's typeof indicates that it is a template expansion. |
| 168 |
* |
| 169 |
* @param Element $node |
| 170 |
* @return ?string The matched type, or null if no match. |
| 171 |
*/ |
| 172 |
public static function matchTplType( Element $node ): ?string { |
| 173 |
return DOMUtils::matchTypeOf( $node, self::TPL_META_TYPE_REGEXP ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Check whether a typeof indicates that it signifies an |
| 178 |
* expanded attribute. |
| 179 |
* |
| 180 |
* @param Element $node |
| 181 |
* @return bool |
| 182 |
*/ |
| 183 |
public static function hasExpandedAttrsType( Element $node ): bool { |
| 184 |
return DOMUtils::matchTypeOf( $node, '/^mw:ExpandedAttrs(\/\S+)*$/' ) !== null; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Check whether a node is a meta tag that signifies a template expansion. |
| 189 |
* |
| 190 |
* @param Node $node |
| 191 |
* @return bool |
| 192 |
*/ |
| 193 |
public static function isTplMarkerMeta( Node $node ): bool { |
| 194 |
return DOMUtils::matchNameAndTypeOf( $node, 'meta', self::TPL_META_TYPE_REGEXP ) !== null; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Check whether a node is a meta signifying the start of a template expansion. |
| 199 |
* |
| 200 |
* @param Node $node |
| 201 |
* @return bool |
| 202 |
*/ |
| 203 |
public static function isTplStartMarkerMeta( Node $node ): bool { |
| 204 |
$t = DOMUtils::matchNameAndTypeOf( $node, 'meta', self::TPL_META_TYPE_REGEXP ); |
| 205 |
return $t !== null && !str_ends_with( $t, '/End' ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Check whether a node is a meta signifying the end of a template expansion. |
| 210 |
* |
| 211 |
* @param Node $node |
| 212 |
* @return bool |
| 213 |
*/ |
| 214 |
public static function isTplEndMarkerMeta( Node $node ): bool { |
| 215 |
$t = DOMUtils::matchNameAndTypeOf( $node, 'meta', self::TPL_META_TYPE_REGEXP ); |
| 216 |
return $t !== null && str_ends_with( $t, '/End' ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Find the first wrapper element of encapsulated content. |
| 221 |
* @param Node $node |
| 222 |
* @return Element|null |
| 223 |
*/ |
| 224 |
public static function findFirstEncapsulationWrapperNode( Node $node ): ?Element { |
| 225 |
if ( !self::isEncapsulatedDOMForestRoot( $node ) ) { |
| 226 |
return null; |
| 227 |
} |
| 228 |
/** @var Element $node */ |
| 229 |
DOMUtils::assertElt( $node ); |
| 230 |
|
| 231 |
$about = DOMCompat::getAttribute( $node, 'about' ); |
| 232 |
$prev = $node; |
| 233 |
do { |
| 234 |
$node = $prev; |
| 235 |
$prev = DiffDOMUtils::previousNonDeletedSibling( $node ); |
| 236 |
} while ( |
| 237 |
$prev instanceof Element && |
| 238 |
DOMCompat::getAttribute( $prev, 'about' ) === $about |
| 239 |
); |
| 240 |
// NOTE: findFirstEncapsulationWrapperNode can be called by code |
| 241 |
// even before templates have been fully encapsulated everywhere. |
| 242 |
// ProcessTreeBuilderFixups::removeAutoInsertedEmptyTags is the main |
| 243 |
// culprit here and it makes the contract for this helper murky |
| 244 |
// by hiding potential brokenness since this should never return null |
| 245 |
// once all templates have been encapsulated! |
| 246 |
$elt = self::isFirstEncapsulationWrapperNode( $node ) ? $node : null; |
| 247 |
'@phan-var ?Element $elt'; // @var ?Element $elt |
| 248 |
return $elt; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* This tests whether a DOM node is a new node added during an edit session |
| 253 |
* or an existing node from parsed wikitext. |
| 254 |
* |
| 255 |
* As written, this function can only be used on non-template/extension content |
| 256 |
* or on the top-level nodes of template/extension content. This test will |
| 257 |
* return the wrong results on non-top-level $nodes of template/extension content. |
| 258 |
* |
| 259 |
* @param Node $node |
| 260 |
* @return bool |
| 261 |
*/ |
| 262 |
public static function isNewElt( Node $node ): bool { |
| 263 |
// We cannot determine newness on text/comment $nodes. |
| 264 |
if ( !( $node instanceof Element ) ) { |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
// For template/extension content, newness should be |
| 269 |
// checked on the encapsulation wrapper $node. |
| 270 |
$node = self::findFirstEncapsulationWrapperNode( $node ) ?? $node; |
| 271 |
return DOMDataUtils::getDataParsoid( $node )->getTempFlag( TempData::IS_NEW ); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Check whether a pre is caused by indentation in the original wikitext. |
| 276 |
* @param Node $node |
| 277 |
* @return bool |
| 278 |
*/ |
| 279 |
public static function isIndentPre( Node $node ): bool { |
| 280 |
return DOMCompat::nodeName( $node ) === "pre" && !self::isLiteralHTMLNode( $node ); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* @param Node $node |
| 285 |
* @return bool |
| 286 |
*/ |
| 287 |
public static function isInlineMedia( Node $node ): bool { |
| 288 |
return self::isGeneratedFigure( $node ) && |
| 289 |
DOMCompat::nodeName( $node ) !== 'figure'; // span, figure-inline |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* @param Node $node |
| 294 |
* @return bool |
| 295 |
*/ |
| 296 |
public static function isGeneratedFigure( Node $node ): bool { |
| 297 |
// TODO: Remove "Image|Video|Audio" when version 2.4.0 of the content |
| 298 |
// is no longer supported |
| 299 |
return DOMUtils::matchTypeOf( $node, '#^mw:(File|Image|Video|Audio)($|/)#D' ) !== null; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Find how much offset is necessary for the DSR of an |
| 304 |
* indent-originated pre tag. |
| 305 |
* |
| 306 |
* @param Node $textNode |
| 307 |
* @return int |
| 308 |
*/ |
| 309 |
public static function indentPreDSRCorrection( Node $textNode ): int { |
| 310 |
// NOTE: This assumes a text-node and doesn't check that it is one. |
| 311 |
// |
| 312 |
// FIXME: Doesn't handle text nodes that are not direct children of the pre |
| 313 |
if ( self::isIndentPre( $textNode->parentNode ) ) { |
| 314 |
$numNLs = substr_count( $textNode->nodeValue, "\n" ); |
| 315 |
if ( $textNode->parentNode->lastChild === $textNode ) { |
| 316 |
// We dont want the trailing newline of the last child of the pre |
| 317 |
// to contribute a pre-correction since it doesn't add new content |
| 318 |
// in the pre-node after the text |
| 319 |
if ( str_ends_with( $textNode->nodeValue, "\n" ) ) { |
| 320 |
$numNLs--; |
| 321 |
} |
| 322 |
} |
| 323 |
return $numNLs; |
| 324 |
} else { |
| 325 |
return 0; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Check if $node is a root in an encapsulated DOM forest. |
| 331 |
* |
| 332 |
* @param Node $node |
| 333 |
* @return bool |
| 334 |
*/ |
| 335 |
public static function isEncapsulatedDOMForestRoot( Node $node ): bool { |
| 336 |
$about = $node instanceof Element ? |
| 337 |
DOMCompat::getAttribute( $node, 'about' ) : null; |
| 338 |
if ( $about !== null ) { |
| 339 |
// FIXME: Ensure that our DOM spec clarifies this expectation |
| 340 |
return Utils::isParsoidObjectId( $about ); |
| 341 |
} else { |
| 342 |
return false; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Does $node represent a redirect link? |
| 348 |
*/ |
| 349 |
public static function isRedirectLink( ?Node $node ): bool { |
| 350 |
return $node instanceof Element && |
| 351 |
DOMCompat::nodeName( $node ) === 'link' && |
| 352 |
DOMUtils::matchRel( $node, '#\bmw:PageProp/redirect\b#' ) !== null; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Does $node represent a category link? |
| 357 |
*/ |
| 358 |
public static function isCategoryLink( ?Node $node ): bool { |
| 359 |
return $node instanceof Element && |
| 360 |
DOMCompat::nodeName( $node ) === 'link' && |
| 361 |
DOMUtils::matchRel( $node, '#\bmw:PageProp/Category\b#' ) !== null; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Does $node represent a link that is sol-transparent? |
| 366 |
*/ |
| 367 |
public static function isSolTransparentLink( ?Node $node ): bool { |
| 368 |
return $node instanceof Element && |
| 369 |
DOMCompat::nodeName( $node ) === 'link' && |
| 370 |
DOMUtils::matchRel( $node, TokenUtils::SOL_TRANSPARENT_LINK_REGEX ) !== null; |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Check if '$node' emits wikitext that is sol-transparent in wikitext form. |
| 375 |
* This is a test for wikitext that doesn't introduce line breaks. |
| 376 |
* |
| 377 |
* Comment, whitespace text $nodes, category links, redirect links, behavior |
| 378 |
* switches, and include directives currently satisfy this definition. |
| 379 |
* |
| 380 |
* This should come close to matching TokenUtils.isSolTransparent() |
| 381 |
* |
| 382 |
* @param Node $node |
| 383 |
* @return bool |
| 384 |
*/ |
| 385 |
public static function emitsSolTransparentSingleLineWT( Node $node ): bool { |
| 386 |
if ( $node instanceof Text ) { |
| 387 |
// NB: We differ here to meet the nl condition. |
| 388 |
return (bool)preg_match( '/^[ \t]*$/D', $node->nodeValue ); |
| 389 |
} elseif ( self::isRenderingTransparentNode( $node ) ) { |
| 390 |
// NB: The only metas in a DOM should be for behavior switches and |
| 391 |
// include directives, other than explicit HTML meta tags. This |
| 392 |
// differs from our counterpart in Util where ref meta tokens |
| 393 |
// haven't been expanded to spans yet. |
| 394 |
return true; |
| 395 |
} else { |
| 396 |
return false; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* This is the span added to headings to add fallback ids for when legacy |
| 402 |
* and HTML5 ids don't match up. This prevents broken links to legacy ids. |
| 403 |
* |
| 404 |
* @param Node $node |
| 405 |
* @return bool |
| 406 |
*/ |
| 407 |
public static function isFallbackIdSpan( Node $node ): bool { |
| 408 |
return DOMUtils::hasNameAndTypeOf( $node, 'span', 'mw:FallbackId' ); |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* These are primarily 'metadata'-like $nodes that don't show up in output rendering. |
| 413 |
* - In Parsoid output, they are represented by link/meta tags. |
| 414 |
* - In the PHP parser, they are completely stripped from the input early on. |
| 415 |
* Because of this property, these rendering-transparent $nodes are also |
| 416 |
* SOL-transparent for the purposes of parsing behavior. |
| 417 |
* |
| 418 |
* @param Node $node |
| 419 |
* @return bool |
| 420 |
*/ |
| 421 |
public static function isRenderingTransparentNode( Node $node ): bool { |
| 422 |
// FIXME: Can we change this entire thing to |
| 423 |
// $node instanceof Comment || |
| 424 |
// DOMUtils::getDataParsoid($node).stx !== 'html' && |
| 425 |
// (DOMCompat::nodeName($node) === 'meta' || DOMCompat::nodeName($node) === 'link') |
| 426 |
// |
| 427 |
return $node instanceof Comment || |
| 428 |
self::isSolTransparentLink( $node ) || ( |
| 429 |
// Catch-all for everything else. |
| 430 |
$node instanceof Element && |
| 431 |
DOMCompat::nodeName( $node ) === 'meta' && |
| 432 |
!self::isMarkerAnnotation( $node ) && |
| 433 |
( DOMDataUtils::getDataParsoid( $node )->stx ?? '' ) !== 'html' |
| 434 |
) || self::isFallbackIdSpan( $node ); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Is $node nested inside a table tag that uses HTML instead of native |
| 439 |
* wikitext? |
| 440 |
* |
| 441 |
* @param Node $node |
| 442 |
* @return bool |
| 443 |
*/ |
| 444 |
public static function inHTMLTableTag( Node $node ): bool { |
| 445 |
$p = $node->parentNode; |
| 446 |
while ( DOMUtils::isTableTag( $p ) ) { |
| 447 |
if ( self::isLiteralHTMLNode( $p ) ) { |
| 448 |
return true; |
| 449 |
} elseif ( DOMCompat::nodeName( $p ) === 'table' ) { |
| 450 |
// Don't cross <table> boundaries |
| 451 |
return false; |
| 452 |
} |
| 453 |
$p = $p->parentNode; |
| 454 |
} |
| 455 |
|
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* Is $node the first wrapper element of encapsulated content? |
| 461 |
* |
| 462 |
* @param Node $node |
| 463 |
* @return bool |
| 464 |
*/ |
| 465 |
public static function isFirstEncapsulationWrapperNode( Node $node ): bool { |
| 466 |
return DOMUtils::matchTypeOf( $node, self::FIRST_ENCAP_REGEXP ) !== null; |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Is $node the first wrapper element of extension content? |
| 471 |
* |
| 472 |
* @param Node $node |
| 473 |
* @return bool |
| 474 |
*/ |
| 475 |
public static function isFirstExtensionWrapperNode( Node $node ): bool { |
| 476 |
return DOMUtils::matchTypeOf( $node, "#mw:Extension/#" ) !== null; |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* Checks whether a first encapsulation wrapper node is encapsulating an extension |
| 481 |
* that outputs MediaWiki Core DOM Spec HTML (https://www.mediawiki.org/wiki/Specs/HTML) |
| 482 |
* @param Node $node |
| 483 |
* @param Env $env |
| 484 |
* @return bool |
| 485 |
*/ |
| 486 |
public static function isExtensionOutputtingCoreMwDomSpec( Node $node, Env $env ): bool { |
| 487 |
if ( DOMUtils::matchTypeOf( $node, self::NON_EXTENSION_ENCAP_REGEXP ) !== null ) { |
| 488 |
return false; |
| 489 |
} |
| 490 |
$extTagName = self::getExtTagName( $node ); |
| 491 |
$extConfig = $env->getSiteConfig()->getExtTagConfig( $extTagName ); |
| 492 |
$htmlType = $extConfig['options']['outputHasCoreMwDomSpecMarkup'] ?? null; |
| 493 |
return $htmlType === true; |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Is $node an encapsulation wrapper elt? |
| 498 |
* |
| 499 |
* All root-level $nodes of generated content are considered |
| 500 |
* encapsulation wrappers and share an about-id. |
| 501 |
* |
| 502 |
* @param Node $node |
| 503 |
* @return bool |
| 504 |
*/ |
| 505 |
public static function isEncapsulationWrapper( Node $node ): bool { |
| 506 |
// True if it has an encapsulation type or while walking backwards |
| 507 |
// over elts with identical about ids, we run into a $node with an |
| 508 |
// encapsulation type. |
| 509 |
if ( !( $node instanceof Element ) ) { |
| 510 |
return false; |
| 511 |
} |
| 512 |
return self::findFirstEncapsulationWrapperNode( $node ) !== null; |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Is $node a DOMFragment wrapper? |
| 517 |
* |
| 518 |
* @param Node $node |
| 519 |
* @return bool |
| 520 |
*/ |
| 521 |
public static function isDOMFragmentWrapper( Node $node ): bool { |
| 522 |
// See TokenUtils::hasDOMFragmentType |
| 523 |
return DOMUtils::matchTypeOf( $node, '#^mw:DOMFragment(/sealed/\w+)?$#D' ) !== null; |
| 524 |
} |
| 525 |
|
| 526 |
/** |
| 527 |
* Is $node a sealed DOMFragment of a specific type? |
| 528 |
* |
| 529 |
* @param Node $node |
| 530 |
* @param string $type |
| 531 |
* @return bool |
| 532 |
*/ |
| 533 |
public static function isSealedFragmentOfType( Node $node, string $type ): bool { |
| 534 |
return DOMUtils::hasTypeOf( $node, "mw:DOMFragment/sealed/$type" ); |
| 535 |
} |
| 536 |
|
| 537 |
/** |
| 538 |
* Is $node a Parsoid-generated <section> tag? |
| 539 |
* |
| 540 |
* @param Node $node |
| 541 |
* @return bool |
| 542 |
*/ |
| 543 |
public static function isParsoidSectionTag( Node $node ): bool { |
| 544 |
return $node instanceof Element && |
| 545 |
DOMCompat::nodeName( $node ) === 'section' && |
| 546 |
$node->hasAttribute( 'data-mw-section-id' ); |
| 547 |
} |
| 548 |
|
| 549 |
/** |
| 550 |
* Is the $node from extension content? |
| 551 |
* @param Node $node |
| 552 |
* @param ?string $extType If non-null, checks for that specific extension |
| 553 |
* @return bool |
| 554 |
*/ |
| 555 |
public static function fromExtensionContent( Node $node, ?string $extType = null ): bool { |
| 556 |
$re = $extType ? "#mw:Extension/$extType#" : "#mw:Extension/\w+#"; |
| 557 |
while ( $node && !DOMUtils::atTheTop( $node ) ) { |
| 558 |
if ( DOMUtils::matchTypeOf( $node, $re ) ) { |
| 559 |
return true; |
| 560 |
} |
| 561 |
$node = $node->parentNode; |
| 562 |
} |
| 563 |
return false; |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Is $node from encapsulated (template, extension, etc.) content? |
| 568 |
* @param Node $node |
| 569 |
* @return bool |
| 570 |
*/ |
| 571 |
public static function fromEncapsulatedContent( Node $node ): bool { |
| 572 |
while ( $node && !DOMUtils::atTheTop( $node ) ) { |
| 573 |
if ( self::findFirstEncapsulationWrapperNode( $node ) !== null ) { |
| 574 |
return true; |
| 575 |
} |
| 576 |
$node = $node->parentNode; |
| 577 |
} |
| 578 |
return false; |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
* Compute, when possible, the wikitext source for a $node in |
| 583 |
* an environment env. Returns null if the source cannot be |
| 584 |
* extracted. |
| 585 |
* @param Frame $frame |
| 586 |
* @param Element $node |
| 587 |
* @return string|null |
| 588 |
*/ |
| 589 |
public static function getWTSource( Frame $frame, Element $node ): ?string { |
| 590 |
$dp = DOMDataUtils::getDataParsoid( $node ); |
| 591 |
$dsr = $dp->dsr ?? null; |
| 592 |
// FIXME: We could probably change the null return to '' |
| 593 |
// Just need to verify that code that uses this won't break |
| 594 |
return Utils::isValidDSR( $dsr ) ? |
| 595 |
$dsr->substr( $frame->getSrcText() ) : null; |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Gets all siblings that follow '$node' that have an 'about' as |
| 600 |
* their about id. |
| 601 |
* |
| 602 |
* This is used to fetch transclusion/extension content by using |
| 603 |
* the about-id as the key. This works because |
| 604 |
* transclusion/extension content is a forest of dom-trees formed |
| 605 |
* by adjacent dom-nodes. This is the contract that template |
| 606 |
* encapsulation, dom-reuse, and VE code all have to abide by. |
| 607 |
* |
| 608 |
* The only exception to this adjacency rule is IEW nodes in |
| 609 |
* fosterable positions (in tables) which are not span-wrapped to |
| 610 |
* prevent them from getting fostered out. |
| 611 |
* |
| 612 |
* @param Node $node |
| 613 |
* @param ?string $about |
| 614 |
* @return Node[] |
| 615 |
*/ |
| 616 |
public static function getAboutSiblings( Node $node, ?string $about ): array { |
| 617 |
$nodes = [ $node ]; |
| 618 |
|
| 619 |
if ( $about === null ) { |
| 620 |
return $nodes; |
| 621 |
} |
| 622 |
|
| 623 |
$node = $node->nextSibling; |
| 624 |
while ( $node && ( |
| 625 |
( $node instanceof Element && DOMCompat::getAttribute( $node, 'about' ) === $about ) || |
| 626 |
( DOMUtils::isFosterablePosition( $node ) && DOMUtils::isIEW( $node ) ) |
| 627 |
) ) { |
| 628 |
$nodes[] = $node; |
| 629 |
$node = $node->nextSibling; |
| 630 |
} |
| 631 |
|
| 632 |
// Remove already consumed trailing IEW, if any |
| 633 |
while ( count( $nodes ) > 0 && DOMUtils::isIEW( $nodes[count( $nodes ) - 1] ) ) { |
| 634 |
array_pop( $nodes ); |
| 635 |
} |
| 636 |
|
| 637 |
return $nodes; |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* This function is only intended to be used on encapsulated $nodes |
| 642 |
* (Template/Extension/Param content). |
| 643 |
* |
| 644 |
* Given a '$node' that has an about-id, it is assumed that it is generated |
| 645 |
* by templates or extensions. This function skips over all |
| 646 |
* following content nodes and returns the first non-template node |
| 647 |
* that follows it. |
| 648 |
* |
| 649 |
* @param Node $node |
| 650 |
* @return Node|null |
| 651 |
*/ |
| 652 |
public static function skipOverEncapsulatedContent( Node $node ): ?Node { |
| 653 |
$about = $node instanceof Element ? |
| 654 |
DOMCompat::getAttribute( $node, 'about' ) : null; |
| 655 |
if ( $about !== null ) { |
| 656 |
// Guaranteed not to be empty. It will at least include $node. |
| 657 |
$aboutSiblings = self::getAboutSiblings( $node, $about ); |
| 658 |
return end( $aboutSiblings )->nextSibling; |
| 659 |
} else { |
| 660 |
return $node->nextSibling; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* Comment encoding/decoding. |
| 666 |
* |
| 667 |
* * Some relevant phab tickets: T94055, T70146, T60184, T95039 |
| 668 |
* |
| 669 |
* The wikitext comment rule is very simple: <!-- starts a comment, |
| 670 |
* and --> ends a comment. This means we can have almost anything as the |
| 671 |
* contents of a comment (except the string "-->", but see below), including |
| 672 |
* several things that are not valid in HTML5 comments: |
| 673 |
* |
| 674 |
* * For one, the html5 comment parsing algorithm [0] leniently accepts |
| 675 |
* --!> as a closing comment tag, which differs from the php+tidy combo. |
| 676 |
* |
| 677 |
* * If the comment's data matches /^-?>/, html5 will end the comment. |
| 678 |
* For example, <!-->stuff<--> breaks up as |
| 679 |
* <!--> (the comment) followed by, stuff<--> (as text). |
| 680 |
* |
| 681 |
* * Finally, comment data shouldn't contain two consecutive hyphen-minus |
| 682 |
* characters (--), nor end in a hyphen-minus character (/-$/) as defined |
| 683 |
* in the spec [1]. |
| 684 |
* |
| 685 |
* We work around all these problems by using HTML entity encoding inside |
| 686 |
* the comment body. The characters -, >, and & must be encoded in order |
| 687 |
* to prevent premature termination of the comment by one of the cases |
| 688 |
* above. Encoding other characters is optional; all entities will be |
| 689 |
* decoded during wikitext serialization. |
| 690 |
* |
| 691 |
* In order to allow *arbitrary* content inside a wikitext comment, |
| 692 |
* including the forbidden string "-->" we also do some minimal entity |
| 693 |
* decoding on the wikitext. We are also limited by our inability |
| 694 |
* to encode DSR attributes on the comment $node, so our wikitext entity |
| 695 |
* decoding must be 1-to-1: that is, there must be a unique "decoded" |
| 696 |
* string for every wikitext sequence, and for every decoded string there |
| 697 |
* must be a unique wikitext which creates it. |
| 698 |
* |
| 699 |
* The basic idea here is to replace every string ab*c with the string with |
| 700 |
* one more b in it. This creates a string with no instance of "ac", |
| 701 |
* so you can use 'ac' to encode one more code point. In this case |
| 702 |
* a is "--&", "b" is "amp;", and "c" is "gt;" and we use ac to |
| 703 |
* encode "-->" (which is otherwise unspeakable in wikitext). |
| 704 |
* |
| 705 |
* Note that any user content which does not match the regular |
| 706 |
* expression /--(>|&(amp;)*gt;)/ is unchanged in its wikitext |
| 707 |
* representation, as shown in the first two examples below. |
| 708 |
* |
| 709 |
* User-authored comment text Wikitext HTML5 DOM |
| 710 |
* -------------------------- ------------- ---------------------- |
| 711 |
* & - > & - > & + > |
| 712 |
* Use > here Use > here Use &gt; here |
| 713 |
* --> --> ++> |
| 714 |
* --> --&gt; ++&gt; |
| 715 |
* --&gt; --&amp;gt; ++&amp;gt; |
| 716 |
* |
| 717 |
* [0] http://www.w3.org/TR/html5/syntax.html#comment-start-state |
| 718 |
* [1] http://www.w3.org/TR/html5/syntax.html#comments |
| 719 |
* |
| 720 |
* Map a wikitext-escaped comment to an HTML DOM-escaped comment. |
| 721 |
* |
| 722 |
* @param string $comment Wikitext-escaped comment. |
| 723 |
* @return string DOM-escaped comment. |
| 724 |
*/ |
| 725 |
public static function encodeComment( string $comment ): string { |
| 726 |
// Undo wikitext escaping to obtain "true value" of comment. |
| 727 |
$trueValue = preg_replace_callback( '/--&(amp;)*gt;/', static function ( $m ) { |
| 728 |
return Utils::decodeWtEntities( $m[0] ); |
| 729 |
}, $comment ); |
| 730 |
|
| 731 |
// Now encode '-', '>' and '&' in the "true value" as HTML entities, |
| 732 |
// so that they can be safely embedded in an HTML comment. |
| 733 |
// This part doesn't have to map strings 1-to-1. |
| 734 |
return preg_replace_callback( '/[->&]/', static function ( $m ) { |
| 735 |
return Utils::entityEncodeAll( $m[0] ); |
| 736 |
}, $trueValue ); |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
* Map an HTML DOM-escaped comment to a wikitext-escaped comment. |
| 741 |
* @param string $comment DOM-escaped comment. |
| 742 |
* @return string Wikitext-escaped comment. |
| 743 |
*/ |
| 744 |
public static function decodeComment( string $comment ): string { |
| 745 |
// Undo HTML entity escaping to obtain "true value" of comment. |
| 746 |
$trueValue = Utils::decodeWtEntities( $comment ); |
| 747 |
|
| 748 |
// ok, now encode this "true value" of the comment in such a way |
| 749 |
// that the string "-->" never shows up. (See above.) |
| 750 |
return preg_replace_callback( '/--(&(amp;)*gt;|>)/', static function ( $m ) { |
| 751 |
$s = $m[0]; |
| 752 |
return $s === '-->' ? '-->' : '--&' . substr( $s, 3 ); |
| 753 |
}, $trueValue ); |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
* Utility function: we often need to know the wikitext DSR length for |
| 758 |
* an HTML DOM comment value. |
| 759 |
* |
| 760 |
* @param Comment|CommentTk $node A comment node containing a DOM-escaped comment. |
| 761 |
* @return int The wikitext length in UTF-8 bytes necessary to encode this |
| 762 |
* comment, including 7 characters for the `<!--` and `-->` delimiters. |
| 763 |
*/ |
| 764 |
public static function decodedCommentLength( $node ): int { |
| 765 |
// Add 7 for the "<!--" and "-->" delimiters in wikitext. |
| 766 |
$syntaxLen = 7; |
| 767 |
if ( $node instanceof Comment ) { |
| 768 |
$value = $node->nodeValue; |
| 769 |
if ( $node->previousSibling && |
| 770 |
DOMUtils::hasTypeOf( $node->previousSibling, "mw:Placeholder/UnclosedComment" ) |
| 771 |
) { |
| 772 |
$syntaxLen = 4; |
| 773 |
} |
| 774 |
} elseif ( $node instanceof CommentTk ) { |
| 775 |
if ( isset( $node->dataParsoid->unclosedComment ) ) { |
| 776 |
$syntaxLen = 4; |
| 777 |
} |
| 778 |
$value = $node->value; |
| 779 |
} else { |
| 780 |
throw new UnreachableException( 'Should not be here!' ); |
| 781 |
} |
| 782 |
return strlen( self::decodeComment( $value ) ) + $syntaxLen; |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* @param Node $node |
| 787 |
* @return ?string |
| 788 |
*/ |
| 789 |
public static function getExtTagName( Node $node ): ?string { |
| 790 |
$match = DOMUtils::matchTypeOf( $node, '#^mw:Extension/(.+?)$#D' ); |
| 791 |
return $match ? mb_strtolower( substr( $match, strlen( 'mw:Extension/' ) ) ) : null; |
| 792 |
} |
| 793 |
|
| 794 |
/** |
| 795 |
* @param Env $env |
| 796 |
* @param Node $node |
| 797 |
* @return ?ExtensionTagHandler |
| 798 |
*/ |
| 799 |
public static function getNativeExt( Env $env, Node $node ): ?ExtensionTagHandler { |
| 800 |
$extTagName = self::getExtTagName( $node ); |
| 801 |
return $extTagName ? $env->getSiteConfig()->getExtTagImpl( $extTagName ) : null; |
| 802 |
} |
| 803 |
|
| 804 |
/** |
| 805 |
* Is this an include directive? |
| 806 |
* @param string $name |
| 807 |
* @return bool |
| 808 |
*/ |
| 809 |
public static function isIncludeTag( string $name ): bool { |
| 810 |
return $name === 'includeonly' || $name === 'noinclude' || $name === 'onlyinclude'; |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* Check if tag is annotation or extension directive |
| 815 |
* Adapted from similar grammar function |
| 816 |
* |
| 817 |
* @param Env $env |
| 818 |
* @param string $name |
| 819 |
* @return bool |
| 820 |
*/ |
| 821 |
public static function isAnnOrExtTag( Env $env, string $name ): bool { |
| 822 |
$tagName = mb_strtolower( $name ); |
| 823 |
$siteConfig = $env->getSiteConfig(); |
| 824 |
$extTags = $siteConfig->getExtensionTagNameMap(); |
| 825 |
$isInstalledExt = isset( $extTags[$tagName] ); |
| 826 |
$isIncludeTag = self::isIncludeTag( $tagName ); |
| 827 |
$isAnnotationTag = $siteConfig->isAnnotationTag( $tagName ); |
| 828 |
|
| 829 |
if ( !$isAnnotationTag ) { |
| 830 |
// avoid crashing on <tvar|name> even if we don't support that syntax explicitly |
| 831 |
$pipepos = strpos( $tagName, '|' ); |
| 832 |
if ( $pipepos ) { |
| 833 |
$strBeforePipe = substr( $tagName, 0, $pipepos ); |
| 834 |
$isAnnotationTag = $siteConfig->isAnnotationTag( $strBeforePipe ); |
| 835 |
} |
| 836 |
} |
| 837 |
return $isInstalledExt || $isIncludeTag || $isAnnotationTag; |
| 838 |
} |
| 839 |
|
| 840 |
/** |
| 841 |
* Creates a DocumentFragment containing a single span with type "mw:I18n". The created span |
| 842 |
* should be filled in with setDataNodeI18n to be valid. |
| 843 |
* @param Document $doc |
| 844 |
* @return DocumentFragment |
| 845 |
* @throws DOMException |
| 846 |
*/ |
| 847 |
public static function createEmptyLocalizationFragment( Document $doc ): DocumentFragment { |
| 848 |
$frag = $doc->createDocumentFragment(); |
| 849 |
$span = $doc->createElement( 'span' ); |
| 850 |
DOMUtils::addTypeOf( $span, 'mw:I18n' ); |
| 851 |
$frag->appendChild( $span ); |
| 852 |
return $frag; |
| 853 |
} |
| 854 |
|
| 855 |
/** |
| 856 |
* Creates an internationalization (i18n) message that will be localized into the page content |
| 857 |
* language. The returned DocumentFragment contains, as a single child, a span |
| 858 |
* element with the appropriate information for later localization. |
| 859 |
* @param Document $doc |
| 860 |
* @param string $key message key for the message to be localized |
| 861 |
* @param ?array $params parameters for localization |
| 862 |
* @return DocumentFragment |
| 863 |
* @throws DOMException |
| 864 |
*/ |
| 865 |
public static function createPageContentI18nFragment( |
| 866 |
Document $doc, string $key, ?array $params = null |
| 867 |
): DocumentFragment { |
| 868 |
$frag = self::createEmptyLocalizationFragment( $doc ); |
| 869 |
$i18n = I18nInfo::createPageContentI18n( $key, $params ); |
| 870 |
DOMDataUtils::setDataNodeI18n( $frag->firstChild, $i18n ); |
| 871 |
return $frag; |
| 872 |
} |
| 873 |
|
| 874 |
/** |
| 875 |
* Creates an internationalization (i18n) message that will be localized into the user |
| 876 |
* interface language. The returned DocumentFragment contains, as a single child, a span |
| 877 |
* element with the appropriate information for later localization. |
| 878 |
* @param Document $doc |
| 879 |
* @param string $key message key for the message to be localized |
| 880 |
* @param ?array $params parameters for localization |
| 881 |
* @return DocumentFragment |
| 882 |
* @throws DOMException |
| 883 |
*/ |
| 884 |
public static function createInterfaceI18nFragment( |
| 885 |
Document $doc, string $key, ?array $params = null |
| 886 |
): DocumentFragment { |
| 887 |
$frag = self::createEmptyLocalizationFragment( $doc ); |
| 888 |
$i18n = I18nInfo::createInterfaceI18n( $key, $params ); |
| 889 |
DOMDataUtils::setDataNodeI18n( $frag->firstChild, $i18n ); |
| 890 |
return $frag; |
| 891 |
} |
| 892 |
|
| 893 |
/** |
| 894 |
* Creates an internationalization (i18n) message that will be localized into an arbitrary |
| 895 |
* language. The returned DocumentFragment contains, as a single child, a span |
| 896 |
* element with the appropriate information for later localization. |
| 897 |
* The use of this method is discouraged; use ::createPageContentI18nFragment(...) and |
| 898 |
* ::createInterfaceI18nFragment(...) where possible rather than, respectively, |
| 899 |
* ::createLangI18nFragment(..., $wgContLang, ...) and |
| 900 |
* ::createLangI18nFragment(..., $wgLang,...). |
| 901 |
* @param Document $doc |
| 902 |
* @param Bcp47Code $lang language for the localization |
| 903 |
* @param string $key message key for the message to be localized |
| 904 |
* @param ?array $params parameters for localization |
| 905 |
* @return DocumentFragment |
| 906 |
* @throws DOMException |
| 907 |
*/ |
| 908 |
public static function createLangI18nFragment( |
| 909 |
Document $doc, Bcp47Code $lang, string $key, ?array $params = null |
| 910 |
): DocumentFragment { |
| 911 |
$frag = self::createEmptyLocalizationFragment( $doc ); |
| 912 |
$i18n = I18nInfo::createLangI18n( $lang, $key, $params ); |
| 913 |
DOMDataUtils::setDataNodeI18n( $frag->firstChild, $i18n ); |
| 914 |
return $frag; |
| 915 |
} |
| 916 |
|
| 917 |
/** |
| 918 |
* Adds to $element the internationalization information needed for the attribute $name to be |
| 919 |
* localized in a later pass into the page content language. |
| 920 |
* @param Element $element element on which to add internationalization information |
| 921 |
* @param string $name name of the attribute whose value will be localized |
| 922 |
* @param string $key message key used for the attribute value localization |
| 923 |
* @param ?array $params parameters for localization |
| 924 |
*/ |
| 925 |
public static function addPageContentI18nAttribute( |
| 926 |
Element $element, string $name, string $key, ?array $params = null |
| 927 |
): void { |
| 928 |
$i18n = I18nInfo::createPageContentI18n( $key, $params ); |
| 929 |
DOMUtils::addTypeOf( $element, 'mw:LocalizedAttrs' ); |
| 930 |
DOMDataUtils::setDataAttrI18n( $element, $name, $i18n ); |
| 931 |
} |
| 932 |
|
| 933 |
/** Adds to $element the internationalization information needed for the attribute $name to be |
| 934 |
* localized in a later pass into the user interface language. |
| 935 |
* @param Element $element element on which to add internationalization information |
| 936 |
* @param string $name name of the attribute whose value will be localized |
| 937 |
* @param string $key message key used for the attribute value localization |
| 938 |
* @param ?array $params parameters for localization |
| 939 |
*/ |
| 940 |
public static function addInterfaceI18nAttribute( |
| 941 |
Element $element, string $name, string $key, ?array $params = null |
| 942 |
): void { |
| 943 |
$i18n = I18nInfo::createInterfaceI18n( $key, $params ); |
| 944 |
DOMUtils::addTypeOf( $element, 'mw:LocalizedAttrs' ); |
| 945 |
DOMDataUtils::setDataAttrI18n( $element, $name, $i18n ); |
| 946 |
} |
| 947 |
|
| 948 |
/** |
| 949 |
* Adds to $element the internationalization information needed for the attribute $name to be |
| 950 |
* localized in a later pass into the provided language. |
| 951 |
* The use of this method is discouraged; ; use ::addPageContentI18nAttribute(...) and |
| 952 |
* ::addInterfaceI18nAttribute(...) where possible rather than, respectively, |
| 953 |
* ::addLangI18nAttribute(..., $wgContLang, ...) and ::addLangI18nAttribute(..., $wgLang, ...). |
| 954 |
* @param Element $element element on which to add internationalization information |
| 955 |
* @param Bcp47Code $lang language in which the message will be localized |
| 956 |
* @param string $name name of the attribute whose value will be localized |
| 957 |
* @param string $key message key used for the attribute value localization |
| 958 |
* @param ?array $params parameters for localization |
| 959 |
*/ |
| 960 |
public static function addLangI18nAttribute( |
| 961 |
Element $element, Bcp47Code $lang, string $name, string $key, ?array $params = null |
| 962 |
): void { |
| 963 |
$i18n = I18nInfo::createLangI18n( $lang, $key, $params ); |
| 964 |
DOMUtils::addTypeOf( $element, 'mw:LocalizedAttrs' ); |
| 965 |
DOMDataUtils::setDataAttrI18n( $element, $name, $i18n ); |
| 966 |
} |
| 967 |
|
| 968 |
/** Check whether a node is an annotation meta; if yes, returns its type |
| 969 |
* @param Node $node |
| 970 |
* @return ?string |
| 971 |
*/ |
| 972 |
public static function matchAnnotationMeta( Node $node ): ?string { |
| 973 |
return DOMUtils::matchNameAndTypeOf( $node, 'meta', self::ANNOTATION_META_TYPE_REGEXP ); |
| 974 |
} |
| 975 |
|
| 976 |
/** |
| 977 |
* Extract the annotation type, excluding potential "/End" suffix; returns null if not a valid |
| 978 |
* annotation meta. &$isStart is set to true if the annotation is a start tag, false otherwise. |
| 979 |
* |
| 980 |
* @param Node $node |
| 981 |
* @param bool &$isStart |
| 982 |
* @return ?string The matched type, or null if no match. |
| 983 |
*/ |
| 984 |
public static function extractAnnotationType( Node $node, bool &$isStart = false ): ?string { |
| 985 |
$t = DOMUtils::matchTypeOf( $node, self::ANNOTATION_META_TYPE_REGEXP ); |
| 986 |
if ( $t !== null && preg_match( self::ANNOTATION_META_TYPE_REGEXP, $t, $matches ) ) { |
| 987 |
$isStart = !str_ends_with( $t, '/End' ); |
| 988 |
return $matches[1]; |
| 989 |
} |
| 990 |
return null; |
| 991 |
} |
| 992 |
|
| 993 |
/** |
| 994 |
* Check whether a node is a meta signifying the start of an annotated part of the DOM |
| 995 |
* |
| 996 |
* @param Node $node |
| 997 |
* @return bool |
| 998 |
*/ |
| 999 |
public static function isAnnotationStartMarkerMeta( Node $node ): bool { |
| 1000 |
if ( !$node instanceof Element || DOMCompat::nodeName( $node ) !== 'meta' ) { |
| 1001 |
return false; |
| 1002 |
} |
| 1003 |
$isStart = false; |
| 1004 |
$t = self::extractAnnotationType( $node, $isStart ); |
| 1005 |
return $t !== null && $isStart; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/** |
| 1009 |
* Check whether a node is a meta signifying the end of an annotated part of the DOM |
| 1010 |
* |
| 1011 |
* @param Node $node |
| 1012 |
* @return bool |
| 1013 |
*/ |
| 1014 |
public static function isAnnotationEndMarkerMeta( Node $node ): bool { |
| 1015 |
if ( !$node instanceof Element || DOMCompat::nodeName( $node ) !== 'meta' ) { |
| 1016 |
return false; |
| 1017 |
} |
| 1018 |
$isStart = false; |
| 1019 |
$t = self::extractAnnotationType( $node, $isStart ); |
| 1020 |
return $t !== null && !$isStart; |
| 1021 |
} |
| 1022 |
|
| 1023 |
/** |
| 1024 |
* Check whether the meta tag was moved from its initial position |
| 1025 |
* @param Node $node |
| 1026 |
* @return bool |
| 1027 |
*/ |
| 1028 |
public static function isMovedMetaTag( Node $node ): bool { |
| 1029 |
if ( $node instanceof Element && self::matchAnnotationMeta( $node ) !== null ) { |
| 1030 |
$parsoidData = DOMDataUtils::getDataParsoid( $node ); |
| 1031 |
if ( isset( $parsoidData->wasMoved ) ) { |
| 1032 |
return $parsoidData->wasMoved; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
return false; |
| 1036 |
} |
| 1037 |
|
| 1038 |
/** Returns true if a node is a (start or end) annotation meta tag |
| 1039 |
* @param ?Node $n |
| 1040 |
* @return bool |
| 1041 |
*/ |
| 1042 |
public static function isMarkerAnnotation( ?Node $n ): bool { |
| 1043 |
return $n !== null && self::matchAnnotationMeta( $n ) !== null; |
| 1044 |
} |
| 1045 |
|
| 1046 |
/** |
| 1047 |
* Extracts the media format from the attribute string |
| 1048 |
* |
| 1049 |
* @param Element $node |
| 1050 |
* @return string |
| 1051 |
*/ |
| 1052 |
public static function getMediaFormat( Element $node ): string { |
| 1053 |
// TODO: Remove "Image|Video|Audio" when version 2.4.0 of the content |
| 1054 |
// is no longer supported |
| 1055 |
$mediaType = DOMUtils::matchTypeOf( $node, '#^mw:(File|Image|Video|Audio)(/|$)#' ); |
| 1056 |
$parts = explode( '/', $mediaType ?? '' ); |
| 1057 |
return $parts[1] ?? ''; |
| 1058 |
} |
| 1059 |
|
| 1060 |
/** |
| 1061 |
* @param Element $node |
| 1062 |
* @return bool |
| 1063 |
*/ |
| 1064 |
public static function hasVisibleCaption( Element $node ): bool { |
| 1065 |
$format = self::getMediaFormat( $node ); |
| 1066 |
return in_array( |
| 1067 |
$format, [ 'Thumb', /* 'Manualthumb', FIXME(T305759) */ 'Frame' ], true |
| 1068 |
); |
| 1069 |
} |
| 1070 |
|
| 1071 |
/** |
| 1072 |
* Ref dom post-processing happens after adding media info, so the |
| 1073 |
* linkbacks aren't available in the textContent added to the alt. |
| 1074 |
* However, when serializing, they are in the caption elements. So, this |
| 1075 |
* special handler drops the linkbacks for the purpose of comparison. |
| 1076 |
* |
| 1077 |
* @param Node $node |
| 1078 |
* @return string |
| 1079 |
*/ |
| 1080 |
public static function textContentFromCaption( Node $node ): string { |
| 1081 |
$content = ''; |
| 1082 |
$c = $node->firstChild; |
| 1083 |
while ( $c ) { |
| 1084 |
if ( $c instanceof Text ) { |
| 1085 |
$content .= $c->nodeValue; |
| 1086 |
} elseif ( |
| 1087 |
$c instanceof Element && |
| 1088 |
!DOMUtils::isMetaDataTag( $c ) && |
| 1089 |
!DOMUtils::hasTypeOf( $c, "mw:Extension/ref" ) |
| 1090 |
) { |
| 1091 |
$content .= self::textContentFromCaption( $c ); |
| 1092 |
} |
| 1093 |
$c = $c->nextSibling; |
| 1094 |
} |
| 1095 |
return $content; |
| 1096 |
} |
| 1097 |
|
| 1098 |
} |