wiki.techinc.nl/includes/Html/HtmlHelperTrait.php
C. Scott Ananian 84d0dff968 HtmlHelper: Add "legacy compatibility" mode
When using HtmlHelper for style deduplication, slight differences in
the serialization format used by the legacy parser caused test failures.
Add a "compatibility" mode which tries to better match legacy parser
behavior for void elements, character escapes, and other details.

Parsoid HTML has always been serialized using an HTML5 serializer,
so this compatibility mode will be disabled when processing Parsoid
HTML.

Change-Id: I0441aa3e44f6562e05e95a18cc282c53fe446788
2023-12-11 21:04:50 +01:00

43 lines
1.2 KiB
PHP

<?php
namespace MediaWiki\Html;
use Wikimedia\Assert\Assert;
use Wikimedia\RemexHtml\Serializer\SerializerNode;
/**
* Internal helper trait for HtmlHelper::modifyHtml.
*
* This is designed to extend a HtmlFormatter.
*
* @phan-file-suppress PhanTraitParentReference
*/
trait HtmlHelperTrait {
/** @var callable */
private $shouldModifyCallback;
/** @var callable */
private $modifyCallback;
public function __construct( $options, callable $shouldModifyCallback, callable $modifyCallback ) {
parent::__construct( $options );
$this->shouldModifyCallback = $shouldModifyCallback;
$this->modifyCallback = $modifyCallback;
}
public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
if ( ( $this->shouldModifyCallback )( $node ) ) {
$node = clone $node;
$node->attrs = clone $node->attrs;
$newNode = ( $this->modifyCallback )( $node );
Assert::parameterType( SerializerNode::class, $newNode, 'return value' );
return parent::element( $parent, $newNode, $contents );
} else {
return parent::element( $parent, $node, $contents );
}
}
public function startDocument( $fragmentNamespace, $fragmentName ) {
return '';
}
}