$t->linkPrefixRegex ?? null,
'linkTrailRegex' => $t->linkTrailRegex ?? null,
] );
$doc = ContentUtils::createAndLoadDocument( $t->html );
$node = DOMCompat::getBody( $doc )->firstChild;
DOMUtils::assertElt( $node );
$dataParsoid = DOMDataUtils::getDataParsoid( $node );
// Test ConstrainedText.fromSelSer
$ct = ConstrainedText::fromSelSer( $t->text, $node, $dataParsoid, $env );
$this->assertIsArray( $ct );
$this->assertSameSize( $t->types, $ct );
$actualNames = array_map( static function ( $x ) {
return get_class( $x );
}, $ct );
foreach ( $t->types as $i => $name ) {
$this->assertEquals( $name, $actualNames[$i] );
}
// Test ConstrainedText::escapeLine
foreach ( $t->escapes as $e ) {
$e = (object)$e;
$nct = $ct; // copy
if ( isset( $e->left ) ) {
$n = $node->ownerDocument->createTextNode( $e->left );
array_unshift( $nct, ConstrainedText::cast( $e->left, $n ) );
}
if ( isset( $e->right ) ) {
$n = $node->ownerDocument->createTextNode( $e->right );
$nct[] = ConstrainedText::cast( $e->right, $n );
}
$r = ConstrainedText::escapeLine( $nct );
$this->assertEquals( $e->output, $r );
}
}
// phpcs:disable Generic.Files.LineLength.TooLong
public function provideConstrainedText(): array {
return [
[ [
'name' => 'WikiLinkText: Simple',
'linkTrailRegex' => /* RegExp */ '/^([a-z]+)/', // enwiki
'html' => "Foo",
'types' => [ WikiLinkText::class ],
'text' => '[[Foo]]',
'escapes' => [
[
'output' => '[[Foo]]'
],
[
'left' => 'bar ',
'right' => ' bat',
'output' => 'bar [[Foo]] bat'
],
[
'left' => '[',
'right' => ']',
'output' => '[[[Foo]]]'
],
[
// not a link trail
'right' => "'s",
'output' => "[[Foo]]'s"
],
[
// a link trail
'right' => 's',
'output' => '[[Foo]]s'
]
]
] ],
[ [
'name' => 'WikiLinkText: iswiki linkprefix/linktrail',
'linkPrefixRegex' => /* RegExp */ "/[áÁðÐéÉíÍóÓúÚýÝþÞæÆöÖA-Za-z–-]+\$/",
'linkTrailRegex' => /* RegExp */ "/^([áðéíóúýþæöa-z-–]+)/",
'html' => "Foo",
'types' => [ WikiLinkText::class ],
'text' => '[[Foo]]',
'escapes' => [
[
'left' => 'bar ',
'right' => ' bat',
'output' => 'bar [[Foo]] bat'
],
[
'left' => '-',
'right' => '-',
'output' => '-[[Foo]]-'
]
]
] ],
[ [
'name' => 'WikiLinkText: iswiki greedy linktrails',
'linkPrefixRegex' => /* RegExp */ "/[áÁðÐéÉíÍóÓúÚýÝþÞæÆöÖA-Za-z–-]+\$/",
'linkTrailRegex' => /* RegExp */ "/^([áðéíóúýþæöa-z-–]+)/",
'html' => "
a-b
",
'types' => [
ConstrainedText::class,
WikiLinkText::class,
ConstrainedText::class,
WikiLinkText::class,
],
'text' => '[[a]]-[[b]]',
'escapes' => [ [
// this would be '[[a]]-[[b]] if the "greedy"
// functionality wasn't present; see commit
// 88605a4a7a37a61da76238db6d3fff756e8514f1
'output' => '[[a]]-[[b]]'
]
]
] ],
[ [
'name' => 'ExtLinkText',
'html' => "",
'types' => [
ExtLinkText::class,
],
'text' => '[https://example.com]',
'escapes' => [
[
// ExtLinkText isn't very interesting
'output' => '[https://example.com]'
],
[
'left' => '[',
'right' => ']',
// FIXME This output is wrong! See: T220018
'output' => '[[https://example.com]]'
]
]
] ],
[ [
'name' => 'AutoURLLinkText: no paren',
'html' => "http://example.com",
'types' => [
AutoURLLinkText::class
],
'text' => 'https://example.com',
'escapes' => [
[
'output' => 'https://example.com'
],
[
// Non-word characters are find in the prefix
'left' => '(',
'output' => '(https://example.com'
],
[
// Word characters need an escape
'left' => 'foo',
'right' => 'bar',
'output' => 'foohttps://example.combar'
],
[
// Close paren is fine in the trailing context so long
// as the URL doesn't have a paren.
'left' => '(',
'right' => ')',
'output' => '(https://example.com)'
],
[
// Ampersand isn't allowed in the trailing context...
'right' => '&',
'output' => 'https://example.com&'
],
[
// ...but an entity will terminate the autourl
'right' => '<',
'output' => 'https://example.com<'
],
[
// Single quote isn't allowed...
'right' => "'",
'output' => "https://example.com'"
],
[
// ...but double-quote (aka bold or italics) is fine
'left' => "''",
'right' => "''",
'output' => "''https://example.com''"
],
[
// Punctuation is okay.
'right' => '.',
'output' => 'https://example.com.'
],
[
'left' => '[',
'right' => ' foo]',
// FIXME This output is wrong! See: T220018
'output' => '[https://example.com foo]'
]
]
] ],
[ [
'name' => 'AutoURLLinkText: w/ paren',
'html' => "http://example.com/foo(bar",
'types' => [
AutoURLLinkText::class
],
'text' => 'https://example.com/foo(bar',
'escapes' => [
[
'output' => 'https://example.com/foo(bar'
],
[
// Close paren is escaped in the trailing context since
// the URL has a paren.
'left' => '(',
'right' => ')',
'output' => '(https://example.com/foo(bar)'
]
]
] ],
[ [
'name' => 'AutoURLLinkText: w/ ampersand',
'html' => "http://example.com?foo<",
'types' => [
AutoURLLinkText::class
],
'text' => 'https://example.com?foo<',
'escapes' => [
[
'output' => 'https://example.com?foo<'
],
[
'right' => '.',
'output' => 'https://example.com?foo<.'
],
[
// Careful of right contexts which could complete an
// entity
'right' => ';',
'output' => 'https://example.com?foo<'
]
]
] ],
[ [
'name' => 'MagicLinkText',
'html' => "ISBN 1234567890",
'types' => [
MagicLinkText::class
],
'text' => 'ISBN 1234567890',
'escapes' => [
[
'output' => 'ISBN 1234567890'
],
[
'left' => 'I',
'right' => '1',
'output' => 'IISBN 12345678901'
]
]
] ],
[ [
'name' => 'LanguageVariantText',
'html' => "",
'types' => [
LanguageVariantText::class
],
'text' => '-{raw}-',
'escapes' => [
[
'output' => '-{raw}-'
],
[
// single | at SOL causes issues with table markup
'left' => '|',
'output' => '|-{raw}-'
],
[
'left' => '||',
'output' => '||-{raw}-'
]
]
] ],
];
}
}