Merge "Accept BCP 47 codes in LanguageConverter rules"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 27 Nov 2018 18:49:25 +0000 (18:49 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 27 Nov 2018 18:49:25 +0000 (18:49 +0000)
1  2 
languages/ConverterRule.php
languages/LanguageConverter.php
tests/parser/parserTests.txt

@@@ -20,7 -20,7 +20,7 @@@
   */
  
  /**
 - * Parser for rules of language conversion , parse rules in -{ }- tag.
 + * Parser for rules of language conversion, parse rules in -{ }- tag.
   * @ingroup Language
   * @author fdcn <fdcn64@gmail.com>, PhiLiP <philip.npc@gmail.com>
   */
@@@ -29,13 -29,13 +29,13 @@@ class ConverterRule 
        public $mConverter; // LanguageConverter object
        public $mRuleDisplay = '';
        public $mRuleTitle = false;
 -      public $mRules = '';// string : the text of the rules
 +      public $mRules = ''; // string : the text of the rules
        public $mRulesAction = 'none';
        public $mFlags = [];
        public $mVariantFlags = [];
        public $mConvTable = [];
 -      public $mBidtable = [];// array of the translation in each variant
 -      public $mUnidtable = [];// array of the translation in each variant
 +      public $mBidtable = []; // array of the translation in each variant
 +      public $mUnidtable = []; // array of the translation in each variant
  
        /**
         * @param string $text The text between -{ and }-
                        $to = trim( $v[1] );
                        $v = trim( $v[0] );
                        $u = explode( '=>', $v, 2 );
+                       $vv = $this->mConverter->validateVariant( $v );
                        // if $to is empty (which is also used as $from in bidtable),
                        // strtr() could return a wrong result.
-                       if ( count( $u ) == 1 && $to !== '' && in_array( $v, $variants ) ) {
-                               $bidtable[$v] = $to;
+                       if ( count( $u ) == 1 && $to !== '' && $vv ) {
+                               $bidtable[$vv] = $to;
                        } elseif ( count( $u ) == 2 ) {
                                $from = trim( $u[0] );
                                $v = trim( $u[1] );
+                               $vv = $this->mConverter->validateVariant( $v );
                                // if $from is empty, strtr() could return a wrong result.
-                               if ( array_key_exists( $v, $unidtable )
-                                       && !is_array( $unidtable[$v] )
+                               if ( array_key_exists( $vv, $unidtable )
+                                       && !is_array( $unidtable[$vv] )
                                        && $from !== ''
-                                       && in_array( $v, $variants ) ) {
-                                       $unidtable[$v] = [ $from => $to ];
-                               } elseif ( $from !== '' && in_array( $v, $variants ) ) {
-                                       $unidtable[$v][$from] = $to;
+                                       && $vv ) {
+                                       $unidtable[$vv] = [ $from => $to ];
+                               } elseif ( $from !== '' && $vv ) {
+                                       $unidtable[$vv][$from] = $to;
                                }
                        }
                        // syntax error, pass
-                       if ( !isset( $this->mConverter->mVariantNames[$v] ) ) {
+                       if ( !isset( $this->mConverter->mVariantNames[$vv] ) ) {
                                $bidtable = [];
                                $unidtable = [];
                                break;
                                case 'N':
                                        // process N flag: output current variant name
                                        $ruleVar = trim( $rules );
 -                                      if ( isset( $this->mConverter->mVariantNames[$ruleVar] ) ) {
 -                                              $this->mRuleDisplay = $this->mConverter->mVariantNames[$ruleVar];
 -                                      } else {
 -                                              $this->mRuleDisplay = '';
 -                                      }
 +                                      $this->mRuleDisplay = $this->mConverter->mVariantNames[$ruleVar] ?? '';
                                        break;
                                case 'D':
                                        // process D flag: output rules description
@@@ -140,7 -140,10 +140,7 @@@ class LanguageConverter 
         *   main code if there is no fallback
         */
        public function getVariantFallbacks( $variant ) {
 -              if ( isset( $this->mVariantFallbacks[$variant] ) ) {
 -                      return $this->mVariantFallbacks[$variant];
 -              }
 -              return $this->mMainLanguageCode;
 +              return $this->mVariantFallbacks[$variant] ?? $this->mMainLanguageCode;
        }
  
        /**
        }
  
        /**
 -       * Validate the variant
 +       * Validate the variant and return an appropriate strict internal
 +       * variant code if one exists.  Compare to Language::hasVariant()
 +       * which does a strict test.
 +       *
         * @param string|null $variant The variant to validate
 -       * @return mixed Returns the variant if it is valid, null otherwise
 +       * @return mixed Returns an equivalent valid variant code if possible,
 +       *   null otherwise
         */
        public function validateVariant( $variant = null ) {
                if ( $variant === null ) {
                        //    [1] => 'zh-hant:<span style="font-size:120%;">yyy</span>'
                        //    [2] => ''
                        //  ]
-                       $pat = '/;\s*(?=';
+                       $expandedVariants = [];
                        foreach ( $this->mVariants as $variant ) {
+                               $expandedVariants[ $variant ] = 1;
+                               // Accept standard BCP 47 names for variants as well.
+                               $expandedVariants[ LanguageCode::bcp47( $variant ) ] = 1;
+                       }
+                       // Accept old deprecated names for variants
+                       foreach ( LanguageCode::getDeprecatedCodeMapping() as $old => $new ) {
+                               if ( isset( $expandedVariants[ $new ] ) ) {
+                                       $expandedVariants[ $old ] = 1;
+                               }
+                       }
+                       $pat = '/;\s*(?=';
+                       foreach ( $expandedVariants as $variant => $ignore ) {
                                // zh-hans:xxx;zh-hant:yyy
                                $pat .= $variant . '\s*:|';
                                // xxx=>zh-hans:yyy; xxx=>zh-hant:zzz
@@@ -37,7 -37,7 +37,7 @@@
  # You can also set the following parser properties via test options:
  #  wgEnableUploads, wgAllowExternalImages, wgMaxTocLevel,
  #  wgLinkHolderBatchSize, wgRawHtml, wgInterwikiMagic,
 -#  wgEnableMagicLinks
 +#  wgEnableMagicLinks, wgMediaInTargetLanguage
  #
  # For testing purposes, temporary articles can created:
  # !!article / NAMESPACE:TITLE / !!text / ARTICLE TEXT / !!endarticle
@@@ -56,12 -56,6 +56,12 @@@ Fo
  FOO
  !!endarticle
  
 +!!article
 +Foo''s bar''s
 +!!text
 +Article titles can contain single quotes!
 +!!endarticle
 +
  !!article
  Template:Foo
  !!text
@@@ -1435,7 -1429,7 +1435,7 @@@ Non-word characters don't terminate ta
  !! end
  
  ###
 -### See tests/parser/parserTestsParserHook.php for the <tåg> extension)
 +### See tests/parser/ParserTestParserHook.php for the <tåg> extension)
  ### This checks that HTML5 tags (with non-word characters in the tag
  ### name) make it safely through the parser -- the Sanitizer will
  ### munge them later, as it should.
@@@ -2834,7 -2828,7 +2834,7 @@@ two">hi</pre
  <pre class="one two">hi</pre>
  
  !! html/parsoid
 -<pre typeof="mw:Extension/pre" about="#mwt2" class="one two" data-mw='{"name":"pre","attrs":{"class":"one two"},"body":{"extsrc":"hi"}}'>hi</pre>
 +<pre class="one two" typeof="mw:Extension/pre" about="#mwt2" data-mw='{"name":"pre","attrs":{"class":"one two"},"body":{"extsrc":"hi"}}'>hi</pre>
  !! end
  
  !! test
@@@ -2869,7 -2863,7 +2869,7 @@@ parsoid=wt2htm
  <pre>x</pre>
  &lt;pre <table></table>
  !! html/parsoid
 -<pre typeof="mw:Transclusion mw:Extension/pre" about="#mwt2" data-parsoid='{"stx":"html","a":{"&lt;pre":null},"sa":{"&lt;pre":""},"pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;pre &lt;pre>x&lt;/pre>"}},"i":0}}]}'>x</pre>
 +<pre typeof="mw:Extension/pre mw:Transclusion" about="#mwt2" data-parsoid='{"stx":"html","a":{"&lt;pre":null},"sa":{"&lt;pre":""},"pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;pre &lt;pre>x&lt;/pre>"}},"i":0}}]}'>x</pre>
  
  <pre data-parsoid='{"stx":"html","src":"&lt;pre &lt;/table>","tagWidths":[13,0],"a":{"&lt;":null,"table":null},"sa":{"&lt;":"","table":""},"fostered":true,"autoInsertedEnd":true}'></pre><table data-parsoid='{"stx":"html","autoInsertedEnd":true}'></table>
  !! end
@@@ -2914,23 -2908,6 +2914,23 @@@ Self-closed pr
  <pre typeof="mw:Extension/pre" about="#mwt2" data-mw='{"name":"pre","attrs":{}}'></pre>
  !! end
  
 +!! test
 +Newline before table-close generates empty table row: T208619
 +!! wikitext
 +{|
 +
 +|}
 +!! html/php+tidy
 +<table>
 +
 +<tbody><tr><td></td></tr></tbody></table>
 +!! html/parsoid
 +<table data-parsoid='{}'>
 +
 +</table>
 +!! end
 +
 +# PHP has one more row in the output than Parsoid does: T208619
  !! test
  Parsoid: Don't paragraph-wrap fosterable content even if table syntax is unbalanced
  !! options
@@@ -2944,16 -2921,7 +2944,16 @@@ parsoid=wt2htm
  
  
  |}
 -!! html
 +!! html/php+tidy
 +<table>
 +<tbody><tr><td>
 +</td><td>
 +</td>
 +
 +
 +
 +</tr><tr><td></td></tr></tbody></table>
 +!! html/parsoid
  <table>
  
  <tbody>
@@@ -3231,13 -3199,12 +3231,13 @@@ data-mw='{"parts":[{"template":{"target
  
  !! test
  Parsoid: Pipe in template with nested template in external link target in template parameter (seriously)
 -!! options
 -parsoid
  !! wikitext
  {{echo|[{{fullurl:{{FULLPAGENAME}}|action=edit}} bar]}}
 -!! html
 -<p typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[{{fullurl:{{FULLPAGENAME}}|action=edit}} bar]"}},"i":0}}]}'>[Main Page bar]</p>
 +!! html/php+tidy
 +<p><a rel="nofollow" class="external text" href="http://example.org/index.php?title=Parser_test&amp;action=edit">bar</a>
 +</p>
 +!! html/parsoid
 +<p><a rel="mw:ExtLink" class="external text" href="http://example.org/index.php?title=Parser_test&amp;action=edit" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[{{fullurl:{{FULLPAGENAME}}|action=edit}} bar]"}},"i":0}}]}'>bar</a></p>
  !! end
  
  !! test
@@@ -3681,7 -3648,7 +3681,7 @@@ array 
   <pre class="123">hi</pre>
  
  !! html/parsoid
 - <pre typeof="mw:Extension/pre" about="#mwt2" class="123" data-mw='{"name":"pre","attrs":{"class":"123"},"body":{"extsrc":"hi"}}'>hi</pre>
 + <pre class="123" typeof="mw:Extension/pre" about="#mwt2" data-mw='{"name":"pre","attrs":{"class":"123"},"body":{"extsrc":"hi"}}'>hi</pre>
  !! end
  
  !!test
@@@ -6291,22 -6258,7 +6291,22 @@@ parsoid=wt2htm
  {|style="color: red;" |} id="foo"
  |foo
  |}
 -!! html
 +!! html/php+tidy
 +<table style="color: red;">
 +
 +</table><table style="color: red;">
 +<tbody><tr>
 +<td>foo
 +</td></tr></tbody></table>
 +<table style="color: red;" id="foo">
 +<tbody><tr>
 +<td>foo
 +</td></tr></tbody></table>
 +<table style="color: red;" id="foo">
 +<tbody><tr>
 +<td>foo
 +</td></tr></tbody></table>
 +!! html/parsoid
  <table style="color: red;"></table>
  
  <table style="color: red;">
@@@ -6923,9 -6875,9 +6923,9 @@@ Don't break on | in extension attribut
  
  <references />
  !! html/parsoid
 -<p><sup about="#mwt2" class="mw-ref" id="cite_ref-hi|ho_1-0" rel="dc:references" typeof="mw:Transclusion  mw:Extension/ref" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;ref name=\"hi|ho\">ha&lt;/ref>"}},"i":0}}]}'><a href="./Main_Page#cite_note-hi|ho-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></sup></p>
 +<p><sup about="#mwt2" class="mw-ref" id="cite_ref-hi|ho_1-0" rel="dc:references" typeof="mw:Transclusion  mw:Extension/ref" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;ref name=\"hi|ho\">ha&lt;/ref>"}},"i":0}}]}'><a href="./Parser_test#cite_note-hi|ho-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></sup></p>
  
 -<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt5" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-hi|ho-1" id="cite_note-hi|ho-1"><a href="./Main_Page#cite_ref-hi|ho_1-0" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-hi|ho-1" class="mw-reference-text">ha</span></li></ol>
 +<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt5" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-hi|ho-1" id="cite_note-hi|ho-1"><a href="./Parser_test#cite_ref-hi|ho_1-0" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-hi|ho-1" class="mw-reference-text">ha</span></li></ol>
  !! end
  
  ## We don't support roundtripping of these attributes in Parsoid.
@@@ -7098,9 -7050,9 +7098,9 @@@ T107652: <ref>s in templates that also 
  <references />
  !! html/parsoid
  <table>
 -<tbody><tr><td style="background:#f9f9f9;" typeof="mw:Transclusion" about="#mwt1" data-mw='{"parts":["|",{"template":{"target":{"wt":"table_attribs_7","href":"./Template:Table_attribs_7"},"params":{},"i":0}}]}'>Foo<sup class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Extension/ref" data-mw='{"name":"ref","attrs":{},"body":{"id":"mw-reference-text-cite_note-1"}}'><a href="./Main_Page#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></s></td></tr>
 +<tbody><tr><td style="background:#f9f9f9;" typeof="mw:Transclusion" about="#mwt1" data-mw='{"parts":["|",{"template":{"target":{"wt":"table_attribs_7","href":"./Template:Table_attribs_7"},"params":{},"i":0}}]}'>Foo<sup class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Extension/ref" data-mw='{"name":"ref","attrs":{},"body":{"id":"mw-reference-text-cite_note-1"}}'><a href="./Parser_test#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></s></td></tr>
  </tbody></table>
 -<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt5" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Main_Page#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text" data-parsoid="{}">foo</span></li></ol>
 +<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt5" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Parser_test#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text" data-parsoid="{}">foo</span></li></ol>
  !! end
  
  !! test
