Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
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 / 31
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 / 25
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\Core\DOMCompat;
8use Wikimedia\Parsoid\DOM\DocumentFragment;
9use Wikimedia\Parsoid\DOM\Element;
10use Wikimedia\Parsoid\DOM\Node;
11use Wikimedia\Parsoid\Ext\DOMDataUtils;
12use Wikimedia\Parsoid\Ext\DOMProcessor as ExtDOMProcessor;
13use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
14use Wikimedia\Parsoid\Utils\DOMUtils;
15use Wikimedia\Parsoid\Utils\WTUtils;
16
17/**
18 * See tests/parser/ParserTestParserHook.php in core.
19 */
20class ParserHookProcessor extends ExtDOMProcessor {
21
22    public function staticTagPostProcessor(
23        Node $node, ParsoidExtensionAPI $extApi, stdClass $obj
24    ): void {
25        if ( $node instanceof Element ) {
26            if ( DOMUtils::hasTypeOf( $node, 'mw:Extension/statictag' ) ) {
27                $dataMw = DOMDataUtils::getDataMw( $node );
28                if ( $dataMw->getExtAttrib( 'action' ) === 'flush' ) {
29                    $node->appendChild( $node->ownerDocument->createTextNode( $obj->buf ) );
30                    $obj->buf = '';
31                } else {
32                    $obj->buf .= $dataMw->body->extsrc;
33                }
34            } elseif ( WTUtils::isSealedFragmentOfType( $node, 'sealtag' ) ) {
35                $content = WTUtils::getDOMFragmentContents( $node, clearAfter: true );
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            }
51            $extApi->processAttributeEmbeddedDom(
52                $node, function ( $domFragment ) use ( $extApi ) {
53                    $this->wtPostprocess( $extApi, $domFragment, [] );
54                    return true; // Conservatively say we changed things
55                }
56            );
57        }
58    }
59
60    /**
61     * @inheritDoc
62     */
63    public function wtPostprocess(
64        ParsoidExtensionAPI $extApi, DocumentFragment|Element $root, array $options
65    ): void {
66        // Pass an object since we want the data to be carried around across
67        // nodes in the DOM. Passing an array won't work since visitDOM doesn't
68        // use a reference on its end. Maybe we could fix that separately.
69        DOMUtils::visitDOM(
70            $root,
71            [ $this, 'staticTagPostProcessor' ],
72            $extApi,
73            (object)[ 'buf' => '' ]
74        );
75    }
76}