2023-11-30 20:41:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Html;
|
|
|
|
|
|
|
|
|
|
use Wikimedia\Assert\Assert;
|
2025-02-25 17:02:34 +00:00
|
|
|
use Wikimedia\RemexHtml\Serializer\HtmlFormatter;
|
2023-11-30 20:41:58 +00:00
|
|
|
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;
|
2025-02-25 17:02:34 +00:00
|
|
|
// Escape U+0338 (T387130)
|
|
|
|
|
'@phan-var HtmlFormatter $this';
|
|
|
|
|
$this->textEscapes["\u{0338}"] = '̸';
|
2023-11-30 20:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 '';
|
|
|
|
|
}
|
|
|
|
|
}
|