@@@ -7537,7 -7489,6 +7537,7 @@@ fo
  </tbody></table>
  !!end
  
 +# Note: PHP parser omits empty rows
  !! test
  Tables: Digest broken attributes on table and tr tag
  !! options
@@@ -7547,12 -7498,7 +7547,12 @@@ parsoid=wt2htm
  |- || || ++ --
  |- > [
  |}
 -!! html
 +!! html/php+tidy
 +<table>
 +
 +
 +</table>
 +!! html/parsoid
  <table>
  <tbody>
  <tr class='mw-empty-elt'></tr>
@@@ -7667,7 -7613,7 +7667,7 @@@ parsoid=html2w
  Wikitext tables can be nested inside HTML tables
  !! options
  parsoid=html2wt
 -!! html
 +!! html/parsoid
  <table data-parsoid='{"stx":"html"}'>
  <tr><td>
  <table>
  |}
  </td></tr>
  </table>
 +!! html/php+tidy
 +<table>
 +<tbody><tr><td>
 +<table>
 +<tbody><tr>
 +<td>foo
 +</td></tr></tbody></table>
 +</td></tr>
 +</tbody></table>
  !! end
  
  ###
@@@ -8042,8 -7979,6 +8042,8 @@@ Link containing % as a double hex seque
  ## Example for such a section: == < ==
  !! test
  Link containing "#<" and "#>" % as a hex sequences- these are valid section anchors
 +!! options
 +title=[[Main Page]]
  !! config
  wgFragmentMode=[ 'html5', 'legacy' ]
  !! wikitext
@@@ -8087,31 -8022,6 +8087,31 @@@ Link containing an equals sig
  <p><a rel="mw:WikiLink" href="./Special:BookSources/isbn=4-00-026157-6" title="Special:BookSources/isbn=4-00-026157-6">Special:BookSources/isbn=4-00-026157-6</a></p>
  !! 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 &amp; bar]]
 +
 +[[Foo &amp;amp; bar]]
 +!! html/php+tidy
 +<p><a href="/wiki/Foo_%26_bar" title="Foo &amp; bar">Foo &amp; bar</a>
 +</p><p><a href="/wiki/Foo_%26_bar" title="Foo &amp; bar">Foo &amp; bar</a>
 +</p><p>[[Foo &amp;amp; bar]]
 +</p>
 +!! html/parsoid
 +<p><a rel="mw:WikiLink" href="./Foo_&amp;_bar" title="Foo &amp; bar">Foo &amp; bar</a></p>
 +<p><a rel="mw:WikiLink" href="./Foo_&amp;_bar" title="Foo &amp; bar" data-parsoid='{"stx":"simple","a":{"href":"./Foo_&amp;_bar"},"sa":{"href":"Foo &amp;amp; bar"}}'>Foo &amp; bar</a></p>
 +<p>[[Foo <span typeof="mw:Entity" data-parsoid='{"src":"&amp;amp;","srcContent":"&amp;"}'>&amp;</span>amp; bar]]</p>
 +!! end
 +
  !! article
  Foo~bar
  !! text
@@@ -8592,32 -8502,27 +8592,32 @@@ parsoid=html2w
  1. Interaction of linktrail and template encapsulation
  !! wikitext
  {{echo|[[Foo]]}}l
 +!! html/php+tidy
 +<p><a href="/wiki/Foo" title="Foo">Fool</a>
 +</p>
  !! html/parsoid
  <p><a rel="mw:WikiLink" href="./Foo" title="Foo" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[[Foo]]"}},"i":0}},"l"]}'>Fool</a></p>
  !! end
  
  !! test
  2. Interaction of linktrail and template encapsulation
 -!! options
 -parsoid
  !! wikitext
  {{echo|Some [[Fool]]}}s
 -!! html
 +!! html/php+tidy
 +<p>Some <a href="/index.php?title=Fool&amp;action=edit&amp;redlink=1" class="new" title="Fool (page does not exist)">Fools</a>
 +</p>
 +!! html/parsoid
  <p><span about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"Some [[Fool]]"}},"i":0}},"s"]}' data-parsoid='{"pi":[[{"k":"1"}]]}'>Some </span><a rel="mw:WikiLink" href="./Fool" title="Fool" about="#mwt1" data-parsoid='{"stx":"simple","a":{"href":"./Fool"},"sa":{"href":"Fool"},"tail":"s"}'>Fools</a></p>
  !! end
  
  !! test
  3. Interaction of linktrail and template encapsulation
 -!! options
 -parsoid
  !! wikitext
  {{echo|Some [[Fool]]s are '''bold and foolish'''}}
 -!! html
 +!! html/php+tidy
 +<p>Some <a href="/index.php?title=Fool&amp;action=edit&amp;redlink=1" class="new" title="Fool (page does not exist)">Fools</a> are <b>bold and foolish</b>
 +</p>
 +!! html/parsoid
  <p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"Some [[Fool]]s are &#39;&#39;&#39;bold and foolish&#39;&#39;&#39;"}},"i":0}}]}' data-parsoid='{"pi":[[{"k":"1"}]]}'>Some <a rel="mw:WikiLink" href="./Fool" title="Fool" data-parsoid='{"stx":"simple","a":{"href":"./Fool"},"sa":{"href":"Fool"},"tail":"s"}'>Fools</a> are <b>bold and foolish</b></p>
  !! end
  
@@@ -8797,13 -8702,15 +8797,13 @@@ parsoid=wt2html,wt2w
  !! wikitext
  *[[Wikipedia:ro:Olteni&#0355;a]]
  *[[Wikipedia:ro:Olteni&#355;a]]
 -!! html
 +!! html/php
  <ul><li><a href="http://en.wikipedia.org/wiki/ro:Olteni%C5%A3a" class="extiw" title="wikipedia:ro:Olteniţa">Wikipedia:ro:Olteni&#355;a</a></li>
  <li><a href="http://en.wikipedia.org/wiki/ro:Olteni%C5%A3a" class="extiw" title="wikipedia:ro:Olteniţa">Wikipedia:ro:Olteni&#355;a</a></li></ul>
  
  !! html/php+tidy
 -<ul>
 -<li><a href="http://en.wikipedia.org/wiki/ro:Olteni%C5%A3a" class="extiw" title="wikipedia:ro:Olteniţa">Wikipedia:ro:Olteniţa</a></li>
 -<li><a href="http://en.wikipedia.org/wiki/ro:Olteni%C5%A3a" class="extiw" title="wikipedia:ro:Olteniţa">Wikipedia:ro:Olteniţa</a></li>
 -</ul>
 +<ul><li><a href="http://en.wikipedia.org/wiki/ro:Olteni%C5%A3a" class="extiw" title="wikipedia:ro:Olteniţa">Wikipedia:ro:Olteni&#355;a</a></li>
 +<li><a href="http://en.wikipedia.org/wiki/ro:Olteni%C5%A3a" class="extiw" title="wikipedia:ro:Olteniţa">Wikipedia:ro:Olteni&#355;a</a></li></ul>
  !! html/parsoid
  <ul>
  <li><a rel="mw:WikiLink/Interwiki" href="http://en.wikipedia.org/wiki/ro:Olteniţa" title="wikipedia:ro:Olteniţa">Wikipedia:ro:Olteniţa</a></li>
@@@ -8822,8 -8729,6 +8822,8 @@@ Interwiki link with fragment (T4130
  
  !! test
  Link scenarios with escaped fragments
 +!! options
 +title=[[Main Page]]
  !! config
  wgFragmentMode=[ 'html5', 'legacy' ]
  !! wikitext
@@@ -9650,14 -9555,17 +9650,14 @@@ Handling html with a div self-closing t
  <div title=bar />
  <div title=bar/>
  <div title=bar/ >
 -!! html/php
 -<p>&lt;div title /&gt;
 -&lt;div title/&gt;
 -</p>
 -<div>
 -<p>&lt;div title=bar /&gt;
 -&lt;div title=bar/&gt;
 -</p>
 -<div title="bar/"></div>
 -</div>
 -
 +!! html/php+tidy
 +<div title=""></div>
 +<div title=""></div>
 +<div title="">
 +<div title="bar"></div>
 +<div title="bar"></div>
 +<div title="bar/">
 +</div></div>
  !! html/parsoid
  <div title="" data-parsoid='{"stx":"html","selfClose":true}'></div>
  <div title="" data-parsoid='{"stx":"html","selfClose":true}'></div>
@@@ -9698,10 -9606,10 +9698,10 @@@ Handling html with a br self-closing ta
  <br title=bar />
  <br title=bar/>
  <br title=bar/ >
 -!! html/php
 +!! html/php+tidy
  <p><br title="" />
  <br title="" />
 -<br />
 +<br title="" />
  <br title="bar" />
  <br title="bar" />
  <br title="bar/" />
  </p>
  !! end
  
 +!! test
 +Quoted attributes without spaces
 +!! options
 +parsoid=wt2html
 +!! wikitext
 +<div class="foo"style="color:red">red</div>
 +!! html/php+tidy
 +<div class="foo" style="color:red">red</div>
 +!! html/parsoid
 +<div class="foo" style="color:red">red</div>
 +!! end
 +
  !! test
  Horizontal ruler (should it add that extra space?)
  !! wikitext
@@@ -9983,14 -9879,12 +9983,14 @@@ Nested lists 8 (multiple nesting transi
  
  !! end
  
 +# XXX this test should be moved to citeParserTests, since it depends
 +# on the Cite extension, which is "built in" to Parsoid.
  !! test
  Nested lists 9 (extension interaction)
 -!! options
 -parsoid
  !! wikitext
  *<references />
 +!! html/php+tidy-DISABLED
 +<ul><li class="mw-empty-elt"></li></ul>
  !! html/parsoid
  <ul><li data-parsoid='{}'><ol class="mw-references references" typeof="mw:Extension/references" about="#mwt2" data-parsoid='{}' data-mw='{"name":"references","attrs":{}}'></ol></li></ul>
  !! end
@@@ -10229,6 -10123,8 +10229,6 @@@ parsoid=wt2html,wt2w
  
  !! test
  Parsoid: Make sure nested lists are serialized on their own line even if HTML contains no newlines
 -!! options
 -parsoid
  !! wikitext
  #foo
  ##bar
  
  :foo
  ::bar
 -!! html
 +!! html/php+tidy
 +<ol><li>foo
 +<ol><li>bar</li></ol></li></ol>
 +<ul><li>foo
 +<ul><li>bar</li></ul></li></ul>
 +<dl><dd>foo
 +<dl><dd>bar</dd></dl></dd></dl>
 +!! html/parsoid
  <ol>
  <li>foo<ol>
  <li>bar</li>
  # From plwiki:PLOS_ONE
  !! test
  Parsoid: Page property magic word with magic word contents
 +!! options
 +showtitle
 +!! config
 +wgAllowDisplayTitle=true
 +wgRestrictDisplayTitle=false
  !! wikitext
  {{DISPLAYTITLE:''{{PAGENAME}}''}}
 +!! html/php+tidy
 +<i>Parser test</i>
 +
  !! html/parsoid
 -<meta property="mw:PageProp/displaytitle" content="Main Page" about="#mwt3" typeof="mw:ExpandedAttrs" data-parsoid='{"src":"{{DISPLAYTITLE:&#39;&#39;{{PAGENAME}}&#39;&#39;}}"}' data-mw='{"attribs":[[{"txt":"content"},{"html":"DISPLAYTITLE:&lt;i data-parsoid=&#39;{\"dsr\":[15,31,2,2]}&#39;>&lt;span about=\"#mwt1\" typeof=\"mw:Transclusion\" data-parsoid=&#39;{\"pi\":[[]],\"dsr\":[17,29,null,null]}&#39; data-mw=&#39;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"PAGENAME\",\"function\":\"pagename\"},\"params\":{},\"i\":0}}]}&#39;>Main Page&lt;/span>&lt;/i>"}]]}'/>
 +<meta property="mw:PageProp/displaytitle" content="Parser test" about="#mwt3" typeof="mw:ExpandedAttrs" data-parsoid='{"src":"{{DISPLAYTITLE:&#39;&#39;{{PAGENAME}}&#39;&#39;}}"}' data-mw='{"attribs":[[{"txt":"content"},{"html":"DISPLAYTITLE:&lt;i data-parsoid=&#39;{\"dsr\":[15,31,2,2]}&#39;>&lt;span about=\"#mwt1\" typeof=\"mw:Transclusion\" data-parsoid=&#39;{\"pi\":[[]],\"dsr\":[17,29,null,null]}&#39; data-mw=&#39;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"PAGENAME\",\"function\":\"pagename\"},\"params\":{},\"i\":0}}]}&#39;>Parser test&lt;/span>&lt;/i>"}]]}'/>
  !! end
  
  # NOTE: mw:ExpandedAttrs is not the best typeof here. mw:Transclusion is better.
  <table></table>
  
  b}}
 -!! html
 +!! html/php+tidy
 +<p>ab</p><table></table><p>cd
 +</p><p>ab
 +</p>
 +<table></table>
 +<p>cd
 +</p><p>a
 +</p>
 +<table></table>
 +<p>b
 +</p>
 +!! html/parsoid
  <p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":["a",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"b&lt;table>&lt;/table>c"}},"i":0}},"d"]}' data-parsoid='{"pi":[[{"k":"1"}]]}'>ab</p><table about="#mwt1" data-parsoid='{"stx":"html"}'></table><p about="#mwt1">cd</p>
  
  <p about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":["a",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"b\n&lt;table>&lt;/table>\nc"}},"i":0}},"d"]}' data-parsoid='{"pi":[[{"k":"1"}]]}'>ab</p><span about="#mwt2">
  
  !! test
  Parsoid: Merge double tds (T52603)
 -!! options
 -parsoid
  !! wikitext
  {|
  |{{echo|{{!}} foo}}
  |}
 -!! html
 +!! html/php+tidy
 +<table>
 +<tbody><tr>
 +<td>foo
 +</td></tr></tbody></table>
 +!! html/parsoid
  <table><tbody>
  <tr><td about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":["|",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"{{!}} foo"}},"i":0}}]}'> foo</td></tr>
  </tbody></table>
  
  !! test
  Parsoid: Merge double tds in nested transclusion content (T52603)
 -!! options
 -parsoid
  !! wikitext
  {{echo|<div>}}
  {|
  |{{echo|{{!}} foo}}
  |}
  {{echo|</div>}}
 -!! html
 +!! html/php+tidy
 +<div>
 +<table>
 +<tbody><tr>
 +<td>foo
 +</td></tr></tbody></table>
 +</div>
 +!! html/parsoid
  <div about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"<div>"}},"i":0}},"\n{|\n|",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"{{!}} foo"}},"i":1}},"\n|}\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"</div>"}},"i":2}}]}'>
  <table><tbody>
  <tr><td data-mw='{"parts":["|"]}'> foo</td></tr>
