Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
45.45% |
5 / 11 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ContentDOMTransformStage | |
45.45% |
5 / 11 |
|
0.00% |
0 / 2 |
4.46 | |
0.00% |
0 / 1 |
| transform | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
1.00 | |||
| transformDOM | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| createElement | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace MediaWiki\OutputTransform; |
| 5 | |
| 6 | use MediaWiki\Parser\ContentHolder; |
| 7 | use MediaWiki\Parser\ParserOptions; |
| 8 | use MediaWiki\Parser\ParserOutput; |
| 9 | use Wikimedia\Parsoid\DOM\Document; |
| 10 | use Wikimedia\Parsoid\DOM\DocumentFragment; |
| 11 | use Wikimedia\Parsoid\DOM\Element; |
| 12 | use Wikimedia\Parsoid\DOM\Node; |
| 13 | use Wikimedia\Parsoid\Utils\DOMCompat; |
| 14 | |
| 15 | /** |
| 16 | * OutputTransformStages that modify the content as a HTML DOM tree. |
| 17 | * |
| 18 | * Subclasses are expected to implement ::transformDOM() to mutate the |
| 19 | * DOM-structured content as a Document in-place. |
| 20 | * |
| 21 | * @internal |
| 22 | */ |
| 23 | abstract class ContentDOMTransformStage extends OutputTransformStage { |
| 24 | |
| 25 | /** |
| 26 | * @inheritDoc |
| 27 | */ |
| 28 | public function transform( |
| 29 | ParserOutput $po, ParserOptions $popts, array &$options |
| 30 | ): ParserOutput { |
| 31 | $contentHolder = $po->getContentHolder(); |
| 32 | $df = $contentHolder->getAsDom( ContentHolder::BODY_FRAGMENT ) ?? |
| 33 | $contentHolder->createFragment(); |
| 34 | |
| 35 | $df = $this->transformDOM( $df, $po, $popts, $options ); |
| 36 | |
| 37 | $contentHolder->setAsDom( ContentHolder::BODY_FRAGMENT, $df ); |
| 38 | return $po; |
| 39 | } |
| 40 | |
| 41 | /** Applies the transformation to a DOM document */ |
| 42 | abstract public function transformDOM( |
| 43 | DocumentFragment $df, ParserOutput $po, ParserOptions $popts, array &$options |
| 44 | ): DocumentFragment; |
| 45 | |
| 46 | /** |
| 47 | * Helper method for DOM transforms to easily create DOM Elements with |
| 48 | * the given attributes and children. |
| 49 | * |
| 50 | * @param Document $doc Document holding the new element |
| 51 | * @param string $name Lowercase tag name of the new element |
| 52 | * @param array<string,string> $attribs Associative array between the |
| 53 | * name and (unescaped) value of the attributes of the new element |
| 54 | * @param Node|string ...$children List of child nodes for the new element. |
| 55 | * Unescaped strings are converted to new Text Nodes before their |
| 56 | * insertion in the tree. |
| 57 | * @return Element |
| 58 | * @throws \DOMException |
| 59 | */ |
| 60 | public function createElement( |
| 61 | Document $doc, string $name, array $attribs = [], Node|string ...$children |
| 62 | ): Element { |
| 63 | $el = $doc->createElement( $name ); |
| 64 | foreach ( $attribs as $key => $value ) { |
| 65 | $el->setAttribute( $key, $value ); |
| 66 | } |
| 67 | DOMCompat::append( $el, ...$children ); |
| 68 | return $el; |
| 69 | } |
| 70 | } |