Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
HtmlHelperTrait
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 element
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 startDocument
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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
11/**
12 * Internal helper trait for HtmlHelper::modifyHtml.
13 *
14 * This is designed to extend a HtmlFormatter.
15 */
16trait HtmlHelperTrait {
17    /** @var callable */
18    private $shouldModifyCallback;
19
20    /** @var callable */
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
32    /**
33     * @param SerializerNode $parent
34     * @param SerializerNode $node
35     * @param string|null $contents
36     * @return string
37     */
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
54    /**
55     * @param string|null $fragmentNamespace
56     * @param string|null $fragmentName
57     * @return string
58     */
59    public function startDocument( $fragmentNamespace, $fragmentName ) {
60        return '';
61    }
62}