@@@ -12736,7 -12598,7 +12736,7 @@@ array 
  <li><span typeof="mw:Nowiki">foo-{bar}bat</span></li>
  <li><span typeof="mw:Transclusion mw:Nowiki" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;nowiki>foo-{bar}bat&lt;/nowiki>"}},"i":0}}]}'>foo-{bar}bat</span></li>
  <li><pre typeof="mw:Extension/pre" data-mw='{"name":"pre","attrs":{},"body":{"extsrc":"foo-{bar}bat"}}'>foo-{bar}bat</pre></li>
 -<li><pre typeof="mw:Transclusion mw:Extension/pre" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;pre>foo-{bar}bat&lt;/pre>"}},"i":0}}]}'>foo-{bar}bat</pre></li>
 +<li><pre typeof="mw:Extension/pre mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;pre>foo-{bar}bat&lt;/pre>"}},"i":0}}]}'>foo-{bar}bat</pre></li>
  </ul>
  <pre typeof="mw:Extension/tag" data-mw='{"name":"tag","attrs":{},"body":{"extsrc":"foo-{bar}bat"}}'></pre> <pre typeof="mw:Extension/tag mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;tag>foo-{bar}bat&lt;/tag>"}},"i":0}}]}'></pre>
  !! end
@@@ -13063,15 -12925,14 +13063,15 @@@ bar <div>baz</div
  bar </p><div>baz</div>
  !! end
  
 -!!test
 +!! test
  Templates: P-wrapping: 1d. Template preceded by comment-only line
 -!!options
 -parsoid
  !! wikitext
  <!-- foo -->
  {{echo|Bar}}
 -!! html
 +!! html/php+tidy
 +<p>Bar
 +</p>
 +!! html/parsoid
  <!-- foo -->
  
  <p about="#mwt223" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"Bar"}},"i":0}}]}'>Bar</p>
@@@ -13084,10 -12945,7 +13084,10 @@@ parsoid=wt2html,wt2w
  !! wikitext
  {{echo|<div>a</div>}}b{{echo|
  <div>c</div>}}
 -!! html
 +!! html/php+tidy
 +<div>a</div><p>b
 +</p><div>c</div>
 +!! html/parsoid
  <div about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;div>a&lt;/div>"}},"i":0}}]}'>a</div><p>b</p><span about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"\n&lt;div>c&lt;/div>"}},"i":0}}]}'>
  </span><div about="#mwt2">c</div>
  !! end
@@@ -13504,10 -13362,7 +13504,10 @@@ parsoid=wt2html,wt2w
  <table>[[Category:foo1]]<tr><td>foo</td></tr></table>
  <!--Two categories (T52330)-->
  <table>[[Category:bar1]][[Category:bar2]]<tr><td>foo</td></tr></table>
 -!! html
 +!! html/php+tidy
 +<table><tbody><tr><td>foo</td></tr></tbody></table>
 +<table><tbody><tr><td>foo</td></tr></tbody></table>
 +!! html/parsoid
  <link rel="mw:PageProp/Category" href="./Category:Foo1"><table><tbody><tr><td>foo</td></tr></tbody></table>
  <!--Two categories (T52330)-->
  <link rel="mw:PageProp/Category" href="./Category:Bar1"><link rel="mw:PageProp/Category" href="./Category:Bar2"><table><tbody><tr><td>foo</td></tr></tbody></table>
@@@ -13658,11 -13513,11 +13658,11 @@@ Templates: Wiki Tables: 7. Fosterable <
  
  <references />
  !! html/parsoid
 -<p about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"PartialTable","href":"./Template:PartialTable"},"params":{},"i":0}},"&lt;ref>foo&lt;/ref>\n|}"]}'><sup about="#mwt3" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Extension/ref" data-mw='{"name":"ref","attrs":{},"body":{"id":"mw-reference-text-cite_note-1"}}'><a href="./Main_Page#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></sup></p><table about="#mwt2">
 +<p about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"PartialTable","href":"./Template:PartialTable"},"params":{},"i":0}},"&lt;ref>foo&lt;/ref>\n|}"]}'><sup about="#mwt3" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Extension/ref" data-mw='{"name":"ref","attrs":{},"body":{"id":"mw-reference-text-cite_note-1"}}'><a href="./Parser_test#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></sup></p><table about="#mwt2">
  <tbody>
  </tbody></table>
  
 -<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt5" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Main_Page#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text">foo</span></li></ol>
 +<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt5" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Parser_test#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text">foo</span></li></ol>
  !! end
  
  !! test
@@@ -13723,9 -13578,7 +13723,9 @@@ Templates: Ugly nesting: 3. Quotes open
  parsoid=wt2html,wt2wt
  !! wikitext
  {{echo_with_div|''a}}{{echo_with_div|b''c''d}}{{echo_with_div|''e}}
 -!! html
 +!! html/php+tidy
 +<div><i>a</i></div><div><i>b</i>c<i>d</i></div><div>e</div>
 +!! html/parsoid
  <div about="#mwt1" typeof="mw:Transclusion" data-mw="{&quot;parts&quot;:[{&quot;template&quot;:{&quot;target&quot;:{&quot;wt&quot;:&quot;echo_with_div&quot;,&quot;href&quot;:&quot;./Template:Echo_with_div&quot;},&quot;params&quot;:{&quot;1&quot;:{&quot;wt&quot;:&quot;''a&quot;}},&quot;i&quot;:0}}]}"><i>a</i></div>
  <div about="#mwt2" typeof="mw:Transclusion" data-mw="{&quot;parts&quot;:[{&quot;template&quot;:{&quot;target&quot;:{&quot;wt&quot;:&quot;echo_with_div&quot;,&quot;href&quot;:&quot;./Template:Echo_with_div&quot;},&quot;params&quot;:{&quot;1&quot;:{&quot;wt&quot;:&quot;b''c''d&quot;}},&quot;i&quot;:0}}]}"><i>b</i>c<i>d</i></div>
  <div about="#mwt3" typeof="mw:Transclusion" data-mw="{&quot;parts&quot;:[{&quot;template&quot;:{&quot;target&quot;:{&quot;wt&quot;:&quot;echo_with_div&quot;,&quot;href&quot;:&quot;./Template:Echo_with_div&quot;},&quot;params&quot;:{&quot;1&quot;:{&quot;wt&quot;:&quot;''e&quot;}},&quot;i&quot;:0}}]}">e</div>
@@@ -13838,7 -13691,6 +13838,7 @@@ Parser Functions: 2. Nested use (only o
  !! test
  Template nested in extension tag in template
  !! options
 +title=[[Main Page]]
  language=zh
  !! wikitext
  {{echo|hi<ref>[[ho|{{echo|hi}}]]</ref>}}
  {{echo|hi<ref>-{ho|{{echo|hi}}}-</ref>}}
  <references />
  !! html/parsoid
 -<p><span about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"hi&lt;ref>[[ho|{{echo|hi}}]]&lt;/ref>"}},"i":0}}]}'>hi</span><sup about="#mwt2" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Extension/ref" data-mw='{"name":"ref","attrs":{},"body":{"id":"mw-reference-text-cite_note-1"}}'><a href="./Wikipedia:首页#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></sup>
 -<span about="#mwt8" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"hi&lt;ref>[http://test.com?q={{echo|ho}}]&lt;/ref>"}},"i":0}}]}'>hi</span><sup about="#mwt8" class="mw-ref" id="cite_ref-2" rel="dc:references" typeof="mw:Extension/ref" data-mw='{"name":"ref","attrs":{},"body":{"id":"mw-reference-text-cite_note-2"}}'><a href="./Wikipedia:首页#cite_note-2" style="counter-reset: mw-Ref 2;"><span class="mw-reflink-text">[2]</span></a></sup>
 -<span about="#mwt13" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"hi&lt;ref>-{ho|{{echo|hi}}}-&lt;/ref>"}},"i":0}}]}'>hi</span><sup about="#mwt13" class="mw-ref" id="cite_ref-3" rel="dc:references" typeof="mw:Extension/ref" data-mw='{"name":"ref","attrs":{},"body":{"id":"mw-reference-text-cite_note-3"}}'><a href="./Wikipedia:首页#cite_note-3" style="counter-reset: mw-Ref 3;"><span class="mw-reflink-text">[3]</span></a></sup></p>
 -<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt17" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Wikipedia:首页#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text"><a rel="mw:WikiLink" href="./Ho" title="Ho">hi</a></span></li><li about="#cite_note-2" id="cite_note-2"><a href="./Wikipedia:首页#cite_ref-2" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-2" class="mw-reference-text"><a rel="mw:ExtLink" class="external autonumber" href="http://test.com?q=ho"></a></span></li><li about="#cite_note-3" id="cite_note-3"><a href="./Wikipedia:首页#cite_ref-3" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-3" class="mw-reference-text"><span typeof="mw:LanguageVariant" data-mw-variant='{"filter":{"l":["ho"],"t":"hi"}}'></span></span></li></ol>
 +<p><span about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"hi&lt;ref>[[ho|{{echo|hi}}]]&lt;/ref>"}},"i":0}}]}'>hi</span><sup about="#mwt2" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Extension/ref" data-mw='{"name":"ref","attrs":{},"body":{"id":"mw-reference-text-cite_note-1"}}'><a href="./Main_Page#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></sup>
 +<span about="#mwt8" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"hi&lt;ref>[http://test.com?q={{echo|ho}}]&lt;/ref>"}},"i":0}}]}'>hi</span><sup about="#mwt8" class="mw-ref" id="cite_ref-2" rel="dc:references" typeof="mw:Extension/ref" data-mw='{"name":"ref","attrs":{},"body":{"id":"mw-reference-text-cite_note-2"}}'><a href="./Main_Page#cite_note-2" style="counter-reset: mw-Ref 2;"><span class="mw-reflink-text">[2]</span></a></sup>
 +<span about="#mwt13" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"hi&lt;ref>-{ho|{{echo|hi}}}-&lt;/ref>"}},"i":0}}]}'>hi</span><sup about="#mwt13" class="mw-ref" id="cite_ref-3" rel="dc:references" typeof="mw:Extension/ref" data-mw='{"name":"ref","attrs":{},"body":{"id":"mw-reference-text-cite_note-3"}}'><a href="./Main_Page#cite_note-3" style="counter-reset: mw-Ref 3;"><span class="mw-reflink-text">[3]</span></a></sup></p>
 +<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt17" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Main_Page#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text"><a rel="mw:WikiLink" href="./Ho" title="Ho">hi</a></span></li><li about="#cite_note-2" id="cite_note-2"><a href="./Main_Page#cite_ref-2" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-2" class="mw-reference-text"><a rel="mw:ExtLink" class="external autonumber" href="http://test.com?q=ho"></a></span></li><li about="#cite_note-3" id="cite_note-3"><a href="./Main_Page#cite_ref-3" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-3" class="mw-reference-text"><span typeof="mw:LanguageVariant" data-mw-variant='{"filter":{"l":["ho"],"t":"hi"}}'></span></span></li></ol>
  !! end
  
  ###
@@@ -14434,10 -14286,7 +14434,10 @@@ Parsoid: Recognize nowiki with odd capi
  parsoid=wt2html
  !! wikitext
  <noWikI ><div>[[foo]]</Nowiki >
 -!! html
 +!! html/php+tidy
 +<p>&lt;div&gt;[[foo]]
 +</p>
 +!! html/parsoid
  <p><span typeof="mw:Nowiki">&lt;div&gt;[[foo]]</span></p>
  !! end
  
