jenkins-bot has submitted this change and it was merged.

Change subject: T54341: Recover content from non-string tokens for img option 
alt
......................................................................


T54341: Recover content from non-string tokens for img option alt

* Updated stringifyOptionTokens to extract string from
  arbitrary wikitext for the alt image option as well (used
  to be for link only). Fixed a bug in that function as well.

* Fixes the parsing of snippets in the bug report.

* New added parser test is green with this change.

Change-Id: I962f8276bc10fcf9bde7e45a5b7196bf4976d398
---
M lib/ext.core.LinkHandler.js
M tests/parserTests.txt
2 files changed, 24 insertions(+), 9 deletions(-)

Approvals:
  Cscott: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/ext.core.LinkHandler.js b/lib/ext.core.LinkHandler.js
index 03b4464..4df0fd9 100644
--- a/lib/ext.core.LinkHandler.js
+++ b/lib/ext.core.LinkHandler.js
@@ -839,14 +839,14 @@
        prefix = prefix || '';
 
        for ( i = 0; i < tstream.length; i++ ) {
+               currentToken = tstream[i];
+
                if ( skipToEndOf ) {
                        if ( currentToken.name === skipToEndOf && 
currentToken.constructor === EndTagTk ) {
                                skipToEndOf = undefined;
                        }
                        continue;
                }
-
-               currentToken = tstream[i];
 
                if ( currentToken.constructor === String ) {
                        resultStr += currentToken;
@@ -858,20 +858,24 @@
                        }
 
                        resultStr += nextResult;
-               } else {
+               } else if (currentToken.constructor !== EndTagTk) {
                        // This is actually a token
                        if ( currentToken.name === 'a' ) {
                                if ( optInfo === undefined ) {
                                        optInfo = getOptionInfo( prefix + 
resultStr, env );
                                        if ( optInfo === null ) {
-                                               // An a tag before a valid 
option? This is most
-                                               // likely a caption.
+                                               // An <a> tag before a valid 
option?
+                                               // This is most likely a 
caption.
                                                optInfo = undefined;
                                                return null;
                                        }
                                }
 
-                               if ( optInfo.ck === 'link' ) {
+                               // link and alt options are whitelisted for 
accepting arbitrary
+                               // wikitext (even though only strings are 
supported in reality)
+                               // SSS FIXME: Is this actually true of all 
options rather than
+                               // just link and alt?
+                               if ( optInfo.ck === 'link' || optInfo.ck === 
'alt' ) {
                                        tokenType = Util.lookup( 
currentToken.attribs, 'rel' );
                                        tkHref = Util.lookup( 
currentToken.attribs, 'href' );
 
@@ -885,9 +889,9 @@
                                                // Tell our loop to skip to the 
end of this tag
                                                skipToEndOf = 'a';
                                        } else if ( tokenType === 'mw:WikiLink' 
) {
-                                               // This maybe assumes some 
stuff about wikilinks,
-                                               // but nothing we haven't 
already assumed
-                                               resultStr += tkHref.replace( 
/^(\.\.?\/)*/g, '' );
+                                               /* jshint noempty: false */
+                                               // Nothing to do -- the link 
content will be
+                                               // captured by walking the rest 
of the tokens.
                                        } else {
                                                // There shouldn't be any other 
kind of link...
                                                // This is likely a caption.
diff --git a/tests/parserTests.txt b/tests/parserTests.txt
index fd0701e..293f25f 100644
--- a/tests/parserTests.txt
+++ b/tests/parserTests.txt
@@ -12229,6 +12229,17 @@
 <p><span class="mw-default-size" typeof="mw:Image"><a 
href="File:Foobar.jpg"><img alt="testing bold in alt" 
resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" 
height="220" width="1941"/></a></span></p>
 !! end
 
+!! test
+Alt image option should handle most kinds of wikitext without barfing
+!! wikitext
+[[Image:Foobar.jpg|thumb|This is the image caption|alt=This is a [[link]] and 
a {{echo|''bold template''}}.]]
+!! html/php
+<div class="thumb tright"><div class="thumbinner" style="width:182px;"><a 
href="/wiki/File:Foobar.jpg" class="image"><img alt="This is a link and a bold 
template." 
src="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg"; 
width="180" height="20" class="thumbimage" 
srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/270px-Foobar.jpg 1.5x, 
http://example.com/images/thumb/3/3a/Foobar.jpg/360px-Foobar.jpg 2x" /></a>  
<div class="thumbcaption"><div class="magnify"><a href="/wiki/File:Foobar.jpg" 
class="internal" title="Enlarge"></a></div>This is the image 
caption</div></div></div>
+
+!! html/parsoid
+<figure class="mw-default-size" typeof="mw:Image/Thumb mw:ExpandedAttrs" 
about="#mwt2" 
data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"caption","ak":"This
 is the image caption"},{"ck":"alt","ak":"alt=This is a [[link]] and a 
{{echo|&#39;&#39;bold template&#39;&#39;}}."}]}' 
data-mw='{"attribs":[["thumbnail",{"html":"thumb"}],["alt",{"html":"alt=This is 
a &lt;a rel=\"mw:WikiLink\" href=\"./Link\" title=\"Link\" 
data-parsoid=\"{&amp;quot;stx&amp;quot;:&amp;quot;simple&amp;quot;,&amp;quot;a&amp;quot;:{&amp;quot;href&amp;quot;:&amp;quot;./Link&amp;quot;},&amp;quot;sa&amp;quot;:{&amp;quot;href&amp;quot;:&amp;quot;link&amp;quot;},&amp;quot;dsr&amp;quot;:[65,73,2,2]}\">link&lt;/a>
 and a &lt;i about=\"#mwt1\" typeof=\"mw:Transclusion\" 
data-parsoid=\"{&amp;quot;dsr&amp;quot;:[80,106,null,null],&amp;quot;pi&amp;quot;:[[{&amp;quot;k&amp;quot;:&amp;quot;1&amp;quot;,&amp;quot;spc&amp;quot;:[&amp;quot;&amp;quot;,&amp;quot;&amp;quot;,&amp;quot;&amp;quot;,&amp;quot;&amp;quot;]}]]}\"
 
data-mw=\"{&amp;quot;parts&amp;quot;:[{&amp;quot;template&amp;quot;:{&amp;quot;target&amp;quot;:{&amp;quot;wt&amp;quot;:&amp;quot;echo&amp;quot;,&amp;quot;href&amp;quot;:&amp;quot;./Template:Echo&amp;quot;},&amp;quot;params&amp;quot;:{&amp;quot;1&amp;quot;:{&amp;quot;wt&amp;quot;:&amp;quot;&#39;&#39;bold
 template&#39;&#39;&amp;quot;}},&amp;quot;i&amp;quot;:0}}]}\">bold 
template&lt;/i>."}]]}'><a href="./File:Foobar.jpg" 
data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{}}'><img alt="This is a 
link and a bold template." resource="./File:Foobar.jpg" 
src="//example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" height="25" 
width="220" data-parsoid='{"a":{"alt":"This is a link and a bold 
template.","resource":"./File:Foobar.jpg","height":"25","width":"220"},"sa":{"alt":"alt=This
 is a [[link]] and a {{echo|&#39;&#39;bold 
template&#39;&#39;}}.","resource":"Image:Foobar.jpg"}}'/></a><figcaption>This 
is the image caption</figcaption></figure>
+!! end
+
 ###################
 # Conflicting image format options.
 # First option specified should 'win'.

-- 
To view, visit https://gerrit.wikimedia.org/r/197120
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I962f8276bc10fcf9bde7e45a5b7196bf4976d398
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: Cscott <[email protected]>
Gerrit-Reviewer: Marcoil <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to