Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
55.00% |
11 / 20 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
Pre | |
55.00% |
11 / 20 |
|
50.00% |
1 / 2 |
2.36 | |
0.00% |
0 / 1 |
getConfig | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
sourceToDom | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\Ext\Pre; |
5 | |
6 | use Wikimedia\Parsoid\Core\Sanitizer; |
7 | use Wikimedia\Parsoid\DOM\DocumentFragment; |
8 | use Wikimedia\Parsoid\Ext\DOMDataUtils; |
9 | use Wikimedia\Parsoid\Ext\ExtensionModule; |
10 | use Wikimedia\Parsoid\Ext\ExtensionTagHandler; |
11 | use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
12 | use Wikimedia\Parsoid\Ext\PHPUtils; |
13 | use 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 | */ |
19 | class 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 | ] |
30 | ] |
31 | ]; |
32 | } |
33 | |
34 | /** @inheritDoc */ |
35 | public function sourceToDom( |
36 | ParsoidExtensionAPI $extApi, string $txt, array $extArgs |
37 | ): DocumentFragment { |
38 | $domFragment = $extApi->htmlToDom( '' ); |
39 | $doc = $domFragment->ownerDocument; |
40 | $pre = $doc->createElement( 'pre' ); |
41 | |
42 | Sanitizer::applySanitizedArgs( $extApi->getSiteConfig(), $pre, $extArgs ); |
43 | DOMDataUtils::getDataParsoid( $pre )->stx = 'html'; |
44 | |
45 | // Support nowikis in pre. Do this before stripping newlines, see test, |
46 | // "<pre> with <nowiki> inside (compatibility with 1.6 and earlier)" |
47 | $txt = preg_replace( '/<nowiki\s*>(.*?)<\/nowiki\s*>/s', '$1', $txt ); |
48 | |
49 | // Strip leading newline to match legacy php parser. This is probably because |
50 | // it doesn't do xml serialization accounting for `newlineStrippingElements` |
51 | // Of course, this leads to indistinguishability between n=0 and n=1 |
52 | // newlines, but that only seems to affect parserTests output. Rendering |
53 | // is the same, and the newline is preserved for rt in the `extSrc`. |
54 | $txt = PHPUtils::stripPrefix( $txt, "\n" ); |
55 | |
56 | // `extSrc` will take care of rt'ing these |
57 | $txt = Utils::decodeWtEntities( $txt ); |
58 | |
59 | $pre->appendChild( $doc->createTextNode( $txt ) ); |
60 | $domFragment->appendChild( $pre ); |
61 | |
62 | return $domFragment; |
63 | } |
64 | |
65 | } |