Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
46.67% |
14 / 30 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Pre | |
46.67% |
14 / 30 |
|
0.00% |
0 / 2 |
4.37 | |
0.00% |
0 / 1 |
| getConfig | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| sourceToDom | |
66.67% |
14 / 21 |
|
0.00% |
0 / 1 |
2.15 | |||
| 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 $content, array $args |
| 37 | ): DocumentFragment { |
| 38 | $domFragment = $extApi->htmlToDom( '' ); |
| 39 | $doc = $domFragment->ownerDocument; |
| 40 | $pre = $doc->createElement( 'pre' ); |
| 41 | |
| 42 | $kvArgs = $extApi->extArgsToArray( $args ); |
| 43 | if ( ( $kvArgs['format'] ?? '' ) === 'wikitext' ) { |
| 44 | return $extApi->extTagToDOM( $args, $content, [ |
| 45 | 'wrapperTag' => 'pre', |
| 46 | 'parseOpts' => [ |
| 47 | 'extTag' => 'pre', |
| 48 | 'context' => 'inline', |
| 49 | ], |
| 50 | ] ); |
| 51 | } |
| 52 | |
| 53 | Sanitizer::applySanitizedArgs( $extApi->getSiteConfig(), $pre, $args ); |
| 54 | DOMDataUtils::getDataParsoid( $pre )->stx = 'html'; |
| 55 | |
| 56 | // #tag parserfunction may return nowikis as a fragments in the |
| 57 | // body which is needed for when we're processing format wikitext |
| 58 | // above |
| 59 | $content = $extApi->removeNowikiEscapesFromContent( $content ); |
| 60 | |
| 61 | // Support nowikis in pre. Do this before stripping newlines, see test, |
| 62 | // "<pre> with <nowiki> inside (compatibility with 1.6 and earlier)" |
| 63 | $content = preg_replace( '/<nowiki\s*>(.*?)<\/nowiki\s*>/s', '$1', $content ); |
| 64 | |
| 65 | // Strip leading newline to match legacy php parser. This is probably because |
| 66 | // it doesn't do xml serialization accounting for `newlineStrippingElements` |
| 67 | // Of course, this leads to indistinguishability between n=0 and n=1 |
| 68 | // newlines, but that only seems to affect parserTests output. Rendering |
| 69 | // is the same, and the newline is preserved for rt in the `extSrc`. |
| 70 | $content = PHPUtils::stripPrefix( $content, "\n" ); |
| 71 | |
| 72 | // `extSrc` will take care of rt'ing these |
| 73 | $content = Utils::decodeWtEntities( $content ); |
| 74 | |
| 75 | $pre->appendChild( $doc->createTextNode( $content ) ); |
| 76 | $domFragment->appendChild( $pre ); |
| 77 | |
| 78 | return $domFragment; |
| 79 | } |
| 80 | |
| 81 | } |