@@@ -15153,416 -15002,54 +15153,416 @@@ Alt image option should handle most kin
  !! end
  
  !! test
 -Image with table with attributes in caption
 +Ampersand in alt attribute (T206940)
  !! options
 -parsoid=wt2html,html2html
 +parsoid = {
 +  "nativeGallery": true
 +}
  !! wikitext
 -[[File:Foobar.jpg|thumb|
 -{| class="123" |
 -|- class="456" |
 -| ha
 -|}
 -]]
 +[[File:Foobar.jpg|alt=&amp;amp;]]
 +
 +<!-- consistency with gallery extension -->
 +<gallery>
 +File:Foobar.jpg|alt=&amp;amp;
 +</gallery>
 +!! html/php+tidy
 +<p><a href="/wiki/File:Foobar.jpg" class="image"><img alt="&amp;amp;" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +<ul class="gallery mw-gallery-traditional">
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="&amp;amp;" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +                      </div>
 +              </div></li>
 +</ul>
  !! html/parsoid
 -<figure class="mw-default-size" typeof="mw:Image/Thumb" data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"caption","ak":"\n{| class=\"123\" |\n|- class=\"456\" |\n| ha\n|}\n"}]}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{"href":"File:Foobar.jpg"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="25" width="220" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"25","width":"220"},"sa":{"resource":"File:Foobar.jpg"}}'/></a><figcaption>
 -<table class="123">
 -<tbody><tr class="456" data-parsoid='{"startTagSrc":"|-"}'>
 -<td> ha</td></tr>
 -</tbody></table>
 -</figcaption></figure>
 +<p><figure-inline class="mw-default-size" typeof="mw:Image"><a href="./File:Foobar.jpg"><img alt="&amp;amp;" resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941"/></a></figure-inline></p>
 +
 +<!-- consistency with gallery extension -->
 +<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" data-mw='{"name":"gallery","attrs":{},"body":{}}'>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="./File:Foobar.jpg"><img alt="&amp;amp;" resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext"></div>
 +</li>
 +</ul>
  !! end
  
  !! test
 -Image with table with rows from templates in caption
 +Italics markup in alt attribute (T206940)
  !! wikitext
 -[[File:Foobar.jpg|thumb|
 -{|
 -{{echo|{{!}} hi}}
 -|}
 -]]
 +[[File:Foobar.jpg|alt=''x''|caption]]
 +
 +<!-- consistency with gallery extension -->
 +<gallery>
 +File:Foobar.jpg|alt=''x''|caption
 +</gallery>
 +!! html/php+tidy
 +<p><a href="/wiki/File:Foobar.jpg" class="image" title="caption"><img alt="x" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +<ul class="gallery mw-gallery-traditional">
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="x" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +<p>caption
 +</p>
 +                      </div>
 +              </div></li>
 +</ul>
  !! html/parsoid
 -<figure class="mw-default-size" typeof="mw:Image/Thumb" data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"caption","ak":"\n{|\n{{echo|{{!}} hi}}\n|}\n"}]}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{"href":"File:Foobar.jpg"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="25" width="220" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"25","width":"220"},"sa":{"resource":"File:Foobar.jpg"}}'/></a><figcaption>
 -<table>
 -<tbody about="#mwt4" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"{{!}} hi"}},"i":0}},"\n"]}'><tr><td> hi</td></tr>
 -</tbody></table>
 -</figcaption></figure>
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"alt","ak":"alt=&apos;&apos;x&apos;&apos;"},{"ck":"caption","ak":"caption"}]}' data-mw='{"caption":"caption"}'><a href="./File:Foobar.jpg"><img alt="x" resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"alt":"x","resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"alt":"alt=&apos;&apos;x&apos;&apos;","resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 +
 +<!-- consistency with gallery extension -->
 +<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" about="#mwt2" data-mw='{"name":"gallery","attrs":{},"body":{"extsrc":"\nFile:Foobar.jpg|alt=&apos;&apos;x&apos;&apos;|caption\n"}}'>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="./File:Foobar.jpg"><img alt="x" resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext">caption</div>
 +</li>
 +</ul>
  !! end
  
  !! test
 -Image with nested tables in caption
 +Nowiki markup in alt attribute (T206940)
  !! wikitext
 -[[File:Foobar.jpg|thumb|Foo<br />
 -{|
 -|
 -{|
 -|z
 -|}
 -|}
 -]]
 -!! html/parsoid
 -<figure class="mw-default-size" typeof="mw:Image/Thumb" data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"caption","ak":"Foo&lt;br/>\n{|\n|\n{|\n|z\n|}\n|}\n"}]}'><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="25" width="220" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"25","width":"220"},"sa":{"resource":"File:Foobar.jpg"}}'/></a><figcaption data-parsoid='{"dsr":[null,50,null,null]}'>Foo<br data-parsoid='{"stx":"html","selfClose":true}'/>
 +[[File:Foobar.jpg|alt=<nowiki>''</nowiki>x<nowiki>''</nowiki>|caption]]
 +
 +<!-- consistency with gallery extension -->
 +<gallery>
 +File:Foobar.jpg|alt=<nowiki>''</nowiki>x<nowiki>''</nowiki>|caption
 +</gallery>
 +!! html/php+tidy
 +<p><a href="/wiki/File:Foobar.jpg" class="image" title="caption"><img alt="&#39;&#39;x&#39;&#39;" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +<ul class="gallery mw-gallery-traditional">
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="&#39;&#39;x&#39;&#39;" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +<p>caption
 +</p>
 +                      </div>
 +              </div></li>
 +</ul>
 +!! html/parsoid
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"alt","ak":"alt=&lt;nowiki>&apos;&apos;&lt;/nowiki>x&lt;nowiki>&apos;&apos;&lt;/nowiki>"},{"ck":"caption","ak":"caption"}],"dsr":[0,71,null,null]}' data-mw='{"caption":"caption"}'><a href="./File:Foobar.jpg"><img alt="''x''" resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"alt":"&apos;&apos;x&apos;&apos;","resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"alt":"alt=&lt;nowiki>&apos;&apos;&lt;/nowiki>x&lt;nowiki>&apos;&apos;&lt;/nowiki>","resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 +
 +<!-- consistency with gallery extension -->
 +<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" data-mw='{"name":"gallery","attrs":{},"body":{"extsrc":"\nFile:Foobar.jpg|alt=&lt;nowiki>&apos;&apos;&lt;/nowiki>x&lt;nowiki>&apos;&apos;&lt;/nowiki>|caption\n"}}'>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="./File:Foobar.jpg"><img alt="''x''" resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext">caption</div>
 +</li>
 +</ul>
 +!! end
 +
 +!! test
 +Nowiki markup in alt attribute (edited html, no data-parsoid) (T206940)
 +!! options
 +parsoid = {
 +  "nativeGallery": true
 +}
 +!! wikitext
 +[[File:Foobar.jpg|alt=<nowiki>''x''</nowiki>|caption]]
 +
 +<!-- consistency with gallery extension -->
 +<gallery>
 +File:Foobar.jpg|alt=<nowiki>''x''</nowiki>|caption
 +</gallery>
 +!! html/php+tidy
 +<p><a href="/wiki/File:Foobar.jpg" class="image" title="caption"><img alt="&#39;&#39;x&#39;&#39;" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +<ul class="gallery mw-gallery-traditional">
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="&#39;&#39;x&#39;&#39;" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +<p>caption
 +</p>
 +                      </div>
 +              </div></li>
 +</ul>
 +!! html/parsoid
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-mw='{"caption":"caption"}'><a href="./File:Foobar.jpg"><img alt="''x''" resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941"/></a></figure-inline></p>
 +
 +<!-- consistency with gallery extension -->
 +<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" data-mw='{"name":"gallery","attrs":{},"body":{}}'>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="./File:Foobar.jpg"><img alt="''x''" resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext">caption</div>
 +</li>
 +</ul>
 +!! end
 +
 +!! test
 +Ampersand in link attribute (T206940)
 +!! wikitext
 +[[File:Foobar.jpg|link=Foo &amp; bar]]
 +
 +<!-- consistency with gallery extension -->
 +<gallery>
 +File:Foobar.jpg|link=Foo &amp; bar
 +</gallery>
 +!! html/php+tidy
 +<p><a href="/wiki/Foo_%26_bar" title="Foo &amp; bar"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +<ul class="gallery mw-gallery-traditional">
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/Foo_%26_bar"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +                      </div>
 +              </div></li>
 +</ul>
 +!! html/parsoid
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"link","ak":"link=Foo &amp;amp; bar"}]}'><a href="./Foo_&amp;_bar" data-parsoid='{"a":{"href":"./Foo_&amp;_bar"},"sa":{"href":"link=Foo &amp;amp; bar"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941"/></a></figure-inline></p>
 +
 +<!-- consistency with gallery extension -->
 +<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" data-mw='{"name":"gallery","attrs":{},"body":{"extsrc":"\nFile:Foobar.jpg|link=Foo &amp;amp; bar\n"}}'>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="./Foo_&amp;_bar"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext"></div>
 +</li>
 +</ul>
 +!! end
 +
 +!! test
 +Ampersand in link attribute (edited html, no data-parsoid) (T206940)
 +!! options
 +parsoid = {
 +  "nativeGallery": true
 +}
 +!! wikitext
 +[[File:Foobar.jpg|link=Foo_&_bar]]
 +
 +<!-- consistency with gallery extension -->
 +<gallery>
 +File:Foobar.jpg|link=Foo_&_bar
 +</gallery>
 +!! html/php+tidy
 +<p><a href="/wiki/Foo_%26_bar" title="Foo &amp; bar"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +<ul class="gallery mw-gallery-traditional">
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/Foo_%26_bar"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +                      </div>
 +              </div></li>
 +</ul>
 +!! html/parsoid
 +<p><figure-inline class="mw-default-size" typeof="mw:Image"><a href="./Foo_&amp;_bar"><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941"/></a></figure-inline></p>
 +
 +<!-- consistency with gallery extension -->
 +<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" data-mw='{"name":"gallery","attrs":{},"body":{}}'>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="./Foo_&amp;_bar"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext"></div>
 +</li>
 +</ul>
 +!! end
 +
 +!! test
 +Italics markup in link attribute (T206940)
 +!! wikitext
 +[[Foo''s bar''s]]
 +
 +<!-- Note that "italics" are stripped, even though this is a valid page title -->
 +[[File:Foobar.jpg|link=Foo''s bar''s|caption1]]
 +
 +[[File:Foobar.jpg|link=''Main Page''|caption2]]
 +
 +<!-- consistency with gallery extension -->
 +<gallery>
 +File:Foobar.jpg|link=Foo''s bar''s|caption1
 +File:Foobar.jpg|link=''Main Page''|caption2
 +</gallery>
 +!! html/php+tidy
 +<p><a href="/wiki/Foo%27%27s_bar%27%27s" title="Foo&#39;&#39;s bar&#39;&#39;s">Foo''s bar''s</a>
 +</p><p><a href="/wiki/Foos_bars" title="caption1"><img alt="caption1" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p><p><a href="/wiki/Main_Page" title="caption2"><img alt="caption2" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +<ul class="gallery mw-gallery-traditional">
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/Foos_bars"><img alt="" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +<p>caption1
 +</p>
 +                      </div>
 +              </div></li>
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/Main_Page"><img alt="" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +<p>caption2
 +</p>
 +                      </div>
 +              </div></li>
 +</ul>
 +!! html/parsoid
 +<p><a rel="mw:WikiLink" href="./Foo''s_bar''s" title="Foo''s bar''s">Foo''s bar''s</a></p>
 +
 +<!-- Note that "italics" are stripped, even though this is a valid page title -->
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"link","ak":"link=Foo&apos;&apos;s bar&apos;&apos;s"},{"ck":"caption","ak":"caption1"}]}' data-mw='{"caption":"caption1"}'><a href="./Foos_bars" data-parsoid='{"a":{"href":"./Foos_bars"},"sa":{"href":"link=Foo&apos;&apos;s bar&apos;&apos;s"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 +
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"link","ak":"link=&apos;&apos;Main Page&apos;&apos;"},{"ck":"caption","ak":"caption2"}]}' data-mw='{"caption":"caption2"}'><a href="./Main_Page" data-parsoid='{"a":{"href":"./Main_Page"},"sa":{"href":"link=&apos;&apos;Main Page&apos;&apos;"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 +
 +<!-- consistency with gallery extension -->
 +<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" data-mw='{"name":"gallery","attrs":{},"body":{"extsrc":"\nFile:Foobar.jpg|link=Foo&apos;&apos;s bar&apos;&apos;s|caption1\nFile:Foobar.jpg|link=&apos;&apos;Main Page&apos;&apos;|caption2\n"}}'>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="./Foos_bars"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext">caption1</div>
 +</li>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="./Main_Page"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext">caption2</div>
 +</li>
 +</ul>
 +!! end
 +
 +!! test
 +Nowiki markup in link attribute (T206940)
 +!! wikitext
 +[[File:Foobar.jpg|link=Foo<nowiki>''</nowiki>s_bar<nowiki>''</nowiki>s|caption]]
 +
 +<!-- consistency with gallery extension -->
 +<gallery>
 +File:Foobar.jpg|link=Foo<nowiki>''</nowiki>s_bar<nowiki>''</nowiki>s|caption
 +</gallery>
 +!! html/php+tidy
 +<p><a href="/wiki/Foo%27%27s_bar%27%27s" title="caption"><img alt="caption" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +<ul class="gallery mw-gallery-traditional">
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/Foo%27%27s_bar%27%27s"><img alt="" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +<p>caption
 +</p>
 +                      </div>
 +              </div></li>
 +</ul>
 +!! html/parsoid
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"link","ak":"link=Foo&lt;nowiki>&apos;&apos;&lt;/nowiki>s_bar&lt;nowiki>&apos;&apos;&lt;/nowiki>s"},{"ck":"caption","ak":"caption"}]}' data-mw='{"caption":"caption"}'><a href="./Foo''s_bar''s" data-parsoid='{"a":{"href":"./Foo&apos;&apos;s_bar&apos;&apos;s"},"sa":{"href":"link=Foo&lt;nowiki>&apos;&apos;&lt;/nowiki>s_bar&lt;nowiki>&apos;&apos;&lt;/nowiki>s"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 +
 +<!-- consistency with gallery extension -->
 +<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" data-mw='{"name":"gallery","attrs":{},"body":{"extsrc":"\nFile:Foobar.jpg|link=Foo&lt;nowiki>&apos;&apos;&lt;/nowiki>s_bar&lt;nowiki>&apos;&apos;&lt;/nowiki>s|caption\n"}}'>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="./Foo''s_bar''s"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext">caption</div>
 +</li>
 +</ul>
 +!! end
 +
 +!! test
 +Nowiki markup in link attribute (edited html, no data-parsoid) (T206940)
 +!! options
 +parsoid = {
 +  "nativeGallery": true
 +}
 +!! wikitext
 +[[File:Foobar.jpg|link=Foo<nowiki>''s_bar''</nowiki>s|caption]]
 +
 +<!-- consistency with gallery extension -->
 +<gallery>
 +File:Foobar.jpg|link=Foo<nowiki>''s_bar''</nowiki>s|caption
 +</gallery>
 +!! html/php+tidy
 +<p><a href="/wiki/Foo%27%27s_bar%27%27s" title="caption"><img alt="caption" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +<ul class="gallery mw-gallery-traditional">
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/Foo%27%27s_bar%27%27s"><img alt="" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +<p>caption
 +</p>
 +                      </div>
 +              </div></li>
 +</ul>
 +!! html/parsoid
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-mw='{"caption":"caption"}'><a href="./Foo''s_bar''s"><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941"/></a></figure-inline></p>
 +
 +<!-- consistency with gallery extension -->
 +<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" data-mw='{"name":"gallery","attrs":{},"body":{}}'>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="./Foo''s_bar''s"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext">caption</div>
 +</li>
 +</ul>
 +!! end
 +
 +!! test
 +HTML entity prefix in link markup (T209236)
 +!! wikitext
 +[[File:Foobar.jpg|link=https://example.com?foo&params=bar]]
 +
 +<!-- consistency with gallery extension -->
 +<gallery>
 +File:Foobar.jpg|link=https://example.com?foo&params=bar
 +</gallery>
 +!! html/php+tidy
 +<p><a href="https://example.com?foo&amp;params=bar" rel="nofollow"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +<ul class="gallery mw-gallery-traditional">
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="https://example.com?foo&amp;params=bar"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +                      </div>
 +              </div></li>
 +</ul>
 +!! html/parsoid
 +<p><figure-inline class="mw-default-size" typeof="mw:Image"><a href="https://example.com?foo&amp;params=bar"><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941"/></a></figure-inline></p>
 +
 +<!-- consistency with gallery extension -->
 +<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" data-mw='{"name":"gallery","attrs":{},"body":{"extsrc":"\nFile:Foobar.jpg|link=https://example.com?foo&amp;params=bar\n"}}'>
 +<li class="gallerybox">
 +<div class="thumb"><figure-inline typeof="mw:Image"><a href="https://example.com?foo&amp;params=bar"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="14" width="120"/></a></figure-inline></div>
 +<div class="gallerytext"></div>
 +</li>
 +</ul>
 +!! end
 +
 +!! test
 +Image with table with attributes in caption
 +!! options
 +parsoid=wt2html,html2html
 +!! wikitext
 +[[File:Foobar.jpg|thumb|
 +{| class="123" |
 +|- class="456" |
 +| ha
 +|}
 +]]
 +!! html/parsoid
 +<figure class="mw-default-size" typeof="mw:Image/Thumb" data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"caption","ak":"\n{| class=\"123\" |\n|- class=\"456\" |\n| ha\n|}\n"}]}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{"href":"File:Foobar.jpg"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="25" width="220" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"25","width":"220"},"sa":{"resource":"File:Foobar.jpg"}}'/></a><figcaption>
 +<table class="123">
 +<tbody><tr class="456" data-parsoid='{"startTagSrc":"|-"}'>
 +<td> ha</td></tr>
 +</tbody></table>
 +</figcaption></figure>
 +!! end
 +
 +!! test
 +Image with table with rows from templates in caption
 +!! wikitext
 +[[File:Foobar.jpg|thumb|
 +{|
 +{{echo|{{!}} hi}}
 +|}
 +]]
 +!! html/parsoid
 +<figure class="mw-default-size" typeof="mw:Image/Thumb" data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"caption","ak":"\n{|\n{{echo|{{!}} hi}}\n|}\n"}]}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{"href":"File:Foobar.jpg"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="25" width="220" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"25","width":"220"},"sa":{"resource":"File:Foobar.jpg"}}'/></a><figcaption>
 +<table>
 +<tbody about="#mwt4" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"{{!}} hi"}},"i":0}},"\n"]}'><tr><td> hi</td></tr>
 +</tbody></table>
 +</figcaption></figure>
 +!! end
 +
 +!! test
 +Image with nested tables in caption
 +!! wikitext
 +[[File:Foobar.jpg|thumb|Foo<br />
 +{|
 +|
 +{|
 +|z
 +|}
 +|}
 +]]
 +!! html/parsoid
 +<figure class="mw-default-size" typeof="mw:Image/Thumb" data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"caption","ak":"Foo&lt;br/>\n{|\n|\n{|\n|z\n|}\n|}\n"}]}'><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="25" width="220" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"25","width":"220"},"sa":{"resource":"File:Foobar.jpg"}}'/></a><figcaption data-parsoid='{"dsr":[null,50,null,null]}'>Foo<br data-parsoid='{"stx":"html","selfClose":true}'/>
  <table>
  <tbody><tr><td>
  <table>
