Debian Codegraph @

includes/Html/HtmlHelperTrait.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Html;
4
5 use Wikimedia\Assert\Assert;
6 use Wikimedia\RemexHtml\Serializer\HtmlFormatter;
7 use Wikimedia\RemexHtml\Serializer\SerializerNode;
8
9 /**
10 * Internal helper trait for HtmlHelper::modifyHtml.
11 *
12 * This is designed to extend a HtmlFormatter.
13 *
14 * @phan-file-suppress PhanTraitParentReference
15 */
16 trait HtmlHelperTrait {
17 /** @var callable */
18 private $shouldModifyCallback;
19
20 /** @var callable */
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 }