Debian Codegraph @

vendor/wikimedia/parsoid/src/ParserTests/ParserHookProcessor.php

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Wikimedia\Parsoid\ParserTests;
5
6 use stdClass;
7 use Wikimedia\Parsoid\DOM\Element;
8 use Wikimedia\Parsoid\DOM\Node;
9 use Wikimedia\Parsoid\Ext\DOMDataUtils;
10 use Wikimedia\Parsoid\Ext\DOMProcessor as ExtDOMProcessor;
11 use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
12 use Wikimedia\Parsoid\Utils\DOMUtils;
13
14 /**
15 * See tests/parser/ParserTestParserHook.php in core.
16 */
17 class ParserHookProcessor extends ExtDOMProcessor {
18
19 public function staticTagPostProcessor(
20 Node $node, stdClass $obj
21 ): void {
22 if ( $node instanceof Element ) {
23 if ( DOMUtils::hasTypeOf( $node, 'mw:Extension/statictag' ) ) {
24 $dataMw = DOMDataUtils::getDataMw( $node );
25 // T367616: ->attrs-> should be renamed to extAttrs
26 if ( ( $dataMw->attrs->action ?? null ) === 'flush' ) {
27 $node->appendChild( $node->ownerDocument->createTextNode( $obj->buf ) );
28 $obj->buf = '';
29 } else {
30 $obj->buf .= $dataMw->body->extsrc;
31 }
32 }
33 }
34 }
35
36 /**
37 * @inheritDoc
38 */
39 public function wtPostprocess(
40 ParsoidExtensionAPI $extApi, Node $node, array $options
41 ): void {
42 // Pass an object since we want the data to be carried around across
43 // nodes in the DOM. Passing an array won't work since visitDOM doesn't
44 // use a reference on its end. Maybe we could fix that separately.
45 DOMUtils::visitDOM( $node, [ $this, 'staticTagPostProcessor' ], (object)[ 'buf' => '' ] );
46 }
47 }