@@@ -15883,30 -15370,6 +15883,30 @@@ parsoid=wt2html,wt2wt,html2htm
  <figure class="mw-default-size" typeof="mw:Image/Thumb"><a href="./File:Foobar.svg"><img resource="./File:Foobar.svg" src="//example.com/images/thumb/f/ff/Foobar.svg/220px-Foobar.svg.png" data-file-width="240" data-file-height="180" data-file-type="drawing" height="165" width="220"/></a><figcaption>lang=invalid:language:code</figcaption></figure>
  !! end
  
 +!! test
 +SVG thumbnails in page language
 +!! options
 +language=ru
 +wgMediaInTargetLanguage = true
 +!! wikitext
 +[[File:Foobar.svg]] [[File:Foobar.svg|lang=en]]
 +!! html/php
 +<p><a href="/wiki/%D0%A4%D0%B0%D0%B9%D0%BB:Foobar.svg" class="image"><img alt="Foobar.svg" src="http://example.com/images/thumb/f/ff/Foobar.svg/langru-240px-Foobar.svg.png" width="240" height="180" srcset="http://example.com/images/thumb/f/ff/Foobar.svg/langru-360px-Foobar.svg.png 1.5x, http://example.com/images/thumb/f/ff/Foobar.svg/langru-480px-Foobar.svg.png 2x" /></a> <a href="/index.php?title=%D0%A4%D0%B0%D0%B9%D0%BB:Foobar.svg&amp;lang=en" class="image"><img alt="Foobar.svg" src="http://example.com/images/thumb/f/ff/Foobar.svg/240px-Foobar.svg.png" width="240" height="180" srcset="http://example.com/images/thumb/f/ff/Foobar.svg/360px-Foobar.svg.png 1.5x, http://example.com/images/thumb/f/ff/Foobar.svg/480px-Foobar.svg.png 2x" /></a>
 +</p>
 +!! end
 +
 +!! test
 +SVG thumbnails in page language not present in the file
 +!! options
 +language=de
 +wgMediaInTargetLanguage = true
 +!! wikitext
 +[[File:Foobar.svg]] [[File:Foobar.svg|lang=ru]]
 +!! html/php
 +<p><a href="/wiki/Datei:Foobar.svg" class="image"><img alt="Foobar.svg" src="http://example.com/images/thumb/f/ff/Foobar.svg/240px-Foobar.svg.png" width="240" height="180" srcset="http://example.com/images/thumb/f/ff/Foobar.svg/360px-Foobar.svg.png 1.5x, http://example.com/images/thumb/f/ff/Foobar.svg/480px-Foobar.svg.png 2x" /></a> <a href="/index.php?title=Datei:Foobar.svg&amp;lang=ru" class="image"><img alt="Foobar.svg" src="http://example.com/images/thumb/f/ff/Foobar.svg/langru-240px-Foobar.svg.png" width="240" height="180" srcset="http://example.com/images/thumb/f/ff/Foobar.svg/langru-360px-Foobar.svg.png 1.5x, http://example.com/images/thumb/f/ff/Foobar.svg/langru-480px-Foobar.svg.png 2x" /></a>
 +</p>
 +!! end
 +
  !! test
  T3887: A ISBN with a thumbnail
  !! wikitext
@@@ -16461,9 -15924,9 +16461,9 @@@ T93580: 1. Templated <ref> inside bloc
  
  <references />
  !! html/parsoid
 -<figure class="mw-default-size" typeof="mw:Image/Thumb" data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"caption","ak":"Caption with templated ref: {{echo|&lt;ref>foo&lt;/ref>}}"}]}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{}}'><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="25" width="220" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"25","width":"220"},"sa":{"resource":"File:Foobar.jpg"}}'/></a><figcaption>Caption with templated ref: <sup about="#mwt5" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Transclusion  mw:Extension/ref" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;ref>foo&lt;/ref>"}},"i":0}}]}'><a href="./Main_Page#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></sup></figcaption></figure>
 +<figure class="mw-default-size" typeof="mw:Image/Thumb" data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"caption","ak":"Caption with templated ref: {{echo|&lt;ref>foo&lt;/ref>}}"}]}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{}}'><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="25" width="220" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"25","width":"220"},"sa":{"resource":"File:Foobar.jpg"}}'/></a><figcaption>Caption with templated ref: <sup about="#mwt5" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Transclusion  mw:Extension/ref" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;ref>foo&lt;/ref>"}},"i":0}}]}'><a href="./Parser_test#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></sup></figcaption></figure>
  
 -<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt6" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Main_Page#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text" data-parsoid="{}">foo</span></li></ol>
 +<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt6" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Parser_test#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text" data-parsoid="{}">foo</span></li></ol>
  !! end
  
  !! test
@@@ -16473,9 -15936,9 +16473,9 @@@ T93580: 2. <ref> inside inline image
  
  <references />
  !! html/parsoid
 -<p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"caption","ak":"Undisplayed caption in inline image with ref: &lt;ref>foo&lt;/ref>"}]}' data-mw='{"caption":"Undisplayed caption in inline image with ref: &lt;sup about=\"#mwt2\" class=\"mw-ref\" id=\"cite_ref-1\" rel=\"dc:references\" typeof=\"mw:Extension/ref\" data-parsoid=&#39;{\"dsr\":[64,78,5,6]}&#39; data-mw=&#39;{\"name\":\"ref\",\"attrs\":{},\"body\":{\"id\":\"mw-reference-text-cite_note-1\"}}&#39;>&lt;a href=\"./Main_Page#cite_note-1\" style=\"counter-reset: mw-Ref 1;\" data-parsoid=\"{}\">&lt;span class=\"mw-reflink-text\" data-parsoid=\"{}\">[1]&lt;/span>&lt;/a>&lt;/sup>"}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{"href":"File:Foobar.jpg"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"caption","ak":"Undisplayed caption in inline image with ref: &lt;ref>foo&lt;/ref>"}]}' data-mw='{"caption":"Undisplayed caption in inline image with ref: &lt;sup about=\"#mwt2\" class=\"mw-ref\" id=\"cite_ref-1\" rel=\"dc:references\" typeof=\"mw:Extension/ref\" data-parsoid=&#39;{\"dsr\":[64,78,5,6]}&#39; data-mw=&#39;{\"name\":\"ref\",\"attrs\":{},\"body\":{\"id\":\"mw-reference-text-cite_note-1\"}}&#39;>&lt;a href=\"./Parser_test#cite_note-1\" style=\"counter-reset: mw-Ref 1;\" data-parsoid=\"{}\">&lt;span class=\"mw-reflink-text\" data-parsoid=\"{}\">[1]&lt;/span>&lt;/a>&lt;/sup>"}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{"href":"File:Foobar.jpg"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
  
 -<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt4" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Main_Page#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text">foo</span></li></ol>
 +<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt4" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Parser_test#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text">foo</span></li></ol>
  !! end
  
  !! test
@@@ -16485,9 -15948,9 +16485,9 @@@ T93580: 3. Templated <ref> inside inlin
  
  <references />
  !! html/parsoid
 -<p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"caption","ak":"Undisplayed caption in inline image with ref: {{echo|&lt;ref>{{echo|foo}}&lt;/ref>}}"}]}' data-mw='{"caption":"Undisplayed caption in inline image with ref: &lt;sup about=\"#mwt2\" class=\"mw-ref\" id=\"cite_ref-1\" rel=\"dc:references\" typeof=\"mw:Transclusion  mw:Extension/ref\" data-parsoid=&#39;{\"dsr\":[64,96,null,null],\"pi\":[[{\"k\":\"1\"}]]}&#39; data-mw=&#39;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"echo\",\"href\":\"./Template:Echo\"},\"params\":{\"1\":{\"wt\":\"&amp;lt;ref>{{echo|foo}}&amp;lt;/ref>\"}},\"i\":0}}]}&#39;>&lt;a href=\"./Main_Page#cite_note-1\" style=\"counter-reset: mw-Ref 1;\" data-parsoid=\"{}\">&lt;span class=\"mw-reflink-text\" data-parsoid=\"{}\">[1]&lt;/span>&lt;/a>&lt;/sup>"}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{"href":"File:Foobar.jpg"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"caption","ak":"Undisplayed caption in inline image with ref: {{echo|&lt;ref>{{echo|foo}}&lt;/ref>}}"}]}' data-mw='{"caption":"Undisplayed caption in inline image with ref: &lt;sup about=\"#mwt2\" class=\"mw-ref\" id=\"cite_ref-1\" rel=\"dc:references\" typeof=\"mw:Transclusion  mw:Extension/ref\" data-parsoid=&#39;{\"dsr\":[64,96,null,null],\"pi\":[[{\"k\":\"1\"}]]}&#39; data-mw=&#39;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"echo\",\"href\":\"./Template:Echo\"},\"params\":{\"1\":{\"wt\":\"&amp;lt;ref>{{echo|foo}}&amp;lt;/ref>\"}},\"i\":0}}]}&#39;>&lt;a href=\"./Parser_test#cite_note-1\" style=\"counter-reset: mw-Ref 1;\" data-parsoid=\"{}\">&lt;span class=\"mw-reflink-text\" data-parsoid=\"{}\">[1]&lt;/span>&lt;/a>&lt;/sup>"}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{"href":"File:Foobar.jpg"}}'><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
  
 -<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt6" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Main_Page#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text">foo</span></li></ol>
 +<ol class="mw-references references" typeof="mw:Extension/references" about="#mwt6" data-mw='{"name":"references","attrs":{}}'><li about="#cite_note-1" id="cite_note-1"><a href="./Parser_test#cite_ref-1" rel="mw:referencedBy"><span class="mw-linkback-text">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text">foo</span></li></ol>
  !! end
  
  ###
@@@ -16890,7 -16353,7 +16890,7 @@@ parsoid=wt2htm
  * So should this
  
  {{echo|[[Category:Foo]] and this should be part of the same list item}}
 -!! html
 +!! html/php+tidy
  <ul><li>This and this should be part of the same list item</li>
  <li>So should this and this should be part of the same list item</li></ul>
  !! html/parsoid
@@@ -17103,15 -16566,13 +17103,15 @@@ Category links with multiple namespace
  
  !! test
  Parsoid: Serialize link to category page with colon escape
 -!! options
 -parsoid
  !! wikitext
  
  [[:Category:Foo]]
  [[:Category:Foo|Bar]]
 -!! html
 +!! html/php+tidy
 +<p><a href="/index.php?title=Category:Foo&amp;action=edit&amp;redlink=1" class="new" title="Category:Foo (page does not exist)">Category:Foo</a>
 +<a href="/index.php?title=Category:Foo&amp;action=edit&amp;redlink=1" class="new" title="Category:Foo (page does not exist)">Bar</a>
 +</p>
 +!! html/parsoid
  <p>
  <a rel="mw:WikiLink" href="./Category:Foo" title="Category:Foo">Category:Foo</a>
  <a rel="mw:WikiLink" href="./Category:Foo" title="Category:Foo">Bar</a>
@@@ -17158,15 -16619,13 +17158,15 @@@ x[[es:Foo]]
  
  !! test
  Parsoid: Serialize link to file page with colon escape
 -!! options
 -parsoid
  !! wikitext
  
  [[:File:Foo.png]]
  [[:File:Foo.png|Bar]]
 -!! html
 +!! html/php+tidy
 +<p><a href="/index.php?title=File:Foo.png&amp;action=edit&amp;redlink=1" class="new" title="File:Foo.png (page does not exist)">File:Foo.png</a>
 +<a href="/index.php?title=File:Foo.png&amp;action=edit&amp;redlink=1" class="new" title="File:Foo.png (page does not exist)">Bar</a>
 +</p>
 +!! html/parsoid
  <p>
  <a rel="mw:WikiLink" href="./File:Foo.png" title="File:Foo.png">File:Foo.png</a>
  <a rel="mw:WikiLink" href="./File:Foo.png" title="File:Foo.png">Bar</a>
  
  !! test
  Parsoid: Serialize a genuine category link without colon escape
 -!! options
 -parsoid
  !! wikitext
  [[Category:Foo]]
  [[Category:Foo|Bar]]
 -!! html
 +!! html/php+tidy
 +!! html/parsoid
  <link rel="mw:PageProp/Category" href="./Category:Foo">
  <link rel="mw:PageProp/Category" href="./Category:Foo#Bar">
  !! end
