Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
47.83% |
11 / 23 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Pre | |
47.83% |
11 / 23 |
|
50.00% |
1 / 2 |
2.57 | |
0.00% |
0 / 1 |
| getConfig | |
0.00% |
0 / 12 |
|
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 | '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 $content, array $args |
| 42 | ): DocumentFragment { |
| 43 | $domFragment = $extApi->htmlToDom( '' ); |
| 44 | $doc = $domFragment->ownerDocument; |
| 45 | $pre = $doc->createElement( 'pre' ); |
| 46 | |
| 47 | Sanitizer::applySanitizedArgs( $extApi->getSiteConfig(), $pre, $args ); |
| 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 | $content = preg_replace( '/<nowiki\s*>(.*?)<\/nowiki\s*>/s', '$1', $content ); |
| 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 | $content = PHPUtils::stripPrefix( $content, "\n" ); |
| 60 | |
| 61 | // `extSrc` will take care of rt'ing these |
| 62 | $content = Utils::decodeWtEntities( $content ); |
| 63 | |
| 64 | $pre->appendChild( $doc->createTextNode( $content ) ); |
| 65 | $domFragment->appendChild( $pre ); |
| 66 | |
| 67 | return $domFragment; |
| 68 | } |
| 69 | |
| 70 | } |