MediaWiki master
HtmlHelperTrait.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Html;
4
5use Wikimedia\Assert\Assert;
6use Wikimedia\RemexHtml\Serializer\HtmlFormatter;
7use Wikimedia\RemexHtml\Serializer\SerializerNode;
8
18 private $shouldModifyCallback;
19
21 private $modifyCallback;
22
23 public function __construct( $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
32 public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
33 if ( ( $this->shouldModifyCallback )( $node ) ) {
34 $node = clone $node;
35 $node->attrs = clone $node->attrs;
36 $newNode = ( $this->modifyCallback )( $node );
37 Assert::parameterType( [ SerializerNode::class, 'string' ], $newNode, 'return value' );
38 if ( is_string( $newNode ) ) {
39 // Replace this element with an "outerHTML" string.
40 return $newNode;
41 }
42 return parent::element( $parent, $newNode, $contents );
43 } else {
44 return parent::element( $parent, $node, $contents );
45 }
46 }
47
48 public function startDocument( $fragmentNamespace, $fragmentName ) {
49 return '';
50 }
51}
__construct( $options, callable $shouldModifyCallback, callable $modifyCallback)
element(SerializerNode $parent, SerializerNode $node, $contents)
trait HtmlHelperTrait
Internal helper trait for HtmlHelper::modifyHtml.
startDocument( $fragmentNamespace, $fragmentName)