@@@ -17822,12 -17282,12 +17822,12 @@@ section 
  <h2 id="Underscore-Entity_between_Text">Underscore-Entity<span typeof="mw:Entity" data-parsoid='{"src":"&amp;#95;","srcContent":"_"}'>_</span>between<span typeof="mw:Entity" data-parsoid='{"src":"&amp;#95;","srcContent":"_"}'>_</span>Text</h2>
  <p>section 6</p>
  
 -<p><a rel="mw:WikiLink" href="./Main_Page#Space_between_Text" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#Space_between_Text"},"sa":{"href":"#Space between Text"}}'>#Space between Text</a>
 -<a rel="mw:WikiLink" href="./Main_Page#Space-Entity_between_Text" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#Space-Entity_between_Text"},"sa":{"href":"#Space-Entity&amp;#32;between&amp;#32;Text"}}'>#Space-Entity between Text</a>
 -<a rel="mw:WikiLink" href="./Main_Page#Plus+between+Text" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#Plus+between+Text"},"sa":{"href":"#Plus+between+Text"}}'>#Plus+between+Text</a>
 -<a rel="mw:WikiLink" href="./Main_Page#Plus-Entity+between+Text" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#Plus-Entity+between+Text"},"sa":{"href":"#Plus-Entity&amp;#43;between&amp;#43;Text"}}'>#Plus-Entity+between+Text</a>
 -<a rel="mw:WikiLink" href="./Main_Page#Underscore_between_Text" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#Underscore_between_Text"},"sa":{"href":"#Underscore_between_Text"}}'>#Underscore_between_Text</a>
 -<a rel="mw:WikiLink" href="./Main_Page#Underscore-Entity_between_Text" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#Underscore-Entity_between_Text"},"sa":{"href":"#Underscore-Entity&amp;#95;between&amp;#95;Text"}}'>#Underscore-Entity_between_Text</a></p>
 +<p><a rel="mw:WikiLink" href="./Parser_test#Space_between_Text" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#Space_between_Text"},"sa":{"href":"#Space between Text"}}'>#Space between Text</a>
 +<a rel="mw:WikiLink" href="./Parser_test#Space-Entity_between_Text" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#Space-Entity_between_Text"},"sa":{"href":"#Space-Entity&amp;#32;between&amp;#32;Text"}}'>#Space-Entity between Text</a>
 +<a rel="mw:WikiLink" href="./Parser_test#Plus+between+Text" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#Plus+between+Text"},"sa":{"href":"#Plus+between+Text"}}'>#Plus+between+Text</a>
 +<a rel="mw:WikiLink" href="./Parser_test#Plus-Entity+between+Text" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#Plus-Entity+between+Text"},"sa":{"href":"#Plus-Entity&amp;#43;between&amp;#43;Text"}}'>#Plus-Entity+between+Text</a>
 +<a rel="mw:WikiLink" href="./Parser_test#Underscore_between_Text" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#Underscore_between_Text"},"sa":{"href":"#Underscore_between_Text"}}'>#Underscore_between_Text</a>
 +<a rel="mw:WikiLink" href="./Parser_test#Underscore-Entity_between_Text" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#Underscore-Entity_between_Text"},"sa":{"href":"#Underscore-Entity&amp;#95;between&amp;#95;Text"}}'>#Underscore-Entity_between_Text</a></p>
  !! end
  
  # Parsoid html2wt disabled because it adds padding spaces around =
@@@ -18220,7 -17680,8 +18220,7 @@@ HTML tag with leading space is parsed a
  </p>
  !! end
  
 -## Don't expect Parsoid and PHP to match, since PHP isn't exactly following
 -## the HTML5 parsing spec.
 +## FIXME: The untrimmed attribute in Parsoid is T205737
  !! test
  Element with broken attribute syntax
  !! options
@@@ -18229,7 -17690,7 +18229,7 @@@ parsoid=wt2htm
  <div style=" style="123">hi</div>
  <div =>ho</div>
  !! html/php
 -<div style="123">hi</div>
 +<div style="style=">hi</div>
  <div>ho</div>
  
  !! html/parsoid
@@@ -19033,7 -18494,7 +19033,7 @@@ Tags which are hidden from tidiers cann
  !! end
  
  ###
 -### Parser hooks (see tests/parser/parserTestsParserHook.php for the <tag> extension)
 +### Parser hooks (see tests/parser/ParserTestParserHook.php for the <tag> extension)
  ###
  
  !! test
@@@ -19303,7 -18764,7 +19303,7 @@@ array 
  !! end
  
  ###
 -### (see tests/parser/parserTestsParserHook.php for the <statictag> extension)
 +### (see tests/parser/ParserTestParserHook.php for the <statictag> extension)
  ###
  
  !! test
@@@ -19317,8 -18778,8 +19317,8 @@@ Parser hook: static parser hook not ins
  hello, world
  </p>
  !! html/parsoid
 -<p><span typeof="mw:Extension/statictag" data-mw='{"name":"statictag","attrs":{},"body":{"extsrc":"hello, world"}}' data-parsoid='{}' about="#mwt2"></span></p>
 -<p typeof="mw:Extension/statictag" data-mw='{"name":"statictag","attrs":{"action":"flush"}}' data-parsoid='{}' about="#mwt4">hello, world</p>
 +<p><span typeof="mw:Extension/statictag" data-mw='{"name":"statictag","attrs":{},"body":{"extsrc":"hello, world"}}' about="#mwt2"></span></p>
 +<p><span typeof="mw:Extension/statictag" data-mw='{"name":"statictag","attrs":{"action":"flush"}}' about="#mwt4">hello, world</span></p>
  !! end
  
  !! test
@@@ -19331,7 -18792,7 +19331,7 @@@ Parser hook: static parser hook inside 
  </p>
  !! html/parsoid
  <!-- <statictag&#x3E;hello, world</statictag&#x3E; -->
 -<p typeof='mw:Extension/statictag' data-mw='{"name":"statictag","attrs":{"action":"flush"}}' data-parsoid='{}' about='#mwt2'></p>
 +<p><span typeof='mw:Extension/statictag' data-mw='{"name":"statictag","attrs":{"action":"flush"}}' data-parsoid='{}' about='#mwt2'></span></p>
  !! end
  
  # Nested template calls; this case was broken by Parser.php rev 1.506,
@@@ -19406,8 -18867,6 +19406,8 @@@ Table not started</td></tr></table
  
  !! test
  Sanitizer: Escaping of spaces, multibyte characters, colons & other stuff in id=""
 +!! options
 +title=[[Main Page]]
  !! config
  wgFragmentMode=[ 'html5', 'legacy' ]
  !! wikitext
@@@ -19899,17 -19358,19 +19899,17 @@@ parsoid=wt2html,html2htm
  !! wikitext
  ==a==
  {| STYLE=__TOC__
 -!! html
 +!! html/php
  <h2><span class="mw-headline" id="a">a</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=1" title="Edit section: a">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
  <table style="&#95;_TOC&#95;_">
  <tr><td></td></tr>
  </table>
  
 -!! html+tidy
 +!! html/php+tidy
  <h2><span class="mw-headline" id="a">a</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=1" title="Edit section: a">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 -<table style="__TOC__">
 -<tr>
 -<td></td>
 -</tr>
 -</table>
 +<table style="&#95;_TOC&#95;_">
 +<tbody><tr><td></td></tr>
 +</tbody></table>
  !! html/parsoid
  <h2 id="a">a</h2>
  <table style="__TOC__"></table>
@@@ -20099,8 -19560,8 +20099,8 @@@ parsoid=wt2htm
  <pre dir="&#10;"></pre>
  
  !! html/parsoid
 -<pre typeof="mw:Extension/pre" about="#mwt2" dir="
 -" data-mw='{"name":"pre","attrs":{"dir":"\n"},"body":{"extsrc":""}}'></pre>
 +<pre dir="
 +" typeof="mw:Extension/pre" about="#mwt2"data-mw='{"name":"pre","attrs":{"dir":"\n"},"body":{"extsrc":""}}'></pre>
  !! end
  
  !! test
@@@ -20126,7 -19587,7 +20126,7 @@@ Templates in extension attributes are n
  <pre dir="{{echo|ltr}}"></pre>
  
  !! html/parsoid
 -<pre typeof="mw:Extension/pre" about="#mwt2" dir="{{echo|ltr}}" data-mw='{"name":"pre","attrs":{"dir":"{{echo|ltr}}"},"body":{"extsrc":""}}'></pre>
 +<pre dir="{{echo|ltr}}" typeof="mw:Extension/pre" about="#mwt2" data-mw='{"name":"pre","attrs":{"dir":"{{echo|ltr}}"},"body":{"extsrc":""}}'></pre>
  !! end
  
  !! test
@@@ -21563,6 -21024,7 +21563,6 @@@ image:foobar.jpg|link=Main Page#section
  </ul>
  !! end
  
 -## Whoops, Parsoid shouldn't be parsing templates in the attribute caption!
  !! test
  Gallery with template inside caption
  !! options
@@@ -21575,7 -21037,7 +21575,7 @@@ File:Foobar.jpg|{{echo|ho}
  </gallery>
  !! html/php
  <ul class="gallery mw-gallery-traditional">
 -      <li class='gallerycaption'>{{echo|hi}}</li>
 +      <li class='gallerycaption'>hi</li>
                <li class="gallerybox" style="width: 155px"><div style="width: 155px">
                        <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
                        <div class="gallerytext">
  </ul>
  !! end
  
 +!! test
 +Gallery with wikitext inside gallery caption
 +!! wikitext
 +<gallery caption="# List item
 +
 +Text '''bold''' [[link]] {{ns:-1}}
 +
 +[[File:Foobar.jpg|thumb|File in gallery caption]]">
 +File:Foobar.jpg|Image caption
 +</gallery>
 +!! html/php
 +<ul class="gallery mw-gallery-traditional">
 +      <li class='gallerycaption'># List item Text <b>bold</b> <a href="/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> Special <div class="thumb tright"><div class="thumbinner" style="width:182px;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="" 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>File in gallery caption</div></div></div></li>
 +              <li class="gallerybox" style="width: 155px"><div style="width: 155px">
 +                      <div class="thumb" style="width: 150px;"><div style="margin:68px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="" src="http://example.com/images/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg" width="120" height="14" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/240px-Foobar.jpg 2x" /></a></div></div>
 +                      <div class="gallerytext">
 +<p>Image caption
 +</p>
 +                      </div>
 +              </div></li>
 +</ul>
 +
 +!! end
 +
  !! test
  Gallery with wikitext inside caption
  !! options
@@@ -22290,8 -21728,6 +22290,8 @@@ wgRawHtml=
  !! html/php
  <p><script>alert(1);</script>
  </p>
 +!! html/parsoid
 +<p><script typeof="mw:Extension/html" about="#mwt3" data-mw='{"name":"html","attrs":{},"body":{"extsrc":"&lt;script>alert(1);&lt;/script>"}}'>alert(1);</script></p>
  !! end
  
  !! test
@@@ -22998,6 -22434,19 +22998,19 @@@ language=zh variant=zh-t
  <p><span typeof="mw:LanguageVariant" data-parsoid='{"tSp":[6]}' data-mw-variant='{"twoway":[{"l":"zh","t":"China"},{"l":"zh-tw","t":"Taiwan"}]}'></span>, not China</p>
  !! end
  
+ !! test
+ Explicit definition of language variant alternatives (BCP 47 codes)
+ !! options
+ language=zh variant=zh-tw
+ !! wikitext
+ -{zh:China;zh-Hant-TW:Taiwan}-, not China
+ !! html/php
+ <p>Taiwan, not China
+ </p>
+ !! html/parsoid
+ <p><span typeof="mw:LanguageVariant" data-parsoid='{"tSp":[6]}' data-mw-variant='{"twoway":[{"l":"zh","t":"China"},{"l":"zh-Hant-TW","t":"Taiwan"}]}'></span>, not China</p>
+ !! end
  !! test
  Filter syntax for language variants
  !! options
@@@ -24489,7 -23938,7 +24502,7 @@@ comment title=[[Main Page]
  !! wikitext
  /* External links */ removed bogus entries
  !! html/php
 -<a href="/wiki/Main_Page#External_links" title="Main Page">→</a>‎<span dir="auto"><span class="autocomment">External links: </span> removed bogus entries</span>
 +<a href="/wiki/Main_Page#External_links" title="Main Page">→External links</a>‎<span dir="auto"><span class="autocomment">: </span> removed bogus entries</span>
  !!end
  
  !! test
@@@ -24499,7 -23948,7 +24512,7 @@@ comment title=[[Main Page]
  !! wikitext
  pre-comment text /* External links */ removed bogus entries
  !! html/php
 -pre-comment text <a href="/wiki/Main_Page#External_links" title="Main Page">→</a>‎<span dir="auto"><span class="autocomment">External links: </span> removed bogus entries</span>
 +pre-comment text <a href="/wiki/Main_Page#External_links" title="Main Page">→External links</a>‎<span dir="auto"><span class="autocomment">: </span> removed bogus entries</span>
  !!end
  
  !! test
@@@ -24509,7 -23958,7 +24522,7 @@@ comment local title=[[Main Page]
  !! wikitext
  /* External links */ removed bogus entries
  !! html/php
 -<a href="#External_links">→</a>‎<span dir="auto"><span class="autocomment">External links: </span> removed bogus entries</span>
 +<a href="#External_links">→External links</a>‎<span dir="auto"><span class="autocomment">: </span> removed bogus entries</span>
  !!end
  
  !! test
@@@ -24620,7 -24069,7 +24633,7 @@@ title=[[Main Page]
  !! wikitext
  /* __hello__world__ */
  !! html/php
 -<a href="/wiki/Main_Page#hello_world" title="Main Page">→</a>‎<span dir="auto"><span class="autocomment">__hello__world__</span></span>
 +<a href="/wiki/Main_Page#hello_world" title="Main Page">→__hello__world__</a>‎
  !! end
  
  !! test
@@@ -25429,30 -24878,9 +25442,30 @@@ __TOC_
  
  <h2><span class="mw-headline" id="Style"><style>.foo {}</style>Style<style>.bar {}</style></span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=1" title="Edit section: Style">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
  
 +!! html/parsoid
 +<meta property="mw:PageProp/toc" data-parsoid="{}"/>
 +<h2 id="Style" data-parsoid="{}"><style typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{},"body":{"extsrc":".foo {}"}}'>.foo {}</style>Style<style typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{},"body":{"extsrc":".bar {}"}}'>.bar {}</style></h2>
 +!! end
 +
 +!! test
 +T198618: script element in ToC
 +!! options
 +wgRawHtml=1
 +!! wikitext
 +__TOC__
 +==<html><script>alert(1);</script></html>Script<html><script>alert(1);</script></html>==
 +!! html/php
 +<div id="toc" class="toc"><input type="checkbox" role="button" id="toctogglecheckbox" class="toctogglecheckbox" style="display:none"/><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2><span class="toctogglespan"><label class="toctogglelabel" for="toctogglecheckbox"></label></span></div>
 +<ul>
 +<li class="toclevel-1 tocsection-1"><a href="#Script"><span class="tocnumber">1</span> <span class="toctext">Script</span></a></li>
 +</ul>
 +</div>
 +
 +<h2><span class="mw-headline" id="Script"><script>alert(1);</script>Script<script>alert(1);</script></span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=1" title="Edit section: Script">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 +
  !! html/parsoid
  <meta property="mw:PageProp/toc" data-parsoid='{}'/>
 -<h2 id="Style" data-parsoid='{}'><style>.foo {}</style>Style<style>.bar {}</style></h2>
 +<h2 id="Script" data-parsoid='{}'><script typeof="mw:Extension/html" about="#mwt4" data-mw='{"name":"html","attrs":{},"body":{"extsrc":"&lt;script>alert(1);&lt;/script>"}}'>alert(1);</script>Script<script typeof="mw:Extension/html" about="#mwt6" data-mw='{"name":"html","attrs":{},"body":{"extsrc":"&lt;script>alert(1);&lt;/script>"}}'>alert(1);</script></h2>
  !! end
  
  !! test
  ###
  !!test
  1. SOL-sensitive wikitext tokens as template-args
 -!!options
 +!! options
  parsoid=wt2html,wt2wt
  !! wikitext
  {{echo|*a}}
  {{echo|#a}}
  {{echo|:a}}
 -!! html
 +!! html/php+tidy
 +<ul><li>a</li></ul>
 +<ol><li>a</li></ol>
 +<dl><dd>a</dd></dl>
 +!! html/parsoid
  <span about="#mwt1" typeof="mw:Transclusion">
  </span><ul about="#mwt1"><li>a</li>
  </ul>
@@@ -26868,33 -26292,6 +26881,33 @@@ parsoid=html2w
  
  !! end
  
 +!! test
 +Tables: 4e. Escape }
 +!! options
 +parsoid=html2wt
 +!! html/parsoid
 +<table>
 +<tr><td>}</td></tr>
 +<tr><td>x</td><td data-parsoid='{"stx":"row"}'>}</td></tr></table>
 +</table>
 +!! wikitext
 +{|
 +|<nowiki>}</nowiki>
 +|-
 +|x||}
 +|}
 +!! html/php
 +<table>
 +<tr>
 +<td>}
 +</td></tr>
 +<tr>
 +<td>x</td>
 +<td>}
 +</td></tr></table>
 +
 +!! end
 +
  !! test
  Tables: 5. Empty table cells should get whitespace to avoid need for nowikis
  !! options
