MediaWiki master
HtmlHelperTrait.php
Go to the documentation of this file.
1<?php
2
3// @phan-file-suppress PhanTraitParentReference
4
5namespace MediaWiki\Html;
6
7use Wikimedia\Assert\Assert;
8use Wikimedia\RemexHtml\Serializer\HtmlFormatter;
9use Wikimedia\RemexHtml\Serializer\SerializerNode;
10
18 private $shouldModifyCallback;
19
21 private $modifyCallback;
22
23 public function __construct( array $options, callable $shouldModifyCallback, callable $modifyCallback ) {
24 parent::__construct( $options );
25 $this->shouldModifyCallback = $shouldModifyCallback;
26 $this->modifyCallback = $modifyCallback;
27 // Escape U+0338 (T387130)
28 '@phan-var HtmlFormatter $this';
29 $this->textEscapes["\u{0338}"] = '&#x338;';
30 }
31
38 public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
39 if ( ( $this->shouldModifyCallback )( $node ) ) {
40 $node = clone $node;
41 $node->attrs = clone $node->attrs;
42 $newNode = ( $this->modifyCallback )( $node );
43 Assert::parameterType( [ SerializerNode::class, 'string' ], $newNode, 'return value' );
44 if ( is_string( $newNode ) ) {
45 // Replace this element with an "outerHTML" string.
46 return $newNode;
47 }
48 return parent::element( $parent, $newNode, $contents );
49 } else {
50 return parent::element( $parent, $node, $contents );
51 }
52 }
53
59 public function startDocument( $fragmentNamespace, $fragmentName ) {
60 return '';
61 }
62}
element(SerializerNode $parent, SerializerNode $node, $contents)
trait HtmlHelperTrait
Internal helper trait for HtmlHelper::modifyHtml.
__construct(array $options, callable $shouldModifyCallback, callable $modifyCallback)
startDocument( $fragmentNamespace, $fragmentName)