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\SerializerNode;
7
17 private $shouldModifyCallback;
18
20 private $modifyCallback;
21
22 public function __construct( $options, callable $shouldModifyCallback, callable $modifyCallback ) {
23 parent::__construct( $options );
24 $this->shouldModifyCallback = $shouldModifyCallback;
25 $this->modifyCallback = $modifyCallback;
26 }
27
28 public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
29 if ( ( $this->shouldModifyCallback )( $node ) ) {
30 $node = clone $node;
31 $node->attrs = clone $node->attrs;
32 $newNode = ( $this->modifyCallback )( $node );
33 Assert::parameterType( SerializerNode::class, $newNode, 'return value' );
34 return parent::element( $parent, $newNode, $contents );
35 } else {
36 return parent::element( $parent, $node, $contents );
37 }
38 }
39
40 public function startDocument( $fragmentNamespace, $fragmentName ) {
41 return '';
42 }
43}
__construct( $options, callable $shouldModifyCallback, callable $modifyCallback)
element(SerializerNode $parent, SerializerNode $node, $contents)
trait HtmlHelperTrait
Internal helper trait for HtmlHelper::modifyHtml.
startDocument( $fragmentNamespace, $fragmentName)