@@@ -28144,20 -27541,17 +28157,20 @@@ Indent and comment before table ro
  </tbody></table>
  !! end
  
 -# Parsoid-specific since PHP parser doesn't handle this mixed tbl-wikitext
 +# PHP parser omits empty TR
  !!test
  Empty TR followed by a template-generated TR
 -!!options
 -parsoid
  !! wikitext
  {|
  |-
  {{echo|<tr><td>foo</td></tr>}}
  |}
 -!! html
 +!! html/php+tidy
 +<table>
 +
 +<tbody><tr><td>foo</td></tr>
 +</tbody></table>
 +!! html/parsoid
  <table>
  <tbody>
  <tr class='mw-empty-elt'></tr>
  </tbody></table>
  !!end
  
 -## PHP and parsoid output differ for this, and since this is primarily
 -## for testing Parsoid's serializer, marking this Parsoid only
 +## PHP and parsoid output differ for this; as usual PHP omits empty
 +## elements, and since it strips the comments the TR is empty.
  !!test
  Empty TR followed by mixed-ws-comment line should RT correctly
 -!!options
 -parsoid
  !! wikitext
  {|
  |-
  |-
  <!--c--> <!--d-->
  |}
 -!! html
 +!! html/php+tidy
 +<table>
 +
 +
 +</table>
 +!! html/parsoid
  <table>
  <tbody>
  <tr class='mw-empty-elt'></tr>
@@@ -28850,11 -28241,11 +28863,11 @@@ parsoid=
  
  !! test
  Image: empty alt attribute (T50924)
 -!! options
 -parsoid
  !! wikitext
  [[File:Foobar.jpg|thumb|alt=|bar]]
 -!! html
 +!! html/php+tidy
 +<div class="thumb tright"><div class="thumbinner" style="width:182px;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="" 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>bar</div></div></div>
 +!! html/parsoid
  <figure class="mw-default-size" typeof="mw:Image/Thumb" data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"alt","ak":"alt="},{"ck":"caption","ak":"bar"}]}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"}}'><img alt="" resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="25" width="220" data-parsoid='{"a":{"alt":"","resource":"./File:Foobar.jpg","height":"25","width":"220"},"sa":{"alt":"alt=","resource":"File:Foobar.jpg"}}'/></a><figcaption>bar</figcaption></figure>
  !! end
  
@@@ -28975,27 -28366,6 +28988,27 @@@ Image: Invalid title as lin
  <p><figure-inline class="mw-default-size" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"link","ak":"link=&lt;"}]}' data-mw='{"caption":"link=&amp;lt;"}'><a href="./File:Foobar.jpg" data-parsoid='{"a":{"href":"./File:Foobar.jpg"},"sa":{}}'><img resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
  !! end
  
 +!! test
 +Various link types in alt and link options
 +!! wikitext
 +[[File:Foobar.jpg|link=[[Main Page]]|alt=[[Main Page]]|caption]]
 +
 +[[File:Foobar.jpg|link=[[Media:Thumb.png]]|alt=[[Media:Thumb.png]]|caption]]
 +
 +[[File:Foobar.jpg|link=[[wikipedia:Foo]]|alt=[[wikipedia:Foo]]|caption]]
 +!! html/php+tidy
 +<p><a href="/wiki/Main_Page" title="caption"><img alt="Main Page" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p><p><a href="/wiki/Media:Thumb.png" title="caption"><img alt="Media:Thumb.png" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p><p><a href="http://en.wikipedia.org/wiki/Foo" title="caption"><img alt="wikipedia:Foo" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 +</p>
 +!! html/parsoid
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-mw='{"caption":"caption"}'><a href="./Main_Page" data-parsoid='{"a":{"href":"./Main_Page"},"sa":{"href":"link=[[Main Page]]"}}'><img alt="Main Page" resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"alt":"Main Page","resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"alt":"alt=[[Main Page]]","resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 +
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-mw='{"caption":"caption"}'><a href="./Media:Thumb.png" data-parsoid='{"a":{"href":"./Media:Thumb.png"},"sa":{"href":"link=[[Media:Thumb.png]]"}}'><img alt="Media:Thumb.png" resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"alt":"Media:Thumb.png","resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"alt":"alt=[[Media:Thumb.png]]","resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 +
 +<p><figure-inline class="mw-default-size" typeof="mw:Image" data-mw='{"caption":"caption"}'><a href="http://en.wikipedia.org/wiki/Foo" data-parsoid='{"a":{"href":"http://en.wikipedia.org/wiki/Foo"},"sa":{"href":"link=[[wikipedia:Foo]]"}}'><img alt="wikipedia:Foo" resource="./File:Foobar.jpg" src="//example.com/images/3/3a/Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="220" width="1941" data-parsoid='{"a":{"alt":"wikipedia:Foo","resource":"./File:Foobar.jpg","height":"220","width":"1941"},"sa":{"alt":"alt=[[wikipedia:Foo]]","resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 +!! end
 +
  !! test
  Lists: Serialize correctly even when list content is wrapped in p-tags (like VE does)
  !! options
@@@ -30142,7 -29512,7 +30155,7 @@@ parsoid=
  Serialize wikitext list items as HTML list items when embedded in a HTML list
  !! options
  parsoid=html2wt
 -!! html
 +!! html/parsoid
  <ul data-parsoid='{"stx": "html"}'>
  <li data-parsoid='{}'>a</li>
  <li>b</li>
  <li>a</li>
  <li>b</li>
  </ul>
 +!! html/php+tidy
 +<ul>
 +<li>a</li>
 +<li>b</li>
 +</ul>
  !! end
  
 -# SSS FIXME: Is this actually a good thing given the
 -# odd nested list output that is generated by MW?
 +# Nested list should be inside <li>, that is
  # <ul><li>foo<ul>..</ul></li></ul> instead of
  # <ul><li>foo</li><ul>..</ul></ul>
 +# See https://stackoverflow.com/questions/5899337/proper-way-to-make-html-nested-list
  !! test
  Wikitext lists can be nested inside HTML lists
  !! options
  parsoid=html2wt
 -!! html
 +!! html/parsoid
  <ul data-parsoid='{"stx": "html"}'>
  <li data-parsoid='{"stx": "html"}'>a
  <ul><li>b</li></ul>
  * y
  </li>
  </ul>
 +!! html/php+tidy
 +<ul>
 +<li>a
 +<ul><li>b</li></ul>
 +</li>
 +</ul>
 +<ul>
 +<li>x
 +<ul><li>y</li></ul>
 +</li>
 +</ul>
  !! end
  
  !! test
@@@ -30645,7 -29999,7 +30658,7 @@@ parsoid=
    "modes": ["html2wt"],
    "scrubWikitext": true
  }
 -!! html
 +!! html/parsoid
  <h2><i></i></h2>
  <p><a href='Foo' rel='mw:WikiLink'>foo<i></i>
   </a><b><i></i></b>x</p>
@@@ -30663,14 -30017,15 +30676,14 @@@ parsoid=
    "modes": ["selser"],
    "scrubWikitext": true,
    "changes": [
 -    [ "#x", "after", "<h1><i></i></h1>\n<p> x<b></b></p>"]
 +    [ "#x", "after", "<h1><i></i></h1>\n<p> bar<b></b></p>"]
    ]
  }
  !! wikitext
 -<span id="x">foo</span>
 +<div id="x">foo</div>
  !! wikitext/edited
 -<span id="x">foo</span>
 -
 -x
 +<div id="x">foo</div>
 +bar
  !! end
  
  !! test
@@@ -30803,12 -30158,11 +30816,12 @@@ parsoid=
    "modes": ["html2wt"],
    "scrubWikitext": false
  }
 -!! html
 +!! html/parsoid
  <table>
  <tr><td>a</td></tr>
  <tr><td>-</td></tr>
  <tr><td>+</td></tr>
 +<tr><td>}</td></tr>
  </table>
  !! wikitext
  {|
  |<nowiki>-</nowiki>
  |-
  |<nowiki>+</nowiki>
 +|-
 +|<nowiki>}</nowiki>
  |}
  !! end
  
@@@ -30829,12 -30181,11 +30842,12 @@@ parsoid=
    "modes": ["html2wt"],
    "scrubWikitext": true
  }
 -!! html
 +!! html/parsoid
  <table>
  <tr><td>a</td></tr>
  <tr><td>-</td></tr>
  <tr><td>+</td></tr>
 +<tr><td>}</td></tr>
  </table>
  !! wikitext
  {|
  | -
  |-
  | +
 +|-
 +| }
  |}
  !! end
  
@@@ -30989,7 -30338,7 +31002,7 @@@ parsoid=
    "modes": ["html2wt"],
    "scrubWikitext": true
  }
 -!! html
 +!! html/parsoid
  <font>foo</font>
  <font><font>bar</font></font>
  <font class="x">boo</font>
@@@ -31006,176 -30355,12 +31019,176 @@@ parsoid=
    "modes": ["html2wt"],
    "scrubWikitext": false
  }
 -!! html
 +!! html/parsoid
  <font>foo</font>
  !! wikitext
  <font>foo</font>
  !! end
  
 +!! test
 +Ignore empty <p></p> when scrubWikitext is false
 +!! options
 +parsoid={
 +  "modes": ["html2wt"],
 +  "scrubWikitext": false
 +}
 +!! html/parsoid
 +<div>1</div>
 +<p>a</p><p></p><p>b</p>
 +<div>2</div>
 +<p>a</p>
 +<p></p>
 +<p>b</p>
 +<div>3</div>
 +<p>a</p>
 +<p></p>
 +<p></p>
 +<p></p>
 +<p></p>
 +<p>b</p>
 +!! wikitext
 +<div>1</div>
 +a
 +
 +b
 +<div>2</div>
 +a
 +
 +b
 +<div>3</div>
 +a
 +
 +b
 +!! html/php+tidy
 +<div>1</div>
 +<p>a
 +</p><p>b
 +</p>
 +<div>2</div>
 +<p>a
 +</p><p>b
 +</p>
 +<div>3</div>
 +<p>a
 +</p><p>b
 +</p>
 +!! end
 +
 +!! test
 +Normalize empty paragraphs to HTML form that html2wt expects
 +!! options
 +parsoid={
 +  "modes": ["html2wt"],
 +  "scrubWikitext": true
 +}
 +!! html/parsoid
 +<div>1</div>
 +<p>a</p><p></p><p>b</p>
 +<div>2</div>
 +<p>a</p>
 +<p></p>
 +<p>b</p>
 +<div>3</div>
 +<p>a</p>
 +<p></p>
 +<p></p>
 +<p></p>
 +<p></p>
 +<p>b</p>
 +<div>4</div>
 +<p>a</p>
 +<p></p>
 +<div>foo</div>
 +!! wikitext
 +<div>1</div>
 +a
 +
 +
 +b
 +<div>2</div>
 +a
 +
 +
 +b
 +<div>3</div>
 +a
 +
 +
 +
 +
 +
 +b
 +<div>4</div>
 +a
 +
 +<br />
 +<div>foo</div>
 +!! html/php+tidy
 +<div>1</div>
 +<p>a
 +</p><p><br />
 +b
 +</p>
 +<div>2</div>
 +<p>a
 +</p><p><br />
 +b
 +</p>
 +<div>3</div>
 +<p>a
 +</p><p><br />
 +</p><p><br />
 +</p><p>b
 +</p>
 +<div>4</div>
 +<p>a
 +</p><p><br />
 +</p>
 +<div>foo</div>
 +!! end
 +
 +!! test
 +Empty paragraphs (marked with mw-empty-elt) found in source should not be normalized away
 +!! options
 +parsoid={
 +  "modes": ["html2wt"],
 +  "scrubWikitext": true
 +}
 +!! html/parsoid
 +<table>
 +<tbody>
 +<tr>
 +<td><div>foo
 +</div>
 +<p class="mw-empty-elt"></p>
 +</td>
 +</tr>
 +</tbody>
 +<caption></caption>
 +</table>
 +!! wikitext
 +{|
 +|<div>foo
 +</div>
 +|+
 +|}
 +!! end
 +
 +!! test
 +Templated content should be skipped over by normalization
 +!! options
 +parsoid={
 +  "modes": ["html2wt"],
 +  "scrubWikitext": true
 +}
 +!! html/parsoid
 +<p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"SomeTemplate1","href":"./Template:SomeTemplate1"},"params":{"1":{"wt":"boo"}},"i":0}}]}'>foobar</p><p about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"SomeTemplate2","href":"./Template:SomeTemplate2"},"params":{"1":{"wt":"booboo"}},"i":0}}]}'>foobar</p><span about="#mwt2">
 +</span><p about="#mwt2"></p><span about="#mwt2">
 +</span>
 +!! wikitext
 +{{SomeTemplate1|boo}}{{SomeTemplate2|booboo}}
 +!! end
 +
  !! test
  Escape nowiki DOM elements
  !! options
