| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Wt2Html\DOM\Handlers; |
| 5 |
|
| 6 |
use Wikimedia\Assert\Assert; |
| 7 |
use Wikimedia\Parsoid\Config\Env; |
| 8 |
use Wikimedia\Parsoid\Core\DomSourceRange; |
| 9 |
use Wikimedia\Parsoid\DOM\Comment; |
| 10 |
use Wikimedia\Parsoid\DOM\Element; |
| 11 |
use Wikimedia\Parsoid\DOM\Node; |
| 12 |
use Wikimedia\Parsoid\DOM\Text; |
| 13 |
use Wikimedia\Parsoid\NodeData\TempData; |
| 14 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 15 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 16 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 17 |
use Wikimedia\Parsoid\Utils\DTState; |
| 18 |
use Wikimedia\Parsoid\Utils\Utils; |
| 19 |
use Wikimedia\Parsoid\Utils\WTUtils; |
| 20 |
use Wikimedia\Parsoid\Wikitext\Consts; |
| 21 |
use Wikimedia\Parsoid\Wt2Html\TT\PreHandler; |
| 22 |
|
| 23 |
class CleanUp { |
| 24 |
/** |
| 25 |
* @param Element $node |
| 26 |
* @return bool|Element |
| 27 |
*/ |
| 28 |
public static function stripMarkerMetas( Element $node ) { |
| 29 |
// This meta tag can never have data-mw associated with it. |
| 30 |
// If it were produced by a template, it would always have a <pre> |
| 31 |
// wrapper around which carries any relevant data-mw & typeof properties. |
| 32 |
$isIndentPreSpace = PreHandler::isIndentPreWS( $node ); |
| 33 |
if ( $isIndentPreSpace || |
| 34 |
DOMUtils::hasTypeOf( $node, "mw:Placeholder/UnclosedComment" ) || |
| 35 |
// Sometimes a non-tpl meta node might get the mw:Transclusion typeof |
| 36 |
// element attached to it. So, check if the node has data-mw, |
| 37 |
// in which case we also have to keep it. |
| 38 |
( !DOMDataUtils::validDataMw( $node ) && ( |
| 39 |
( |
| 40 |
DOMUtils::hasTypeOf( $node, 'mw:Placeholder/StrippedTag' ) && |
| 41 |
// NOTE: In ComputeDSR, we don't zero out the width of these |
| 42 |
// markers because they're staying in the DOM and serializeDOMNode |
| 43 |
// only handles a few cases of zero width nodes. |
| 44 |
!DOMUtils::isNestedInListItem( $node ) |
| 45 |
) || |
| 46 |
DOMUtils::hasTypeOf( $node, 'mw:Transclusion' ) |
| 47 |
) ) |
| 48 |
) { |
| 49 |
$nextNode = $node->nextSibling; |
| 50 |
$parent = $node->parentNode; |
| 51 |
if ( $isIndentPreSpace ) { |
| 52 |
$dsr = DOMDataUtils::getDataParsoid( $parent )->dsr ?? null; |
| 53 |
if ( $dsr ) { |
| 54 |
// @see explanation in PreHandler::newIndentPreWS() |
| 55 |
$dsr->openWidth = 1; |
| 56 |
} |
| 57 |
// Strip this in the cleanup handler since |
| 58 |
// DOM passes till the end may need DSR info from this tag. |
| 59 |
return true; |
| 60 |
} else { |
| 61 |
$parent->removeChild( $node ); |
| 62 |
// stop the traversal, since this node is no longer in the DOM. |
| 63 |
return $nextNode; |
| 64 |
} |
| 65 |
} else { |
| 66 |
return true; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* The following are considered "empty node"s: |
| 72 |
* - Comments, rendering transparent nodes, nowiki spans without content |
| 73 |
* are all stripped by the core parser. |
| 74 |
* - Text nodes with whitespace don't count either. |
| 75 |
* - Parsoid-added span wrappers around other "empty node"s. |
| 76 |
* |
| 77 |
* @param Node $node |
| 78 |
* @param bool &$hasRTNodes Set to true if the node contained rendering transparent nodes. |
| 79 |
* Note this value is only reliable if ::isEmptyNode() returns true. |
| 80 |
* @return bool |
| 81 |
*/ |
| 82 |
private static function isEmptyNode( Node $node, bool &$hasRTNodes ): bool { |
| 83 |
for ( $n = $node->firstChild; $n !== null; $n = $n->nextSibling ) { |
| 84 |
if ( $n instanceof Comment ) { |
| 85 |
continue; |
| 86 |
} elseif ( $n instanceof Text ) { |
| 87 |
if ( !preg_match( '/^[ \t\r\n]*$/D', $n->nodeValue ) ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
continue; |
| 91 |
} elseif ( $n instanceof Element ) { |
| 92 |
if ( WTUtils::isRenderingTransparentNode( $n ) ) { |
| 93 |
$hasRTNodes = true; |
| 94 |
continue; |
| 95 |
} |
| 96 |
if ( |
| 97 |
( |
| 98 |
DOMUtils::hasTypeOf( $n, 'mw:Nowiki' ) || |
| 99 |
DOMDataUtils::getDataParsoid( $n )->getTempFlag( TempData::WRAPPER ) |
| 100 |
) && self::isEmptyNode( $n, $hasRTNodes ) |
| 101 |
) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
return false; |
| 105 |
} else { |
| 106 |
return false; |
| 107 |
} |
| 108 |
} |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
// These template-wrapping attributes can be ignored while looking for |
| 113 |
// empty elements. Note that data-mw & data-parsoid are unlikely to exist |
| 114 |
// at this stage of DOM processing. This is conservative but safe. |
| 115 |
// In this case, it is also sufficient since only p, li, tr can be deleted. |
| 116 |
public const ALLOWED_TPL_WRAPPER_ATTRS = [ |
| 117 |
'about' => 1, |
| 118 |
'typeof' => 1, |
| 119 |
]; |
| 120 |
|
| 121 |
/** |
| 122 |
* @param Node $node |
| 123 |
* @param DTState $state |
| 124 |
* @return bool|Node |
| 125 |
*/ |
| 126 |
public static function handleEmptyElements( Node $node, DTState $state ) { |
| 127 |
// Set by isEmptyNode() to indicate whether a node which is "empty" contained |
| 128 |
// invisible "rendering transparent" nodes. |
| 129 |
$hasRTNodes = false; |
| 130 |
|
| 131 |
if ( !( $node instanceof Element ) || |
| 132 |
!isset( Consts::$Output['FlaggedEmptyElts'][DOMCompat::nodeName( $node )] ) || |
| 133 |
!self::isEmptyNode( $node, $hasRTNodes ) |
| 134 |
) { |
| 135 |
return true; |
| 136 |
} |
| 137 |
foreach ( DOMUtils::attributes( $node ) as $name => $value ) { |
| 138 |
// Skip the Parsoid-added data attribute and template-wrapping attributes |
| 139 |
if ( $name === DOMDataUtils::DATA_OBJECT_ATTR_NAME || |
| 140 |
( ( $state->tplInfo ?? null ) && isset( self::ALLOWED_TPL_WRAPPER_ATTRS[$name] ) ) |
| 141 |
) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
|
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* The node is known to be empty and a deletion candidate |
| 150 |
* - If node is part of template content and is not the |
| 151 |
* first encapsulation wrapper node, and doesn't contain |
| 152 |
* any rendering transparent nodes, it can be deleted. |
| 153 |
* - If not, we add the mw-empty-elt class so that wikis |
| 154 |
* can decide what to do with them. |
| 155 |
*/ |
| 156 |
if ( |
| 157 |
$state->tplInfo && |
| 158 |
$state->tplInfo->first !== $node && |
| 159 |
!$hasRTNodes |
| 160 |
) { |
| 161 |
$nextNode = $node->nextSibling; |
| 162 |
$node->parentNode->removeChild( $node ); |
| 163 |
return $nextNode; |
| 164 |
} |
| 165 |
|
| 166 |
DOMCompat::getClassList( $node )->add( 'mw-empty-elt' ); |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* FIXME: Worry about "about" siblings |
| 172 |
* |
| 173 |
* @param Env $env |
| 174 |
* @param Element $node |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
private static function inNativeContent( Env $env, Element $node ): bool { |
| 178 |
while ( !DOMUtils::atTheTop( $node ) ) { |
| 179 |
if ( WTUtils::getNativeExt( $env, $node ) !== null ) { |
| 180 |
return true; |
| 181 |
} |
| 182 |
$node = $node->parentNode; |
| 183 |
} |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Whitespace in this function refers to [ \t] only |
| 189 |
* @param Element $node |
| 190 |
* @param ?DomSourceRange $dsr |
| 191 |
*/ |
| 192 |
private static function trimWhiteSpace( Element $node, ?DomSourceRange $dsr ): void { |
| 193 |
// Trim leading ws (on the first line) |
| 194 |
$trimmedLen = 0; |
| 195 |
$updateDSR = true; |
| 196 |
$skipped = false; |
| 197 |
for ( $c = $node->firstChild; $c; $c = $next ) { |
| 198 |
$next = $c->nextSibling; |
| 199 |
if ( $c instanceof Text && preg_match( '/^[ \t]*$/D', $c->nodeValue ) ) { |
| 200 |
$node->removeChild( $c ); |
| 201 |
$trimmedLen += strlen( $c->nodeValue ); |
| 202 |
$updateDSR = !$skipped; |
| 203 |
} elseif ( !WTUtils::isRenderingTransparentNode( $c ) ) { |
| 204 |
break; |
| 205 |
} else { |
| 206 |
// We are now skipping over a rendering transparent node |
| 207 |
// and will trim additional whitespace => we cannot reliably |
| 208 |
// maintain info about trimmed whitespace. |
| 209 |
$skipped = true; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
if ( $c instanceof Text && |
| 214 |
preg_match( '/^([ \t]+)([\s\S]*)$/D', $c->nodeValue, $matches ) |
| 215 |
) { |
| 216 |
$updateDSR = !$skipped; |
| 217 |
$c->nodeValue = $matches[2]; |
| 218 |
$trimmedLen += strlen( $matches[1] ); |
| 219 |
} |
| 220 |
|
| 221 |
if ( $dsr ) { |
| 222 |
$dsr->leadingWS = $updateDSR ? $trimmedLen : -1; |
| 223 |
} |
| 224 |
|
| 225 |
// Trim trailing ws (on the last line) |
| 226 |
$trimmedLen = 0; |
| 227 |
$updateDSR = true; |
| 228 |
$skipped = false; |
| 229 |
for ( $c = $node->lastChild; $c; $c = $prev ) { |
| 230 |
$prev = $c->previousSibling; |
| 231 |
if ( $c instanceof Text && preg_match( '/^[ \t]*$/D', $c->nodeValue ) ) { |
| 232 |
$trimmedLen += strlen( $c->nodeValue ); |
| 233 |
$node->removeChild( $c ); |
| 234 |
$updateDSR = !$skipped; |
| 235 |
} elseif ( !WTUtils::isRenderingTransparentNode( $c ) ) { |
| 236 |
break; |
| 237 |
} else { |
| 238 |
// We are now skipping over a rendering transparent node |
| 239 |
// and will trim additional whitespace => we cannot reliably |
| 240 |
// maintain info about trimmed whitespace. |
| 241 |
$skipped = true; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if ( $c instanceof Text && |
| 246 |
preg_match( '/^([\s\S]*\S)([ \t]+)$/D', $c->nodeValue, $matches ) |
| 247 |
) { |
| 248 |
$updateDSR = !$skipped; |
| 249 |
$c->nodeValue = $matches[1]; |
| 250 |
$trimmedLen += strlen( $matches[2] ); |
| 251 |
} |
| 252 |
|
| 253 |
if ( $dsr ) { |
| 254 |
$dsr->trailingWS = $updateDSR ? $trimmedLen : -1; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Perform some final cleanup |
| 260 |
* |
| 261 |
* @param Node $node |
| 262 |
* @param DTState $state |
| 263 |
* @return bool|Node The next node or true to continue with $node->nextSibling |
| 264 |
*/ |
| 265 |
public static function finalCleanup( Node $node, DTState $state ) { |
| 266 |
if ( !( $node instanceof Element ) ) { |
| 267 |
return true; |
| 268 |
} |
| 269 |
|
| 270 |
Assert::invariant( $state->atTopLevel, 'This pass should only be run on the top-level' ); |
| 271 |
|
| 272 |
$dp = DOMDataUtils::getDataParsoid( $node ); |
| 273 |
// Delete from data parsoid, wikitext originating autoInsertedEnd info |
| 274 |
if ( !empty( $dp->autoInsertedEnd ) && !WTUtils::hasLiteralHTMLMarker( $dp ) && |
| 275 |
isset( Consts::$WTTagsWithNoClosingTags[DOMCompat::nodeName( $node )] ) |
| 276 |
) { |
| 277 |
unset( $dp->autoInsertedEnd ); |
| 278 |
} |
| 279 |
|
| 280 |
$isFirstEncapsulationWrapperNode = ( $state->tplInfo->first ?? null ) === $node || |
| 281 |
// Traversal isn't done with tplInfo for section tags, but we should |
| 282 |
// still clean them up as if they are the head of encapsulation. |
| 283 |
WTUtils::isParsoidSectionTag( $node ); |
| 284 |
|
| 285 |
// Remove dp.src from elements that have valid data-mw and dsr. |
| 286 |
// This should reduce data-parsoid bloat. |
| 287 |
// |
| 288 |
// Presence of data-mw is a proxy for us knowing how to serialize |
| 289 |
// this content from HTML. Token handlers should strip src for |
| 290 |
// content where data-mw isn't necessary and html2wt knows how to |
| 291 |
// handle the HTML markup. |
| 292 |
$validDSR = DOMDataUtils::validDataMw( $node ) && Utils::isValidDSR( $dp->dsr ?? null ); |
| 293 |
$isPageProp = DOMCompat::nodeName( $node ) === 'meta' && |
| 294 |
str_starts_with( DOMCompat::getAttribute( $node, 'property' ) ?? '', 'mw:PageProp/' ); |
| 295 |
if ( $validDSR && !$isPageProp ) { |
| 296 |
unset( $dp->src ); |
| 297 |
} elseif ( $isFirstEncapsulationWrapperNode && empty( $dp->tsr ) ) { |
| 298 |
// Transcluded nodes will not have dp.tsr set |
| 299 |
// and don't need dp.src either. |
| 300 |
unset( $dp->src ); |
| 301 |
} |
| 302 |
|
| 303 |
// Remove tsr |
| 304 |
if ( property_exists( $dp, 'tsr' ) ) { |
| 305 |
unset( $dp->tsr ); |
| 306 |
} |
| 307 |
|
| 308 |
// Remove temporary information |
| 309 |
// @phan-suppress-next-line PhanTypeObjectUnsetDeclaredProperty |
| 310 |
unset( $dp->tmp ); |
| 311 |
|
| 312 |
// Various places, like ContentUtils::shiftDSR, can set this to `null` |
| 313 |
if ( property_exists( $dp, 'dsr' ) && $dp->dsr === null ) { |
| 314 |
unset( $dp->dsr ); |
| 315 |
} |
| 316 |
|
| 317 |
// Make dsr zero-range for fostered content |
| 318 |
// to prevent selser from duplicating this content |
| 319 |
// outside the table from where this came. |
| 320 |
// |
| 321 |
// But, do not zero it out if the node has template encapsulation |
| 322 |
// information. That will be disastrous (see T54638, T54488). |
| 323 |
if ( !empty( $dp->fostered ) && !empty( $dp->dsr ) && !$isFirstEncapsulationWrapperNode ) { |
| 324 |
$dp->dsr->start = $dp->dsr->end; |
| 325 |
} |
| 326 |
|
| 327 |
// Strip nowiki spans from encapsulated content but leave behind |
| 328 |
// wrappers on root nodes since they have valid about ids and we |
| 329 |
// don't want to break the about-chain by stripping the wrapper |
| 330 |
// and associated ids (we cannot add an about id on the nowiki-ed |
| 331 |
// content since that would be a text node). |
| 332 |
if ( ( $state->tplInfo ?? null ) && !WTUtils::isEncapsulatedDOMForestRoot( $node ) && |
| 333 |
DOMUtils::hasTypeOf( $node, 'mw:Nowiki' ) |
| 334 |
) { |
| 335 |
DOMUtils::migrateChildren( $node, $node->parentNode, $node->nextSibling ); |
| 336 |
$next = $node->nextSibling; |
| 337 |
$node->parentNode->removeChild( $node ); |
| 338 |
return $next; |
| 339 |
} |
| 340 |
|
| 341 |
// Strip IndentPre marker metas |
| 342 |
if ( PreHandler::isIndentPreWS( $node ) ) { |
| 343 |
$nextNode = $node->nextSibling; |
| 344 |
$node->parentNode->removeChild( $node ); |
| 345 |
return $nextNode; |
| 346 |
} |
| 347 |
|
| 348 |
// Trim whitespace from some wikitext markup |
| 349 |
// not involving explicit HTML tags (T157481) |
| 350 |
if ( !WTUtils::hasLiteralHTMLMarker( $dp ) && |
| 351 |
isset( Consts::$WikitextTagsWithTrimmableWS[DOMCompat::nodeName( $node )] ) |
| 352 |
) { |
| 353 |
self::trimWhiteSpace( $node, $dp->dsr ?? null ); |
| 354 |
} |
| 355 |
|
| 356 |
return true; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Perform some final cleanup |
| 361 |
* |
| 362 |
* @param Node $node |
| 363 |
* @param DTState $state |
| 364 |
* @return bool|Node The next node or true to continue with $node->nextSibling |
| 365 |
*/ |
| 366 |
public static function saveDataParsoid( Node $node, DTState $state ) { |
| 367 |
if ( !( $node instanceof Element ) ) { |
| 368 |
return true; |
| 369 |
} |
| 370 |
Assert::invariant( $state->atTopLevel, 'This pass should only be run on the top-level' ); |
| 371 |
$usedIdIndex = &$state->usedIdIndex; |
| 372 |
if ( DOMUtils::isBody( $node ) ) { |
| 373 |
// Initialization |
| 374 |
$usedIdIndex = DOMDataUtils::usedIdIndex( $node ); |
| 375 |
} |
| 376 |
|
| 377 |
$env = $state->env; |
| 378 |
$dp = DOMDataUtils::getDataParsoid( $node ); |
| 379 |
$isFirstEncapsulationWrapperNode = ( $state->tplInfo->first ?? null ) === $node || |
| 380 |
// Traversal isn't done with tplInfo for section tags, but we should |
| 381 |
// still clean them up as if they are the head of encapsulation. |
| 382 |
WTUtils::isParsoidSectionTag( $node ); |
| 383 |
$discardDataParsoid = $env->discardDataParsoid; |
| 384 |
|
| 385 |
// Strip data-parsoid from templated content, where unnecessary. |
| 386 |
if ( ( $state->tplInfo ?? null ) && |
| 387 |
// Always keep info for the first node |
| 388 |
!$isFirstEncapsulationWrapperNode && |
| 389 |
// We can't remove data-parsoid from inside <references> text, |
| 390 |
// as that's the only HTML representation we have left for it. |
| 391 |
!self::inNativeContent( $env, $node ) && |
| 392 |
// FIXME: We can't remove dp from nodes with stx information |
| 393 |
// because the serializer uses stx information in some cases to |
| 394 |
// emit the right newline separators. |
| 395 |
// |
| 396 |
// For example, "a\n\nb" and "<p>a</p><p>b/p>" both generate |
| 397 |
// identical html but serialize to different wikitext. |
| 398 |
// |
| 399 |
// This is only needed for the last top-level node . |
| 400 |
( empty( $dp->stx ) || ( $state->tplInfo->last ?? null ) !== $node ) |
| 401 |
) { |
| 402 |
$discardDataParsoid = true; |
| 403 |
} |
| 404 |
|
| 405 |
DOMDataUtils::storeDataAttribs( $node, [ |
| 406 |
'discardDataParsoid' => $discardDataParsoid, |
| 407 |
// Even though we're passing in the `env`, this is the only place |
| 408 |
// we want the storage to happen, so don't refactor this in there. |
| 409 |
'storeInPageBundle' => $env->pageBundle, |
| 410 |
'idIndex' => $usedIdIndex, |
| 411 |
'env' => $env |
| 412 |
] |
| 413 |
); |
| 414 |
|
| 415 |
return true; |
| 416 |
} |
| 417 |
|
| 418 |
} |