| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Html2Wt\DOMHandlers; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\DOM\Element; |
| 7 |
use Wikimedia\Parsoid\DOM\Node; |
| 8 |
use Wikimedia\Parsoid\DOM\Text; |
| 9 |
use Wikimedia\Parsoid\Html2Wt\SerializerState; |
| 10 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 11 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 12 |
use Wikimedia\Parsoid\Utils\TokenUtils; |
| 13 |
|
| 14 |
/** |
| 15 |
* Used as a fallback in other tag handles. |
| 16 |
*/ |
| 17 |
class FallbackHTMLHandler extends DOMHandler { |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
parent::__construct( false ); |
| 21 |
} |
| 22 |
|
| 23 |
/** @inheritDoc */ |
| 24 |
public function handle( |
| 25 |
Element $node, SerializerState $state, bool $wrapperUnmodified = false |
| 26 |
): ?Node { |
| 27 |
$serializer = $state->serializer; |
| 28 |
|
| 29 |
// Wikitext supports the following list syntax: |
| 30 |
// |
| 31 |
// * <li class="a"> hello world |
| 32 |
// |
| 33 |
// The "LI Hack" gives support for this syntax, and we need to |
| 34 |
// specially reconstruct the above from a single <li> tag. |
| 35 |
$serializer->handleLIHackIfApplicable( $node ); |
| 36 |
|
| 37 |
$tag = $serializer->serializeHTMLTag( $node, $wrapperUnmodified ); |
| 38 |
$state->emitChunk( $tag, $node ); |
| 39 |
|
| 40 |
if ( $node->hasChildNodes() ) { |
| 41 |
$inPHPBlock = $state->inPHPBlock; |
| 42 |
if ( |
| 43 |
TokenUtils::tagOpensBlockScope( DOMCompat::nodeName( $node ) ) || |
| 44 |
// Blockquote is special in that it doesn't suppress paragraphs |
| 45 |
// but does suppress pre wrapping |
| 46 |
DOMCompat::nodeName( $node ) === 'blockquote' |
| 47 |
) { |
| 48 |
$state->inPHPBlock = true; |
| 49 |
} |
| 50 |
|
| 51 |
// TODO(arlolra): As of 1.3.0, html pre is considered an extension |
| 52 |
// and wrapped in encapsulation. When that version is no longer |
| 53 |
// accepted for serialization, we can remove this backwards |
| 54 |
// compatibility code. |
| 55 |
if ( DOMCompat::nodeName( $node ) === 'pre' ) { |
| 56 |
// Handle html-pres specially |
| 57 |
// 1. If the node has a leading newline, add one like it (logic copied from VE) |
| 58 |
// 2. If not, and it has a data-parsoid strippedNL flag, add it back. |
| 59 |
// This patched DOM will serialize html-pres correctly. |
| 60 |
|
| 61 |
$lostLine = ''; |
| 62 |
$fc = $node->firstChild; |
| 63 |
if ( $fc instanceof Text ) { |
| 64 |
$lostLine = str_starts_with( $fc->nodeValue, "\n" ) ? "\n" : ''; |
| 65 |
} |
| 66 |
|
| 67 |
if ( !$lostLine && ( DOMDataUtils::getDataParsoid( $node )->strippedNL ?? false ) ) { |
| 68 |
$lostLine = "\n"; |
| 69 |
} |
| 70 |
|
| 71 |
$state->emitChunk( $lostLine, $node ); |
| 72 |
} |
| 73 |
|
| 74 |
$state->serializeChildren( $node ); |
| 75 |
$state->inPHPBlock = $inPHPBlock; |
| 76 |
} |
| 77 |
|
| 78 |
$endTag = $serializer->serializeHTMLEndTag( $node, $wrapperUnmodified ); |
| 79 |
$state->emitChunk( $endTag, $node ); |
| 80 |
return $node->nextSibling; |
| 81 |
} |
| 82 |
} |