@@@ -31492,18 -30677,8 +31505,18 @@@ styletag=
  <style>.foo::before { content: "<foo>"; }</style>
  <style data-mw-foobar="baz">.foo::after { content: "<bar>"; }</style>
  </div>
 +!! html/parsoid
 +<div class="foo">
 +<style typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{},"body":{"extsrc":".foo::before { content: \"&lt;foo>\"; }"}}'>.foo::before { content: "<foo>"; }</style>
 +<style data-x-data-mw-foobar="baz" typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{"data-x-data-mw-foobar":"baz"},"body":{"extsrc":".foo::after { content: \"&lt;bar>\"; }"}}'>.foo::after { content: "<bar>"; }</style>
 +</div>
  !! end
  
 +## Right now, Parsoid doesn't de-duplicate style tags.
 +## So, we shouldn't see link tags that need to bypass the sanitizer.
 +## In a followup patch, when we de-duplicate style tags and
 +## introduce link tags, we'll add a hook for link tags in
 +## the parser test runner script.
  !! test
  Validating that <style> isn't wrapped in a paragraph (T186965)
  !! options
  <style>.foo::before { content: "<foo>"; }</style>
  bar
  </p>
 +!! html/parsoid
 +<p>A style tag, by itself or with other style/link tags, shouldn't be wrapped in a paragraph</p>
 +
 +<style typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{},"body":{"extsrc":".foo::before { content: \"&lt;foo>\"; }"}}'>.foo::before { content: "<foo>"; }</style>
 +
 +<p><style typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{},"body":{"extsrc":".foo::before { content: \"&lt;foo>\"; }"}}'>.foo::before { content: "<foo>"; }</style> &lt;link rel="foo" href="bar"/><style typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{},"body":{"extsrc":".foo::before { content: \"&lt;foo>\"; }"}}'>.foo::before { content: "<foo>"; }</style></p>
 +
 +<p>But if it's on a line with other content, let it be wrapped.</p>
 +
 +<p><style typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{},"body":{"extsrc":".foo::before { content: \"&lt;foo>\"; }"}}'>.foo::before { content: "<foo>"; }</style> bar</p>
 +
 +<p>foo <style typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{},"body":{"extsrc":".foo::before { content: \"&lt;foo>\"; }"}}'>.foo::before { content: "<foo>"; }</style></p>
 +
 +<p>foo <style typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{},"body":{"extsrc":".foo::before { content: \"&lt;foo>\"; }"}}'>.foo::before { content: "<foo>"; }</style> bar</p>
 +
 +<p>And the same if we have non-paragraph-breaking whitespace</p>
 +
 +<p>foo
 +<style typeof="mw:Extension/style" data-mw='{"name":"style","attrs":{},"body":{"extsrc":".foo::before { content: \"&lt;foo>\"; }"}}'>.foo::before { content: "<foo>"; }</style>
 +bar</p>
  !! end
  
  !! test
  </p>
  !! end
  
 +!! test
 +Extension returning multiple nodes starting with a style tag roundtrips
 +!! options
 +wgRawHtml=1
 +!! wikitext
 +<table>
 +{{echo|<html><style>.hi { color: red; }</style>
 +</html>}}
 +<tr><td class="hi">ho</td></tr>
 +</table>
 +!! html/parsoid
 +<p about="#mwt5" typeof="mw:Transclusion" data-parsoid='{"fostered":true,"autoInsertedEnd":true,"autoInsertedStart":true,"firstWikitextNode":"TABLE_html","pi":[[{"k":"1"}]]}' data-mw='{"parts":["&lt;table>\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;html>&lt;style>.hi { color: red; }&lt;/style>\n&lt;/html>"}},"i":0}},"\n&lt;tr>&lt;td class=\"hi\">ho&lt;/td>&lt;/tr>\n&lt;/table>"]}'><style typeof="mw:Extension/html" about="#mwt4" data-mw='{"name":"html","attrs":{},"body":{"extsrc":"&lt;style>.hi { color: red; }&lt;/style>\n"}}'>.hi { color: red; }</style><span about="#mwt4">
 +</span></p><table about="#mwt5" data-parsoid='{"stx":"html"}'>
 +
 +<tbody><tr><td class="hi">ho</td></tr>
 +</tbody></table>
 +!! end
 +
  !! test
  Decoding of HTML entities in headings and links for IDs and link fragments (T103714)
  !! config
@@@ -31635,7 -30772,7 +31648,7 @@@ wgFragmentMode=[ 'html5', 'legacy' 
  </p>
  !! html/parsoid
  <h2 id="A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E"><span id="A.26B.26C.26amp.3BD.26amp.3Bamp.3BE" typeof="mw:FallbackId" data-parsoid="{}"></span>A&amp;B<span typeof="mw:Entity" data-parsoid='{"src":"&amp;amp;","srcContent":"&amp;"}'>&amp;</span>C<span typeof="mw:Entity" data-parsoid='{"src":"&amp;amp;","srcContent":"&amp;"}'>&amp;</span>amp;D<span typeof="mw:Entity" data-parsoid='{"src":"&amp;amp;","srcContent":"&amp;"}'>&amp;</span>amp;amp;E</h2>
 -<p><a rel="mw:WikiLink" href="./Main_Page#A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E"},"sa":{"href":"#A&amp;B&amp;amp;C&amp;amp;amp;D&amp;amp;amp;amp;E"}}'>#A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E</a></p>
 +<p><a rel="mw:WikiLink" href="./Parser_test#A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E"},"sa":{"href":"#A&amp;B&amp;amp;C&amp;amp;amp;D&amp;amp;amp;amp;E"}}'>#A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E</a></p>
  !! end
  
  !! test
@@@ -31748,12 -30885,12 +31761,12 @@@ wgFragmentMode=[ 'html5', 'legacy' 
  <h2 id="тест"><span id=".D1.82.D0.B5.D1.81.D1.82" typeof="mw:FallbackId"></span>тест</h2>
  
  <h2 id="Hey_&lt;_#_&quot;_>_%_:_'"><span id="Hey_.3C_.23_.22_.3E_.25_:_.27" typeof="mw:FallbackId"></span>Hey &lt; # " > %<span typeof="mw:DisplaySpace mw:Placeholder" data-parsoid='{"src":" ","isDisplayHack":true}'> </span>: '</h2>
 -<p><a rel="mw:WikiLink" href="./Main_Page#Foo_bar">#Foo bar</a> <a rel="mw:WikiLink" href="./Main_Page#foo_Bar">#foo Bar</a> <a rel="mw:WikiLink" href="./Main_Page#Тест">#Тест</a> <a rel="mw:WikiLink" href="./Main_Page#тест">#тест</a> <a rel="mw:WikiLink" href="./Main_Page#Hey_&lt;_#_&quot;_>_%_:_'" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#Hey_&lt;_#_\"_>_%_:_&#39;"},"sa":{"href":"#Hey &lt; # \" > % : &#39;"}}'>#Hey &lt; # " > % : '</a></p>
 +<p><a rel="mw:WikiLink" href="./Parser_test#Foo_bar">#Foo bar</a> <a rel="mw:WikiLink" href="./Parser_test#foo_Bar">#foo Bar</a> <a rel="mw:WikiLink" href="./Parser_test#Тест">#Тест</a> <a rel="mw:WikiLink" href="./Parser_test#тест">#тест</a> <a rel="mw:WikiLink" href="./Parser_test#Hey_&lt;_#_&quot;_>_%_:_'" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#Hey_&lt;_#_\"_>_%_:_&#39;"},"sa":{"href":"#Hey &lt; # \" > % : &#39;"}}'>#Hey &lt; # " > % : '</a></p>
  
  <p><span about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"anchorencode:💩","function":"anchorencode"},"params":{},"i":0}}]}'>💩</span> <span id="💩" about="#mwt3" typeof="mw:ExpandedAttrs" data-mw='{"attribs":[[{"txt":"id"},{"html":"&lt;span about=\"#mwt2\" typeof=\"mw:Transclusion\" data-parsoid=&#39;{\"pi\":[[]],\"dsr\":[178,197,null,null]}&#39; data-mw=&#39;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"anchorencode:💩\",\"function\":\"anchorencode\"},\"params\":{},\"i\":0}}]}&#39;>💩&lt;/span>"}]]}'></span></p>
  
  <!-- These two links should produce identical HTML -->
 -<p><a rel="mw:WikiLink" href="./Main_Page#啤酒">#啤酒</a> <a rel="mw:WikiLink" href="./Main_Page#啤酒" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#啤酒"},"sa":{"href":"#%E5%95%A4%E9%85%92"}}'>#啤酒</a></p>
 +<p><a rel="mw:WikiLink" href="./Parser_test#啤酒">#啤酒</a> <a rel="mw:WikiLink" href="./Parser_test#啤酒" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#啤酒"},"sa":{"href":"#%E5%95%A4%E9%85%92"}}'>#啤酒</a></p>
  !! end
  
  # Parsoid doesn't support this mode
@@@ -31866,7 -31003,7 +31879,7 @@@ wgFragmentMode=[ 'html5', 'legacy' 
  </p>
  !! html/parsoid
  <h2 id="Foo_bar"> Foo<span typeof="mw:Entity" data-parsoid='{"src":"&amp;nbsp;","srcContent":" "}'> </span>bar </h2>
 -<p><a rel="mw:WikiLink" href="./Main_Page#Foo_bar" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#Foo_bar"},"sa":{"href":"#Foo&amp;nbsp;bar"}}'>#Foo bar</a></p>
 +<p><a rel="mw:WikiLink" href="./Parser_test#Foo_bar" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#Foo_bar"},"sa":{"href":"#Foo&amp;nbsp;bar"}}'>#Foo bar</a></p>
  !! end
  
  !! test
@@@ -31923,7 -31060,7 +31936,7 @@@ wgFragmentMode=[ 'html5' 
  <p><span id="&#91;foo&#93;"></span><a href="#[foo]">#&#91;foo&#93;</a>
  </p>
  !! html/parsoid
 -<p><span id="[foo]" about="#mwt3" typeof="mw:ExpandedAttrs" data-parsoid='{"stx":"html","a":{"id":"[foo]"},"sa":{"id":"{{anchorencode:[foo]}}"}}' data-mw='{"attribs":[[{"txt":"id"},{"html":"&lt;span typeof=\"mw:Transclusion mw:Entity\" about=\"#mwt1\" data-parsoid=&apos;{\"srcContent\":\"[\",\"dsr\":[10,32,null,null],\"pi\":[[]]}&apos; data-mw=&apos;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"anchorencode:[foo]\",\"function\":\"anchorencode\"},\"params\":{},\"i\":0}}]}&apos;>[&lt;/span>&lt;span about=\"#mwt1\" data-parsoid=\"{}\">foo&lt;/span>&lt;span typeof=\"mw:Entity\" about=\"#mwt1\" data-parsoid=&apos;{\"src\":\"&amp;amp;#x5D;\",\"srcContent\":\"]\"}&apos;>]&lt;/span>"}]]}'></span><a typeof="mw:ExpandedAttrs" about="#mwt4" rel="mw:WikiLink" href="./Main_Page#[foo]" data-parsoid='{"stx":"simple","a":{"href":"./Main_Page#[foo]"},"sa":{"href":"#{{anchorencode:[foo]}}"}}' data-mw='{"attribs":[[{"txt":"href"},{"html":"#&lt;span typeof=\"mw:Transclusion mw:Entity\" about=\"#mwt2\" data-parsoid=&apos;{\"srcContent\":\"[\",\"dsr\":[44,66,null,null],\"pi\":[[]]}&apos; data-mw=&apos;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"anchorencode:[foo]\",\"function\":\"anchorencode\"},\"params\":{},\"i\":0}}]}&apos;>[&lt;/span>&lt;span about=\"#mwt2\" data-parsoid=\"{}\">foo&lt;/span>&lt;span typeof=\"mw:Entity\" about=\"#mwt2\" data-parsoid=&apos;{\"src\":\"&amp;amp;#x5D;\",\"srcContent\":\"]\"}&apos;>]&lt;/span>"}]]}'>#[foo]</a></p>
 +<p><span id="[foo]" about="#mwt3" typeof="mw:ExpandedAttrs" data-parsoid='{"stx":"html","a":{"id":"[foo]"},"sa":{"id":"{{anchorencode:[foo]}}"}}' data-mw='{"attribs":[[{"txt":"id"},{"html":"&lt;span typeof=\"mw:Transclusion mw:Entity\" about=\"#mwt1\" data-parsoid=&apos;{\"srcContent\":\"[\",\"dsr\":[10,32,null,null],\"pi\":[[]]}&apos; data-mw=&apos;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"anchorencode:[foo]\",\"function\":\"anchorencode\"},\"params\":{},\"i\":0}}]}&apos;>[&lt;/span>&lt;span about=\"#mwt1\" data-parsoid=\"{}\">foo&lt;/span>&lt;span typeof=\"mw:Entity\" about=\"#mwt1\" data-parsoid=&apos;{\"src\":\"&amp;amp;#x5D;\",\"srcContent\":\"]\"}&apos;>]&lt;/span>"}]]}'></span><a typeof="mw:ExpandedAttrs" about="#mwt4" rel="mw:WikiLink" href="./Parser_test#[foo]" data-parsoid='{"stx":"simple","a":{"href":"./Parser_test#[foo]"},"sa":{"href":"#{{anchorencode:[foo]}}"}}' data-mw='{"attribs":[[{"txt":"href"},{"html":"#&lt;span typeof=\"mw:Transclusion mw:Entity\" about=\"#mwt2\" data-parsoid=&apos;{\"srcContent\":\"[\",\"dsr\":[44,66,null,null],\"pi\":[[]]}&apos; data-mw=&apos;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"anchorencode:[foo]\",\"function\":\"anchorencode\"},\"params\":{},\"i\":0}}]}&apos;>[&lt;/span>&lt;span about=\"#mwt2\" data-parsoid=\"{}\">foo&lt;/span>&lt;span typeof=\"mw:Entity\" about=\"#mwt2\" data-parsoid=&apos;{\"src\":\"&amp;amp;#x5D;\",\"srcContent\":\"]\"}&apos;>]&lt;/span>"}]]}'>#[foo]</a></p>
  !! end
  
  ## ------------------------------