From ad80f0bca27c2b0905b2b137977586bfab80db34 Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Mon, 15 Oct 2018 16:39:19 -0400 Subject: [PATCH] Handle and other markup consistently in image link/alt options Use Parser::stripAltText() consistently to handle link and alt options in both Parser::makeImage() and Parser::renderImageGallery(). This ensures that link option text can use to escape problematic text so that (for example) the following works: ``` [[File:Foobar.jpg|link=a''b''c|alt=a''b''c]] File:Foobar.jpg|link=a''b''c|alt=a''b''c ``` Previously the handling of the link option in Parser::renderImageGallery() used a bespoke `strip_tags` invocation which didn't replace s, and the handling of the link option in Parser::makeImage() didn't strip tags at all, nor did it replace s. For example, in Parser::makeImage() double quotes in titles would be converted to embedded `` tags before being passed to Parser::parseLinkParameter(), with predictably poor results. Tests added to confirm behavior of alt/link with HTML-escaped entities and s exposed a bug in Remex: T207088. Tests will fail on PHP 7.0 until that is fixed. Bug: T206940 Depends-On: Ide67bba20f771868c0e119cb2874464dcf1d758a Change-Id: Ife4c0edaa85e0cb294c5d4c1e31d5d7d828d9df4 --- includes/parser/Parser.php | 7 +- tests/parser/parserTests.txt | 346 +++++++++++++++++++++++++++++++++++ 2 files changed, 351 insertions(+), 2 deletions(-) diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index db0407752b..efd98fd285 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -5133,7 +5133,7 @@ class Parser { $alt = $this->stripAltText( $match, false ); break; case 'gallery-internal-link': - $linkValue = strip_tags( $this->replaceLinkHoldersText( $match ) ); + $linkValue = $this->stripAltText( $match, false ); if ( preg_match( '/^-{R|(.*)}-$/', $linkValue ) ) { // Result of LanguageConverter::markNoConversion // invoked on an external link. @@ -5327,7 +5327,10 @@ class Parser { $value = $this->stripAltText( $value, $holders ); break; case 'link': - list( $paramName, $value ) = $this->parseLinkParameter( $value ); + list( $paramName, $value ) = + $this->parseLinkParameter( + $this->stripAltText( $value, $holders ) + ); if ( $paramName ) { $validated = true; if ( $paramName === 'no-link' ) { diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index bbd9ecbe99..f56e4c8301 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -56,6 +56,12 @@ Foo FOO !!endarticle +!!article +Foo''s bar''s +!!text +Article titles can contain single quotes! +!!endarticle + !!article Template:Foo !!text @@ -8022,6 +8028,31 @@ Link containing an equals sign

Special:BookSources/isbn=4-00-026157-6

!! end +!! article +Foo & bar +!! text +Just a test of an article title containing an ampersand +!! endarticle + +!! test +Link containing an ampersand +!! wikitext +[[Foo & bar]] + +[[Foo & bar]] + +[[Foo & bar]] +!! html/php+tidy +

Foo & bar +

Foo & bar +

[[Foo & bar]] +

+!! html/parsoid +

Foo & bar

+

Foo & bar

+

[[Foo & bar]]

+!! end + !! article Foo~bar !! text @@ -15001,6 +15032,321 @@ Alt image option should handle most kinds of wikitext without barfing
This is a link and a bold template.
This is the image caption
!! end +!! test +Ampersand in alt attribute (T206940) +!! wikitext +[[File:Foobar.jpg|alt=&]] + + + +File:Foobar.jpg|alt=& + +!! html/php+tidy +

& +

+ +!! html/parsoid +

&

+ + + +!! end + +!! test +Italics markup in alt attribute (T206940) +!! wikitext +[[File:Foobar.jpg|alt=''x''|caption]] + + + +File:Foobar.jpg|alt=''x''|caption + +!! html/php+tidy +

x +

+ +!! html/parsoid +

x

+ + + +!! end + +!! test +Nowiki markup in alt attribute (T206940) +!! wikitext +[[File:Foobar.jpg|alt=''x''|caption]] + + + +File:Foobar.jpg|alt=''x''|caption + +!! html/php+tidy +

''x'' +

+ +!! html/parsoid +

''x''

+ + + +!! end + +!! test +Nowiki markup in alt attribute (edited html, no data-parsoid) (T206940) +!! wikitext +[[File:Foobar.jpg|alt=''x''|caption]] + + + +File:Foobar.jpg|alt=''x''|caption + +!! html/php+tidy +

''x'' +

+ +!! html/parsoid +

''x''

+ + + +!! end + +!! test +Ampersand in link attribute (T206940) +!! wikitext +[[File:Foobar.jpg|link=Foo & bar]] + + + +File:Foobar.jpg|link=Foo & bar + +!! html/php+tidy +

Foobar.jpg +

+ +!! html/parsoid +

+ + + +!! end + +!! test +Ampersand in link attribute (edited html, no data-parsoid) (T206940) +!! wikitext +[[File:Foobar.jpg|link=Foo_&_bar]] + + + +File:Foobar.jpg|link=Foo_&_bar + +!! html/php+tidy +

Foobar.jpg +

+ +!! html/parsoid +

+ + + +!! end + +!! test +Italics markup in link attribute (T206940) +!! wikitext +[[Foo''s bar''s]] + + +[[File:Foobar.jpg|link=Foo''s bar''s|caption1]] + +[[File:Foobar.jpg|link=''Main Page''|caption2]] + + + +File:Foobar.jpg|link=Foo''s bar''s|caption1 +File:Foobar.jpg|link=''Main Page''|caption2 + +!! html/php+tidy +

Foo''s bar''s +

caption1 +

caption2 +

+ +!! html/parsoid +

Foo''s bar''s

+ + +

+ +

+ + + +!! end + +!! test +Nowiki markup in link attribute (T206940) +!! wikitext +[[File:Foobar.jpg|link=Foo''s_bar''s|caption]] + + + +File:Foobar.jpg|link=Foo''s_bar''s|caption + +!! html/php+tidy +

caption +

+ +!! html/parsoid +

+ + + +!! end + +!! test +Nowiki markup in link attribute (edited html, no data-parsoid) (T206940) +!! wikitext +[[File:Foobar.jpg|link=Foo''s_bar''s|caption]] + + + +File:Foobar.jpg|link=Foo''s_bar''s|caption + +!! html/php+tidy +

caption +

+ +!! html/parsoid +

+ + + +!! end + !! test Image with table with attributes in caption !! options -- 2.20.1