Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParserHookProcessor
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 staticTagPostProcessor
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
30
 wtPostprocess
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\ParserTests;
5
6use stdClass;
7use Wikimedia\Parsoid\DOM\Element;
8use Wikimedia\Parsoid\DOM\Node;
9use Wikimedia\Parsoid\Ext\DOMDataUtils;
10use Wikimedia\Parsoid\Ext\DOMProcessor as ExtDOMProcessor;
11use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
12use Wikimedia\Parsoid\Utils\DOMCompat;
13use Wikimedia\Parsoid\Utils\DOMUtils;
14use Wikimedia\Parsoid\Utils\WTUtils;
15
16/**
17 * See tests/parser/ParserTestParserHook.php in core.
18 */
19class ParserHookProcessor extends ExtDOMProcessor {
20
21    public function staticTagPostProcessor(
22        Node $node, ParsoidExtensionAPI $extApi, stdClass $obj
23    ): void {
24        if ( $node instanceof Element ) {
25            if ( DOMUtils::hasTypeOf( $node, 'mw:Extension/statictag' ) ) {
26                $dataMw = DOMDataUtils::getDataMw( $node );
27                if ( $dataMw->getExtAttrib( 'action' ) === 'flush' ) {
28                    $node->appendChild( $node->ownerDocument->createTextNode( $obj->buf ) );
29                    $obj->buf = '';
30                } else {
31                    $obj->buf .= $dataMw->body->extsrc;
32                }
33            } elseif ( WTUtils::isSealedFragmentOfType( $node, 'sealtag' ) ) {
34                $dp = DOMDataUtils::getDataParsoid( $node );
35                $content = $dp->html;
36                $span = $content->firstChild;
37
38                // In case it's templated
39                DOMUtils::addAttributes( $span, [
40                    'typeof' => DOMCompat::getAttribute( $node, 'typeof' ),
41                    'about' => DOMCompat::getAttribute( $node, 'about' ) ??
42                        DOMCompat::getAttribute( $span, 'about' ),
43                ] );
44                DOMDataUtils::setDataMw( $span, DOMDataUtils::getDataMw( $node ) );
45
46                DOMUtils::removeTypeOf( $span, 'mw:DOMFragment/sealed/sealtag' );
47                DOMUtils::addTypeOf( $span, 'mw:Extension/sealtag' );
48
49                $node->parentNode->replaceChild( $span, $node );
50                unset( $dp->html );
51            }
52            $extApi->processAttributeEmbeddedDom(
53                $node, function ( $domFragment ) use ( $extApi ) {
54                    $this->wtPostprocess( $extApi, $domFragment, [] );
55                    return true; // Conservatively say we changed things
56                }
57            );
58        }
59    }
60
61    /**
62     * @inheritDoc
63     */
64    public function wtPostprocess(
65        ParsoidExtensionAPI $extApi, Node $root, array $options
66    ): void {
67        // Pass an object since we want the data to be carried around across
68        // nodes in the DOM. Passing an array won't work since visitDOM doesn't
69        // use a reference on its end. Maybe we could fix that separately.
70        DOMUtils::visitDOM(
71            $root,
72            [ $this, 'staticTagPostProcessor' ],
73            $extApi,
74            (object)[ 'buf' => '' ]
75        );
76    }
77}