Debian Codegraph @

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

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Wikimedia\Parsoid\ParserTests;
5
6 use Closure;
7 use Error;
8 use Wikimedia\Parsoid\DOM\DocumentFragment;
9 use Wikimedia\Parsoid\DOM\Element;
10 use Wikimedia\Parsoid\Ext\ExtensionModule;
11 use Wikimedia\Parsoid\Ext\ExtensionTagHandler;
12 use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
13 use Wikimedia\Parsoid\Utils\DOMCompat;
14 use Wikimedia\Parsoid\Utils\DOMDataUtils;
15 use Wikimedia\Parsoid\Utils\WTUtils;
16
17 /**
18 * See tests/parser/ParserTestParserHook.php in core.
19 */
20 class ParserHook extends ExtensionTagHandler implements ExtensionModule {
21
22 /** @inheritDoc */
23 public function sourceToDom(
24 ParsoidExtensionAPI $extApi, string $content, array $args
25 ): DocumentFragment {
26 $extName = $extApi->extTag->getName();
27 if ( $extApi->extTag->isSelfClosed() ) {
28 $content = null;
29 }
30 switch ( $extName ) {
31 case 'tag':
32 case 'tåg':
33 return $extApi->htmlToDom(
34 "<pre>\n" .
35 var_export( $content, true ) . "\n" .
36 var_export( $extApi->extArgsToArray( $args ), true ) . "\n" .
37 "</pre>"
38 );
39
40 case 'statictag':
41 // FIXME: Choose a better DOM representation that doesn't mess with
42 // newline constraints.
43 return $extApi->htmlToDom( '<span />' );
44
45 case 'asidetag':
46 // T278565
47 return $extApi->htmlToDom( '<aside>Some aside content</aside>' );
48
49 case 'pwraptest':
50 return $extApi->htmlToDom( '<!--CMT--><style>p{}</style>' );
51
52 case 'spantag':
53 // "Transparent" tag which wraps wikitext in a <span>;
54 // useful in testing various parsoid wrapping scenarios
55 // (we used to use <ref> for this)
56 // NOTE: This tag disables p-wrapping and indent-pre transforms.
57 return $extApi->extTagToDOM( $args, $content, [
58 'wrapperTag' => 'span',
59 'parseOpts' => [
60 'extTag' => $extName,
61 'context' => 'inline',
62 ],
63 ] );
64
65 default:
66 throw new Error( "Unexpected tag name: $extName in ParserHook" );
67 }
68 }
69
70 /** @inheritDoc */
71 public function processAttributeEmbeddedHTML(
72 ParsoidExtensionAPI $extApi, Element $elt, Closure $proc
73 ): void {
74 $dataMw = DOMDataUtils::getDataMw( $elt );
75 if ( isset( $dataMw->body->html ) ) {
76 $dataMw->body->html = $proc( $dataMw->body->html );
77 }
78 }
79
80 /** @inheritDoc */
81 public function domToWikitext(
82 ParsoidExtensionAPI $extApi, Element $node, bool $wrapperUnmodified
83 ) {
84 $dataMw = DOMDataUtils::getDataMw( $node );
85 $extName = WTUtils::getExtTagName( $node ) ?? $dataMw->name;
86 if ( $extName !== 'spantag' ) {
87 return false; // use default serialization
88 }
89 $html2wtOpts = [
90 'extName' => $extName,
91 // FIXME: One-off PHP parser state leak. This needs a better solution.
92 'inPHPBlock' => true
93 ];
94 $src = '';
95 if ( $wrapperUnmodified && isset( $dataMw->body->extsrc ) ) {
96 $src = $dataMw->body->extsrc;
97 } elseif ( isset( $dataMw->body->html ) ) {
98 // First look for the extension's content in data-mw.body.html
99 $src = $extApi->htmlToWikitext( $html2wtOpts, $dataMw->body->html );
100 } else {
101 $src = $extApi->htmlToWikitext( $html2wtOpts, DOMCompat::getInnerHTML( $node ) );
102 }
103 return "<$extName>" . $src . "</$extName>";
104 }
105
106 /** @inheritDoc */
107 public function getConfig(): array {
108 return [
109 'name' => 'ParserHook',
110 'tags' => [
111 [ 'name' => 'tag', 'handler' => self::class ],
112 [ 'name' => 'tåg', 'handler' => self::class ],
113 [ 'name' => 'statictag', 'handler' => self::class ],
114 [ 'name' => 'asidetag', 'handler' => self::class ],
115 [ 'name' => 'pwraptest', 'handler' => self::class ],
116 [ 'name' => 'spantag', 'handler' => self::class,
117 'options' => [
118 'wt2html' => [
119 'embedsHTMLInAttributes' => true,
120 ],
121 'outputHasCoreMwDomSpecMarkup' => true,
122 ],
123 ],
124 ],
125 'domProcessors' => [
126 ParserHookProcessor::class
127 ]
128 ];
129 }
130 }