Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
ParserHookProcessor | |
0.00% |
0 / 35 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
staticTagPostProcessor | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
30 | |||
wtPostprocess | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 |
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\DOMCompat; |
13 | use Wikimedia\Parsoid\Utils\DOMUtils; |
14 | use Wikimedia\Parsoid\Utils\WTUtils; |
15 | |
16 | /** |
17 | * See tests/parser/ParserTestParserHook.php in core. |
18 | */ |
19 | class 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 | // T367616: ->attrs-> should be renamed to extAttrs |
28 | if ( ( $dataMw->attrs->action ?? null ) === '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 | $dp = DOMDataUtils::getDataParsoid( $node ); |
36 | $contentId = $dp->html; |
37 | $content = $extApi->getContentDOM( $contentId ); |
38 | $span = $content->firstChild; |
39 | |
40 | // In case it's templated |
41 | DOMUtils::addAttributes( $span, [ |
42 | 'typeof' => DOMCompat::getAttribute( $node, 'typeof' ), |
43 | 'about' => DOMCompat::getAttribute( $node, 'about' ) ?? |
44 | DOMCompat::getAttribute( $span, 'about' ), |
45 | ] ); |
46 | DOMDataUtils::setDataMw( $span, DOMDataUtils::getDataMw( $node ) ); |
47 | |
48 | DOMUtils::removeTypeOf( $span, 'mw:DOMFragment/sealed/sealtag' ); |
49 | DOMUtils::addTypeOf( $span, 'mw:Extension/sealtag' ); |
50 | |
51 | $node->parentNode->replaceChild( $span, $node ); |
52 | $extApi->clearContentDOM( $contentId ); |
53 | } |
54 | $extApi->processAttributeEmbeddedHTML( |
55 | $node, function ( string $html ) use ( $extApi ) { |
56 | $domFragment = $extApi->htmlToDom( $html ); |
57 | $this->wtPostprocess( $extApi, $domFragment, [] ); |
58 | return $extApi->domToHtml( $domFragment, true, true ); |
59 | } |
60 | ); |
61 | } |
62 | } |
63 | |
64 | /** |
65 | * @inheritDoc |
66 | */ |
67 | public function wtPostprocess( |
68 | ParsoidExtensionAPI $extApi, Node $node, array $options |
69 | ): void { |
70 | // Pass an object since we want the data to be carried around across |
71 | // nodes in the DOM. Passing an array won't work since visitDOM doesn't |
72 | // use a reference on its end. Maybe we could fix that separately. |
73 | DOMUtils::visitDOM( |
74 | $node, |
75 | [ $this, 'staticTagPostProcessor' ], |
76 | $extApi, |
77 | (object)[ 'buf' => '' ] |
78 | ); |
79 | } |
80 | } |