Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Indicator | |
0.00% |
0 / 43 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
| getConfig | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
2 | |||
| processAttributeEmbeddedDom | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| sourceToDom | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\Parsoid\Ext\Indicator; |
| 5 | |
| 6 | use Wikimedia\Parsoid\DOM\DocumentFragment; |
| 7 | use Wikimedia\Parsoid\DOM\Element; |
| 8 | use Wikimedia\Parsoid\Ext\DiffDOMUtils; |
| 9 | use Wikimedia\Parsoid\Ext\DOMDataUtils; |
| 10 | use Wikimedia\Parsoid\Ext\DOMUtils; |
| 11 | use Wikimedia\Parsoid\Ext\ExtensionModule; |
| 12 | use Wikimedia\Parsoid\Ext\ExtensionTagHandler; |
| 13 | use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
| 14 | |
| 15 | /** |
| 16 | * Implements the php parser's `indicator` hook natively. |
| 17 | */ |
| 18 | class Indicator extends ExtensionTagHandler implements ExtensionModule { |
| 19 | /** @inheritDoc */ |
| 20 | public function getConfig(): array { |
| 21 | return [ |
| 22 | 'name' => 'Indicator', |
| 23 | 'tags' => [ |
| 24 | [ |
| 25 | 'name' => 'indicator', |
| 26 | 'handler' => self::class, |
| 27 | 'options' => [ |
| 28 | 'wt2html' => [ |
| 29 | 'embedsDomInAttributes' => true, |
| 30 | 'customizesDataMw' => true, |
| 31 | ], |
| 32 | 'outputHasCoreMwDomSpecMarkup' => true |
| 33 | ], |
| 34 | ] |
| 35 | ], |
| 36 | ]; |
| 37 | } |
| 38 | |
| 39 | /** @inheritDoc */ |
| 40 | public function processAttributeEmbeddedDom( |
| 41 | ParsoidExtensionAPI $extApi, Element $elt, callable $proc |
| 42 | ): void { |
| 43 | $dmw = DOMDataUtils::getDataMw( $elt ); |
| 44 | if ( isset( $dmw->html ) ) { |
| 45 | $proc( $dmw->html ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** @inheritDoc */ |
| 50 | public function sourceToDom( |
| 51 | ParsoidExtensionAPI $extApi, string $content, array $args |
| 52 | ): DocumentFragment { |
| 53 | $dataMw = $extApi->extTag->getDefaultDataMw(); |
| 54 | $kvArgs = $extApi->extArgsToArray( $args ); |
| 55 | $name = $kvArgs['name'] ?? ''; |
| 56 | if ( trim( $name ) === '' ) { |
| 57 | $out = $extApi->pushError( 'invalid-indicator-name' ); |
| 58 | DOMDataUtils::setDataMw( $out->firstChild, $dataMw ); |
| 59 | return $out; |
| 60 | } |
| 61 | |
| 62 | // Convert indicator wikitext to DOM |
| 63 | $domFragment = $extApi->extTagToDOM( [] /* No args to apply */, $content, [ |
| 64 | 'parseOpts' => [ 'extTag' => 'indicator' ], |
| 65 | ] ); |
| 66 | |
| 67 | // Strip an outer paragraph if it is the sole paragraph without additional attributes |
| 68 | $content = DiffDOMUtils::firstNonSepChild( $domFragment ); |
| 69 | if ( $content && |
| 70 | DOMUtils::nodeName( $content ) === 'p' && |
| 71 | DiffDOMUtils::nextNonSepSibling( $content ) === null && |
| 72 | $content instanceof Element && // Needed to mollify Phan |
| 73 | DOMDataUtils::noAttrs( $content ) |
| 74 | ) { |
| 75 | DOMUtils::migrateChildren( $content, $domFragment, $content->nextSibling ); |
| 76 | $domFragment->removeChild( $content ); |
| 77 | } |
| 78 | |
| 79 | $dataMw->html = $domFragment; |
| 80 | |
| 81 | // Use a meta tag whose data-mw we will stuff this HTML into later. |
| 82 | // NOTE: Till T214994 is resolved, this HTML will not get processed |
| 83 | // by all the top-level DOM passes that may need to process this (ex: linting) |
| 84 | $meta = $domFragment->ownerDocument->createElement( 'meta' ); |
| 85 | DOMDataUtils::setDataMw( $meta, $dataMw ); |
| 86 | |
| 87 | $domFragment = $meta->ownerDocument->createDocumentFragment(); |
| 88 | $domFragment->appendChild( $meta ); |
| 89 | return $domFragment; |
| 90 | } |
| 91 | } |