Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContentDOMTransformStage
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 transform
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
12
 transformDOM
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3namespace MediaWiki\OutputTransform;
4
5use MediaWiki\Parser\ParserOutput;
6use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter;
7use ParserOptions;
8use Wikimedia\Parsoid\Core\PageBundle;
9use Wikimedia\Parsoid\DOM\Document;
10use Wikimedia\Parsoid\Mocks\MockEnv;
11use Wikimedia\Parsoid\Utils\ContentUtils;
12use Wikimedia\Parsoid\Utils\DOMCompat;
13use Wikimedia\Parsoid\Utils\DOMDataUtils;
14use Wikimedia\Parsoid\Utils\DOMUtils;
15
16/**
17 * OutputTransformStages that modify the content as a HTML DOM tree.
18 *
19 * Subclasses are expected to implement ::transformDOM() to mutate the
20 * DOM-structured content as a Document in-place.
21 *
22 * @internal
23 */
24abstract class ContentDOMTransformStage implements OutputTransformStage {
25
26    /**
27     * @inheritDoc
28     */
29    public function transform( ParserOutput $po, ?ParserOptions $popts, array &$options ): ParserOutput {
30        // TODO will use HTMLHolder in the future
31        $doc = null;
32        $hasPageBundle = PageBundleParserOutputConverter::hasPageBundle( $po );
33        if ( $hasPageBundle ) {
34            $pb = PageBundleParserOutputConverter::pageBundleFromParserOutput( $po );
35            $doc = DOMUtils::parseHTML( $po->getContentHolderText() );
36            PageBundle::apply( $doc, $pb );
37            DOMDataUtils::prepareDoc( $doc );
38            DOMDataUtils::visitAndLoadDataAttribs(
39                DOMCompat::getBody( $doc )
40            );
41        } else {
42            $doc = ContentUtils::createAndLoadDocument(
43                $po->getContentHolderText(),
44            );
45        }
46
47        $doc = $this->transformDOM( $doc, $po, $popts, $options );
48
49        // TODO will use HTMLHolder in the future
50        if ( $hasPageBundle ) {
51            DOMDataUtils::visitAndStoreDataAttribs(
52                DOMCompat::getBody( $doc ),
53                [
54                    'storeInPageBundle' => true,
55                    'env' => new MockEnv( [] ),
56                ]
57            );
58            $pb = DOMDataUtils::getPageBundle( $doc );
59            $po = PageBundleParserOutputConverter::parserOutputFromPageBundle(
60                $pb, $po
61            );
62            $text = ContentUtils::toXML( DOMCompat::getBody( $doc ), [
63                'innerXML' => true,
64            ] );
65        } else {
66            $text = ContentUtils::ppToXML( DOMCompat::getBody( $doc ), [
67                'innerXML' => true,
68            ] );
69        }
70        $po->setContentHolderText( $text );
71        return $po;
72    }
73
74    /** Applies the transformation to a DOM document */
75    abstract public function transformDOM(
76        Document $dom, ParserOutput $po, ?ParserOptions $popts, array &$options
77    ): Document;
78
79}