| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use LogicException; |
| 7 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem; |
| 8 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem; |
| 9 |
use MediaWiki\MediaWikiServices; |
| 10 |
use UnexpectedValueException; |
| 11 |
use Wikimedia\Assert\Assert; |
| 12 |
use Wikimedia\Parsoid\DOM\Document; |
| 13 |
use Wikimedia\Parsoid\DOM\DocumentFragment; |
| 14 |
use Wikimedia\Parsoid\DOM\Element; |
| 15 |
use Wikimedia\Parsoid\DOM\Node; |
| 16 |
use Wikimedia\Parsoid\DOM\Text; |
| 17 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 18 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 19 |
|
| 20 |
class CommentModifier { |
| 21 |
|
| 22 |
private function __construct() { |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private static function whitespaceParsoidHack( Element $listItem ): void { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
$listItem->setAttribute( 'data-parsoid', '{}' ); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static function sanitizeWikitextLinebreaks( string $wikitext ): string { |
| 39 |
$wikitext = CommentUtils::htmlTrim( $wikitext ); |
| 40 |
$wikitext = preg_replace( "/\r/", "\n", $wikitext ); |
| 41 |
$wikitext = preg_replace( "/\n+/", "\n", $wikitext ); |
| 42 |
return $wikitext; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static function addReplyLink( ContentCommentItem $comment, Node $linkNode ): void { |
| 53 |
$target = $comment->getRange()->endContainer; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
if ( $target instanceof Text ) { |
| 61 |
preg_match( '/\s*$/', $target->nodeValue ?? '', $matches, PREG_OFFSET_CAPTURE ); |
| 62 |
$byteOffset = $matches[0][1]; |
| 63 |
$charOffset = mb_strlen( |
| 64 |
substr( $target->nodeValue ?? '', 0, $byteOffset ) |
| 65 |
); |
| 66 |
$target->splitText( $charOffset ); |
| 67 |
} |
| 68 |
|
| 69 |
$target->parentNode->insertBefore( $linkNode, $target->nextSibling ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static function addListItem( ContentThreadItem $comment, string $replyIndentation ): Element { |
| 86 |
$listTypeMap = [ |
| 87 |
'li' => 'ul', |
| 88 |
'dd' => 'dl' |
| 89 |
]; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
$curComment = $comment; |
| 97 |
while ( count( $curComment->getReplies() ) ) { |
| 98 |
$replies = $curComment->getReplies(); |
| 99 |
$curComment = end( $replies ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
if ( $replyIndentation === 'invisible' ) { |
| 104 |
$itemType = 'dd'; |
| 105 |
} elseif ( $replyIndentation === 'bullet' ) { |
| 106 |
$itemType = 'li'; |
| 107 |
} else { |
| 108 |
throw new InvalidArgumentException( "Invalid reply indentation syntax '$replyIndentation'" ); |
| 109 |
} |
| 110 |
$listType = $listTypeMap[ $itemType ]; |
| 111 |
|
| 112 |
$desiredLevel = $comment->getLevel() + 1; |
| 113 |
$target = $curComment->getRange()->endContainer; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
$parent = CommentUtils::closestElement( $target, [ 'li', 'dd', 'p' ] ) ?? |
| 121 |
$target->parentNode; |
| 122 |
while ( $target->parentNode !== $parent ) { |
| 123 |
$target = $target->parentNode; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
$excludedWrapper = CommentUtils::closestElement( $target, [ 'section' ] ) ?: |
| 133 |
$curComment->getRootNode(); |
| 134 |
$covered = CommentUtils::getFullyCoveredSiblings( $curComment, $excludedWrapper ); |
| 135 |
if ( $curComment->getLevel() === 1 && $covered ) { |
| 136 |
$target = end( $covered ); |
| 137 |
$parent = $target->parentNode; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
$transclusionNode = CommentUtils::getTranscludedFromElement( $target ); |
| 144 |
if ( $transclusionNode ) { |
| 145 |
while ( |
| 146 |
( $nextSibling = $transclusionNode->nextSibling ) && |
| 147 |
$nextSibling instanceof Element && |
| 148 |
$nextSibling->getAttribute( 'about' ) === $transclusionNode->getAttribute( 'about' ) |
| 149 |
) { |
| 150 |
$transclusionNode = $nextSibling; |
| 151 |
} |
| 152 |
$target = $transclusionNode; |
| 153 |
$parent = $target->parentNode; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if ( |
| 164 |
strtolower( $parent->tagName ) === 'p' || |
| 165 |
strtolower( $parent->tagName ) === 'pre' || |
| 166 |
strtolower( $parent->tagName ) === 'ul' || |
| 167 |
strtolower( $parent->tagName ) === 'dl' |
| 168 |
) { |
| 169 |
$parent = $parent->parentNode; |
| 170 |
$target = $target->parentNode; |
| 171 |
} |
| 172 |
|
| 173 |
Assert::precondition( $target !== null, 'We have not stepped outside the document' ); |
| 174 |
|
| 175 |
|
| 176 |
$curLevel = CommentUtils::getIndentLevel( $target, $curComment->getRootNode() ) + 1; |
| 177 |
|
| 178 |
$item = null; |
| 179 |
if ( $desiredLevel === 1 ) { |
| 180 |
|
| 181 |
|
| 182 |
throw new UnexpectedValueException( "Can't add a top-level comment" ); |
| 183 |
|
| 184 |
} elseif ( $curLevel < $desiredLevel ) { |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
$pointer = $target; |
| 192 |
while ( |
| 193 |
$pointer->nextSibling && ( |
| 194 |
CommentUtils::isRenderingTransparentNode( $pointer->nextSibling ) || |
| 195 |
( |
| 196 |
$pointer->nextSibling instanceof Text && |
| 197 |
CommentUtils::htmlTrim( $pointer->nextSibling->nodeValue ?? '' ) === '' && |
| 198 |
|
| 199 |
|
| 200 |
!preg_match( '/(\r?\n){2,}/', $pointer->nextSibling->nodeValue ?? '' ) |
| 201 |
) |
| 202 |
) |
| 203 |
) { |
| 204 |
$pointer = $pointer->nextSibling; |
| 205 |
if ( CommentUtils::isRenderingTransparentNode( $pointer ) ) { |
| 206 |
$target = $pointer; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
while ( $curLevel < $desiredLevel ) { |
| 212 |
$list = $target->ownerDocument->createElement( $listType ); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
$item = $target->ownerDocument->createElement( $itemType ); |
| 217 |
|
| 218 |
|
| 219 |
$parent->insertBefore( $list, $target->nextSibling ); |
| 220 |
$list->appendChild( $item ); |
| 221 |
|
| 222 |
$target = $item; |
| 223 |
$parent = $list; |
| 224 |
$curLevel++; |
| 225 |
} |
| 226 |
} else { |
| 227 |
|
| 228 |
|
| 229 |
do { |
| 230 |
if ( !$target || !$parent ) { |
| 231 |
throw new LogicException( 'Can not decrease nesting any more' ); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
if ( $target->nextSibling ) { |
| 236 |
|
| 237 |
$newNode = $parent->cloneNode( false ); |
| 238 |
|
| 239 |
$parent->parentNode->insertBefore( $newNode, $parent->nextSibling ); |
| 240 |
|
| 241 |
|
| 242 |
while ( $target->nextSibling ) { |
| 243 |
$newNode->appendChild( $target->nextSibling ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
$target = $parent; |
| 248 |
$parent = $parent->parentNode; |
| 249 |
|
| 250 |
|
| 251 |
if ( isset( $listTypeMap[ strtolower( $target->tagName ) ] ) ) { |
| 252 |
$curLevel--; |
| 253 |
} |
| 254 |
} while ( $curLevel >= $desiredLevel ); |
| 255 |
|
| 256 |
|
| 257 |
if ( $itemType === strtolower( $target->tagName ) ) { |
| 258 |
$item = $target->ownerDocument->createElement( $itemType ); |
| 259 |
|
| 260 |
$parent->insertBefore( $item, $target->nextSibling ); |
| 261 |
|
| 262 |
} else { |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
if ( $target->nextSibling ) { |
| 267 |
|
| 268 |
$newNode = $parent->cloneNode( false ); |
| 269 |
|
| 270 |
$parent->parentNode->insertBefore( $newNode, $parent->nextSibling ); |
| 271 |
|
| 272 |
|
| 273 |
while ( $target->nextSibling ) { |
| 274 |
$newNode->appendChild( $target->nextSibling ); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
$target = $parent; |
| 279 |
$parent = $parent->parentNode; |
| 280 |
|
| 281 |
|
| 282 |
$list = $target->ownerDocument->createElement( $listType ); |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
$item = $target->ownerDocument->createElement( $itemType ); |
| 287 |
|
| 288 |
|
| 289 |
$parent->insertBefore( $list, $target->nextSibling ); |
| 290 |
$list->appendChild( $item ); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
if ( $item === null ) { |
| 295 |
throw new LogicException( 'No item found' ); |
| 296 |
} |
| 297 |
|
| 298 |
return $item; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
private static function allOfType( iterable $nodes, string $type ): bool { |
| 311 |
$hasElements = false; |
| 312 |
foreach ( $nodes as $node ) { |
| 313 |
if ( $node instanceof Element ) { |
| 314 |
if ( strtolower( $node->tagName ) !== strtolower( $type ) ) { |
| 315 |
return false; |
| 316 |
} |
| 317 |
$hasElements = true; |
| 318 |
} |
| 319 |
} |
| 320 |
return $hasElements; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public static function unwrapFragment( DocumentFragment $fragment ): void { |
| 331 |
|
| 332 |
$list = null; |
| 333 |
if ( static::allOfType( $fragment->childNodes, 'dd' ) ) { |
| 334 |
$list = $fragment->ownerDocument->createElement( 'dl' ); |
| 335 |
} elseif ( static::allOfType( $fragment->childNodes, 'li' ) ) { |
| 336 |
$list = $fragment->ownerDocument->createElement( 'ul' ); |
| 337 |
} |
| 338 |
if ( $list ) { |
| 339 |
while ( $fragment->firstChild ) { |
| 340 |
$list->appendChild( $fragment->firstChild ); |
| 341 |
} |
| 342 |
$fragment->appendChild( $list ); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
while ( |
| 347 |
static::allOfType( $fragment->childNodes, 'dl' ) || |
| 348 |
static::allOfType( $fragment->childNodes, 'ul' ) || |
| 349 |
static::allOfType( $fragment->childNodes, 'ol' ) || |
| 350 |
( |
| 351 |
|
| 352 |
count( $fragment->childNodes ) > 1 && |
| 353 |
static::allOfType( [ $fragment->childNodes[0] ], 'ul' ) && |
| 354 |
static::allOfType( array_slice( iterator_to_array( $fragment->childNodes ), 1 ), 'dl' ) |
| 355 |
) |
| 356 |
) { |
| 357 |
|
| 358 |
$childNodeList = iterator_to_array( $fragment->childNodes ); |
| 359 |
foreach ( $childNodeList as $node ) { |
| 360 |
static::unwrapList( $node, $fragment ); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public static function unwrapList( Node $list, ?DocumentFragment $fragment = null ): void { |
| 377 |
if ( !( |
| 378 |
$list instanceof Element && ( |
| 379 |
strtolower( $list->tagName ) === 'dl' || |
| 380 |
strtolower( $list->tagName ) === 'ol' || |
| 381 |
strtolower( $list->tagName ) === 'ul' |
| 382 |
) |
| 383 |
) ) { |
| 384 |
|
| 385 |
return; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
if ( CommentUtils::getTranscludedFromElement( $list ) ) { |
| 390 |
return; |
| 391 |
} |
| 392 |
|
| 393 |
$doc = $list->ownerDocument; |
| 394 |
$container = $fragment ?: $list->parentNode; |
| 395 |
$referenceNode = $list; |
| 396 |
while ( $list->firstChild ) { |
| 397 |
if ( $list->firstChild instanceof Element ) { |
| 398 |
|
| 399 |
$p = $doc->createElement( 'p' ); |
| 400 |
while ( $list->firstChild->firstChild ) { |
| 401 |
|
| 402 |
|
| 403 |
if ( CommentUtils::isBlockElement( $list->firstChild->firstChild ) ) { |
| 404 |
if ( $p->firstChild ) { |
| 405 |
$insertBefore = $referenceNode->nextSibling; |
| 406 |
$referenceNode = $p; |
| 407 |
$container->insertBefore( $p, $insertBefore ); |
| 408 |
} |
| 409 |
$insertBefore = $referenceNode->nextSibling; |
| 410 |
$referenceNode = $list->firstChild->firstChild; |
| 411 |
$container->insertBefore( $list->firstChild->firstChild, $insertBefore ); |
| 412 |
$p = $doc->createElement( 'p' ); |
| 413 |
} else { |
| 414 |
$p->appendChild( $list->firstChild->firstChild ); |
| 415 |
} |
| 416 |
} |
| 417 |
if ( $p->firstChild ) { |
| 418 |
$insertBefore = $referenceNode->nextSibling; |
| 419 |
$referenceNode = $p; |
| 420 |
$container->insertBefore( $p, $insertBefore ); |
| 421 |
} |
| 422 |
$list->removeChild( $list->firstChild ); |
| 423 |
} else { |
| 424 |
|
| 425 |
$insertBefore = $referenceNode->nextSibling; |
| 426 |
$referenceNode = $list->firstChild; |
| 427 |
$container->insertBefore( $list->firstChild, $insertBefore ); |
| 428 |
} |
| 429 |
} |
| 430 |
$container->removeChild( $list ); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public static function addSiblingListItem( Element $previousItem ): Element { |
| 437 |
$listItem = $previousItem->ownerDocument->createElement( $previousItem->tagName ); |
| 438 |
$previousItem->parentNode->insertBefore( $listItem, $previousItem->nextSibling ); |
| 439 |
return $listItem; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public static function createWikitextNode( Document $doc, string $wikitext ): Element { |
| 446 |
$span = $doc->createElement( 'span' ); |
| 447 |
|
| 448 |
$span->setAttribute( 'typeof', 'mw:Transclusion' ); |
| 449 |
$span->setAttribute( 'data-mw', json_encode( [ 'parts' => [ $wikitext ] ] ) ); |
| 450 |
|
| 451 |
return $span; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
private static function isWikitextNodeListItem( Element $node ): bool { |
| 458 |
$dataMw = json_decode( $node->getAttribute( 'data-mw' ) ?? '', true ); |
| 459 |
$wikitextLine = $dataMw['parts'][0] ?? null; |
| 460 |
return $wikitextLine && is_string( $wikitextLine ) && |
| 461 |
in_array( $wikitextLine[0], [ '*', '#', ':', ';' ], true ); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
public static function appendSignature( DocumentFragment $container, string $signature ): void { |
| 468 |
$doc = $container->ownerDocument; |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
$wrapperNode = $container->lastChild; |
| 474 |
if ( |
| 475 |
!( $wrapperNode instanceof Element ) || |
| 476 |
strtolower( $wrapperNode->tagName ) !== 'p' || |
| 477 |
( |
| 478 |
|
| 479 |
|
| 480 |
( $wtNode = $wrapperNode->lastChild ) && |
| 481 |
$wtNode instanceof Element && |
| 482 |
static::isWikitextNodeListItem( $wtNode ) |
| 483 |
) |
| 484 |
) { |
| 485 |
$container->appendChild( $doc->createElement( 'p' ) ); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
if ( !$container->lastChild->firstChild ) { |
| 490 |
$signature = ltrim( $signature, ' ' ); |
| 491 |
} |
| 492 |
|
| 493 |
$container->lastChild->appendChild( |
| 494 |
static::createWikitextNode( |
| 495 |
$doc, |
| 496 |
$signature |
| 497 |
) |
| 498 |
); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
public static function appendSignatureWikitext( string $wikitext, string $signature ): string { |
| 505 |
$wikitext = CommentUtils::htmlTrim( $wikitext ); |
| 506 |
|
| 507 |
$lines = explode( "\n", $wikitext ); |
| 508 |
$lastLine = end( $lines ); |
| 509 |
|
| 510 |
|
| 511 |
if ( $lastLine && in_array( $lastLine[0], [ '*', '#', ':', ';' ], true ) ) { |
| 512 |
$wikitext .= "\n"; |
| 513 |
|
| 514 |
$signature = ltrim( $signature, ' ' ); |
| 515 |
} |
| 516 |
|
| 517 |
return $wikitext . $signature; |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
public static function addReply( ContentThreadItem $comment, DocumentFragment $container ): void { |
| 527 |
$services = MediaWikiServices::getInstance(); |
| 528 |
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' ); |
| 529 |
$replyIndentation = $dtConfig->get( 'DiscussionToolsReplyIndentation' ); |
| 530 |
|
| 531 |
$newParsoidItem = null; |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
while ( $container->childNodes->length ) { |
| 538 |
if ( !$newParsoidItem ) { |
| 539 |
$newParsoidItem = static::addListItem( $comment, $replyIndentation ); |
| 540 |
} else { |
| 541 |
$newParsoidItem = static::addSiblingListItem( $newParsoidItem ); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
if ( !( |
| 547 |
$replyIndentation === 'bullet' && |
| 548 |
( $wtNode = $container->firstChild->lastChild ) && |
| 549 |
$wtNode instanceof Element && |
| 550 |
!static::isWikitextNodeListItem( $wtNode ) |
| 551 |
) ) { |
| 552 |
static::whitespaceParsoidHack( $newParsoidItem ); |
| 553 |
} |
| 554 |
|
| 555 |
$newParsoidItem->appendChild( $container->firstChild ); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
public static function transferReply( DocumentFragment $container ): Element { |
| 567 |
$services = MediaWikiServices::getInstance(); |
| 568 |
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' ); |
| 569 |
$replyIndentation = $dtConfig->get( 'DiscussionToolsReplyIndentation' ); |
| 570 |
|
| 571 |
$doc = $container->ownerDocument; |
| 572 |
|
| 573 |
|
| 574 |
$list = $doc->createElement( $replyIndentation === 'invisible' ? 'dl' : 'ul' ); |
| 575 |
while ( $container->childNodes->length ) { |
| 576 |
$item = $doc->createElement( $replyIndentation === 'invisible' ? 'dd' : 'li' ); |
| 577 |
|
| 578 |
|
| 579 |
if ( !( |
| 580 |
$replyIndentation === 'bullet' && |
| 581 |
( $wtNode = $container->firstChild->lastChild ) && |
| 582 |
$wtNode instanceof Element && |
| 583 |
!static::isWikitextNodeListItem( $wtNode ) |
| 584 |
) ) { |
| 585 |
static::whitespaceParsoidHack( $item ); |
| 586 |
} |
| 587 |
$item->appendChild( $container->firstChild ); |
| 588 |
$list->appendChild( $item ); |
| 589 |
} |
| 590 |
return $list; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
public static function prepareWikitextReply( Document $doc, string $wikitext ): DocumentFragment { |
| 601 |
$container = $doc->createDocumentFragment(); |
| 602 |
|
| 603 |
$wikitext = static::sanitizeWikitextLinebreaks( $wikitext ); |
| 604 |
|
| 605 |
$lines = explode( "\n", $wikitext ); |
| 606 |
foreach ( $lines as $line ) { |
| 607 |
$p = $doc->createElement( 'p' ); |
| 608 |
$p->appendChild( static::createWikitextNode( $doc, $line ) ); |
| 609 |
$container->appendChild( $p ); |
| 610 |
} |
| 611 |
|
| 612 |
return $container; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
public static function prepareHtmlReply( Document $doc, string $html ): DocumentFragment { |
| 623 |
$container = DOMUtils::parseHTMLToFragment( $doc, $html ); |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
$childNodeList = iterator_to_array( $container->childNodes ); |
| 631 |
foreach ( $childNodeList as $node ) { |
| 632 |
if ( ( |
| 633 |
$node instanceof Text && |
| 634 |
CommentUtils::htmlTrim( $node->nodeValue ?? '' ) === '' |
| 635 |
) || ( |
| 636 |
$node instanceof Element && |
| 637 |
strtolower( $node->tagName ) === 'p' && |
| 638 |
CommentUtils::htmlTrim( DOMCompat::getInnerHTML( $node ) ) === '' |
| 639 |
) ) { |
| 640 |
$container->removeChild( $node ); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
return $container; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
public static function addWikitextReply( |
| 655 |
ContentCommentItem $comment, string $wikitext, ?string $signature = null |
| 656 |
): void { |
| 657 |
$doc = $comment->getRange()->endContainer->ownerDocument; |
| 658 |
$container = static::prepareWikitextReply( $doc, $wikitext ); |
| 659 |
if ( $signature !== null ) { |
| 660 |
static::appendSignature( $container, $signature ); |
| 661 |
} |
| 662 |
static::addReply( $comment, $container ); |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
public static function addHtmlReply( |
| 673 |
ContentCommentItem $comment, string $html, ?string $signature = null |
| 674 |
): void { |
| 675 |
$doc = $comment->getRange()->endContainer->ownerDocument; |
| 676 |
$container = static::prepareHtmlReply( $doc, $html ); |
| 677 |
if ( $signature !== null ) { |
| 678 |
static::appendSignature( $container, $signature ); |
| 679 |
} |
| 680 |
static::addReply( $comment, $container ); |
| 681 |
} |
| 682 |
} |