Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.83% covered (danger)
47.83%
11 / 23
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Pre
47.83% covered (danger)
47.83%
11 / 23
50.00% covered (danger)
50.00%
1 / 2
2.57
0.00% covered (danger)
0.00%
0 / 1
 getConfig
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 sourceToDom
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Ext\Pre;
5
6use Wikimedia\Parsoid\Core\Sanitizer;
7use Wikimedia\Parsoid\DOM\DocumentFragment;
8use Wikimedia\Parsoid\Ext\DOMDataUtils;
9use Wikimedia\Parsoid\Ext\ExtensionModule;
10use Wikimedia\Parsoid\Ext\ExtensionTagHandler;
11use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
12use Wikimedia\Parsoid\Ext\PHPUtils;
13use Wikimedia\Parsoid\Ext\Utils;
14
15/**
16 * The `<pre>` extension tag shadows the html pre tag, but has different
17 * semantics.  It treats anything inside it as plaintext.
18 */
19class Pre extends ExtensionTagHandler implements ExtensionModule {
20
21    /** @inheritDoc */
22    public function getConfig(): array {
23        return [
24            'name' => '<pre>',
25            'tags' => [
26                [
27                    'name' => 'pre',
28                    'handler' => self::class,
29                    'options' => [
30                        // Strip nowiki markers from #tag parser-function
31                        // arguments (T299103)
32                        'stripNowiki' => true,
33                    ],
34                ]
35            ]
36        ];
37    }
38
39    /** @inheritDoc */
40    public function sourceToDom(
41        ParsoidExtensionAPI $extApi, string $txt, array $extArgs
42    ): DocumentFragment {
43        $domFragment = $extApi->htmlToDom( '' );
44        $doc = $domFragment->ownerDocument;
45        $pre = $doc->createElement( 'pre' );
46
47        Sanitizer::applySanitizedArgs( $extApi->getSiteConfig(), $pre, $extArgs );
48        DOMDataUtils::getDataParsoid( $pre )->stx = 'html';
49
50        // Support nowikis in pre.  Do this before stripping newlines, see test,
51        // "<pre> with <nowiki> inside (compatibility with 1.6 and earlier)"
52        $txt = preg_replace( '/<nowiki\s*>(.*?)<\/nowiki\s*>/s', '$1', $txt );
53
54        // Strip leading newline to match legacy php parser.  This is probably because
55        // it doesn't do xml serialization accounting for `newlineStrippingElements`
56        // Of course, this leads to indistinguishability between n=0 and n=1
57        // newlines, but that only seems to affect parserTests output.  Rendering
58        // is the same, and the newline is preserved for rt in the `extSrc`.
59        $txt = PHPUtils::stripPrefix( $txt, "\n" );
60
61        // `extSrc` will take care of rt'ing these
62        $txt = Utils::decodeWtEntities( $txt );
63
64        $pre->appendChild( $doc->createTextNode( $txt ) );
65        $domFragment->appendChild( $pre );
66
67        return $domFragment;
68    }
69
70}