Complete coverage of Parser::getTemplateDom()
[lhc/web/wiklou.git] / tests / parser / parserTests.txt
index aa03c0e..216d7e5 100644 (file)
@@ -62,6 +62,12 @@ Template:Foo
 FOO
 !!endarticle
 
+!! article
+Template:redirect to foo
+!! text
+#REDIRECT [[Template:Foo]]
+!! endarticle
+
 !! article
 Template:Blank
 !! text
@@ -109,6 +115,14 @@ Template:echo
 {{{1}}}
 !! endarticle
 
+!! article
+Template:echo3
+!! text
+{{{1}}}
+{{{1}}}
+{{{1}}}
+!! endarticle
+
 // For Serbian; localize Template namespace
 !! article
 Шаблон:Echo
@@ -128,6 +142,12 @@ Template:echo_with_div
 <div>{{{1}}}</div>
 !! endarticle
 
+!! article
+Template:echo with depth
+!! text
+{{echo|{{{1}}}}}
+!! endarticle
+
 !! article
 Template:blank_param
 !! text
@@ -279,12 +299,6 @@ Template:EmptyTRWithHTMLAttrTest
 </table>
 !!endarticle
 
-!! article
-Template:CircularRef
-!! text
-<ref>{{CircularRef}}</ref>
-!! endarticle
-
 !! article
 Template:With: Colon
 !! text
@@ -294,22 +308,13 @@ Template with colon
 ###
 ### Basic tests
 ###
+
 !! test
 Blank input
 !! wikitext
 !! html
 !! end
 
-!! test
-CircularRef
-!! wikitext
-{{CircularRef}}
-<references />
-!! html/parsoid
-<p><span about="#mwt1" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Transclusion  mw:Extension/ref" data-parsoid='{"pi":[[]]}' data-mw='{"parts":[{"template":{"target":{"wt":"CircularRef","href":"./Template:CircularRef"},"params":{},"i":0}}]}'><a href="./Main_Page#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></span></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" data-parsoid="{}">Error: Expansion loop detected at <a data-parsoid='{"a":{"href":null},"sa":{"href":"Template:CircularRef"}}'>Template:CircularRef</a></span></li></ol>
-!! end
-
 !! test
 Simple paragraph
 !! wikitext
@@ -549,8 +554,8 @@ parsoid=wt2html
 !! config
 wgFragmentMode=[ 'html5', 'legacy' ]
 !! wikitext
-== A <nowiki>B
-C</nowiki> ==
+==A <nowiki>B
+C</nowiki>==
 !! html/php
 <h2><span id="A_B.0AC"></span><span class="mw-headline" id="A_B
 C">A B
@@ -558,8 +563,8 @@ C</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</spa
 
 !! html/parsoid
 <h2 id="A_B
-C"><span id="A_B.0AC" typeof="mw:FallbackId"></span> A <span typeof="mw:Nowiki">B
-C</span> </h2>
+C"><span id="A_B.0AC" typeof="mw:FallbackId"></span>A <span typeof="mw:Nowiki">B
+C</span></h2>
 !! end
 
 !! test
@@ -572,58 +577,51 @@ http://fr.wikipedia.org/wiki/🍺
 </p>
 !! end
 
-# Note that the html+tidy output removes the spaces after the <li>,
-# which is a bug (https://sourceforge.net/p/tidy/bugs/945/, etc).
-# This is an issue for all tests with lists.  We intentionally do
-# *not* add html+tidy clauses for these, as we don't want to
-# document/test the broken behavior.  (Parsoid matches the non-tidy
-# output in these cases.)
-
 !! test
 Simple list
 !! wikitext
-* Item 1
-* Item 2
+*Item 1
+*Item 2
 !! html
-<ul><li> Item 1</li>
-<li> Item 2</li></ul>
+<ul><li>Item 1</li>
+<li>Item 2</li></ul>
 
 !! end
 
 !! test
 Italics and bold
 !! wikitext
-* plain
-* plain''italic''plain
-* plain''italic''plain''italic''plain
-* plain'''bold'''plain
-* plain'''bold'''plain'''bold'''plain
-* plain''italic''plain'''bold'''plain
-* plain'''bold'''plain''italic''plain
-* plain''italic'''bold-italic'''italic''plain
-* plain'''bold''bold-italic''bold'''plain
-* plain'''''bold-italic'''italic''plain
-* plain'''''bold-italic''bold'''plain
-* plain''italic'''bold-italic'''''plain
-* plain'''bold''bold-italic'''''plain
-* plain l'''italic''plain
-* plain l''''bold''' plain
-!! html
-<ul><li> plain</li>
-<li> plain<i>italic</i>plain</li>
-<li> plain<i>italic</i>plain<i>italic</i>plain</li>
-<li> plain<b>bold</b>plain</li>
-<li> plain<b>bold</b>plain<b>bold</b>plain</li>
-<li> plain<i>italic</i>plain<b>bold</b>plain</li>
-<li> plain<b>bold</b>plain<i>italic</i>plain</li>
-<li> plain<i>italic<b>bold-italic</b>italic</i>plain</li>
-<li> plain<b>bold<i>bold-italic</i>bold</b>plain</li>
-<li> plain<i><b>bold-italic</b>italic</i>plain</li>
-<li> plain<b><i>bold-italic</i>bold</b>plain</li>
-<li> plain<i>italic<b>bold-italic</b></i>plain</li>
-<li> plain<b>bold<i>bold-italic</i></b>plain</li>
-<li> plain l'<i>italic</i>plain</li>
-<li> plain l'<b>bold</b> plain</li></ul>
+*plain
+*plain''italic''plain
+*plain''italic''plain''italic''plain
+*plain'''bold'''plain
+*plain'''bold'''plain'''bold'''plain
+*plain''italic''plain'''bold'''plain
+*plain'''bold'''plain''italic''plain
+*plain''italic'''bold-italic'''italic''plain
+*plain'''bold''bold-italic''bold'''plain
+*plain'''''bold-italic'''italic''plain
+*plain'''''bold-italic''bold'''plain
+*plain''italic'''bold-italic'''''plain
+*plain'''bold''bold-italic'''''plain
+*plain l'''italic''plain
+*plain l''''bold''' plain
+!! html
+<ul><li>plain</li>
+<li>plain<i>italic</i>plain</li>
+<li>plain<i>italic</i>plain<i>italic</i>plain</li>
+<li>plain<b>bold</b>plain</li>
+<li>plain<b>bold</b>plain<b>bold</b>plain</li>
+<li>plain<i>italic</i>plain<b>bold</b>plain</li>
+<li>plain<b>bold</b>plain<i>italic</i>plain</li>
+<li>plain<i>italic<b>bold-italic</b>italic</i>plain</li>
+<li>plain<b>bold<i>bold-italic</i>bold</b>plain</li>
+<li>plain<i><b>bold-italic</b>italic</i>plain</li>
+<li>plain<b><i>bold-italic</i>bold</b>plain</li>
+<li>plain<i>italic<b>bold-italic</b></i>plain</li>
+<li>plain<b>bold<i>bold-italic</i></b>plain</li>
+<li>plain l'<i>italic</i>plain</li>
+<li>plain l'<b>bold</b> plain</li></ul>
 
 !! end
 
@@ -1149,8 +1147,7 @@ The ''[[Main Page]]'''s talk page.
 !! end
 
 !! test
-Parsoid only: Quote balancing context should be restricted to td/th cells on the same wikitext line
-(Requires tidy for PHP parser output to be fixed up)
+Quote balancing context should be restricted to td/th cells on the same wikitext line
 !! options
 parsoid=wt2html,wt2wt
 !! wikitext
@@ -1158,20 +1155,15 @@ parsoid=wt2html,wt2wt
 !''a!!''b
 |''a||''b
 |}
-!! html/php+tidy
+!! html+tidy
 <table>
-<tr>
+<tbody><tr>
 <th><i>a</i></th>
-<th><i>b</i></th>
+<th><i>b</i>
+</th>
 <td><i>a</i></td>
-<td><i>b</i></td>
-</tr>
-</table>
-!! html/parsoid
-<table>
-<tbody><tr><th><i>a</i></th><th><i>b</i></th>
-<td><i>a</i></td><td><i>b</i></td></tr>
-</tbody></table>
+<td><i>b</i>
+</td></tr></tbody></table>
 !! end
 
 ###
@@ -1275,32 +1267,33 @@ Text-level semantic html elements in wikitext
 !! test
 Ruby markup (W3C-style)
 !! wikitext
-; Mono-ruby for individual base characters
-: <ruby>日<rt>に</rt>本<rt>ほん</rt>語<rt>ご</rt></ruby>
-; Group ruby
-: <ruby>今日<rt>きょう</rt></ruby>
-; Jukugo ruby
-: <ruby>法<rb>華</rb><rb>経</rb><rt>ほ</rt><rt>け</rt><rt>きょう</rt></ruby>
-; Inline ruby
-: <ruby>東<rb>京</rb><rp>(</rp><rt>とう</rt><rt>きょう</rt><rp>)</rp></ruby>
-; Double-sided ruby
-: <ruby><rb>旧</rb><rb>金</rb><rb>山</rb><rt>jiù</rt><rt>jīn</rt><rt>shān</rt><rtc>San Francisco</rtc></ruby>
+;Mono-ruby for individual base characters
+:<ruby>日<rt>に</rt>本<rt>ほん</rt>語<rt>ご</rt></ruby>
+;Group ruby
+:<ruby>今日<rt>きょう</rt></ruby>
+;Jukugo ruby
+:<ruby>法<rb>華</rb><rb>経</rb><rt>ほ</rt><rt>け</rt><rt>きょう</rt></ruby>
+;Inline ruby
+:<ruby>東<rb>京</rb><rp>(</rp><rt>とう</rt><rt>きょう</rt><rp>)</rp></ruby>
+;Double-sided ruby
+:<ruby><rb>旧</rb><rb>金</rb><rb>山</rb><rt>jiù</rt><rt>jīn</rt><rt>shān</rt><rtc>San Francisco</rtc></ruby>
+
 <ruby>
 <rb>♥</rb><rtc><rt>Heart</rt></rtc><rtc lang="fr"><rt>Cœur</rt></rtc>
 <rb>☘</rb><rtc><rt>Shamrock</rt></rtc><rtc lang="fr"><rt>Trèfle</rt></rtc>
 <rb>✶</rb><rtc><rt>Star</rt></rtc><rtc lang="fr"><rt>Étoile</rt></rtc>
 </ruby>
 !! html
-<dl><dt> Mono-ruby for individual base characters</dt>
-<dd> <ruby>日<rt>に</rt>本<rt>ほん</rt>語<rt>ご</rt></ruby></dd>
-<dt> Group ruby</dt>
-<dd> <ruby>今日<rt>きょう</rt></ruby></dd>
-<dt> Jukugo ruby</dt>
-<dd> <ruby>法<rb>華</rb><rb>経</rb><rt>ほ</rt><rt>け</rt><rt>きょう</rt></ruby></dd>
-<dt> Inline ruby</dt>
-<dd> <ruby>東<rb>京</rb><rp>(</rp><rt>とう</rt><rt>きょう</rt><rp>)</rp></ruby></dd>
-<dt> Double-sided ruby</dt>
-<dd> <ruby><rb>旧</rb><rb>金</rb><rb>山</rb><rt>jiù</rt><rt>jīn</rt><rt>shān</rt><rtc>San Francisco</rtc></ruby></dd></dl>
+<dl><dt>Mono-ruby for individual base characters</dt>
+<dd><ruby>日<rt>に</rt>本<rt>ほん</rt>語<rt>ご</rt></ruby></dd>
+<dt>Group ruby</dt>
+<dd><ruby>今日<rt>きょう</rt></ruby></dd>
+<dt>Jukugo ruby</dt>
+<dd><ruby>法<rb>華</rb><rb>経</rb><rt>ほ</rt><rt>け</rt><rt>きょう</rt></ruby></dd>
+<dt>Inline ruby</dt>
+<dd><ruby>東<rb>京</rb><rp>(</rp><rt>とう</rt><rt>きょう</rt><rp>)</rp></ruby></dd>
+<dt>Double-sided ruby</dt>
+<dd><ruby><rb>旧</rb><rb>金</rb><rb>山</rb><rt>jiù</rt><rt>jīn</rt><rt>shān</rt><rtc>San Francisco</rtc></ruby></dd></dl>
 <p><ruby>
 <rb>♥</rb><rtc><rt>Heart</rt></rtc><rtc lang="fr"><rt>Cœur</rt></rtc>
 <rb>☘</rb><rtc><rt>Shamrock</rt></rtc><rtc lang="fr"><rt>Trèfle</rt></rtc>
@@ -1334,11 +1327,8 @@ Non-word characters don't terminate tag names (T19663, T42670, T54022)
 </p>
 !! end
 
-# There is a tidy bug here: https://sourceforge.net/p/tidy/bugs/946/
-# If the non-word-character tag made it through the sanitizer, tidy
-# would munge it up.
 !! test
-Non-word characters don't terminate tag names + tidy
+Non-word characters don't terminate tag names
 !! wikitext
 <blockquote|>a</blockquote>
 
@@ -1352,12 +1342,13 @@ Non-word characters don't terminate tag names + tidy
 
 <sub-ID#1>
 !! html+tidy
-<p>&lt;blockquote|&gt;a</p>
-<p>&lt;b→&gt; doesn't terminate &lt;/b→&gt;</p>
-<p>&lt;bä&gt; doesn't terminate &lt;/bä&gt;</p>
-<p>&lt;boo&gt; doesn't terminate &lt;/boo&gt;</p>
-<p>&lt;s.foo&gt; doesn't terminate &lt;/s.foo&gt;</p>
-<p>&lt;sub-ID#1&gt;</p>
+<p>&lt;blockquote|&gt;a
+</p><p>&lt;b→&gt; doesn't terminate &lt;/b→&gt;
+</p><p>&lt;bä&gt; doesn't terminate &lt;/bä&gt;
+</p><p>&lt;boo&gt; doesn't terminate &lt;/boo&gt;
+</p><p>&lt;s.foo&gt; doesn't terminate &lt;/s.foo&gt;
+</p><p>&lt;sub-ID#1&gt;
+</p>
 !! end
 
 ###
@@ -1390,7 +1381,9 @@ parsoid=wt2html
 
 <s.foo>s</s>
 !! html/php+tidy
-<p>&lt;s.foo&gt;s</p>
+<p class="mw-empty-elt">
+</p><p>&lt;s.foo&gt;s
+</p>
 !! html/parsoid
 <p>&lt;s.foo&gt;s</p>
 !! end
@@ -1518,7 +1511,8 @@ Entities inside template parameters
 !! wikitext
 {{echo|&ndash;}}
 !! html/php+tidy
-<p>–</p>
+<p>&#8211;
+</p>
 !! html/parsoid
 <p><span typeof="mw:Transclusion mw:Entity" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&amp;ndash;"}},"i":0}}]}'>&ndash;</span></p>
 !! end
@@ -1851,6 +1845,39 @@ b
 </p>
 !! end
 
+!! test
+post-expand include size being exceeded
+!! options
+maxincludesize=20
+!! wikitext
+{{echo3|1234567890}}
+!! html
+<p><a href="/wiki/Template:Echo3" title="Template:Echo3">Template:Echo3</a><!-- WARNING: template omitted, post-expand include size too large -->
+</p>
+!! end
+
+!! test
+max template depth being reached
+!! options
+maxtemplatedepth=1
+!! wikitext
+{{echo with depth|too deep!}}
+!! html
+<p><span class="error">Template recursion depth limit exceeded (1)</span>
+</p>
+!! end
+
+!! test
+multiple templates that are redirects
+!! wikitext
+{{redirect to foo}}
+{{redirect to foo}}
+!! html
+<p>FOO
+FOO
+</p>
+!! end
+
 !! test
 Multiple comments should still parse as SOL-transparent
 !! options
@@ -1888,6 +1915,7 @@ IE conditional comments
 ###
 ### paragraph wrapping tests
 ###
+
 !! test
 No block tags
 !! wikitext
@@ -1911,21 +1939,23 @@ a <div>foo</div>
 <p>b
 </p>
 !! html+tidy
-<p>a</p>
-<div>foo</div>
-<p>b</p>
+<p>a </p><div>foo</div>
+<p>b
+</p>
 !! end
 
+# Remex wraps empty tag runs with p-tags.
+# Parsoid strips them out during p-wrapping.
 !! test
 No p-wrappable content
 !! wikitext
 <span><div>x</div></span>
 <span><s><div>x</div></s></span>
 <small><em></em></small><span><s><div>x</div></s></span>
-!! html+tidy
-<div><span>x</span></div>
-<div><span><s>x</s></span></div>
-<div><span><s>x</s></span></div>
+!! html/php+tidy
+<span><div>x</div></span>
+<span><s><div>x</div></s></span>
+<p><small><em></em></small></p><span><s><div>x</div></s></span>
 !! html/parsoid
 <span><div>x</div></span>
 <span><s><div>x</div></s></span>
@@ -1954,11 +1984,9 @@ a <blockquote>foo</blockquote>
 <p>b
 </p>
 !! html+tidy
-<p>a</p>
-<blockquote>
-<p>foo</p>
-</blockquote>
-<p>b</p>
+<p>a </p><blockquote><p>foo</p></blockquote>
+<p>b
+</p>
 !! end
 
 !! test
@@ -1972,10 +2000,8 @@ a <div>foo</div>
 b <div>foo</div>
 
 !! html+tidy
-<p>a</p>
-<div>foo</div>
-<p>b</p>
-<div>foo</div>
+<p>a </p><div>foo</div><p>
+b </p><div>foo</div>
 !! end
 
 !! test
@@ -1989,14 +2015,8 @@ a <blockquote>foo</blockquote>
 b <blockquote>foo</blockquote>
 
 !! html+tidy
-<p>a</p>
-<blockquote>
-<p>foo</p>
-</blockquote>
-<p>b</p>
-<blockquote>
-<p>foo</p>
-</blockquote>
+<p>a </p><blockquote><p>foo</p></blockquote><p>
+b </p><blockquote><p>foo</p></blockquote>
 !! end
 
 !! test
@@ -2016,19 +2036,21 @@ d e
 x <div>foo</div> z
 
 !! html+tidy
-<div>foo</div>
-<p>a</p>
-<p>b c d e</p>
-<p>x</p>
-<div>foo</div>
-<p>z</p>
+<div>foo</div><p> a
+</p><p>b
+c
+d e
+</p><p>
+x </p><div>foo</div><p> z
+</p>
 !! end
 
-# Tidy strips out the empty <div> tags. Parsoid doesn't.
-# So, we have a separate section for Parsoid. We don't want
-# to mimic this stripping behavior in Parsoid. It affects
-# editing experience and also requires us to maintain additional
-# info for RT-ing.
+# The difference between Parsoid & Remex here
+# is because of Parsoid's Tidy-emulation code
+# for p-wrapping. We'll start work to remove this
+# emulation code in Parsoid sooner than later.
+# Remex wraps empty tag runs with p-tags.
+# Parsoid strips them out in a separate pass.
 !! test
 Empty lines between lines with block tags
 !! wikitext
 <div>e</div>
 
 !! html+tidy
-<p><br /></p>
-<p>a</p>
-<p>b</p>
-<div>a</div>
-<p>b</p>
-<div>b</div>
-<p>d</p>
-<p><br /></p>
+<div></div>
+<p><br />
+</p>
+<div></div><p>a
+</p><p>b
+</p>
+<div>a</div><p>b
+</p><div>b</div><p>d
+</p><p><br />
+</p>
 <div>e</div>
 !! html/parsoid
 <div data-parsoid='{"stx":"html"}'></div>
@@ -2082,7 +2106,6 @@ b
 <div data-parsoid='{"stx":"html"}'>e</div>
 !! end
 
-## PHP parser emits output which is broken
 !! test
 Unclosed HTML p-tags should be handled properly
 !! wikitext
 
 b
 !! html/php+tidy
-<div>
-<p>foo</p>
-</div>
-<p>a</p>
-<p>b</p>
+<div><p>foo</p></div>
+<p>a
+</p><p>b
+</p>
 !! html/parsoid
 <div data-parsoid='{"stx":"html"}'><p data-parsoid='{"stx":"html", "autoInsertedEnd":true}'>foo</p></div>
 <p>a</p>
@@ -2128,9 +2150,55 @@ parsoid=wt2html
 <link rel="mw:PageProp/Category" href="./Category:A1"/><p>a</p>
 !! end
 
+!! test
+No paragraph necessary for SOL transparent template
+!! wikitext
+<span><div>foo</div></span>
+[[Category:Foo]]
+
+<span><div>foo</div></span>
+{{echo|[[Category:Foo]]}}
+!! html/php
+<span><div>foo</div></span>
+<span><div>foo</div></span>
+
+!! html/parsoid
+<span data-parsoid='{"stx":"html"}'><div data-parsoid='{"stx":"html"}'>foo</div></span>
+<link rel="mw:PageProp/Category" href="./Category:Foo"/>
+
+<span data-parsoid='{"stx":"html"}'><div data-parsoid='{"stx":"html"}'>foo</div></span>
+<link rel="mw:PageProp/Category" href="./Category:Foo" about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[[Category:Foo]]"}},"i":0}}]}'/>
+!! end
+
+!! test
+Avoid expanding multiline sol transparent template ranges unnecessarily
+!! wikitext
+hi
+
+
+{{echo|<br/>
+}}
+
+[[Category:Ho]]
+!! html/php
+<p>hi
+</p><p><br />
+<br />
+</p>
+!! html/parsoid
+<p>hi</p>
+
+<p><br />
+<br about="#mwt1" typeof="mw:Transclusion" data-parsoid='{}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;br/>\n"}},"i":0}}]}'/><span about="#mwt1">
+</span></p>
+
+<link rel="mw:PageProp/Category" href="./Category:Ho" />
+!! end
+
 ###
 ### Preformatted text
 ###
+
 !! test
 Preformatted text
 !! wikitext
@@ -2276,9 +2344,6 @@ Foo <del>bar</del> <ins>baz</ins> quux
 
 !! end
 
-# Note that the p-wrapping is newline sensitive, which could be
-# considered a bug: tidy will wrap only the 'Foo' in the example
-# below in a <p> tag. (see comment 23-25 of T8200)
 !! test
 T17491: <ins>/<del> in blockquote (2)
 !! wikitext
@@ -2289,9 +2354,8 @@ T17491: <ins>/<del> in blockquote (2)
 </blockquote>
 
 !! html+tidy
-<blockquote>
-<p>Foo</p>
-<del>bar</del> <ins>baz</ins> quux</blockquote>
+<blockquote><p>Foo </p><del>bar</del> <ins>baz</ins><p> quux
+</p></blockquote>
 !! end
 
 !! test
@@ -2426,7 +2490,6 @@ parsoid=wt2html
 </p>
 !! end
 
-# Parsoid doesn't strip empty tags, like Tidy does.
 !! test
 Empty pre; pre inside other HTML tags (T56946)
 !! wikitext
 foo
 </pre></div>
 <pre></pre>
-!! html/php
+!! html/php+tidy
 <p>a
 </p>
-<div><pre>
-foo
+<div><pre>foo
 </pre></div>
 <pre></pre>
-
-!! html/php+tidy
-<p>a</p>
-<div>
-<pre>
-foo
-</pre></div>
 !! html/parsoid
 <p>a</p>
 
@@ -2469,16 +2524,12 @@ HTML pre followed by indent-pre
 </pre>
 !! end
 
-# Note that tidy removes the empty <p> tags from the start and end.
-# Parsoid does not, by design.
 !! test
 Block tag pre
 !! wikitext
 <p><pre>foo</pre></p>
 !! html/php+tidy
-<pre>
-foo
-</pre>
+<p class="mw-empty-elt"></p><pre>foo</pre><p class="mw-empty-elt"></p>
 !! html/parsoid
 <p class='mw-empty-elt' data-parsoid='{"stx":"html","autoInsertedEnd":true}'></p><pre typeof="mw:Extension/pre" about="#mwt2" data-parsoid='{"stx":"html"}' data-mw='{"name":"pre","attrs":{},"body":{"extsrc":"foo"}}'>foo</pre><p class='mw-empty-elt' data-parsoid='{"autoInsertedStart":true,"stx":"html"}'></p>
 !! end
@@ -2641,10 +2692,8 @@ parsoid=wt2html
 <table>&lt;pre </table>
 
 !! html/php+tidy
-<pre>
-x
-</pre>
-<p>&lt;pre</p>
+<pre>x</pre>
+&lt;pre <table></table>
 !! html/parsoid
 <pre about="#mwt1" typeof="mw:Transclusion mw:Extension/pre" data-parsoid='{"a":{"&lt;pre":null},"sa":{"&lt;pre":""},"stx":"html","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>
 
@@ -2694,6 +2743,17 @@ parsoid=wt2html
 </tbody></table>
 !! end
 
+!! test
+Self-closed pre
+!! wikitext
+<pre />
+!! html/php
+<pre></pre>
+
+!! html/parsoid
+<pre typeof="mw:Extension/pre" about="#mwt2" data-mw='{"name":"pre","attrs":{},"body":null}'></pre>
+!! end
+
 !! test
 Parsoid: Don't paragraph-wrap fosterable content even if table syntax is unbalanced
 !! options
@@ -2745,7 +2805,7 @@ Templates: Strip leading and trailing whitespace from named-param values
 </p><p>b
 </p><p>c
 </p>
-<ul><li> d</li></ul>
+<ul><li>d</li></ul>
 
 !! end
 
@@ -2766,7 +2826,7 @@ Templates: Don't strip whitespace from positional-param values
  e}}
 
 {{echo|
-* f}}
+*f}}
 
 {{echo|
  }}g
@@ -2786,7 +2846,7 @@ Templates: Don't strip whitespace from positional-param values
 </pre>
 <p><br />
 </p>
-<ul><li> f</li></ul>
+<ul><li>f</li></ul>
 <p><br />
 </p>
 <pre>g
@@ -2938,7 +2998,8 @@ Templates: Parsoid parameter escaping test 1
 !! wikitext
 {{echo|[foo]|{{echo|[bar]}}}}
 !! html/php+tidy
-<p>[foo]</p>
+<p>[foo]
+</p>
 !! html/parsoid
 <p about="#mwt1" typeof="mw:Transclusion"
 data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[foo]"},"2":{"wt":"{{echo|[bar]}}"}},"i":0}}]}'>[foo]</p>
@@ -2949,9 +3010,10 @@ Parsoid: Pipes in external links in template parameter
 !! wikitext
 {{echo|[{{echo|http://example.com}} link]}}
 !! html/php+tidy
-<p><a rel="nofollow" class="external text" href="http://example.com">link</a></p>
+<p><a rel="nofollow" class="external text" href="http://example.com">link</a>
+</p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com" about="#mwt31" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[{{echo|http://example.com}} link]"}},"i":0}}]}'>link</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://example.com" about="#mwt31" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[{{echo|http://example.com}} link]"}},"i":0}}]}'>link</a></p>
 !! end
 
 !! test
@@ -2959,11 +3021,10 @@ Parsoid: pipe in transclusion parameter
 !! wikitext
 {{echo|http://foo.com/a&#124;b}}
 !! html/php+tidy
-<p><a rel="nofollow" class="external free" href="http://foo.com/a%7Cb">http://foo.com/a%7Cb</a></p>
+<p><a rel="nofollow" class="external free" href="http://foo.com/a%7Cb">http://foo.com/a%7Cb</a>
+</p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://foo.com/a%7Cb" about="#mwt1"
-typeof="mw:Transclusion"
-data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"http://foo.com/a&amp;#124;b"}},"i":0}}]}'>http://foo.com/a%7Cb</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://foo.com/a%7Cb" about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"http://foo.com/a&amp;#124;b"}},"i":0}}]}'>http://foo.com/a%7Cb</a></p>
 !! end
 
 !! test
@@ -2973,7 +3034,8 @@ parsoid=html2wt,wt2wt
 !! wikitext
 {{echo|[http://foo.com/a&#124;b a&#124;b]}}
 !! html/php+tidy
-<p><a rel="nofollow" class="external text" href="http://foo.com/a%7Cb">a|b</a></p>
+<p><a rel="nofollow" class="external text" href="http://foo.com/a%7Cb">a&#124;b</a>
+</p>
 !! html/parsoid
 <p><a rel="mw:ExtLink" href="http://foo.com/a|b" about="#mwt1"
 typeof="mw:Transclusion"
@@ -3000,7 +3062,10 @@ parsoid=html2wt,wt2wt
 {{echo|<nowiki>&lt;div&gt;</nowiki>}}
 {{echo|<nowiki></nowiki>}}
 !! html/php+tidy
-<p>foo|bar &lt;div&gt;</p>
+<p>foo|bar
+&lt;div&gt;
+
+</p>
 !! html/parsoid
 <p><span about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo<nowiki>|</nowiki>bar"}},"i":0}}]}'}'>foo</span><span typeof="mw:Nowiki" about="#mwt1">|</span><span about="#mwt1">bar</span>
 <span typeof="mw:Transclusion mw:Nowiki" about="#mwt2" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"<nowiki>&amp;lt;div&amp;gt;</nowiki>"}},"i":0}}]}'><span typeof="mw:Entity">&lt;</span>div<span typeof="mw:Entity">&gt;</span></span>
@@ -3016,7 +3081,8 @@ parsoid=html2wt,wt2wt
 !! wikitext
 {{echo|{{echo|1=bar}}}}
 !! html/php+tidy
-<p>bar</p>
+<p>bar
+</p>
 !! html/parsoid
 <p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"{{echo|1=bar}}"}},"i":0}}]}'>bar</p>
 !! end
@@ -3027,7 +3093,8 @@ Templates parameters with special tokenizing behavior dont get modified because
 !! wikitext
 {{echo|a : b}}
 !! html/php+tidy
-<p>a&#160;: b</p>
+<p>a&#160;: b
+</p>
 !! html/parsoid
 <p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"a : b"}},"i":0}}]}'>a<span typeof="mw:DisplaySpace mw:Placeholder" data-parsoid='{"isDisplayHack":true}'> </span>: b</p>
 !! end
@@ -3038,7 +3105,8 @@ Templates: Preserve blank parameter names
 !! wikitext
 {{echo|=foo}}
 !! html/php+tidy
-<p>{{{1}}}</p>
+<p>{{{1}}}
+</p>
 !! html/parsoid
 <p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"":{"wt":"foo"}},"i":0}}]}'>{{{1}}}</p>
 !! end
@@ -3048,7 +3116,9 @@ Templates: Preserve blank parameter names in other positions
 !! wikitext
 {{blank_param|bar|=foo}}
 !! html/php+tidy
-<p>bar foo</p>
+<p>bar
+foo
+</p>
 !! html/parsoid
 <p about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"pi":[[{"k":"1"},{"k":"","named":true}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"blank_param","href":"./Template:Blank_param"},"params":{"1":{"wt":"bar"},"":{"wt":"foo"}},"i":0}}]}'>bar
 foo</p>
 2c. Indent-Pre and tables (T44252)
 !! wikitext
 {|
- |+ foo
- !  | bar
+ |+foo
+ !  |bar
 |}
 !! html
 <table>
-<caption> foo
+<caption>foo
 </caption>
 <tr>
-<th> bar
+<th>bar
 </th></tr></table>
 
 !!end
 !! wikitext
  a
  {|
- | b
+ |b
  |}
 !! html/php
 <pre>a
 </pre>
 <table>
 <tr>
-<td> b
+<td>b
 </td></tr></table>
 
 !! html/parsoid
  <p>c </p><blockquote data-parsoid='{"stx":"html"}'> foo </blockquote>
 <pre><span> foo </span>
 </pre>
-!! html+tidy
-<p>a</p>
-<p>foo</p>
-<p>b</p>
-<div>foo</div>
-<p>c</p>
-<blockquote>
-<p>foo</p>
-</blockquote>
-<pre>
-<span> foo </span>
+!! html/php+tidy
+<p> a </p><p> foo </p><p>
+ b </p><div> foo </div><p>
+ c </p><blockquote><p> foo </p></blockquote>
+<pre><span> foo </span>
 </pre>
 !! end
 
 !! html/parsoid
 <pre>a <span data-parsoid='{"stx":"html"}'>foo</span></pre>
  b <div data-parsoid='{"stx":"html"}'> foo </div>
-!! html+tidy
-<pre>
-a <span>foo</span>
-</pre>
-<p>b</p>
-<div>foo</div>
+!! html/php+tidy
+<pre>a <span>foo</span>
+</pre><p>
+ b </p><div> foo </div>
 !!end
 
 !!test
@@ -3527,7 +3589,8 @@ a <!--
 foo
 --> b
 !! html/php+tidy
-<p>a b</p>
+<p>a  b
+</p>
 !! html/parsoid
 <p>a <!--
 foo
@@ -3558,11 +3621,8 @@ foo
 foo
 </pre>
 !! html/php+tidy
-<pre>
-foo
-</pre>
-<pre>
-foo
+<pre>foo</pre>
+<pre>foo
 </pre>
 <pre>
 
@@ -3693,19 +3753,19 @@ HTML-pre: 3: other wikitext
 !! test
 Simple definition
 !! wikitext
-; name : Definition
+;name :Definition
 !! html
-<dl><dt> name&#160;</dt>
-<dd> Definition</dd></dl>
+<dl><dt>name&#160;</dt>
+<dd>Definition</dd></dl>
 
 !! end
 
 !! test
 Definition list for indentation only
 !! wikitext
-: Indented text
+:Indented text
 !! html
-<dl><dd> Indented text</dd></dl>
+<dl><dd>Indented text</dd></dl>
 
 !! end
 
@@ -3722,10 +3782,10 @@ Definition list with no space
 !! test
 Definition list with URL link
 !! wikitext
-; http://example.com/ : definition
+;http://example.com/ :definition
 !! html
-<dl><dt> <a rel="nofollow" class="external free" href="http://example.com/">http://example.com/</a>&#160;</dt>
-<dd> definition</dd></dl>
+<dl><dt><a rel="nofollow" class="external free" href="http://example.com/">http://example.com/</a>&#160;</dt>
+<dd>definition</dd></dl>
 
 !! end
 
@@ -3742,10 +3802,10 @@ Definition list with bracketed URL link
 !! test
 Definition list with wikilink containing colon
 !! wikitext
-; [[Help:FAQ]]: The least-read page on Wikipedia
+; [[Help:FAQ]]:The least-read page on Wikipedia
 !! html
-<dl><dt> <a href="/index.php?title=Help:FAQ&amp;action=edit&amp;redlink=1" class="new" title="Help:FAQ (page does not exist)">Help:FAQ</a></dt>
-<dd> The least-read page on Wikipedia</dd></dl>
+<dl><dt><a href="/index.php?title=Help:FAQ&amp;action=edit&amp;redlink=1" class="new" title="Help:FAQ (page does not exist)">Help:FAQ</a></dt>
+<dd>The least-read page on Wikipedia</dd></dl>
 
 !! end
 
@@ -3753,13 +3813,13 @@ Definition list with wikilink containing colon
 !! test
 Definition list with news link containing colon
 !! wikitext
-;  news:alt.wikipedia.rox: This isn't even a real newsgroup!
+;news:alt.wikipedia.rox: This isn't even a real newsgroup!
 !! html/php
-<dl><dt>  <a rel="nofollow" class="external free" href="news:alt.wikipedia.rox">news:alt.wikipedia.rox</a></dt>
-<dd> This isn't even a real newsgroup!</dd></dl>
+<dl><dt><a rel="nofollow" class="external free" href="news:alt.wikipedia.rox">news:alt.wikipedia.rox</a></dt>
+<dd>This isn't even a real newsgroup!</dd></dl>
 
 !! html/parsoid
-<dl><dt>  <a rel="mw:ExtLink" href="news:alt.wikipedia.rox" data-parsoid='{"stx":"url"}'>news:alt.wikipedia.rox</a></dt><dd data-parsoid='{"stx":"row"}'> This isn't even a real newsgroup!</dd></dl>
+<dl><dt>  <a rel="mw:ExtLink" class="external free" href="news:alt.wikipedia.rox" data-parsoid='{"stx":"url"}'>news:alt.wikipedia.rox</a></dt><dd data-parsoid='{"stx":"row"}'>This isn't even a real newsgroup!</dd></dl>
 !! end
 
 !! test
@@ -3767,17 +3827,17 @@ Malformed definition list with colon
 !! wikitext
 ;  news:alt.wikipedia.rox -- don't crash or enter an infinite loop
 !! html
-<dl><dt>  <a rel="nofollow" class="external free" href="news:alt.wikipedia.rox">news:alt.wikipedia.rox</a> -- don't crash or enter an infinite loop</dt></dl>
+<dl><dt><a rel="nofollow" class="external free" href="news:alt.wikipedia.rox">news:alt.wikipedia.rox</a> -- don't crash or enter an infinite loop</dt></dl>
 
 !! end
 
 !! test
 Definition lists: colon in external link text
 !! wikitext
-; [http://www.wikipedia2.org/ Wikipedia : The Next Generation]: OK, I made that up
+;[http://www.wikipedia2.org/ Wikipedia :The Next Generation] :OK, I made that up
 !! html
-<dl><dt> <a rel="nofollow" class="external text" href="http://www.wikipedia2.org/">Wikipedia&#160;: The Next Generation</a></dt>
-<dd> OK, I made that up</dd></dl>
+<dl><dt><a rel="nofollow" class="external text" href="http://www.wikipedia2.org/">Wikipedia&#160;:The Next Generation</a>&#160;</dt>
+<dd>OK, I made that up</dd></dl>
 
 !! end
 
@@ -3793,32 +3853,30 @@ Definition lists: colon in HTML attribute
 !! test
 Definition lists: self-closed tag
 !! wikitext
-;one<br/>two : two-line fun
+;one<br/>two :two-line fun
 !! html
 <dl><dt>one<br />two&#160;</dt>
-<dd> two-line fun</dd></dl>
+<dd>two-line fun</dd></dl>
 
 !! end
 
 !! test
 Definition lists: ignore colons inside tags
 !! wikitext
-;one <b>two : tag <i>fun:</i>:</b>: def
+;one <b>two : tag <i>fun:</i>:</b>:def
 !! html
 <dl><dt>one <b>two&#160;: tag <i>fun:</i>:</b></dt>
-<dd> def</dd></dl>
+<dd>def</dd></dl>
 
 !! end
 
 !! test
 Definition lists: excess closed tags
 !! wikitext
-;one</b>two : bad tag fun
+;one</b>two :bad tag fun
 !! html/php+tidy
-<dl>
-<dt>onetwo&#160;</dt>
-<dd>bad tag fun</dd>
-</dl>
+<dl><dt>onetwo&#160;</dt>
+<dd>bad tag fun</dd></dl>
 !! html/parsoid
 <dl>
 <dt>onetwo</dt>
@@ -3849,14 +3907,14 @@ T13748: Literal closing tags
 Definition and unordered list using wiki syntax nested in unordered list using html tags.
 !! wikitext
 <ul><li>
-; term : description
-* unordered
+;term :description
+*unordered
 </li></ul>
 !! html
 <ul><li>
-<dl><dt> term&#160;</dt>
-<dd> description</dd></dl>
-<ul><li> unordered</li></ul>
+<dl><dt>term&#160;</dt>
+<dd>description</dd></dl>
+<ul><li>unordered</li></ul>
 </li></ul>
 
 !! end
@@ -3864,10 +3922,11 @@ Definition and unordered list using wiki syntax nested in unordered list using h
 !! test
 Definition list with empty definition and following paragraph
 !! wikitext
-; term:
+;term:
+
 Paragraph text
 !! html
-<dl><dt> term</dt>
+<dl><dt>term</dt>
 <dd></dd></dl>
 <p>Paragraph text
 </p>
@@ -3954,18 +4013,41 @@ should be left alone
 !! end
 
 !! test
-Definition Lists: Hacky use to indent tables, with comments (T65979)
+Definition Lists: Hacky use to indent tables (with content following table)
 !! wikitext
-<!-- foo -->
-::{|
+:{|
 |foo
 |bar
-|}<!-- bar -->
-this text
-should be left alone
+|} <!--c1--> this text should be part of the dl
+!! html/php+tidy
+<dl><dd><table>
+<tbody><tr>
+<td>foo
+</td>
+<td>bar
+</td></tr></tbody></table>  this text should be part of the dl</dd></dl>
 !! html/parsoid
-<!-- foo -->
-<dl><dd><dl><dd><table><tr>
+<dl><dd><table>
+<tbody><tr>
+<td>foo
+</td>
+<td>bar
+</td></tr></tbody></table> <!--c1--> this text should be part of the dl</dd></dl>
+!! end
+
+!! test
+Definition Lists: Hacky use to indent tables, with comments (T65979)
+!! wikitext
+<!-- foo -->
+::{|
+|foo
+|bar
+|}<!-- bar -->
+this text
+should be left alone
+!! html/parsoid
+<!-- foo -->
+<dl><dd><dl><dd><table><tr>
 <td>foo</td>
 <td>bar</td>
 </tr></table><!-- bar --></dd></dl></dd></dl>
@@ -4045,22 +4127,24 @@ Table / list interaction: indented table with lists in table contents
 !! wikitext
 :{|
 |-
-| a
-* b
+|a
+
+*b
 |-
-| c
-* d
+|c
+
+*d
 |}
 !! html
 <dl><dd><table>
 
 <tr>
-<td> a
-<ul><li> b</li></ul>
+<td>a
+<ul><li>b</li></ul>
 </td></tr>
 <tr>
-<td> c
-<ul><li> d</li></ul>
+<td>c
+<ul><li>d</li></ul>
 </td></tr></table></dd></dl>
 
 !! end
@@ -4097,13 +4181,11 @@ Table / list interaction: lists nested in tables nested in indented lists
 
 !! test
 Definition Lists: Nesting: Multi-level (Parsoid only)
-!! options
-parsoid
 !! wikitext
 ;t1 :d1
 ;;t2 ::d2
 ;;;t3 :::d3
-!! html
+!! html/parsoid
 <dl>
   <dt>t1 </dt>
   <dd>d1</dd>
@@ -4126,72 +4208,26 @@ parsoid
 
 
 !! test
-Definition Lists: Nesting: Test 2 (Parsoid only)
+Definition Lists: Nesting: Test 2
 !! wikitext
 ;t1
 ::d2
-!! html/php+tidy
-<dl>
-<dt>t1</dt>
+!! html+tidy
+<dl><dt>t1</dt>
 <dd>
-<dl>
-<dd>d2</dd>
-</dl>
-</dd>
-</dl>
-!! html/parsoid
-<dl>
-  <dt>t1</dt>
-  <dd>
-    <dl>
-      <dd>d2</dd>
-    </dl>
-  </dd>
-</dl>
-
+<dl><dd>d2</dd></dl></dd></dl>
 !! end
 
 
 !! test
-Definition Lists: Nesting: Test 3 (Parsoid only)
+Definition Lists: Nesting: Test 3
 !! wikitext
 :;t1
 ::::d2
-!! html/php+tidy
-<dl>
-<dd>
-<dl>
-<dt>t1</dt>
-<dd>
-<dl>
+!! html+tidy
+<dl><dd><dl><dt>t1</dt>
 <dd>
-<dl>
-<dd>d2</dd>
-</dl>
-</dd>
-</dl>
-</dd>
-</dl>
-</dd>
-</dl>
-!! html/parsoid
-<dl>
-  <dd>
-    <dl>
-      <dt>t1</dt>
-      <dd>
-        <dl>
-          <dd>
-            <dl>
-              <dd>d2</dd>
-            </dl>
-          </dd>
-        </dl>
-      </dd>
-    </dl>
-  </dd>
-</dl>
-
+<dl><dd><dl><dd>d2</dd></dl></dd></dl></dd></dl></dd></dl>
 !! end
 
 
@@ -4216,42 +4252,30 @@ Definition Lists: Nesting: Test 4
 !! test
 Definition Lists: Mixed Lists: Test 1
 !! wikitext
-:;* foo
-::* bar
-:; baz
+:;*foo
+::*bar
+:;baz
 !! html/php
-<dl><dd><dl><dt><ul><li> foo</li>
-<li> bar</li></ul></dt></dl>
-<dl><dt> baz</dt></dl></dd></dl>
+<dl><dd><dl><dt><ul><li>foo</li>
+<li>bar</li></ul></dt></dl>
+<dl><dt>baz</dt></dl></dd></dl>
 
 !! html/php+tidy
-<dl>
-<dd>
-<dl>
-<dd>
-<ul>
-<li>foo</li>
-<li>bar</li>
-</ul>
-</dd>
-</dl>
-<dl>
-<dt>baz</dt>
-</dl>
-</dd>
-</dl>
+<dl><dd><dl><dt><ul><li>foo</li>
+<li>bar</li></ul></dt></dl>
+<dl><dt>baz</dt></dl></dd></dl>
 !! html/parsoid
 <dl>
 <dd><dl>
 <dt><ul>
-<li> foo
+<li>foo
 </li>
 </ul></dt>
 <dd><ul>
-<li> bar
+<li>bar
 </li>
 </ul></dd>
-<dt> baz</dt>
+<dt>baz</dt>
 </dl></dd>
 </dl>
 !! end
@@ -4259,11 +4283,11 @@ Definition Lists: Mixed Lists: Test 1
 !! test
 Definition Lists: Mixed Lists: Test 2
 !! wikitext
-*: d1
-*: d2
+*:d1
+*:d2
 !! html
-<ul><li><dl><dd> d1</dd>
-<dd> d2</dd></dl></li></ul>
+<ul><li><dl><dd>d1</dd>
+<dd>d2</dd></dl></li></ul>
 
 !! end
 
@@ -4271,11 +4295,11 @@ Definition Lists: Mixed Lists: Test 2
 !! test
 Definition Lists: Mixed Lists: Test 3
 !! wikitext
-*::: d1
-*::: d2
+*:::d1
+*:::d2
 !! html
-<ul><li><dl><dd><dl><dd><dl><dd> d1</dd>
-<dd> d2</dd></dl></dd></dl></dd></dl></li></ul>
+<ul><li><dl><dd><dl><dd><dl><dd>d1</dd>
+<dd>d2</dd></dl></dd></dl></dd></dl></li></ul>
 
 !! end
 
@@ -4298,10 +4322,10 @@ Definition Lists: Mixed Lists: Test 4
 Definition Lists: Mixed Lists: Test 5
 !! wikitext
 *:d1
-*:: d2
+*::d2
 !! html
 <ul><li><dl><dd>d1
-<dl><dd> d2</dd></dl></dd></dl></li></ul>
+<dl><dd>d2</dd></dl></dd></dl></li></ul>
 
 !! end
 
@@ -4310,10 +4334,10 @@ Definition Lists: Mixed Lists: Test 5
 Definition Lists: Mixed Lists: Test 6
 !! wikitext
 #*:d1
-#*::: d3
+#*:::d3
 !! html
 <ol><li><ul><li><dl><dd>d1
-<dl><dd><dl><dd> d3</dd></dl></dd></dl></dd></dl></li></ul></li></ol>
+<dl><dd><dl><dd>d3</dd></dl></dd></dl></dd></dl></li></ul></li></ol>
 
 !! end
 
@@ -4321,11 +4345,11 @@ Definition Lists: Mixed Lists: Test 6
 !! test
 Definition Lists: Mixed Lists: Test 7
 !! wikitext
-:* d1
-:* d2
+:*d1
+:*d2
 !! html
-<dl><dd><ul><li> d1</li>
-<li> d2</li></ul></dd></dl>
+<dl><dd><ul><li>d1</li>
+<li>d2</li></ul></dd></dl>
 
 !! end
 
@@ -4333,11 +4357,11 @@ Definition Lists: Mixed Lists: Test 7
 !! test
 Definition Lists: Mixed Lists: Test 8
 !! wikitext
-:* d1
-::* d2
+:*d1
+::*d2
 !! html
-<dl><dd><ul><li> d1</li></ul>
-<dl><dd><ul><li> d2</li></ul></dd></dl></dd></dl>
+<dl><dd><ul><li>d1</li></ul>
+<dl><dd><ul><li>d2</li></ul></dd></dl></dd></dl>
 
 !! end
 
@@ -4363,27 +4387,28 @@ Definition Lists: Mixed Lists: Test 10
 
 !! end
 
+# The Parsoid team disagrees with the PHP parser's seemingly-random
+# rules regarding dd/dt on the next few tests.  Parsoid is more
+# consistent, and recognizes the shared nesting and keeps the
+# still-open tags around until the nesting is complete.
+
 # This is a regression test for T175099
-# html/php+tidy is insufficient since Tidy covers up the bug.
-# But once Tidy is replaced with RemexHTML, html/php+tidy is good enough
 !! test
 Definition Lists: Mixed Lists: Test 11
 !! wikitext
-; a
-:* b
-!! html/*
-<dl><dt> a</dt>
+;a
+:*b
+!! html/php
+<dl><dt>a</dt>
 <dd>
-<ul><li> b</li></ul></dd></dl>
+<ul><li>b</li></ul></dd></dl>
 
+!! html/parsoid
+<dl><dt>a
+<dd><ul><li>b</li></ul></dd></dl>
 !! end
 
-# The Parsoid team disagrees with the PHP parser's seemingly-random
-# rules regarding dd/dt on the next two tests.  Parsoid is more
-# consistent, and recognizes the shared nesting and keeps the
-# still-open tags around until the nesting is complete.
-# (And tidy again converts <dt> to <dd> before 'bar'.)
-
+# FIXME: Maybe get rid of this test?
 !! test
 Definition Lists: Mixed Lists: Test 12
 !! wikitext
@@ -4396,42 +4421,10 @@ Definition Lists: Mixed Lists: Test 12
 <dd>baz</dd></dl></li></ol></li></ul></li></ol></li></ul>
 
 !! html/php+tidy
-<ul>
-<li>
-<ol>
-<li>
-<ul>
-<li>
-<ol>
-<li>
-<dl>
-<dt>foo&#160;</dt>
-<dd>
-<ul>
-<li>
-<dl>
-<dd>
-<dl>
-<dt>bar</dt>
-</dl>
-</dd>
-</dl>
-</li>
-</ul>
-</dd>
-</dl>
-<dl>
-<dt>boo&#160;</dt>
-<dd>baz</dd>
-</dl>
-</li>
-</ol>
-</li>
-</ul>
-</li>
-</ol>
-</li>
-</ul>
+<ul><li><ol><li><ul><li><ol><li><dl><dt>foo&#160;</dt>
+<dd><ul><li><dl><dt><dl><dt>bar</dt></dl></dt></dl></li></ul></dd></dl></li></ol></li></ul>
+<dl><dt>boo&#160;</dt>
+<dd>baz</dd></dl></li></ol></li></ul>
 !! html/parsoid
 <ul>
 <li>
@@ -4462,52 +4455,17 @@ Definition Lists: Mixed Lists: Test 12
 </ul>
 !! end
 
-
-# Another case where tidy converts a <dt> to a <dd> (but Parsoid doesn't).
+# FIXME: Maybe get rid of this test?
 # From whitelist:
 # * The test is wrong, there are two colons where there should be :;
 # * The PHP parser is wrong to close the <dl> after the <dt> containing the <ul>.
 !! test
 Definition Lists: Weird Ones: Test 1
 !! wikitext
-*#;*::;; foo : bar (who uses this?)
-!! html/php
-<ul><li><ol><li><dl><dt> foo&#160;</dt>
-<dd><ul><li><dl><dd><dl><dd><dl><dt><dl><dt> bar (who uses this?)</dt></dl></dd></dl></dd></dl></dd></dl></li></ul></dd></dl></li></ol></li></ul>
-
+*#;*::;;foo :bar (who uses this?)
 !! html/php+tidy
-<ul>
-<li>
-<ol>
-<li>
-<dl>
-<dt>foo&#160;</dt>
-<dd>
-<ul>
-<li>
-<dl>
-<dd>
-<dl>
-<dd>
-<dl>
-<dd>
-<dl>
-<dt>bar (who uses this?)</dt>
-</dl>
-</dd>
-</dl>
-</dd>
-</dl>
-</dd>
-</dl>
-</li>
-</ul>
-</dd>
-</dl>
-</li>
-</ol>
-</li>
-</ul>
+<ul><li><ol><li><dl><dt>foo&#160;</dt>
+<dd><ul><li><dl><dd><dl><dd><dl><dt><dl><dt>bar (who uses this?)</dt></dl></dt></dl></dd></dl></dd></dl></li></ul></dd></dl></li></ol></li></ul>
 !! html/parsoid
 <ul>
 <li>
@@ -4524,8 +4482,8 @@ Definition Lists: Weird Ones: Test 1
 <dl>
 <dt>
 <dl>
-<dt> foo<span typeof="mw:DisplaySpace mw:Placeholder" data-parsoid='{"src":" ","isDisplayHack":true}'> </span></dt>
-<dd data-parsoid='{"stx":"row"}'> bar (who uses this?)</dd>
+<dt>foo<span typeof="mw:DisplaySpace mw:Placeholder" data-parsoid='{"src":" ","isDisplayHack":true}'> </span></dt>
+<dd data-parsoid='{"stx":"row"}'>bar (who uses this?)</dd>
 </dl></dt>
 </dl></dd>
 </dl></dd>
@@ -4550,37 +4508,18 @@ Definition Lists: colons occurring in tags
 ;{{echo|''a:b''}}
 ;;;''a:b''
 !! html+tidy
-<dl>
-<dt>a</dt>
+<dl><dt>a</dt>
 <dd>b</dd>
 <dt><b>a:b</b></dt>
 <dt><i>a:b</i></dt>
 <dt><span>a:b</span></dt>
-<dd>
-<div>a:b</div>
-</dd>
-<dd>
-<div>a
-<dl>
+<dt><div>a:b</div></dt>
+<dt><div>a</div></dt>
 <dd>b</dd>
-</dl>
-</div>
-</dd>
 <dt>a</dt>
 <dd>b</dd>
-<dt><i>a:b</i></dt>
-</dl>
-<dl>
-<dd>
-<dl>
-<dd>
-<dl>
-<dt><i>a:b</i></dt>
-</dl>
-</dd>
-</dl>
-</dd>
-</dl>
+<dt><i>a:b</i></dt></dl>
+<dl><dt><dl><dt><dl><dt><i>a:b</i></dt></dl></dt></dl></dt></dl>
 !! html/parsoid
 <dl><dt>a</dt><dd data-parsoid='{"stx":"row"}'>b</dd>
 <dt><b>a:b</b></dt>
@@ -4594,52 +4533,40 @@ Definition Lists: colons occurring in tags
 <dl><dt><dl><dt><i>a:b</i></dt></dl></dt></dl></dt></dl>
 !! end
 
+# Parsoid's output differs here again because it shares
+# nesting between the two lists unlike the PHP parser.
+# Unsure which is more desirable.
 !! test
 Definition Lists: colons and tables 1
 !! wikitext
 :{|
-| x
+|x
 |}
 :{|
-| y
+|y
 |}
-!! html
+!! html/php
 <dl><dd><table>
 <tr>
-<td> x
+<td>x
 </td></tr></table></dd></dl>
 <dl><dd><table>
 <tr>
-<td> y
+<td>y
 </td></tr></table></dd></dl>
 
-!! end
-
-# Parsoid's output (as documented below) differs from php's in this case.
-# This is probably a bug. If we fixup parsoid to match php's output, the
-# above test should pass and the below test case can be removed. It is
-# unclear which output is more desirable.
-
-!! test
-Definition Lists: colons and tables 2
-!! wikitext
-:{|
-| x
-|}
-:{|
-| y
-|}
 !! html/parsoid
 <dl><dd><table>
 <tr>
-<td> x
+<td>x
 </td></tr></table></dd>
 <dd><table>
 <tr>
-<td> y
+<td>y
 </td></tr></table></dd></dl>
 !! end
 
+# FIXME: Does this need a html/php section?
 !! test
 Definition Lists: template interaction
 !! wikitext
@@ -4688,9 +4615,9 @@ Numbered: <a rel="nofollow" class="external autonumber" href="http://example.net
 Numbered: <a rel="nofollow" class="external autonumber" href="http://example.com">[3]</a>
 </p>
 !! html/parsoid
-<p>Numbered: <a rel="mw:ExtLink" href="http://example.com"></a>
-Numbered: <a rel="mw:ExtLink" href="http://example.net"></a>
-Numbered: <a rel="mw:ExtLink" href="http://example.com"></a></p>
+<p>Numbered: <a rel="mw:ExtLink" class="external autonumber" href="http://example.com"></a>
+Numbered: <a rel="mw:ExtLink" class="external autonumber" href="http://example.net"></a>
+Numbered: <a rel="mw:ExtLink" class="external autonumber" href="http://example.com"></a></p>
 !!end
 
 !! test
@@ -4729,7 +4656,7 @@ External links: dollar sign in URL (autonumber)
 <p><a rel="nofollow" class="external autonumber" href="http://example.com/1$2345">[1]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/1$2345"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://example.com/1$2345"></a></p>
 !!end
 
 !! test
@@ -4742,7 +4669,7 @@ http://example.com/1[2345
 <p><a rel="nofollow" class="external free" href="http://example.com/1">http://example.com/1</a>[2345
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/1">http://example.com/1</a>[2345</p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com/1">http://example.com/1</a>[2345</p>
 !! end
 
 !! test
@@ -4755,7 +4682,7 @@ parsoid=wt2html,html2html
 <p><a rel="nofollow" class="external text" href="http://example.com/1">[2345</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/1">[2345</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://example.com/1">[2345</a></p>
 !!end
 
 # parsoid adds a space before the link name
@@ -4816,7 +4743,7 @@ External links: protocol-relative URL in brackets without text
 <p><a rel="nofollow" class="external autonumber" href="//example.com">[1]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="//example.com"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="//example.com"></a></p>
 !! end
 
 !! test
@@ -4854,7 +4781,7 @@ parsoid=wt2html,wt2wt
 </p><p><a href="http://en.wikipedia.org/wiki/Foo" class="extiw" title="wikipedia:Foo"><span>Bar</span></a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://en.wikipedia.org/wiki/Foo"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://en.wikipedia.org/wiki/Foo"></a></p>
 <p><a rel="mw:WikiLink/Interwiki" href="http://en.wikipedia.org/wiki/Foo" title="wikipedia:Foo">Bar</a></p>
 <p><a rel="mw:WikiLink/Interwiki" href="http://en.wikipedia.org/wiki/Foo" title="wikipedia:Foo"><span>Bar</span></a></p>
 !! end
@@ -4903,25 +4830,25 @@ http://example.com/url_with_entity&#60;
 <a rel="nofollow" class="external free" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a>&#60;
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com">http://example.com</a>,
-<a rel="mw:ExtLink" href="http://example.com">http://example.com</a>;
-<a rel="mw:ExtLink" href="http://example.com">http://example.com</a>\
-<a rel="mw:ExtLink" href="http://example.com">http://example.com</a>.
-<a rel="mw:ExtLink" href="http://example.com">http://example.com</a>:
-<a rel="mw:ExtLink" href="http://example.com">http://example.com</a>!
-<a rel="mw:ExtLink" href="http://example.com">http://example.com</a>?
-<a rel="mw:ExtLink" href="http://example.com">http://example.com</a>)
-<a rel="mw:ExtLink" href="http://example.com/url_with_(brackets)">http://example.com/url_with_(brackets)</a>
-(<a rel="mw:ExtLink" href="http://example.com/url_without_brackets">http://example.com/url_without_brackets</a>)
-<a rel="mw:ExtLink" href="http://example.com/url_with_entity&amp;">http://example.com/url_with_entity&amp;</a>
-<a rel="mw:ExtLink" href="http://example.com/url_with_entity&amp;">http://example.com/url_with_entity&amp;</a>
-<a rel="mw:ExtLink" href="http://example.com/url_with_entity&amp;">http://example.com/url_with_entity&amp;</a>
-<a rel="mw:ExtLink" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;nbsp;","srcContent":" "}'> </span>
-<a rel="mw:ExtLink" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#xA0;","srcContent":" "}'> </span>
-<a rel="mw:ExtLink" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#160;","srcContent":" "}'> </span>
-<a rel="mw:ExtLink" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;lt;","srcContent":"&lt;"}'>&lt;</span>
-<a rel="mw:ExtLink" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#x3C;","srcContent":"&lt;"}'>&lt;</span>
-<a rel="mw:ExtLink" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#60;","srcContent":"&lt;"}'>&lt;</span></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>,
+<a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>;
+<a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>\
+<a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>.
+<a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>:
+<a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>!
+<a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>?
+<a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>)
+<a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_(brackets)">http://example.com/url_with_(brackets)</a>
+(<a rel="mw:ExtLink" class="external free" href="http://example.com/url_without_brackets">http://example.com/url_without_brackets</a>)
+<a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_entity&amp;">http://example.com/url_with_entity&amp;</a>
+<a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_entity&amp;">http://example.com/url_with_entity&amp;</a>
+<a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_entity&amp;">http://example.com/url_with_entity&amp;</a>
+<a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;nbsp;","srcContent":" "}'> </span>
+<a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#xA0;","srcContent":" "}'> </span>
+<a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#160;","srcContent":" "}'> </span>
+<a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;lt;","srcContent":"&lt;"}'>&lt;</span>
+<a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#x3C;","srcContent":"&lt;"}'>&lt;</span>
+<a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_entity">http://example.com/url_with_entity</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#60;","srcContent":"&lt;"}'>&lt;</span></p>
 !! end
 
 !! test
@@ -4934,7 +4861,7 @@ http://example.com/url_with_entity&amp;amp;
 <p><a rel="nofollow" class="external free" href="http://example.com/url_with_entity&amp;amp">http://example.com/url_with_entity&amp;amp</a>;
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/url_with_entity&amp;amp">http://example.com/url_with_entity&amp;amp</a>;</p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com/url_with_entity&amp;amp">http://example.com/url_with_entity&amp;amp</a>;</p>
 !! end
 
 !! test
@@ -4949,7 +4876,7 @@ news:'a'b''c''d e
 </p>
 !! html/parsoid
 <p><b>News:</b> Stuff here</p>
-<p><a rel="mw:ExtLink" href="news:'a'b">news:'a'b</a><i>c</i>d e</p>
+<p><a rel="mw:ExtLink" class="external free" href="news:'a'b">news:'a'b</a><i>c</i>d e</p>
 !! end
 
 !! test
@@ -4960,7 +4887,7 @@ External links: with entity
 <p><a rel="nofollow" class="external text" href="http://+www.librarieswithoutborders.org">Libraries without borders</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://+www.librarieswithoutborders.org" data-parsoid='{"a":{"href":"http://+www.librarieswithoutborders.org"},"sa":{"href":"http://&amp;#x20;www.librarieswithoutborders.org"}}'>Libraries without borders</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://+www.librarieswithoutborders.org" data-parsoid='{"a":{"href":"http://+www.librarieswithoutborders.org"},"sa":{"href":"http://&amp;#x20;www.librarieswithoutborders.org"}}'>Libraries without borders</a></p>
 !! end
 
 !! test
@@ -5090,10 +5017,10 @@ parsoid=wt2html
 !! wikitext
 URL in text: [http://example.com http://example.com]
 !! html/php
-<p>URL in text: <a rel="nofollow" class="external free" href="http://example.com">http://example.com</a>
+<p>URL in text: <a rel="nofollow" class="external text" href="http://example.com">http://example.com</a>
 </p>
 !! html/parsoid
-<p>URL in text: <a rel="mw:ExtLink" href="http://example.com">http://example.com</a></p>
+<p>URL in text: <a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a></p>
 !! end
 
 !! test
@@ -5104,7 +5031,7 @@ ja-style clickable images: [http://example.com http://meta.wikimedia.org/upload/
 <p>ja-style clickable images: <a rel="nofollow" class="external text" href="http://example.com"><img src="http://meta.wikimedia.org/upload/f/f1/Ncwikicol.png" alt="Ncwikicol.png"/></a>
 </p>
 !! html/parsoid
-<p>ja-style clickable images: <a rel="mw:ExtLink" href="http://example.com"><img src="http://meta.wikimedia.org/upload/f/f1/Ncwikicol.png" alt="Ncwikicol.png" data-parsoid='{"type":"extlink"}'/></a></p>
+<p>ja-style clickable images: <a rel="mw:ExtLink" class="external text" href="http://example.com"><img src="http://meta.wikimedia.org/upload/f/f1/Ncwikicol.png" alt="Ncwikicol.png" data-parsoid='{"type":"extlink"}'/></a></p>
 !! end
 
 !! test
@@ -5124,7 +5051,7 @@ Old &amp; use: http://x&amp;y
 <p>Old &amp; use: <a rel="nofollow" class="external free" href="http://x&amp;y">http://x&amp;y</a>
 </p>
 !! html/parsoid
-<p>Old <span typeof="mw:Entity">&amp;</span> use: <a rel="mw:ExtLink" href="http://x&amp;y">http://x&amp;y</a></p>
+<p>Old <span typeof="mw:Entity">&amp;</span> use: <a rel="mw:ExtLink" class="external free" href="http://x&amp;y">http://x&amp;y</a></p>
 !! end
 
 !! test
@@ -5135,7 +5062,7 @@ http://example.com/?foo&#61;bar
 <p><a rel="nofollow" class="external free" href="http://example.com/?foo=bar">http://example.com/?foo=bar</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/?foo=bar">http://example.com/?foo=bar</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com/?foo=bar">http://example.com/?foo=bar</a></p>
 !! end
 
 ##
@@ -5152,7 +5079,7 @@ Old &amp; use: [http://x&y]
 <p>Old &amp; use: <a rel="nofollow" class="external autonumber" href="http://x&amp;y">[1]</a>
 </p>
 !! html/parsoid
-<p>Old <span typeof="mw:Entity">&amp;</span> use: <a rel="mw:ExtLink" href="http://x&amp;y"></a></p>
+<p>Old <span typeof="mw:Entity">&amp;</span> use: <a rel="mw:ExtLink" class="external autonumber" href="http://x&amp;y"></a></p>
 !! end
 
 # note that parsoid html is identical to [raw ampersand] case; so html2wt
@@ -5167,7 +5094,7 @@ Old &amp; use: [http://x&amp;y]
 <p>Old &amp; use: <a rel="nofollow" class="external autonumber" href="http://x&amp;y">[1]</a>
 </p>
 !! html/parsoid
-<p>Old <span typeof="mw:Entity">&amp;</span> use: <a rel="mw:ExtLink" href="http://x&amp;y"></a></p>
+<p>Old <span typeof="mw:Entity">&amp;</span> use: <a rel="mw:ExtLink" class="external autonumber" href="http://x&amp;y"></a></p>
 !! end
 
 !! test
@@ -5178,7 +5105,7 @@ External links: [raw equals]
 <p><a rel="nofollow" class="external autonumber" href="http://example.com/?foo=bar">[1]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/?foo=bar"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://example.com/?foo=bar"></a></p>
 !! end
 
 # note that parsoid html is identical to [raw equals] case; so html2wt
@@ -5193,7 +5120,7 @@ parsoid=wt2html,wt2wt,html2html
 <p><a rel="nofollow" class="external autonumber" href="http://example.com/?foo=bar">[1]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/?foo=bar"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://example.com/?foo=bar"></a></p>
 !! end
 
 # xxx parsoid strips the IDN character, so the round-trip tests will
@@ -5208,7 +5135,7 @@ parsoid=wt2html,wt2wt,html2html
 <p><a rel="nofollow" class="external autonumber" href="http://example.com/">[1]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://example.com/"></a></p>
 !! end
 
 # FIXME: This test (the IDN characters in the text of a link) is an inconsistency.
@@ -5240,7 +5167,7 @@ http://e&zwnj;xample.com/
 <p><a rel="nofollow" class="external free" href="http://example.com/">http://example.com/</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/">http://example.com/</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com/">http://example.com/</a></p>
 !! end
 
 !! test
@@ -5261,7 +5188,7 @@ External links: URL within URL (T2002)
 <p><a rel="nofollow" class="external autonumber" href="http://www.unausa.org/newindex.asp?place=http://www.unausa.org/programs/mun.asp">[1]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.unausa.org/newindex.asp?place=http://www.unausa.org/programs/mun.asp"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://www.unausa.org/newindex.asp?place=http://www.unausa.org/programs/mun.asp"></a></p>
 !! end
 
 !! test
@@ -5299,7 +5226,7 @@ http://www.example.com/<b>html</b>
 <p><a rel="nofollow" class="external free" href="http://www.example.com/">http://www.example.com/</a><b>html</b>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.example.com/" data-parsoid='{"stx":"url"}'>http://www.example.com/</a><b data-parsoid='{"stx":"html"}'>html</b></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://www.example.com/" data-parsoid='{"stx":"url"}'>http://www.example.com/</a><b data-parsoid='{"stx":"html"}'>html</b></p>
 !! end
 
 !! test
@@ -5358,17 +5285,22 @@ External links: link text with spaces
 </p>
 !! end
 
+# Note edge case difference between PHP and Parsoid here.
 !! test
 External links: wiki links within external link (T5695)
 !! options
 parsoid=wt2html,html2html
 !! wikitext
 [http://example.com [[wikilink]] embedded in ext link]
+
+[http://example.com test [[wikilink]] embedded in ext link]
 !! html/php
 <p><a rel="nofollow" class="external text" href="http://example.com"></a><a href="/index.php?title=Wikilink&amp;action=edit&amp;redlink=1" class="new" title="Wikilink (page does not exist)">wikilink</a><a rel="nofollow" class="external text" href="http://example.com"> embedded in ext link</a>
+</p><p><a rel="nofollow" class="external text" href="http://example.com">test </a><a href="/index.php?title=Wikilink&amp;action=edit&amp;redlink=1" class="new" title="Wikilink (page does not exist)">wikilink</a><a rel="nofollow" class="external text" href="http://example.com"> embedded in ext link</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com"></a><a rel="mw:WikiLink" href="./Wikilink" title="Wikilink">wikilink</a><span> embedded in ext link</span></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://example.com"></a><a rel="mw:WikiLink" href="./Wikilink" title="Wikilink">wikilink</a><span> embedded in ext link</span></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://example.com">test </a><a rel="mw:WikiLink" href="./Wikilink" title="Wikilink">wikilink</a><span> embedded in ext link</span></p>
 !! end
 
 !! test
@@ -5412,8 +5344,8 @@ parsoid=wt2html
 </p><p>{{echo|[[Foo}}
 </p>
 !! html/parsoid
-<p>[<a rel="mw:ExtLink" href="http://example.com">http://example.com</a> x</p>
-<p typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[http://example.com x"}},"i":0}}]}'>[<a rel="mw:ExtLink" href="http://example.com">http://example.com</a> x</p>
+<p>[<a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a> x</p>
+<p typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[http://example.com x"}},"i":0}}]}'>[<a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a> x</p>
 <p>[[Foo</p>
 <p>{{echo|[[Foo}}</p>
 !! end
@@ -5476,7 +5408,7 @@ http://www.example.com/?title=AT%26T
 <p><a rel="nofollow" class="external free" href="http://www.example.com/?title=AT%26T">http://www.example.com/?title=AT%26T</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.example.com/?title=AT%26T">http://www.example.com/?title=AT%26T</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://www.example.com/?title=AT%26T">http://www.example.com/?title=AT%26T</a></p>
 !! end
 
 # According to https://www.w3.org/TR/2011/WD-html5-20110525/Overview.html#parsing-urls a plain
@@ -5489,7 +5421,7 @@ http://www.example.com/?title=100%25_Bran
 <p><a rel="nofollow" class="external free" href="http://www.example.com/?title=100%25_Bran">http://www.example.com/?title=100%25_Bran</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.example.com/?title=100%25_Bran">http://www.example.com/?title=100%25_Bran</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://www.example.com/?title=100%25_Bran">http://www.example.com/?title=100%25_Bran</a></p>
 !! end
 
 !! test
@@ -5500,7 +5432,7 @@ http://www.example.com/?title=Ben-Hur_%281959_film%29
 <p><a rel="nofollow" class="external free" href="http://www.example.com/?title=Ben-Hur_%281959_film%29">http://www.example.com/?title=Ben-Hur_%281959_film%29</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.example.com/?title=Ben-Hur_%281959_film%29">http://www.example.com/?title=Ben-Hur_%281959_film%29</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://www.example.com/?title=Ben-Hur_%281959_film%29">http://www.example.com/?title=Ben-Hur_%281959_film%29</a></p>
 !! end
 
 
@@ -5512,7 +5444,7 @@ T6781: %26 in autonumber URL
 <p><a rel="nofollow" class="external autonumber" href="http://www.example.com/?title=AT%26T">[1]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.example.com/?title=AT%26T"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://www.example.com/?title=AT%26T"></a></p>
 !! end
 
 !! test
@@ -5523,7 +5455,7 @@ T6781, T7267: %26 in autonumber URL
 <p><a rel="nofollow" class="external autonumber" href="http://www.example.com/?title=100%25_Bran">[1]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.example.com/?title=100%25_Bran"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://www.example.com/?title=100%25_Bran"></a></p>
 !! end
 
 !! test
@@ -5534,7 +5466,7 @@ T6781, T7267: %28, %29 in autonumber URL
 <p><a rel="nofollow" class="external autonumber" href="http://www.example.com/?title=Ben-Hur_%281959_film%29">[1]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.example.com/?title=Ben-Hur_%281959_film%29"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://www.example.com/?title=Ben-Hur_%281959_film%29"></a></p>
 !! end
 
 
@@ -5546,7 +5478,7 @@ T6781: %26 in bracketed URL
 <p><a rel="nofollow" class="external text" href="http://www.example.com/?title=AT%26T">link</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.example.com/?title=AT%26T">link</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://www.example.com/?title=AT%26T">link</a></p>
 !! end
 
 !! test
@@ -5566,7 +5498,7 @@ T6781, T7267: %28, %29 in bracketed URL
 <p><a rel="nofollow" class="external text" href="http://www.example.com/?title=Ben-Hur_%281959_film%29">link</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.example.com/?title=Ben-Hur_%281959_film%29">link</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://www.example.com/?title=Ben-Hur_%281959_film%29">link</a></p>
 !! end
 
 !! test
@@ -5580,8 +5512,8 @@ External link containing a period in the anchor. (T65947)
 </p><p><a rel="nofollow" class="external text" href="//foo.org/bar.">bang</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="//foo.org/bar#baz.">bang</a></p>
-<p><a rel="mw:ExtLink" href="//foo.org/bar.">bang</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="//foo.org/bar#baz.">bang</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="//foo.org/bar.">bang</a></p>
 !! end
 
 !! test
@@ -5595,8 +5527,8 @@ External link containing a single quote. (T65947)
 </p><p><a rel="nofollow" class="external text" href="//foo.org/bar&#39;baz">bang</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="//foo.org/bar'baz"></a></p>
-<p><a rel="mw:ExtLink" href="//foo.org/bar'baz">bang</a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="//foo.org/bar'baz"></a></p>
+<p><a rel="mw:ExtLink" class="external text" href="//foo.org/bar'baz">bang</a></p>
 !! end
 
 !! test
@@ -5617,17 +5549,16 @@ External link containing double-single-quotes in text embedded in italics (T6598
 </p>
 !! end
 
+# Don't add the html/php section since the output is broken and there isn't any reason to spec it
 !! test
 External link containing double-single-quotes with no space separating the url from text in italics
 !! wikitext
 [http://www.musee-picasso.fr/pages/page_id18528_u1l2.htm''La muerte de Casagemas'' (1901) en el sitio de [[Museo Picasso (París)|Museo Picasso]].]
-!! html/php
-<p><a rel="nofollow" class="external text" href="http://www.musee-picasso.fr/pages/page_id18528_u1l2.htm"><i>La muerte de Casagemas</i> (1901) en el sitio de <a href="/index.php?title=Museo_Picasso_(Par%C3%ADs)&amp;action=edit&amp;redlink=1" class="new" title="Museo Picasso (París) (page does not exist)">Museo Picasso</a>.</a>
-</p>
 !! html/php+tidy
-<p><a rel="nofollow" class="external text" href="http://www.musee-picasso.fr/pages/page_id18528_u1l2.htm"><i>La muerte de Casagemas</i> (1901) en el sitio de</a> <a href="/index.php?title=Museo_Picasso_(Par%C3%ADs)&amp;action=edit&amp;redlink=1" class="new" title="Museo Picasso (París) (page does not exist)">Museo Picasso</a>.</p>
+<p><a rel="nofollow" class="external text" href="http://www.musee-picasso.fr/pages/page_id18528_u1l2.htm"><i>La muerte de Casagemas</i> (1901) en el sitio de </a><a href="/index.php?title=Museo_Picasso_(Par%C3%ADs)&amp;action=edit&amp;redlink=1" class="new" title="Museo Picasso (París) (page does not exist)">Museo Picasso</a>.
+</p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.musee-picasso.fr/pages/page_id18528_u1l2.htm"><i>La muerte de Casagemas</i> (1901) en el sitio de </a><a rel="mw:WikiLink" href="./Museo_Picasso_(París)" title="Museo Picasso (París)">Museo Picasso</a><span>.</span></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://www.musee-picasso.fr/pages/page_id18528_u1l2.htm"><i>La muerte de Casagemas</i> (1901) en el sitio de </a><a rel="mw:WikiLink" href="./Museo_Picasso_(París)" title="Museo Picasso (París)">Museo Picasso</a><span>.</span></p>
 !! end
 
 !! test
@@ -5638,7 +5569,7 @@ External link with comments in link text
 <p><a rel="nofollow" class="external text" href="http://www.google.com">Google </a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.google.com">Google <!-- comment --></a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://www.google.com">Google <!-- comment --></a></p>
 !! end
 
 !! test
@@ -5649,7 +5580,7 @@ External link to bare IPv4 address
 <p><a rel="nofollow" class="external text" href="http://192.168.0.1">Link</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://192.168.0.1">Link</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://192.168.0.1">Link</a></p>
 !! end
 
 !! test
@@ -5681,9 +5612,9 @@ http://example.com/index.php?foozoid&#x5B;&#x5D;=bar
 </p><p><a rel="nofollow" class="external free" href="http://example.com/index.php?foozoid%5B%5D=bar">http://example.com/index.php?foozoid%5B%5D=bar</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/index.php?foozoid%5B%5D=bar">http://example.com/index.php?foozoid%5B%5D=bar</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com/index.php?foozoid%5B%5D=bar">http://example.com/index.php?foozoid%5B%5D=bar</a></p>
 
-<p><a rel="mw:ExtLink" href="http://example.com/index.php?foozoid%5B%5D=bar" data-parsoid='{"stx":"url","a":{"href":"http://example.com/index.php?foozoid%5B%5D=bar"},"sa":{"href":"http://example.com/index.php?foozoid&amp;#x5B;&amp;#x5D;=bar"}}'>http://example.com/index.php?foozoid%5B%5D=bar</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com/index.php?foozoid%5B%5D=bar" data-parsoid='{"stx":"url","a":{"href":"http://example.com/index.php?foozoid%5B%5D=bar"},"sa":{"href":"http://example.com/index.php?foozoid&amp;#x5B;&amp;#x5D;=bar"}}'>http://example.com/index.php?foozoid%5B%5D=bar</a></p>
 !! end
 
 !! test
@@ -5692,61 +5623,62 @@ IPv6 urls, autolink format (T23261)
 http://[2404:130:0:1000::187:2]/index.php
 
 Examples from RFC 2373, section 2.2:
-* http://[1080::8:800:200C:417A]/unicast
-* http://[FF01::101]/multicast
-* http://[::1]/loopback
-* http://[::]/unspecified
-* http://[::13.1.68.3]/ipv4compat
-* http://[::FFFF:129.144.52.38]/ipv4compat
+
+*http://[1080::8:800:200C:417A]/unicast
+*http://[FF01::101]/multicast
+*http://[::1]/loopback
+*http://[::]/unspecified
+*http://[::13.1.68.3]/ipv4compat
+*http://[::FFFF:129.144.52.38]/ipv4compat
 
 Examples from RFC 2732, section 2:
-* http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html
-* http://[1080:0:0:0:8:800:200C:417A]/index.html
-* http://[3ffe:2a00:100:7031::1]
-* http://[1080::8:800:200C:417A]/foo
-* http://[::192.9.5.5]/ipng
-* http://[::FFFF:129.144.52.38]:80/index.html
-* http://[2010:836B:4179::836B:4179]
 
+*http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html
+*http://[1080:0:0:0:8:800:200C:417A]/index.html
+*http://[3ffe:2a00:100:7031::1]
+*http://[1080::8:800:200C:417A]/foo
+*http://[::192.9.5.5]/ipng
+*http://[::FFFF:129.144.52.38]:80/index.html
+*http://[2010:836B:4179::836B:4179]
 !! html/php
 <p><a rel="nofollow" class="external free" href="http://[2404:130:0:1000::187:2]/index.php">http://[2404:130:0:1000::187:2]/index.php</a>
 </p><p>Examples from <a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc2373">RFC 2373</a>, section 2.2:
 </p>
-<ul><li> <a rel="nofollow" class="external free" href="http://[1080::8:800:200C:417A]/unicast">http://[1080::8:800:200C:417A]/unicast</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[FF01::101]/multicast">http://[FF01::101]/multicast</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[::1]/loopback">http://[::1]/loopback</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[::]/unspecified">http://[::]/unspecified</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[::13.1.68.3]/ipv4compat">http://[::13.1.68.3]/ipv4compat</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[::FFFF:129.144.52.38]/ipv4compat">http://[::FFFF:129.144.52.38]/ipv4compat</a></li></ul>
+<ul><li><a rel="nofollow" class="external free" href="http://[1080::8:800:200C:417A]/unicast">http://[1080::8:800:200C:417A]/unicast</a></li>
+<li><a rel="nofollow" class="external free" href="http://[FF01::101]/multicast">http://[FF01::101]/multicast</a></li>
+<li><a rel="nofollow" class="external free" href="http://[::1]/loopback">http://[::1]/loopback</a></li>
+<li><a rel="nofollow" class="external free" href="http://[::]/unspecified">http://[::]/unspecified</a></li>
+<li><a rel="nofollow" class="external free" href="http://[::13.1.68.3]/ipv4compat">http://[::13.1.68.3]/ipv4compat</a></li>
+<li><a rel="nofollow" class="external free" href="http://[::FFFF:129.144.52.38]/ipv4compat">http://[::FFFF:129.144.52.38]/ipv4compat</a></li></ul>
 <p>Examples from <a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc2732">RFC 2732</a>, section 2:
 </p>
-<ul><li> <a rel="nofollow" class="external free" href="http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html">http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[1080:0:0:0:8:800:200C:417A]/index.html">http://[1080:0:0:0:8:800:200C:417A]/index.html</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[3ffe:2a00:100:7031::1]">http://[3ffe:2a00:100:7031::1]</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[1080::8:800:200C:417A]/foo">http://[1080::8:800:200C:417A]/foo</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[::192.9.5.5]/ipng">http://[::192.9.5.5]/ipng</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[::FFFF:129.144.52.38]:80/index.html">http://[::FFFF:129.144.52.38]:80/index.html</a></li>
-<li> <a rel="nofollow" class="external free" href="http://[2010:836B:4179::836B:4179]">http://[2010:836B:4179::836B:4179]</a></li></ul>
+<ul><li><a rel="nofollow" class="external free" href="http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html">http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html</a></li>
+<li><a rel="nofollow" class="external free" href="http://[1080:0:0:0:8:800:200C:417A]/index.html">http://[1080:0:0:0:8:800:200C:417A]/index.html</a></li>
+<li><a rel="nofollow" class="external free" href="http://[3ffe:2a00:100:7031::1]">http://[3ffe:2a00:100:7031::1]</a></li>
+<li><a rel="nofollow" class="external free" href="http://[1080::8:800:200C:417A]/foo">http://[1080::8:800:200C:417A]/foo</a></li>
+<li><a rel="nofollow" class="external free" href="http://[::192.9.5.5]/ipng">http://[::192.9.5.5]/ipng</a></li>
+<li><a rel="nofollow" class="external free" href="http://[::FFFF:129.144.52.38]:80/index.html">http://[::FFFF:129.144.52.38]:80/index.html</a></li>
+<li><a rel="nofollow" class="external free" href="http://[2010:836B:4179::836B:4179]">http://[2010:836B:4179::836B:4179]</a></li></ul>
 
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://[2404:130:0:1000::187:2]/index.php">http://[2404:130:0:1000::187:2]/index.php</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://[2404:130:0:1000::187:2]/index.php">http://[2404:130:0:1000::187:2]/index.php</a></p>
 
-<p>Examples from <a href="https://tools.ietf.org/html/rfc2373" rel="mw:ExtLink">RFC 2373</a>, section 2.2:</p>
-<ul><li> <a rel="mw:ExtLink" href="http://[1080::8:800:200C:417A]/unicast">http://[1080::8:800:200C:417A]/unicast</a></li>
-<li> <a rel="mw:ExtLink" href="http://[FF01::101]/multicast">http://[FF01::101]/multicast</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::1]/loopback">http://[::1]/loopback</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::]/unspecified">http://[::]/unspecified</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::13.1.68.3]/ipv4compat">http://[::13.1.68.3]/ipv4compat</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::FFFF:129.144.52.38]/ipv4compat">http://[::FFFF:129.144.52.38]/ipv4compat</a></li></ul>
+<p>Examples from <a href="https://tools.ietf.org/html/rfc2373" rel="mw:ExtLink" class="external text">RFC 2373</a>, section 2.2:</p>
+<ul><li><a rel="mw:ExtLink" class="external free" href="http://[1080::8:800:200C:417A]/unicast">http://[1080::8:800:200C:417A]/unicast</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[FF01::101]/multicast">http://[FF01::101]/multicast</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[::1]/loopback">http://[::1]/loopback</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[::]/unspecified">http://[::]/unspecified</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[::13.1.68.3]/ipv4compat">http://[::13.1.68.3]/ipv4compat</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[::FFFF:129.144.52.38]/ipv4compat">http://[::FFFF:129.144.52.38]/ipv4compat</a></li></ul>
 
-<p>Examples from <a href="https://tools.ietf.org/html/rfc2732" rel="mw:ExtLink">RFC 2732</a>, section 2:</p>
-<ul><li> <a rel="mw:ExtLink" href="http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html">http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html</a></li>
-<li> <a rel="mw:ExtLink" href="http://[1080:0:0:0:8:800:200C:417A]/index.html">http://[1080:0:0:0:8:800:200C:417A]/index.html</a></li>
-<li> <a rel="mw:ExtLink" href="http://[3ffe:2a00:100:7031::1]">http://[3ffe:2a00:100:7031::1]</a></li>
-<li> <a rel="mw:ExtLink" href="http://[1080::8:800:200C:417A]/foo">http://[1080::8:800:200C:417A]/foo</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::192.9.5.5]/ipng">http://[::192.9.5.5]/ipng</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::FFFF:129.144.52.38]:80/index.html">http://[::FFFF:129.144.52.38]:80/index.html</a></li>
-<li> <a rel="mw:ExtLink" href="http://[2010:836B:4179::836B:4179]">http://[2010:836B:4179::836B:4179]</a></li></ul>
+<p>Examples from <a href="https://tools.ietf.org/html/rfc2732" rel="mw:ExtLink" class="external text">RFC 2732</a>, section 2:</p>
+<ul><li><a rel="mw:ExtLink" class="external free" href="http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html">http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[1080:0:0:0:8:800:200C:417A]/index.html">http://[1080:0:0:0:8:800:200C:417A]/index.html</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[3ffe:2a00:100:7031::1]">http://[3ffe:2a00:100:7031::1]</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[1080::8:800:200C:417A]/foo">http://[1080::8:800:200C:417A]/foo</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[::192.9.5.5]/ipng">http://[::192.9.5.5]/ipng</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[::FFFF:129.144.52.38]:80/index.html">http://[::FFFF:129.144.52.38]:80/index.html</a></li>
+<li><a rel="mw:ExtLink" class="external free" href="http://[2010:836B:4179::836B:4179]">http://[2010:836B:4179::836B:4179]</a></li></ul>
 !! end
 
 !! test
@@ -5755,61 +5687,62 @@ IPv6 urls, bracketed format (T23261)
 [http://[2404:130:0:1000::187:2]/index.php test]
 
 Examples from RFC 2373, section 2.2:
-* [http://[1080::8:800:200C:417A] unicast]
-* [http://[FF01::101] multicast]
-* [http://[::1]/ loopback]
-* [http://[::] unspecified]
-* [http://[::13.1.68.3] ipv4compat]
-* [http://[::FFFF:129.144.52.38] ipv4compat]
+
+*[http://[1080::8:800:200C:417A] unicast]
+*[http://[FF01::101] multicast]
+*[http://[::1]/ loopback]
+*[http://[::] unspecified]
+*[http://[::13.1.68.3] ipv4compat]
+*[http://[::FFFF:129.144.52.38] ipv4compat]
 
 Examples from RFC 2732, section 2:
-* [http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html 1]
-* [http://[1080:0:0:0:8:800:200C:417A]/index.html 2]
-* [http://[3ffe:2a00:100:7031::1] 3]
-* [http://[1080::8:800:200C:417A]/foo 4]
-* [http://[::192.9.5.5]/ipng 5]
-* [http://[::FFFF:129.144.52.38]:80/index.html 6]
-* [http://[2010:836B:4179::836B:4179] 7]
 
+*[http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html 1]
+*[http://[1080:0:0:0:8:800:200C:417A]/index.html 2]
+*[http://[3ffe:2a00:100:7031::1] 3]
+*[http://[1080::8:800:200C:417A]/foo 4]
+*[http://[::192.9.5.5]/ipng 5]
+*[http://[::FFFF:129.144.52.38]:80/index.html 6]
+*[http://[2010:836B:4179::836B:4179] 7]
 !! html/php
 <p><a rel="nofollow" class="external text" href="http://[2404:130:0:1000::187:2]/index.php">test</a>
 </p><p>Examples from <a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc2373">RFC 2373</a>, section 2.2:
 </p>
-<ul><li> <a rel="nofollow" class="external text" href="http://[1080::8:800:200C:417A]">unicast</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[FF01::101]">multicast</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[::1]/">loopback</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[::]">unspecified</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[::13.1.68.3]">ipv4compat</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[::FFFF:129.144.52.38]">ipv4compat</a></li></ul>
+<ul><li><a rel="nofollow" class="external text" href="http://[1080::8:800:200C:417A]">unicast</a></li>
+<li><a rel="nofollow" class="external text" href="http://[FF01::101]">multicast</a></li>
+<li><a rel="nofollow" class="external text" href="http://[::1]/">loopback</a></li>
+<li><a rel="nofollow" class="external text" href="http://[::]">unspecified</a></li>
+<li><a rel="nofollow" class="external text" href="http://[::13.1.68.3]">ipv4compat</a></li>
+<li><a rel="nofollow" class="external text" href="http://[::FFFF:129.144.52.38]">ipv4compat</a></li></ul>
 <p>Examples from <a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc2732">RFC 2732</a>, section 2:
 </p>
-<ul><li> <a rel="nofollow" class="external text" href="http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html">1</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[1080:0:0:0:8:800:200C:417A]/index.html">2</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[3ffe:2a00:100:7031::1]">3</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[1080::8:800:200C:417A]/foo">4</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[::192.9.5.5]/ipng">5</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[::FFFF:129.144.52.38]:80/index.html">6</a></li>
-<li> <a rel="nofollow" class="external text" href="http://[2010:836B:4179::836B:4179]">7</a></li></ul>
+<ul><li><a rel="nofollow" class="external text" href="http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html">1</a></li>
+<li><a rel="nofollow" class="external text" href="http://[1080:0:0:0:8:800:200C:417A]/index.html">2</a></li>
+<li><a rel="nofollow" class="external text" href="http://[3ffe:2a00:100:7031::1]">3</a></li>
+<li><a rel="nofollow" class="external text" href="http://[1080::8:800:200C:417A]/foo">4</a></li>
+<li><a rel="nofollow" class="external text" href="http://[::192.9.5.5]/ipng">5</a></li>
+<li><a rel="nofollow" class="external text" href="http://[::FFFF:129.144.52.38]:80/index.html">6</a></li>
+<li><a rel="nofollow" class="external text" href="http://[2010:836B:4179::836B:4179]">7</a></li></ul>
 
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://[2404:130:0:1000::187:2]/index.php">test</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://[2404:130:0:1000::187:2]/index.php">test</a></p>
 
-<p>Examples from <a href="https://tools.ietf.org/html/rfc2373" rel="mw:ExtLink">RFC 2373</a>, section 2.2:</p>
-<ul><li> <a rel="mw:ExtLink" href="http://[1080::8:800:200C:417A]">unicast</a></li>
-<li> <a rel="mw:ExtLink" href="http://[FF01::101]">multicast</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::1]/">loopback</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::]">unspecified</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::13.1.68.3]">ipv4compat</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::FFFF:129.144.52.38]">ipv4compat</a></li></ul>
+<p>Examples from <a href="https://tools.ietf.org/html/rfc2373" rel="mw:ExtLink" class="external text">RFC 2373</a>, section 2.2:</p>
+<ul><li><a rel="mw:ExtLink" class="external text" href="http://[1080::8:800:200C:417A]">unicast</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[FF01::101]">multicast</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[::1]/">loopback</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[::]">unspecified</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[::13.1.68.3]">ipv4compat</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[::FFFF:129.144.52.38]">ipv4compat</a></li></ul>
 
-<p>Examples from <a href="https://tools.ietf.org/html/rfc2732" rel="mw:ExtLink">RFC 2732</a>, section 2:</p>
-<ul><li> <a rel="mw:ExtLink" href="http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html">1</a></li>
-<li> <a rel="mw:ExtLink" href="http://[1080:0:0:0:8:800:200C:417A]/index.html">2</a></li>
-<li> <a rel="mw:ExtLink" href="http://[3ffe:2a00:100:7031::1]">3</a></li>
-<li> <a rel="mw:ExtLink" href="http://[1080::8:800:200C:417A]/foo">4</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::192.9.5.5]/ipng">5</a></li>
-<li> <a rel="mw:ExtLink" href="http://[::FFFF:129.144.52.38]:80/index.html">6</a></li>
-<li> <a rel="mw:ExtLink" href="http://[2010:836B:4179::836B:4179]">7</a></li></ul>
+<p>Examples from <a href="https://tools.ietf.org/html/rfc2732" rel="mw:ExtLink" class="external text">RFC 2732</a>, section 2:</p>
+<ul><li><a rel="mw:ExtLink" class="external text" href="http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html">1</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[1080:0:0:0:8:800:200C:417A]/index.html">2</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[3ffe:2a00:100:7031::1]">3</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[1080::8:800:200C:417A]/foo">4</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[::192.9.5.5]/ipng">5</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[::FFFF:129.144.52.38]:80/index.html">6</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://[2010:836B:4179::836B:4179]">7</a></li></ul>
 !! end
 
 !! test
@@ -5849,13 +5782,13 @@ Non-extlinks in brackets
 [foo <i>bar</i>]
 [fool's] errand
 [fool's errand]
-[<span typeof="mw:Placeholder" data-parsoid='{"src":"{{echo|foo}}"}'>foo</span>]
-[<span typeof="mw:Placeholder" data-parsoid='{"src":"{{echo|foo}}"}'>foo</span> bar]
-[<span typeof="mw:Placeholder" data-parsoid='{"src":"{{echo|foo}}"}'>foo</span> <i>bar</i>]
-[<span typeof="mw:Placeholder" data-parsoid='{"src":"{{echo|foo}}l&#39;s"}'>fool's</span>] errand
-[<span typeof="mw:Placeholder" data-parsoid='{"src":"{{echo|foo}}l&#39;s"}'>fool's</span> errand]
-[<span typeof="mw:Placeholder" data-parsoid='{"src":"url={{echo|foo}}"}'>url=foo</span>]
-[url=<a rel="mw:ExtLink" href="http://example.com">http://example.com</a>]
+[<span about="#mwt19" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo"}},"i":0}}]}'>foo</span>]
+[<span about="#mwt20" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo"}},"i":0}}]}'>foo</span> bar]
+[<span about="#mwt21" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo"}},"i":0}}]}'>foo</span> <i>bar</i>]
+[<span about="#mwt22" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo"}},"i":0}}]}'>foo</span>l's] errand
+[<span about="#mwt23" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo"}},"i":0}}]}'>foo</span>l's errand]
+[url=<span about="#mwt24" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo"}},"i":0}}]}'>foo</span>]
+[url=<a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>]
 [http:// bare protocols don't count]</p>
 !! end
 
@@ -5867,8 +5800,7 @@ Percent encoding in external links
 <p><a rel="nofollow" class="external text" href="https://github.com/search?l=&amp;q=ResourceLoader+%40wikimedia">Search</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink"
-href="https://github.com/search?l=&amp;q=ResourceLoader+%40wikimedia">Search</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="https://github.com/search?l=&amp;q=ResourceLoader+%40wikimedia">Search</a></p>
 !! end
 
 !! test
@@ -5879,7 +5811,7 @@ http://example.com
 <p><a rel="nofollow" class="external free" href="http://example.com">http://example.com</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com">http://example.com</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a></p>
 !! end
 
 !! test
@@ -5911,14 +5843,14 @@ http://example.com/a)b
 </p><p><a rel="nofollow" class="external text" href="http://example.com)">foo</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com">http://example.com</a>)</p>
-<p><a rel="mw:ExtLink" href="http://example.com/test">http://example.com/test</a>)</p>
-<p><a rel="mw:ExtLink" href="http://example.com/(test)">http://example.com/(test)</a></p>
-<p><a rel="mw:ExtLink" href="http://example.com/((test)">http://example.com/((test)</a></p>
-<p>(<a rel="mw:ExtLink" href="http://example.com/(test))">http://example.com/(test))</a></p>
-<p>(<a rel="mw:ExtLink" href="http://example.com/(test)))))">http://example.com/(test)))))</a></p>
-<p><a rel="mw:ExtLink" href="http://example.com/a)b">http://example.com/a)b</a></p>
-<p><a rel="mw:ExtLink" href="http://example.com)">foo</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a>)</p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com/test">http://example.com/test</a>)</p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com/(test)">http://example.com/(test)</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com/((test)">http://example.com/((test)</a></p>
+<p>(<a rel="mw:ExtLink" class="external free" href="http://example.com/(test))">http://example.com/(test))</a></p>
+<p>(<a rel="mw:ExtLink" class="external free" href="http://example.com/(test)))))">http://example.com/(test)))))</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com/a)b">http://example.com/a)b</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://example.com)">foo</a></p>
 !! end
 
 !! test
@@ -5932,9 +5864,9 @@ Parenthesis in external links, w/ transclusion or comment
 </p><p>(<a rel="nofollow" class="external free" href="http://example.com">http://example.com</a>)
 </p>
 !! html/parsoid
-<p>(<a typeof="mw:ExpandedAttrs" about="#mwt2" rel="mw:ExtLink" href="http://example.com/hi" data-parsoid='{"stx":"url","a":{"href":"http://example.com/hi"},"sa":{"href":"http://example.com/{{echo|hi}}"}}' data-mw='{"attribs":[[{"txt":"href"},{"html":"http://example.com/&lt;span about=\"#mwt1\" typeof=\"mw:Transclusion\" data-parsoid=&#39;{\"pi\":[[{\"k\":\"1\"}]],\"dsr\":[20,31,null,null]}&#39; data-mw=&#39;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"echo\",\"href\":\"./Template:Echo\"},\"params\":{\"1\":{\"wt\":\"hi\"}},\"i\":0}}]}&#39;>hi&lt;/span>"}]]}'>http://example.com/hi</a>)</p>
+<p>(<a typeof="mw:ExpandedAttrs" about="#mwt2" rel="mw:ExtLink" class="external free" href="http://example.com/hi" data-parsoid='{"stx":"url","a":{"href":"http://example.com/hi"},"sa":{"href":"http://example.com/{{echo|hi}}"}}' data-mw='{"attribs":[[{"txt":"href"},{"html":"http://example.com/&lt;span about=\"#mwt1\" typeof=\"mw:Transclusion\" data-parsoid=&#39;{\"pi\":[[{\"k\":\"1\"}]],\"dsr\":[20,31,null,null]}&#39; data-mw=&#39;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"echo\",\"href\":\"./Template:Echo\"},\"params\":{\"1\":{\"wt\":\"hi\"}},\"i\":0}}]}&#39;>hi&lt;/span>"}]]}'>http://example.com/hi</a>)</p>
 
-<p>(<a rel="mw:ExtLink" href="http://example.com" data-parsoid='{"stx":"url","a":{"href":"http://example.com"},"sa":{"href":"http://example.com&lt;!-- hi -->"}}'>http://example.com</a>)</p>
+<p>(<a rel="mw:ExtLink" class="external free" href="http://example.com" data-parsoid='{"stx":"url","a":{"href":"http://example.com"},"sa":{"href":"http://example.com&lt;!-- hi -->"}}'>http://example.com</a>)</p>
 !! end
 
 !! test
@@ -6138,15 +6070,15 @@ A table with no data (take 2)
 A table with nothing but a caption
 !! wikitext
 {|
-|+ caption
+|+caption
 |}
 !! html/php
 <table>
-<caption> caption
+<caption>caption
 </caption><tr><td></td></tr></table>
 
 !! html/parsoid
-<table><caption> caption</caption></table>
+<table><caption>caption</caption></table>
 !! end
 
 !! test
@@ -6155,14 +6087,14 @@ A table with caption with default-spaced attributes and a table row
 {|
 |+ style="color: red;" | caption1
 |-
-| foo
+|foo
 |}
 !! html
 <table>
-<caption style="color: red;"> caption1
+<caption style="color: red;">caption1
 </caption>
 <tr>
-<td> foo
+<td>foo
 </td></tr></table>
 
 !! end
@@ -6172,18 +6104,18 @@ A table with captions with non-default spaced attributes and a table row
 !! wikitext
 {|
 |+style="color: red;"|caption2
-|+ style="color: red;"| caption3
+|+ style="color: red;"|caption3
 |-
-| foo
+|foo
 |}
 !! html
 <table>
 <caption style="color: red;">caption2
 </caption>
-<caption style="color: red;"> caption3
+<caption style="color: red;">caption3
 </caption>
 <tr>
-<td> foo
+<td>foo
 </td></tr></table>
 
 !! end
@@ -6192,23 +6124,23 @@ A table with captions with non-default spaced attributes and a table row
 Table td-cell syntax variations
 !! wikitext
 {|
-| foo bar foo | baz
-| foo bar foo || baz
-| style='color:red;' | baz
-| style='color:red;' || baz
+|foo bar foo|baz
+|foo bar foo||baz
+|style='color:red;'|baz
+|style='color:red;'||baz
 |}
 !! html
 <table>
 <tr>
-<td> baz
+<td>baz
 </td>
-<td> foo bar foo </td>
-<td> baz
+<td>foo bar foo</td>
+<td>baz
 </td>
-<td style="color:red;"> baz
+<td style="color:red;">baz
 </td>
-<td> style='color:red;' </td>
-<td> baz
+<td>style='color:red;'</td>
+<td>baz
 </td></tr></table>
 
 !! end
@@ -6217,19 +6149,19 @@ Table td-cell syntax variations
 Simple table
 !! wikitext
 {|
-| 1 || 2
+|1||2
 |-
-| 3 || 4
+|3||4
 |}
 !! html
 <table>
 <tr>
-<td> 1 </td>
-<td> 2
+<td>1</td>
+<td>2
 </td></tr>
 <tr>
-<td> 3 </td>
-<td> 4
+<td>3</td>
+<td>4
 </td></tr></table>
 
 !! end
@@ -6238,17 +6170,17 @@ Simple table
 Simple table but with multiple dashes for row wikitext
 !! wikitext
 {|
-| foo
+|foo
 |-----
-| bar
+|bar
 |}
 !! html
 <table>
 <tr>
-<td> foo
+<td>foo
 </td></tr>
 <tr>
-<td> bar
+<td>bar
 </td></tr></table>
 
 !! end
@@ -6259,67 +6191,67 @@ Multiplication table
 {| border="1" cellpadding="2"
 |+Multiplication table
 |-
-! &times; !! 1 !! 2 !! 3
+!&times;!!1!!2!!3
 |-
-! 1
-| 1 || 2 || 3
+!1
+|1||2||3
 |-
-! 2
-| 2 || 4 || 6
+!2
+|2||4||6
 |-
-! 3
-| 3 || 6 || 9
+!3
+|3||6||9
 |-
-! 4
-| 4 || 8 || 12
+!4
+|4||8||12
 |-
-! 5
-| 5 || 10 || 15
+!5
+|5||10||15
 |}
 !! html
 <table border="1" cellpadding="2">
 <caption>Multiplication table
 </caption>
 <tr>
-<th> &#215; </th>
-<th> 1 </th>
-<th> 2 </th>
-<th> 3
+<th>&#215;</th>
+<th>1</th>
+<th>2</th>
+<th>3
 </th></tr>
 <tr>
-<th> 1
+<th>1
 </th>
-<td> 1 </td>
-<td> 2 </td>
-<td> 3
+<td>1</td>
+<td>2</td>
+<td>3
 </td></tr>
 <tr>
-<th> 2
+<th>2
 </th>
-<td> 2 </td>
-<td> 4 </td>
-<td> 6
+<td>2</td>
+<td>4</td>
+<td>6
 </td></tr>
 <tr>
-<th> 3
+<th>3
 </th>
-<td> 3 </td>
-<td> 6 </td>
-<td> 9
+<td>3</td>
+<td>6</td>
+<td>9
 </td></tr>
 <tr>
-<th> 4
+<th>4
 </th>
-<td> 4 </td>
-<td> 8 </td>
-<td> 12
+<td>4</td>
+<td>8</td>
+<td>12
 </td></tr>
 <tr>
-<th> 5
+<th>5
 </th>
-<td> 5 </td>
-<td> 10 </td>
-<td> 15
+<td>5</td>
+<td>10</td>
+<td>15
 </td></tr></table>
 
 !! end
@@ -6328,13 +6260,13 @@ Multiplication table
 Accept "||" in table headings
 !! wikitext
 {|
-!h1 || h2
+!h1||h2
 |}
 !! html
 <table>
 <tr>
-<th>h1 </th>
-<th> h2
+<th>h1</th>
+<th>h2
 </th></tr></table>
 
 !! end
@@ -6343,12 +6275,12 @@ Accept "||" in table headings
 Accept "!!" in table data
 !! wikitext
 {|
-| Foo!! ||
+|Foo!!||
 |}
 !! html
 <table>
 <tr>
-<td> Foo!! </td>
+<td>Foo!!</td>
 <td>
 </td></tr></table>
 
@@ -6362,13 +6294,13 @@ Accept "!!" in table data
 Accept "||" in indented table headings
 !! wikitext
 :{|
-!h1 || h2
+!h1||h2
 |}
 !! html
 <dl><dd><table>
 <tr>
-<th>h1 </th>
-<th> h2
+<th>h1</th>
+<th>h2
 </th></tr></table></dd></dl>
 
 !! end
@@ -6420,15 +6352,12 @@ Accept "!!" in table data of mixed wikitext / html syntax
 !a
 <tr><td>b!!c</td></tr>
 |}
-!! html+tidy
+!! html/php+tidy
 <table>
-<tr>
-<th>a</th>
-</tr>
-<tr>
-<td>b!!c</td>
-</tr>
-</table>
+<tbody><tr>
+<th>a
+</th></tr><tr><td>b!!c</td></tr>
+</tbody></table>
 !! html/parsoid
 <table>
 <tbody><tr><th>a</th></tr>
@@ -6446,9 +6375,9 @@ Accept empty attributes in td/th cells (td/th cells starting with leading ||)
 !! html
 <table>
 <tr>
-<th> h1
+<th>h1
 </th>
-<td> a
+<td>a
 </td></tr></table>
 
 !! end
@@ -6458,13 +6387,13 @@ Accept "| !" at start of line in tables (ignore !-attribute)
 !! wikitext
 {|
 |-
-| !style="color:red" | bar
+|!style="color:red"|bar
 |}
 !! html
 <table>
 
 <tr>
-<td> bar
+<td>bar
 </td></tr></table>
 
 !!end
@@ -6477,8 +6406,8 @@ Allow +/- in 2nd and later cells in a row, in 1st cell when td-attrs are present
 |style='color:red;'|+1
 |style='color:blue;'|-1
 |-
-| 1 || 2 || 3
-| 1 ||+2 ||-3
+|1||2||3
+|1||+2||-3
 |-
 | +1
 | -1
@@ -6492,18 +6421,18 @@ Allow +/- in 2nd and later cells in a row, in 1st cell when td-attrs are present
 <td style="color:blue;">-1
 </td></tr>
 <tr>
-<td> 1 </td>
-<td> 2 </td>
-<td> 3
+<td>1</td>
+<td>2</td>
+<td>3
 </td>
-<td> 1 </td>
-<td>+2 </td>
+<td>1</td>
+<td>+2</td>
 <td>-3
 </td></tr>
 <tr>
-<td> +1
+<td>+1
 </td>
-<td> -1
+<td>-1
 </td></tr></table>
 
 !!end
@@ -6512,26 +6441,26 @@ Allow +/- in 2nd and later cells in a row, in 1st cell when td-attrs are present
 Table rowspan
 !! wikitext
 {| border=1
-| Cell 1, row 1
-|rowspan=2| Cell 2, row 1 (and 2)
-| Cell 3, row 1
+|Cell 1, row 1
+|rowspan=2|Cell 2, row 1 (and 2)
+|Cell 3, row 1
 |-
-| Cell 1, row 2
-| Cell 3, row 2
+|Cell 1, row 2
+|Cell 3, row 2
 |}
 !! html
 <table border="1">
 <tr>
-<td> Cell 1, row 1
+<td>Cell 1, row 1
 </td>
-<td rowspan="2"> Cell 2, row 1 (and 2)
+<td rowspan="2">Cell 2, row 1 (and 2)
 </td>
-<td> Cell 3, row 1
+<td>Cell 3, row 1
 </td></tr>
 <tr>
-<td> Cell 1, row 2
+<td>Cell 1, row 2
 </td>
-<td> Cell 3, row 2
+<td>Cell 3, row 2
 </td></tr></table>
 
 !! end
@@ -6552,7 +6481,7 @@ Nested table
 !! html
 <table border="1">
 <tr>
-<td> &#945;
+<td>&#945;
 </td>
 <td>
 <table bgcolor="#ABCDEF" border="2">
@@ -6597,7 +6526,7 @@ Table cell attributes: Pipes protected by nowikis should be treated as a plain c
 </td>
 <td title="foo&#124;">bar
 </td>
-<td> title="foo|" bar
+<td>title="foo|" bar
 </td></tr></table>
 
 !! html/parsoid
@@ -6630,24 +6559,24 @@ parsoid=wt2html,html2html
 !! html/parsoid
 <table><tbody>
 <tr>
-<td data-parsoid='{"startTagSrc":"| ","attrSepSrc":"|","autoInsertedEnd":true}'>[<a rel="mw:ExtLink" href="ftp://%7Cx" data-parsoid='{"stx":"url","a":{"href":"ftp://%7Cx"},"sa":{"href":"ftp://|x"}}'>ftp://%7Cx</a></td><td data-parsoid='{"stx":"row","autoInsertedEnd":true}'>]" onmouseover="alert(document.cookie)">test</td></tr></tbody></table>
+<td data-parsoid='{"startTagSrc":"| ","attrSepSrc":"|","autoInsertedEnd":true}'>[<a rel="mw:ExtLink" class="external free" href="ftp://%7Cx" data-parsoid='{"stx":"url","a":{"href":"ftp://%7Cx"},"sa":{"href":"ftp://|x"}}'>ftp://%7Cx</a></td><td data-parsoid='{"stx":"row","autoInsertedEnd":true}'>]" onmouseover="alert(document.cookie)">test</td></tr></tbody></table>
 !! end
 
 !! test
 Element attributes with double ! should not be broken up by <th>
 !! wikitext
 {|
-! hi <div class="!!">ha</div> ho
+!hi <div class="!!">ha</div> ho
 |}
 !! html/php
 <table>
 <tr>
-<th> hi <div class="!!">ha</div> ho
+<th>hi <div class="!!">ha</div> ho
 </th></tr></table>
 
 !! html/parsoid
 <table>
-<tbody><tr><th> hi <div class="!!" data-parsoid='{"stx":"html"}'>ha</div> ho</th></tr>
+<tbody><tr><th>hi <div class="!!" data-parsoid='{"stx":"html"}'>ha</div> ho</th></tr>
 </tbody></table>
 !! end
 
@@ -6655,17 +6584,17 @@ Element attributes with double ! should not be broken up by <th>
 ! and || in element attributes should not be parsed as <th>/<td>
 !! wikitext
 {|
-| <div style="color: red !important;" data-contrived="put this here ||">hi</div>
+|<div style="color: red !important;" data-contrived="put this here ||">hi</div>
 |}
 !! html/php
 <table>
 <tr>
-<td> <div style="color: red !important;" data-contrived="put this here &#124;&#124;">hi</div>
+<td><div style="color: red !important;" data-contrived="put this here &#124;&#124;">hi</div>
 </td></tr></table>
 
 !! html/parsoid
 <table>
-<tbody><tr><td> <div style="color: red !important;" data-contrived="put this here ||" data-parsoid='{"stx":"html"}'>hi</div></td></tr>
+<tbody><tr><td><div style="color: red !important;" data-contrived="put this here ||" data-parsoid='{"stx":"html"}'>hi</div></td></tr>
 </tbody></table>
 !! end
 
@@ -6676,18 +6605,18 @@ Element attributes with double ! should not be broken up by <th>
 parsoid=wt2html
 !! wikitext
 {|
-| style="color: red !important;" data-contrived="put this here ||" | foo
+|style="color: red !important;" data-contrived="put this here ||"|foo
 |}
 !! html/php
 <table>
 <tr>
-<td> style="color: red !important;" data-contrived="put this here </td>
-<td> foo
+<td>style="color: red !important;" data-contrived="put this here</td>
+<td>foo
 </td></tr></table>
 
 !! html/parsoid
 <table>
-<tbody><tr><td> style="color: red !important;" data-contrived="put this here </td><td data-parsoid='{"stx":"row","a":{"\"":null},"sa":{"\"":""},"autoInsertedEnd":true}'> foo</td></tr>
+<tbody><tr><td>style="color: red !important;" data-contrived="put this here</td><td data-parsoid='{"stx":"row","a":{"\"":null},"sa":{"\"":""},"autoInsertedEnd":true}'>foo</td></tr>
 </tbody></table>
 !! end
 
@@ -6719,7 +6648,7 @@ Don't break on | in extension attribute in template
 
 <references />
 !! html/parsoid
-<p><span 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></span></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="./Main_Page#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>
 !! end
@@ -6733,22 +6662,22 @@ Invalid text in table attributes should be discarded
 parsoid=wt2html
 !! wikitext
 {| <span>boo</span> style='border:1px solid black'
-|  <span>boo</span> style='color:blue'  | 1
-|<span>boo</span> style='color:blue'| 2
+|  <span>boo</span> style='color:blue'  |1
+|<span>boo</span> style='color:blue'|2
 |}
 !! html/php
 <table style="border:1px solid black">
 <tr>
-<td style="color:blue"> 1
+<td style="color:blue">1
 </td>
-<td style="color:blue"> 2
+<td style="color:blue">2
 </td></tr></table>
 
 !! html/parsoid
 <table style="border:1px solid black">
 <tr>
-<td style="color:blue"> 1</td>
-<td style="color:blue"> 2</td>
+<td style="color:blue">1</td>
+<td style="color:blue">2</td>
 </tr>
 </table>
 !! end
@@ -6793,7 +6722,7 @@ parsoid={
 </td>
 <td style="color:red;">Foo
 </td>
-<td> style="color:red;"</td>
+<td>style="color:red;"</td>
 <td>Bar
 </td>
 <td style="color:red;">Foo
@@ -6894,7 +6823,7 @@ T107652: <ref>s in templates that also generate table cell attributes should be
 <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<span class="mw-ref" id="cite_ref-1" rel="dc:references" data-mw='{"name":"ref","body":{"id":"mw-reference-text-cite_note-1"},"attrs":{}}'><a href="./Main_Page#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></span></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","body":{"id":"mw-reference-text-cite_note-1"},"attrs":{}}'><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></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>
 !! end
@@ -6907,14 +6836,14 @@ parsoid=wt2html,html2html
 {|
 |-
 
-! foo
+!foo
 |}
 !! html/*
 <table>
 
 
 <tr>
-<th> foo
+<th>foo
 </th></tr></table>
 
 !! end
@@ -6927,14 +6856,14 @@ parsoid=wt2html,html2html
 {|
 
 |-
-| foo
+|foo
 |}
 !! html/*
 <table>
 
 
 <tr>
-<td> foo
+<td>foo
 </td></tr></table>
 
 !! end
@@ -6945,17 +6874,17 @@ Table attributes with empty value
 parsoid=wt2html,html2html
 !! wikitext
 {|
-| style=| hello
+| style=|hello
 |}
 !! html/php
 <table>
 <tr>
-<td style=""> hello
+<td style="">hello
 </td></tr></table>
 
 !! html/parsoid
 <table>
-<tbody><tr><td style=""> hello</td></tr>
+<tbody><tr><td style="">hello</td></tr>
 </tbody></table>
 !! end
 
@@ -6964,7 +6893,7 @@ Wikitext table with a lot of comments
 !! wikitext
 {|
 <!-- c0 -->
-| foo
+|foo
 <!-- c1 -->
 |-<!-- c2 -->
 <!-- c3 -->
@@ -6974,7 +6903,7 @@ Wikitext table with a lot of comments
 !! html
 <table>
 <tr>
-<td> foo
+<td>foo
 </td></tr>
 <tr>
 <td>
@@ -6987,18 +6916,18 @@ Wikitext table comments represented in parsoid dom
 !! wikitext
 {|<!--c1--><!--c2-->
 |-<!--c3-->
-| x
+|x
 |}
 !! html/php+tidy
 <table>
-<tr>
-<td>x</td>
-</tr>
-</table>
+
+<tbody><tr>
+<td>x
+</td></tr></tbody></table>
 !! html/parsoid
 <table><!--c1--><!--c2-->
 <tbody><tr data-parsoid='{"startTagSrc":"|-","autoInsertedEnd":true}'><!--c3-->
-<td data-parsoid='{"autoInsertedEnd":true}'> x</td></tr>
+<td data-parsoid='{"autoInsertedEnd":true}'>x</td></tr>
 </tbody></table>
 !! end
 
@@ -7024,14 +6953,14 @@ Table cell with a single comment
 !! wikitext
 {|
 | <!-- c1 -->
-| a
+|a
 |}
 !! html
 <table>
 <tr>
 <td>
 </td>
-<td> a
+<td>a
 </td></tr></table>
 
 !! end
@@ -7042,21 +6971,21 @@ Table-cell after a comment-only-empty-line
 {|
 |a
 <!--c1-->
-<!--c2-->| b
+<!--c2-->|b
 |}
 !! html
 <table>
 <tr>
 <td>a
 </td>
-<td> b
+<td>b
 </td></tr></table>
 
 !! html/parsoid
 <table>
 <tbody><tr data-parsoid='{"autoInsertedEnd":true,"autoInsertedStart":true}'><td data-parsoid='{"autoInsertedEnd":true}'>a</td>
 <!--c1-->
-<!--c2--><td data-parsoid='{"autoInsertedEnd":true}'> b</td></tr>
+<!--c2--><td data-parsoid='{"autoInsertedEnd":true}'>b</td></tr>
 </tbody></table>
 
 !! end
@@ -7065,21 +6994,21 @@ Table-cell after a comment-only-empty-line
 Build table with {{!}}
 !! wikitext
 {{{!}} class="wikitable"
-! header
-! second header
+!header
+!second header
 {{!}}- style="color:red;"
-{{!}} data {{!}}{{!}} style="color:red;" {{!}} second data
+{{!}}data{{!}}{{!}} style="color:red;" {{!}}second data
 {{!}}}
 !! html
 <table class="wikitable">
 <tr>
-<th> header
+<th>header
 </th>
-<th> second header
+<th>second header
 </th></tr>
 <tr style="color:red;">
-<td> data </td>
-<td style="color:red;"> second data
+<td>data</td>
+<td style="color:red;">second data
 </td></tr></table>
 
 !! end
@@ -7088,33 +7017,33 @@ Build table with {{!}}
 Build table with pipe as data
 !! wikitext
 {| class="wikitable"
-! header
-! second header
+!header
+!second header
 |- style="color:red;"
-| data || style="color:red;" | second data
+|data|| style="color:red;" |second data
 |-
-| style="color:red;" | data with | || style="color:red;" | second data with |
+| style="color:red;" |data with | || style="color:red;" | second data with |
 |-
-|| data with | ||| second data with |
+||data with | |||second data with |
 |}
 !! html
 <table class="wikitable">
 <tr>
-<th> header
+<th>header
 </th>
-<th> second header
+<th>second header
 </th></tr>
 <tr style="color:red;">
-<td> data </td>
-<td style="color:red;"> second data
+<td>data</td>
+<td style="color:red;">second data
 </td></tr>
 <tr>
-<td style="color:red;"> data with | </td>
-<td style="color:red;"> second data with |
+<td style="color:red;">data with |</td>
+<td style="color:red;">second data with |
 </td></tr>
 <tr>
-<td> data with | </td>
-<td> second data with |
+<td>data with |</td>
+<td>second data with |
 </td></tr></table>
 
 !! end
@@ -7123,25 +7052,25 @@ Build table with pipe as data
 Build table with wikilink
 !! wikitext
 {| class="wikitable"
-! header || second header
+!header||second header
 |- style="color:red;"
-| data [[Main Page|linktext]] || second data [[Main Page|linktext]]
+|data [[Main Page|linktext]]||second data [[Main Page|linktext]]
 |-
-| data || second data [[Main Page|link|text with pipe]]
+|data||second data [[Main Page|link|text with pipe]]
 |}
 !! html
 <table class="wikitable">
 <tr>
-<th> header </th>
-<th> second header
+<th>header</th>
+<th>second header
 </th></tr>
 <tr style="color:red;">
-<td> data <a href="/wiki/Main_Page" title="Main Page">linktext</a> </td>
-<td> second data <a href="/wiki/Main_Page" title="Main Page">linktext</a>
+<td>data <a href="/wiki/Main_Page" title="Main Page">linktext</a></td>
+<td>second data <a href="/wiki/Main_Page" title="Main Page">linktext</a>
 </td></tr>
 <tr>
-<td> data </td>
-<td> second data <a href="/wiki/Main_Page" title="Main Page">link|text with pipe</a>
+<td>data</td>
+<td>second data <a href="/wiki/Main_Page" title="Main Page">link|text with pipe</a>
 </td></tr></table>
 
 !! end
@@ -7164,7 +7093,7 @@ Wikitext table with html-syntax row
 !! end
 
 !! test
-Implicit <td> after a |-
+Fostered content in tables: Plain text
 !! options
 parsoid=wt2html,html2html
 !! wikitext
@@ -7179,7 +7108,10 @@ a
 </table>
 
 !! html/php+tidy
-<p>a</p>
+
+
+a
+<table></table>
 !! html/parsoid
 <p data-parsoid='{"fostered":true,"autoInsertedEnd":true}'>a</p><table>
 <tbody><tr data-parsoid='{"startTagSrc":"|-","autoInsertedEnd":true}'>
@@ -7188,7 +7120,7 @@ a
 !! end
 
 !! test
-Lists should be recognized in an implicit <td> context
+Fostered content in tables: Lists
 !! options
 parsoid=wt2html,html2html
 !! wikitext
@@ -7203,10 +7135,11 @@ parsoid=wt2html,html2html
 </table>
 
 !! html/php+tidy
-<ul>
-<li>a</li>
-</ul>
-!! html/parsoid
+<ul><li>a</li></ul><table>
+
+
+</table>
+!! html/parsoid
 <ul data-parsoid='{"fostered":true,"autoInsertedEnd":true}'><li>a</li></ul><table>
 <tbody><tr data-parsoid='{"startTagSrc":"|-","autoInsertedEnd":true}'>
 
@@ -7214,24 +7147,24 @@ parsoid=wt2html,html2html
 !! end
 
 !! test
-Table cells not properly parsed in an implicit-td context
+Template generated table cell with attributes
 !! wikitext
 {|
 |-
-{{table_attribs_4}} || a || b
+{{table_attribs_4}} ||a||b
 |}
 !! html/php+tidy
 <table>
-<tr>
+
+<tbody><tr>
 <td style="background-color:#DC241f;" width="10px"></td>
 <td>a</td>
-<td>b</td>
-</tr>
-</table>
+<td>b
+</td></tr></tbody></table>
 !! html/parsoid
 <table>
 <tbody><tr data-parsoid='{"startTagSrc":"|-","autoInsertedEnd":true}'>
-<td style="background-color:#DC241f;" width="10px" about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"autoInsertedEnd":true,"pi":[[]]}' data-mw='{"parts":[{"template":{"target":{"wt":"table_attribs_4","href":"./Template:Table_attribs_4"},"params":{},"i":0}}," || a || b"]}'> </td><td about="#mwt1"> a </td><td about="#mwt1"> b</td></tr>
+<td style="background-color:#DC241f;" width="10px" about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"autoInsertedEnd":true,"pi":[[]]}' data-mw='{"parts":[{"template":{"target":{"wt":"table_attribs_4","href":"./Template:Table_attribs_4"},"params":{},"i":0}}," ||a||b"]}'></td><td about="#mwt1">a</td><td about="#mwt1">b</td></tr>
 !! end
 
 !! test
@@ -7248,17 +7181,14 @@ parsoid=wt2html,wt2wt
 |}<b>quux</b>
 !! html+tidy
 <table>
-<tr>
-<td>foo</td>
-</tr>
-</table>
-<p>bar</p>
-<table>
-<tr>
-<td>baz</td>
-</tr>
-</table>
-<p><b>quux</b></p>
+<tbody><tr>
+<td>foo
+</td></tr></tbody></table><p> bar
+</p><table>
+<tbody><tr>
+<td>baz
+</td></tr></tbody></table><p><b>quux</b>
+</p>
 !! end
 
 !! test
@@ -7299,17 +7229,17 @@ Parsoid: Row-syntax table headings followed by comment & table cells
 parsoid=wt2html,wt2wt
 !! wikitext
 {|
-! foo || bar
-<!-- foo -->  || baz || quux
+!foo||bar
+<!-- foo -->  ||baz||quux
 |}
 !! html/php
 <table>
 <tr>
-<th> foo </th>
-<th> bar
+<th>foo</th>
+<th>bar
 </th>
-<td> baz </td>
-<td> quux
+<td>baz</td>
+<td>quux
 </td></tr></table>
 
 !! html/parsoid
@@ -7330,12 +7260,11 @@ foo
 |}
 !!html/php+tidy
 <table class="foo">
-<tr>
+<tbody><tr>
 <td class="bar">
-<p>foo</p>
-</td>
-</tr>
-</table>
+<p>foo
+</p>
+</td></tr></tbody></table>
 !!html/parsoid
 <table class="foo">
 <tr>
@@ -7409,24 +7338,28 @@ parsoid=html2wt
 |}
 !! html/php+tidy
 <table>
-<caption>Test</caption>
-<tr>
-<th>Month</th>
-<th>Savings</th>
-</tr>
+<caption>Test
+</caption>
+<tbody><tr>
+<th>Month
+</th>
+<th>Savings
+</th></tr>
 <tr>
-<td>January</td>
-<td>$100</td>
-</tr>
+<td>January
+</td>
+<td>$100
+</td></tr>
 <tr>
-<td>February</td>
-<td>$80</td>
-</tr>
+<td>February
+</td>
+<td>$80
+</td></tr>
 <tr>
-<td>Sum</td>
-<td>$180</td>
-</tr>
-</table>
+<td>Sum
+</td>
+<td>$180
+</td></tr></tbody></table>
 !! end
 
 # T137406: No whitespace in the HTML
@@ -8127,7 +8060,7 @@ Plain link to URL
 <p>[<a rel="nofollow" class="external autonumber" href="http://www.example.com">[1]</a>]
 </p>
 !! html/parsoid
-<p>[<a rel="mw:ExtLink" href="http://www.example.com"></a>]</p>
+<p>[<a rel="mw:ExtLink" class="external autonumber" href="http://www.example.com"></a>]</p>
 !! end
 
 !! test
@@ -8147,7 +8080,7 @@ Plain link to protocol-relative URL
 <p>[<a rel="nofollow" class="external autonumber" href="//www.example.com">[1]</a>]
 </p>
 !! html/parsoid
-<p>[<a rel="mw:ExtLink" href="//www.example.com"></a>]</p>
+<p>[<a rel="mw:ExtLink" class="external autonumber" href="//www.example.com"></a>]</p>
 !! end
 
 !! test
@@ -8190,7 +8123,7 @@ Piped link to URL: [[http://www.example.com|an example URL]]
 <p>Piped link to URL: [<a rel="nofollow" class="external text" href="http://www.example.com%7Can">example URL</a>]
 </p>
 !! html/parsoid
-<p>Piped link to URL: [<a rel="mw:ExtLink" href="http://www.example.com%7Can" data-parsoid='{"a":{"href":"http://www.example.com%7Can"},"sa":{"href":"http://www.example.com|an"}}'>example URL</a>]</p>
+<p>Piped link to URL: [<a rel="mw:ExtLink" class="external text" href="http://www.example.com%7Can" data-parsoid='{"a":{"href":"http://www.example.com%7Can"},"sa":{"href":"http://www.example.com|an"}}'>example URL</a>]</p>
 !! end
 
 !! test
@@ -8212,13 +8145,13 @@ parsoid=wt2html
 </p><p>[<a rel="nofollow" class="external free" href="http://www.example.com">http://www.example.com</a> 
 </p>
 !! html/parsoid
-<p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[http://www.example.com "},"2":{"wt":"123]"}},"i":0}}]}'>[<a rel="mw:ExtLink" href="http://www.example.com">http://www.example.com</a> </p>
+<p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[http://www.example.com "},"2":{"wt":"123]"}},"i":0}}]}'>[<a rel="mw:ExtLink" class="external free" href="http://www.example.com">http://www.example.com</a> </p>
 
-<p about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[[http://www.example.com |123]]"}},"i":0}}]}'>[<a rel="mw:ExtLink" href="http://www.example.com">|123</a>]</p>
+<p about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[[http://www.example.com |123]]"}},"i":0}}]}'>[<a rel="mw:ExtLink" class="external text" href="http://www.example.com">|123</a>]</p>
 
-<p>{{echo|[<a rel="mw:ExtLink" href="http://www.example.com" data-parsoid='{"targetOff":114,"contentOffsets":[114,118],"dsr":[90,119,24,1]}'>|123</a>}}</p>
+<p>{{echo|[<a rel="mw:ExtLink" class="external text" href="http://www.example.com" data-parsoid='{"targetOff":114,"contentOffsets":[114,118],"dsr":[90,119,24,1]}'>|123</a>}}</p>
 
-<p about="#mwt3" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[http://www.example.com "},"2":{"wt":"123]]"}},"i":0}}]}'>[<a rel="mw:ExtLink" href="http://www.example.com">http://www.example.com</a> </p>
+<p about="#mwt3" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[http://www.example.com "},"2":{"wt":"123]]"}},"i":0}}]}'>[<a rel="mw:ExtLink" class="external free" href="http://www.example.com">http://www.example.com</a> </p>
 !! end
 
 !! test
@@ -8239,11 +8172,9 @@ T2337: Escaped self-links should be bold
 title=[[Bug462]]
 !! wikitext
 [[Bu&#103;462]] [[Bug462]]
-!! html/php
+!! html/php+tidy
 <p><a class="mw-selflink selflink">Bu&#103;462</a> <a class="mw-selflink selflink">Bug462</a>
 </p>
-!! html/php+tidy
-<p><a class="mw-selflink selflink">Bug462</a> <a class="mw-selflink selflink">Bug462</a></p>
 !! html/parsoid
 <p><a rel="mw:WikiLink" href="./Bug462" title="Bug462">Bug462</a> <a rel="mw:WikiLink" href="./Bug462" title="Bug462">Bug462</a></p>
 !! end
@@ -8684,7 +8615,7 @@ parsoid=wt2html,wt2wt
 <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+tidy
+!! 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>
@@ -8796,8 +8727,8 @@ Interwiki links that cannot be represented in wiki syntax
 <p><a rel="mw:WikiLink/Interwiki" href="http://www.usemod.com/cgi-bin/mb.pl?ok" title="meatball:ok">meatball:ok</a>
 <a rel="mw:WikiLink/Interwiki" href="http://www.usemod.com/cgi-bin/mb.pl?ok#foo" title="meatball:ok">ok with fragment</a>
 <a rel="mw:WikiLink/Interwiki" href="http://www.usemod.com/cgi-bin/mb.pl?ok_as_well?" title="meatball:ok as well?">ok ending with ? mark</a>
-<a rel="mw:ExtLink" href="http://de.wikipedia.org/wiki/Foo?action=history">has query</a>
-<a rel="mw:ExtLink" href="http://de.wikipedia.org/wiki/#foo">is just fragment</a></p>
+<a rel="mw:ExtLink" class="external text" href="http://de.wikipedia.org/wiki/Foo?action=history">has query</a>
+<a rel="mw:ExtLink" class="external text" href="http://de.wikipedia.org/wiki/#foo">is just fragment</a></p>
 !! end
 
 !! test
@@ -9380,6 +9311,7 @@ language=is
 Redirect syntax under text isn't considered a redirect
 !! wikitext
 some text
+
 #redirect [[Main Page]]
 !! html/parsoid
 <p>some text</p>
@@ -9402,9 +9334,9 @@ Redirect followed by block on the same line
 !! options
 parsoid=wt2html
 !! wikitext
-#REDIRECT [[Main Page]]<!-- haha -->== hi ==
+#REDIRECT [[Main Page]]<!-- haha -->==hi==
 !! html/parsoid
-<link rel="mw:PageProp/redirect" href="./Main_Page"/><!-- haha --><h2 id="hi"> hi </h2>
+<link rel="mw:PageProp/redirect" href="./Main_Page"/><!-- haha --><h2 id="hi">hi</h2>
 !! end
 
 !! test
@@ -9475,8 +9407,9 @@ parsoid=wt2html
 
 <br/ >
 !! html+tidy
-<p><br /></p>
-<p><br /></p>
+<p><br />
+</p><p><br />
+</p>
 !! end
 
 !! test
@@ -9568,10 +9501,9 @@ foo <hr
 > bar
 !! html+tidy
 <hr />
-<hr />
-<p>foo</p>
-<hr />
-<p>bar</p>
+<hr /><p>
+foo </p><hr /><p> bar
+</p>
 !! end
 
 !! test
@@ -9620,8 +9552,8 @@ Horizontal ruler -- Supports content following dashes on same line
 <hr /> Foo
 
 !! html+tidy
-<hr />
-<p>Foo</p>
+<hr /><p> Foo
+</p>
 !! end
 
 ###
@@ -9631,11 +9563,11 @@ Horizontal ruler -- Supports content following dashes on same line
 Common list
 !! wikitext
 *Common list
-* item 2
+*item 2
 *item 3
 !! html
 <ul><li>Common list</li>
-<li> item 2</li>
+<li>item 2</li>
 <li>item 3</li></ul>
 
 !! end
@@ -9645,21 +9577,22 @@ Numbered list
 !! wikitext
 #Numbered list
 #item 2
-# item 3
+#item 3
 !! html
 <ol><li>Numbered list</li>
 <li>item 2</li>
-<li> item 3</li></ol>
+<li>item 3</li></ol>
 
 !! end
 
+# the switch from level 3 to ordered should not introduce a newline between
 !! test
 Mixed list
 !! wikitext
 *Mixed list
-*# with numbers
-** and bullets
-*# and numbers
+*#with numbers
+**and bullets
+*#and numbers
 *bullets again
 **bullet level 2
 ***bullet level 3
@@ -9669,13 +9602,13 @@ Mixed list
 **#Number on level 3
 *#number level 2
 *Level 1
-*** Level 3
-#** Level 3, but ordered
+***Level 3
+#**Level 3, but ordered
 !! html
 <ul><li>Mixed list
-<ol><li> with numbers</li></ol>
-<ul><li> and bullets</li></ul>
-<ol><li> and numbers</li></ol></li>
+<ol><li>with numbers</li></ol>
+<ul><li>and bullets</li></ul>
+<ol><li>and numbers</li></ol></li>
 <li>bullets again
 <ul><li>bullet level 2
 <ul><li>bullet level 3
@@ -9685,43 +9618,43 @@ Mixed list
 <li>Number on level 3</li></ol></li></ul>
 <ol><li>number level 2</li></ol></li>
 <li>Level 1
-<ul><li><ul><li> Level 3</li></ul></li></ul></li></ul>
-<ol><li><ul><li><ul><li> Level 3, but ordered</li></ul></li></ul></li></ol>
+<ul><li><ul><li>Level 3</li></ul></li></ul></li></ul>
+<ol><li><ul><li><ul><li>Level 3, but ordered</li></ul></li></ul></li></ol>
 
 !! end
 
 !! test
 1. Nested mixed wikitext and html list
 !! wikitext
-* hi
-* <ul><li>ho</li></ul>
-* hi
-** ho
+*hi
+*<ul><li>ho</li></ul>
+*hi
+**ho
 !! html/php
-<ul><li> hi</li>
-<li> <ul><li>ho</li></ul></li>
-<li> hi
-<ul><li> ho</li></ul></li></ul>
+<ul><li>hi</li>
+<li><ul><li>ho</li></ul></li>
+<li>hi
+<ul><li>ho</li></ul></li></ul>
 
 !! html/parsoid
-<ul><li> hi</li>
-<li> <ul data-parsoid='{"stx":"html"}'><li data-parsoid='{"stx":"html"}'>ho</li></ul></li>
-<li> hi
-<ul><li> ho</li></ul></li></ul>
+<ul><li>hi</li>
+<li><ul data-parsoid='{"stx":"html"}'><li data-parsoid='{"stx":"html"}'>ho</li></ul></li>
+<li>hi
+<ul><li>ho</li></ul></li></ul>
 !! end
 
 !! test
 2. Nested mixed wikitext and html list (incompatible)
 !! wikitext
-; hi
-: {{echo|<li>ho</li>}}
+;hi
+:{{echo|<li>ho</li>}}
 !! html/php
-<dl><dt> hi</dt>
-<dd> <li>ho</li></dd></dl>
+<dl><dt>hi</dt>
+<dd><li>ho</li></dd></dl>
 
 !! html/parsoid
-<dl><dt> hi</dt>
-<dd> <li about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"stx":"html","pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;li>ho&lt;/li>"}},"i":0}}]}'>ho</li></dd></dl>
+<dl><dt>hi</dt>
+<dd><li about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"stx":"html","pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;li>ho&lt;/li>"}},"i":0}}]}'>ho</li></dd></dl>
 !! end
 
 !! test
@@ -9793,24 +9726,24 @@ Nested lists 6 (both elements empty)
 !! test
 Nested lists 7 (skip initial nesting levels)
 !! wikitext
-*** foo
+***foo
 !! html
-<ul><li><ul><li><ul><li> foo</li></ul></li></ul></li></ul>
+<ul><li><ul><li><ul><li>foo</li></ul></li></ul></li></ul>
 
 !! end
 
 !! test
 Nested lists 8 (multiple nesting transitions)
 !! wikitext
-* foo
-*** bar
-** baz
-* boo
+*foo
+***bar
+**baz
+*boo
 !! html
-<ul><li> foo
-<ul><li><ul><li> bar</li></ul></li>
-<li> baz</li></ul></li>
-<li> boo</li></ul>
+<ul><li>foo
+<ul><li><ul><li>bar</li></ul></li>
+<li>baz</li></ul></li>
+<li>boo</li></ul>
 
 !! end
 
@@ -9851,60 +9784,61 @@ parsoid
 !! test
 List items are not parsed correctly following a <pre> block (T2785)
 !! wikitext
-* <pre>foo</pre>
-* <pre>bar</pre>
-* zar
+*<pre>foo</pre>
+*<pre>bar</pre>
+*zar
 !! html/php
-<ul><li> <pre>foo</pre></li>
-<li> <pre>bar</pre></li>
-<li> zar</li></ul>
+<ul><li><pre>foo</pre></li>
+<li><pre>bar</pre></li>
+<li>zar</li></ul>
 
 !! html/parsoid
-<ul><li> <pre typeof="mw:Extension/pre" about="#mwt2" data-mw='{"name":"pre","attrs":{},"body":{"extsrc":"foo"}}'>foo</pre></li>
-<li> <pre typeof="mw:Extension/pre" about="#mwt4" data-mw='{"name":"pre","attrs":{},"body":{"extsrc":"bar"}}'>bar</pre></li>
-<li> zar</li></ul>
+<ul><li><pre typeof="mw:Extension/pre" about="#mwt2" data-mw='{"name":"pre","attrs":{},"body":{"extsrc":"foo"}}'>foo</pre></li>
+<li><pre typeof="mw:Extension/pre" about="#mwt4" data-mw='{"name":"pre","attrs":{},"body":{"extsrc":"bar"}}'>bar</pre></li>
+<li>zar</li></ul>
 !! end
 
+# FIXME: Might benefit from a html/parsoid since this has a template
 !! test
 List items from template
 !! wikitext
 
 {{inner list}}
-* item 2
+*item 2
 
-* item 0
+*item 0
 {{inner list}}
-* item 2
+*item 2
 
-* item 0
-* notSOL{{inner list}}
-* item 2
+*item 0
+*notSOL{{inner list}}
+*item 2
 !! html
-<ul><li> item 1</li>
-<li> item 2</li></ul>
-<ul><li> item 0</li>
-<li> item 1</li>
-<li> item 2</li></ul>
-<ul><li> item 0</li>
-<li> notSOL</li>
-<li> item 1</li>
-<li> item 2</li></ul>
+<ul><li>item 1</li>
+<li>item 2</li></ul>
+<ul><li>item 0</li>
+<li>item 1</li>
+<li>item 2</li></ul>
+<ul><li>item 0</li>
+<li>notSOL</li>
+<li>item 1</li>
+<li>item 2</li></ul>
 
 !! end
 
 !! test
 List interrupted by empty line or heading
 !! wikitext
-* foo
+*foo
 
-** bar
-== A heading ==
-* Another list item
+**bar
+==A heading==
+*Another list item
 !! html
-<ul><li> foo</li></ul>
-<ul><li><ul><li> bar</li></ul></li></ul>
+<ul><li>foo</li></ul>
+<ul><li><ul><li>bar</li></ul></li></ul>
 <h2><span class="mw-headline" id="A_heading">A heading</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 heading">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
-<ul><li> Another list item</li></ul>
+<ul><li>Another list item</li></ul>
 
 !!end
 
@@ -9922,11 +9856,14 @@ Multiple list tags generated by templates
 </li>
 
 !! html+tidy
-<ul>
-<li>a</li>
-<li>b</li>
-<li>c</li>
-</ul>
+<li>a
+</li><li>b
+</li><li>c
+</li>
+!! html/parsoid
+<li about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"stx":"html","autoInsertedEnd":true,"dsr":[0,44,null,null],"pi":[[{"k":"1"}],[{"k":"1"}],[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;li>"}},"i":0}},"a\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;li>"}},"i":1}},"b\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;li>"}},"i":2}},"c"]}'>a
+</li><li about="#mwt1">b
+</li><li about="#mwt1" data-parsoid='{"stx":"html","autoInsertedEnd":true,"dsr":[null,44,null,0]}'>c</li>
 !!end
 
 !!test
@@ -9966,36 +9903,36 @@ Replacing whitespace with tabs still doesn't break the list (gerrit 78327)
 
 !!end
 
+# FIXME: Parsoid has a dedicated DOM pass to mimic this Tidy-specific li-hack
+# That pass could possibly be removed.
 !!test
-Test the li-hack
-(The PHP parser relies on Tidy for the hack)
+Test the li-hack (a hack from Tidy days, but doesn't work as advertised with Remex)
 !!options
 parsoid=wt2html,wt2wt
 !! wikitext
-* foo
-* <li>li-hack
-* {{echo|<li>templated li-hack}}
-* <!--foo--> <li> unsupported li-hack with preceding comments
+*foo
+*<li>li-hack
+*{{echo|<li>templated li-hack}}
+*<!--foo--><li> unsupported li-hack with preceding comments
 
 <ul>
 <li><li>not a li-hack
 </li>
 </ul>
 !! html+tidy
+<ul><li>foo</li>
+<li class="mw-empty-elt"></li><li>li-hack</li>
+<li class="mw-empty-elt"></li><li>templated li-hack</li>
+<li class="mw-empty-elt"></li><li> unsupported li-hack with preceding comments</li></ul>
 <ul>
-<li>foo</li>
-<li>li-hack</li>
-<li>templated li-hack</li>
-<li>unsupported li-hack with preceding comments</li>
-</ul>
-<ul>
-<li>not a li-hack</li>
+<li class="mw-empty-elt"></li><li>not a li-hack
+</li>
 </ul>
 !! html/parsoid
 <ul><li> foo</li>
-<li data-parsoid='{"stx":"html","autoInsertedEnd":true,"liHackSrc":"* "}'>li-hack</li>
-<li about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"stx":"html","autoInsertedEnd":true,,"pi":[[{"k":"1"}]]}' data-mw='{"parts":["* ",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;li>templated li-hack"}},"i":0}}]}'>templated li-hack</li>
-<li data-parsoid='{"autoInsertedEnd":true}'> <!--foo--> </li><li data-parsoid='{"stx":"html","autoInsertedEnd":true}'> unsupported li-hack with preceding comments</li></ul>
+<li data-parsoid='{"stx":"html","autoInsertedEnd":true,"liHackSrc":"*"}'>li-hack</li>
+<li about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"stx":"html","autoInsertedEnd":true,,"pi":[[{"k":"1"}]]}' data-mw='{"parts":["*",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;li>templated li-hack"}},"i":0}}]}'>templated li-hack</li>
+<li data-parsoid='{"autoInsertedEnd":true}'><!--foo--></li><li data-parsoid='{"stx":"html","autoInsertedEnd":true}'>unsupported li-hack with preceding comments</li></ul>
 
 <ul data-parsoid='{"stx":"html"}'>
 <li class="mw-empty-elt" data-parsoid='{"stx":"html","autoInsertedEnd":true}'></li><li data-parsoid='{"stx":"html"}'>not a li-hack
@@ -10009,24 +9946,26 @@ Parsoid: Make sure nested lists are serialized on their own line even if HTML co
 !! options
 parsoid
 !! wikitext
-# foo
-## bar
-* foo
-** bar
-: foo
-:: bar
+#foo
+##bar
+
+*foo
+**bar
+
+:foo
+::bar
 !! html
 <ol>
-<li> foo<ol>
-<li> bar</li>
+<li>foo<ol>
+<li>bar</li>
 </ol></li>
 </ol><ul>
-<li> foo<ul>
-<li> bar</li>
+<li>foo<ul>
+<li>bar</li>
 </ul></li>
 </ul><dl>
-<dd> foo<dl>
-<dd> bar</dd>
+<dd>foo<dl>
+<dd>bar</dd>
 </dl></dd>
 </dl>
 !! end
@@ -10048,101 +9987,103 @@ parsoid
 # tags (parse, minimize scope of fixup, and roundtrip back)
 # ------------------------------------------------------------------------
 
+# Remex and Parsoid output stems from list handling diffs because Parsoid & PHP parser.
+# Parsoid's list handling is more aware of block structure.
 !! test
 Unbalanced closing block tags break a list
-(php parser relies on Tidy to fix up)
 !! wikitext
 <div>
 *a</div><div>
 *b</div>
 !! html+tidy
 <div>
-<ul>
+<ul><li>a</li></ul></div><div>
+<li>b</li></div>
+!! html/parsoid
+<div><ul>
 <li>a</li>
-</ul>
-</div>
-<div>
-<ul>
+</ul></div>
+<div><ul>
 <li>b</li>
-</ul>
-</div>
+</ul></div>
 !! end
 
-# Parsoid fails this test, but it might be tricky to support properly.
-# See T70395.
 !! test
 Unbalanced closing non-block tags don't break a list
-(php parser relies on Tidy to fix up)
 !! wikitext
 <span>
 *a</span><span>
 *b</span>
 !! html/php+tidy
-<ul>
-<li><span>a</span></li>
-<li><span>b</span></li>
-</ul>
+<p><span>
+</span></p>
+<ul><li>a<span></span></li>
+<li>b</li></ul>
 !! html/parsoid
 <span>
 <ul>
-<li>a<span></span>
-</li>
-<li>b
-</li>
+<li>a<span></span></li>
+<li>b</li>
 </ul>
 </span>
 !! end
 
+# Parsoid does some post-dom-building cleanup
+# which is why its output differs from Remex.
 !! test
 Unclosed formatting tags that straddle lists are closed and reopened
-(php parser relies on Tidy to fix up)
 !! options
 parsoid=wt2html,wt2wt,html2html
 !! wikitext
-# <s> a
-# b </s>
+#<s> a
+#b </s>
 !! html/php+tidy
-<ol>
-<li><s>a</s></li>
-<li><s>b</s></li>
-</ol>
+<ol><li><s> a</s></li><s>
+</s><li><s>b </s></li></ol>
 !! html/parsoid
-<ol><li> <s> a</s></li>
-<li><s> b </s></li></ol>
+<ol><li><s> a</s></li>
+<li><s>b </s></li></ol>
 !! end
 
+# Output is ugly because of all the misnested tag fixups.
+# Remex is wrapping p-tags around empty elements.
+# Parsoid has special-case handling of this pattern of
+# wrapping lists in formatting tags.
+# FIXME: Should we remove this code from Parsoid? Or add
+# special support in Remex? If the latter, maybe just wait
+# for Parsoid to become the default parser.
 # See T70395.
 !!test
 1. List embedded in a formatting tag
 !! wikitext
 <small>
-* foo
+*foo
 </small>
 !! html/php+tidy
-<ul>
-<li><small>foo</small></li>
-</ul>
+<p><small>
+</small></p><small><ul><li>foo</li></ul></small><small></small><p><small></small>
+</p>
 !! html/parsoid
 <small>
 <ul>
-<li> foo</li>
+<li>foo</li>
 </ul>
 </small>
 !!end
 
-## Ugly Parsoid output here
-## Not sure what the right output is.
+# Output is ugly because of all the misnested tag fixups
+# Remex is wrapping p-tags around empty elements.
+# Parsoid has code that strips useless p-tags.
 !!test
-2. List embedded in a formatting tag
+2. List embedded in a formatting tag in a misnested way
 !! wikitext
 <small>
 *a
 *b</small>
 !! html/php+tidy
-<ul>
-<li><small>a</small></li>
-<li><small>b</small></li>
-</ul>
+<p><small>
+</small></p><small></small><ul><small><li>a</li>
+</small><li><small>b</small></li></ul>
 !! html/parsoid
 <small></small>
 <ul><small>
@@ -10152,31 +10093,6 @@ parsoid=wt2html,wt2wt,html2html
 </ul>
 !!end
 
-# Ugly Parsoid and PHP parser output here
-# Not sure if we want to make this a test!
-#
-## !!test
-## 3. Unclosed formatting tags in list elements
-## !! wikitext
-## *<small>a
-## *<small>b
-## !! html/php+tidy
-## <ul>
-## <li><small>a</small></li>
-## <li><small><small>b</small></small></li>
-## </ul>
-## !! html/parsoid
-## <ul>
-## <li><small>a</small></li>
-## <small>
-## <li><small>b</small></li>
-## </small></ul>
-## !!end
-
-# This is a bug in the PHP parser + tidy combination.
-# (The </tr> tag gets parsed as text and html-escaped by PHP,
-# and then fostered out of the table by tidy.)
-# We believe the Parsoid output to be correct.
 !! test
 Table with missing opening <tr> tag
 !! options
@@ -10188,10 +10104,9 @@ parsoid=wt2html,wt2wt
 </table>
 !! html+tidy
 <table>
-<tr>
-<td>foo</td>
+<tbody><tr><td>foo</td>
 </tr>
-</table>
+</tbody></table>
 !! end
 
 ###
@@ -10332,35 +10247,35 @@ Magic Word: {{CURRENTTIMESTAMP}}
 !! test
 Magic Words LOCAL (UTC)
 !! wikitext
-* {{LOCALMONTH}}
-* {{LOCALMONTH1}}
-* {{LOCALMONTHNAME}}
-* {{LOCALMONTHNAMEGEN}}
-* {{LOCALMONTHABBREV}}
-* {{LOCALDAY}}
-* {{LOCALDAY2}}
-* {{LOCALDAYNAME}}
-* {{LOCALYEAR}}
-* {{LOCALTIME}}
-* {{LOCALHOUR}}
-* {{LOCALWEEK}}
-* {{LOCALDOW}}
-* {{LOCALTIMESTAMP}}
-!! html
-<ul><li> 01</li>
-<li> 1</li>
-<li> January</li>
-<li> January</li>
-<li> Jan</li>
-<li> 1</li>
-<li> 01</li>
-<li> Thursday</li>
-<li> 1970</li>
-<li> 00:02</li>
-<li> 00</li>
-<li> 1</li>
-<li> 4</li>
-<li> 19700101000203</li></ul>
+*{{LOCALMONTH}}
+*{{LOCALMONTH1}}
+*{{LOCALMONTHNAME}}
+*{{LOCALMONTHNAMEGEN}}
+*{{LOCALMONTHABBREV}}
+*{{LOCALDAY}}
+*{{LOCALDAY2}}
+*{{LOCALDAYNAME}}
+*{{LOCALYEAR}}
+*{{LOCALTIME}}
+*{{LOCALHOUR}}
+*{{LOCALWEEK}}
+*{{LOCALDOW}}
+*{{LOCALTIMESTAMP}}
+!! html
+<ul><li>01</li>
+<li>1</li>
+<li>January</li>
+<li>January</li>
+<li>Jan</li>
+<li>1</li>
+<li>01</li>
+<li>Thursday</li>
+<li>1970</li>
+<li>00:02</li>
+<li>00</li>
+<li>1</li>
+<li>4</li>
+<li>19700101000203</li></ul>
 
 !! end
 
@@ -10669,11 +10584,9 @@ title=[['foo & bar = baz']]
 parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
 !! wikitext
 ''{{PAGENAME}}''
-!! html/php
+!! html+tidy
 <p><i>&#39;foo &#38; bar &#61; baz&#39;</i>
 </p>
-!! html+tidy
-<p><i>'foo &amp; bar = baz'</i></p>
 !! end
 
 !! test
@@ -10683,11 +10596,9 @@ title=[[*RFC 1234 http://example.com/]]
 parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
 !! wikitext
 {{PAGENAME}}
-!! html/php
+!! html+tidy
 <p>&#42;RFC&#32;1234 http&#58;//example.com/
 </p>
-!! html+tidy
-<p>*RFC 1234 http://example.com/</p>
 !! end
 
 !! test
@@ -10709,11 +10620,9 @@ title=[[*RFC 1234 http://example.com/]]
 parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true }
 !! wikitext
 {{PAGENAMEE}}
-!! html/php
+!! html+tidy
 <p>&#42;RFC_1234_http&#58;//example.com/
 </p>
-!! html+tidy
-<p>*RFC_1234_http://example.com/</p>
 !! end
 
 !! test
@@ -11053,7 +10962,7 @@ RFC 822
 <p><a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc822">RFC 822</a>
 </p>
 !! html/parsoid
-<p><a href="https://tools.ietf.org/html/rfc822" rel="mw:ExtLink">RFC 822</a></p>
+<p><a href="https://tools.ietf.org/html/rfc822" rel="mw:ExtLink" class="external text">RFC 822</a></p>
 !! end
 
 !! test
@@ -11064,7 +10973,7 @@ This is RFC 822 but thisRFC 822 is not RFC 822linked.
 <p>This is <a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc822">RFC 822</a> but thisRFC 822 is not RFC 822linked.
 </p>
 !! html/parsoid
-<p>This is <a href="https://tools.ietf.org/html/rfc822" rel="mw:ExtLink">RFC 822</a> but thisRFC 822 is not RFC 822linked.</p>
+<p>This is <a href="https://tools.ietf.org/html/rfc822" rel="mw:ExtLink" class="external text">RFC 822</a> but thisRFC 822 is not RFC 822linked.</p>
 !! end
 
 !! test
@@ -11079,7 +10988,7 @@ RFC
 822
 </p>
 !! html/parsoid
-<p><a href="https://tools.ietf.org/html/rfc822" rel="mw:ExtLink">RFC <span typeof="mw:Entity" data-parsoid='{"src":"&amp;nbsp;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#160;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#0160;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#xA0;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#Xa0;","srcContent":" "}'> </span> 822</a>
+<p><a href="https://tools.ietf.org/html/rfc822" rel="mw:ExtLink" class="external text">RFC <span typeof="mw:Entity" data-parsoid='{"src":"&amp;nbsp;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#160;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#0160;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#xA0;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#Xa0;","srcContent":" "}'> </span> 822</a>
 RFC
 822</p>
 !! end
@@ -11137,7 +11046,7 @@ PMID 1234
 <p><a class="external mw-magiclink-pmid" rel="nofollow" href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract">PMID 1234</a>
 </p>
 !! html/parsoid
-<p><a href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract" rel="mw:ExtLink">PMID 1234</a></p>
+<p><a href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract" rel="mw:ExtLink" class="external text">PMID 1234</a></p>
 !! end
 
 !! test
@@ -11148,7 +11057,7 @@ This is PMID 1234 but thisPMID 1234 is not PMID 1234linked.
 <p>This is <a class="external mw-magiclink-pmid" rel="nofollow" href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract">PMID 1234</a> but thisPMID 1234 is not PMID 1234linked.
 </p>
 !! html/parsoid
-<p>This is <a href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract" rel="mw:ExtLink">PMID 1234</a> but thisPMID 1234 is not PMID 1234linked.</p>
+<p>This is <a href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract" rel="mw:ExtLink" class="external text">PMID 1234</a> but thisPMID 1234 is not PMID 1234linked.</p>
 !! end
 
 !! test
@@ -11163,7 +11072,7 @@ PMID
 1234
 </p>
 !! html/parsoid
-<p><a href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract" rel="mw:ExtLink">PMID <span typeof="mw:Entity" data-parsoid='{"src":"&amp;nbsp;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#160;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#0160;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#xA0;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#Xa0;","srcContent":" "}'> </span> 1234</a>
+<p><a href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract" rel="mw:ExtLink" class="external text">PMID <span typeof="mw:Entity" data-parsoid='{"src":"&amp;nbsp;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#160;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#0160;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#xA0;","srcContent":" "}'> </span><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#Xa0;","srcContent":" "}'> </span> 1234</a>
 PMID
 1234</p>
 !! end
@@ -11182,7 +11091,7 @@ X[https://tools.ietf.org/html/rfc1234 foo]
 </p>
 !! html/parsoid
 <p>X<a rel="mw:WikiLink" href="./Special:BookSources/0978739256" title="Special:BookSources/0978739256">foo</a></p>
-<p>X<a rel="mw:ExtLink" href="https://tools.ietf.org/html/rfc1234">foo</a></p>
+<p>X<a rel="mw:ExtLink" class="external text" href="https://tools.ietf.org/html/rfc1234">foo</a></p>
 !! end
 
 !! test
@@ -11432,11 +11341,11 @@ Templates with templated name
 !! html
 <p>foo
 </p>
-<ul><li> item 1</li></ul>
+<ul><li>item 1</li></ul>
 
 !! html/parsoid
 <p about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"{{echo|echo}}","href":"./Template:Echo"},"params":{"1":{"wt":"foo"}},"i":0}}]}'>foo</p>
-<ul about="#mwt4" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"{{echo|inner list}} ","href":"./Template:Inner_list"},"params":{},"i":0}}]}'><li> item 1</li></ul>
+<ul about="#mwt4" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"{{echo|inner list}} ","href":"./Template:Inner_list"},"params":{},"i":0}}]}'><li>item 1</li></ul>
 !! end
 
 ## Regression test; the output here isn't really that interesting.
@@ -11474,12 +11383,7 @@ Template with thumb image (with link in description)
 This is a test template with parameter <div class="thumb tright"><div class="thumbinner" style="width:182px;"><a href="/index.php?title=Special:Upload&amp;wpDestFile=Noimage.png" class="new" title="File:Noimage.png">File:Noimage.png</a>  <div class="thumbcaption"><a href="/index.php?title=No_link&amp;action=edit&amp;redlink=1" class="new" title="No link (page does not exist)">link</a> <a href="/index.php?title=No_link&amp;action=edit&amp;redlink=1" class="new" title="No link (page does not exist)">caption</a></div></div></div>
 
 !! html+tidy
-<p>This is a test template with parameter</p>
-<div class="thumb tright">
-<div class="thumbinner" style="width:182px;"><a href="/index.php?title=Special:Upload&amp;wpDestFile=Noimage.png" class="new" title="File:Noimage.png">File:Noimage.png</a>
-<div class="thumbcaption"><a href="/index.php?title=No_link&amp;action=edit&amp;redlink=1" class="new" title="No link (page does not exist)">link</a> <a href="/index.php?title=No_link&amp;action=edit&amp;redlink=1" class="new" title="No link (page does not exist)">caption</a></div>
-</div>
-</div>
+<p>This is a test template with parameter </p><div class="thumb tright"><div class="thumbinner" style="width:182px;"><a href="/index.php?title=Special:Upload&amp;wpDestFile=Noimage.png" class="new" title="File:Noimage.png">File:Noimage.png</a>  <div class="thumbcaption"><a href="/index.php?title=No_link&amp;action=edit&amp;redlink=1" class="new" title="No link (page does not exist)">link</a> <a href="/index.php?title=No_link&amp;action=edit&amp;redlink=1" class="new" title="No link (page does not exist)">caption</a></div></div></div>
 !! html/parsoid
 <p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"paramtest","href":"./Template:Paramtest"},"params":{"param":{"wt":"[[Image:noimage.png|thumb|[[no link|link]] [[no link|caption]]]]"}},"i":0}}]}'>This is a test template with parameter </p><figure class="mw-default-size" typeof="mw:Error mw:Image/Thumb" about="#mwt1" data-mw='{"errors":[{"key":"apierror-filedoesnotexist","message":"This image does not exist."}]}'><a href="./File:Noimage.png" ><img resource="./File:Noimage.png" src="./Special:FilePath/Noimage.png" height="220" width="220"/></a><figcaption><a rel="mw:WikiLink" href="./No_link" title="No link">link</a> <a rel="mw:WikiLink" href="./No_link" title="No link">caption</a></figcaption></figure>
 !! end
@@ -11525,28 +11429,28 @@ T2553: link with two variables in a piped link
 Abort table cell attribute parsing on wikilink
 !! wikitext
 {|
-| testing [[one|two]] | three || four
-| testing one two | three || four
-| testing="[[one|two]]" | three || four
+|testing [[one|two]] |three||four
+|testing one two |three||four
+|testing="[[one|two]]" |three||four
 |}
 !! html/php
 <table>
 <tr>
-<td> testing <a href="/index.php?title=One&amp;action=edit&amp;redlink=1" class="new" title="One (page does not exist)">two</a> | three </td>
-<td> four
+<td>testing <a href="/index.php?title=One&amp;action=edit&amp;redlink=1" class="new" title="One (page does not exist)">two</a> |three</td>
+<td>four
 </td>
-<td> three </td>
-<td> four
+<td>three</td>
+<td>four
 </td>
-<td> testing="<a href="/index.php?title=One&amp;action=edit&amp;redlink=1" class="new" title="One (page does not exist)">two</a>" | three </td>
-<td> four
+<td>testing="<a href="/index.php?title=One&amp;action=edit&amp;redlink=1" class="new" title="One (page does not exist)">two</a>" |three</td>
+<td>four
 </td></tr></table>
 
 !! html/parsoid
 <table>
-<tbody><tr data-parsoid='{"autoInsertedEnd":true,"autoInsertedStart":true}'><td data-parsoid='{"autoInsertedEnd":true}'> testing <a rel="mw:WikiLink" href="./One" title="One" data-parsoid='{"stx":"piped","a":{"href":"./One"},"sa":{"href":"one"}}'>two</a> | three </td><td data-parsoid='{"stx":"row","autoInsertedEnd":true}'> four</td>
-<td data-parsoid='{"a":{"testing":null,"one":null,"two":null},"sa":{"testing":"","one":"","two":""},"autoInsertedEnd":true}'> three </td><td data-parsoid='{"stx":"row","autoInsertedEnd":true}'> four</td>
-<td> testing="<a rel="mw:WikiLink" href="./One" title="One" data-parsoid='{"stx":"piped","a":{"href":"./One"},"sa":{"href":"one"}}'>two</a>" | three </td><td data-parsoid='{"stx":"row","autoInsertedEnd":true}'> four</td></tr>
+<tbody><tr data-parsoid='{"autoInsertedEnd":true,"autoInsertedStart":true}'><td data-parsoid='{"autoInsertedEnd":true}'>testing <a rel="mw:WikiLink" href="./One" title="One" data-parsoid='{"stx":"piped","a":{"href":"./One"},"sa":{"href":"one"}}'>two</a> |three</td><td data-parsoid='{"stx":"row","autoInsertedEnd":true}'>four</td>
+<td data-parsoid='{"a":{"testing":null,"one":null,"two":null},"sa":{"testing":"","one":"","two":""},"autoInsertedEnd":true}'>three</td><td data-parsoid='{"stx":"row","autoInsertedEnd":true}'>four</td>
+<td>testing="<a rel="mw:WikiLink" href="./One" title="One" data-parsoid='{"stx":"piped","a":{"href":"./One"},"sa":{"href":"one"}}'>two</a>" |three</td><td data-parsoid='{"stx":"row","autoInsertedEnd":true}'>four</td></tr>
 </tbody></table>
 !! end
 
@@ -11554,11 +11458,11 @@ Abort table cell attribute parsing on wikilink
 Don't abort table cell attribute parsing if wikilink is found in template arg
 !! wikitext
 {|
-| Test {{#tag:ref|One two "[[three]]" four}}
+|Test {{#tag:ref|One two "[[three]]" four}}
 |}
 !! html/parsoid
 <table>
-<tbody><tr><td> Test <ref about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"#tag:ref","function":"tag"},"params":{"1":{"wt":"One two \"[[three]]\" four"}},"i":0}}]}'>One two "<a rel="mw:WikiLink" href="./Three" title="Three">three</a>" four</ref></td></tr>
+<tbody><tr><td>Test <ref about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"#tag:ref","function":"tag"},"params":{"1":{"wt":"One two \"[[three]]\" four"}},"i":0}}]}'>One two "<a rel="mw:WikiLink" href="./Three" title="Three">three</a>" four</ref></td></tr>
 </tbody></table>
 !! end
 
@@ -11653,6 +11557,17 @@ Template from main namespace
 </p>
 !! end
 
+!! test
+Template from non-includable namespace
+!! options
+wgNonincludableNamespaces=10
+!! wikitext
+{{echo|uh oh!}}
+!! html
+<p><a href="/wiki/Template:Echo" title="Template:Echo">Template:Echo</a>
+</p>
+!! end
+
 !! article
 Template:table
 !! text
@@ -11672,12 +11587,12 @@ foo {{table}}
 </p>
 <table>
 <tr>
-<td> 1 </td>
-<td> 2
+<td>1</td>
+<td>2
 </td></tr>
 <tr>
-<td> 3 </td>
-<td> 4
+<td>3</td>
+<td>4
 </td></tr></table>
 
 !! end
@@ -11692,12 +11607,12 @@ foo
 </p>
 <table>
 <tr>
-<td> 1 </td>
-<td> 2
+<td>1</td>
+<td>2
 </td></tr>
 <tr>
-<td> 3 </td>
-<td> 4
+<td>3</td>
+<td>4
 </td></tr></table>
 
 !! end
@@ -11763,21 +11678,13 @@ Templates with intersecting and overlapping ranges
 {{echo|{{!}}hi}}
 |}
 !! html/php+tidy
-<p>ha</p>
-<p>ho</p>
-<table>
-<tr>
-<td></td>
-</tr>
-<tr>
-<td>hi</td>
-</tr>
-</table>
-<table>
-<tr>
-<td></td>
-</tr>
-</table>
+<p>ha</p><table>
+
+</table><p>ho</p><table>
+
+<tbody><tr>
+<td>hi
+</td></tr></tbody></table>
 !! html/parsoid
 <p about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"stx":"html","autoInsertedEnd":true,"pi":[[{"k":"1"}],[{"k":"1"}],[{"k":"1"}]],"firstWikitextNode":"table"}' data-mw='{"parts":["{|",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"\n&lt;p>ha&lt;/p>"}},"i":0}},"\n","{|",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"\n&lt;p>ho&lt;/p>"}},"i":1}},"\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"{{!}}hi"}},"i":2}},"\n|}"]}'>ha</p><table about="#mwt1" typeof="mw:ExpandedAttrs" data-mw='{"attribs":[[{"txt":"","html":""},{"html":""}]]}'>
 
@@ -12023,32 +11930,32 @@ Includes and comments at SOL
 !! options
 parsoid=wt2html,html2html
 !! wikitext
-<!-- comment --><noinclude><!-- comment --></noinclude><!-- comment -->== hu ==
+<!-- comment --><noinclude><!-- comment --></noinclude><!-- comment -->==hu==
 
 <noinclude>
 some
-</noinclude>* stuff
-* here
+</noinclude>*stuff
+*here
 
-<includeonly>can have stuff</includeonly>=== here ===
+<includeonly>can have stuff</includeonly>===here===
 
 !! html/php
 <h2><span class="mw-headline" id="hu">hu</span></h2>
 <p>some
 </p>
-<ul><li> stuff</li>
-<li> here</li></ul>
+<ul><li>stuff</li>
+<li>here</li></ul>
 <h3><span class="mw-headline" id="here">here</span></h3>
 
 !! html/parsoid
-<!-- comment --><meta typeof="mw:Includes/NoInclude" data-parsoid='{"src":"&lt;noinclude>"}'/><!-- comment --><meta typeof="mw:Includes/NoInclude/End" data-parsoid='{"src":"&lt;/noinclude>"}'/><!-- comment --><h2 id="hu"> hu </h2>
+<!-- comment --><meta typeof="mw:Includes/NoInclude" data-parsoid='{"src":"&lt;noinclude>"}'/><!-- comment --><meta typeof="mw:Includes/NoInclude/End" data-parsoid='{"src":"&lt;/noinclude>"}'/><!-- comment --><h2 id="hu">hu</h2>
 
 <meta typeof="mw:Includes/NoInclude" data-parsoid='{"src":"&lt;noinclude>"}'/>
 <p>some</p>
-<meta typeof="mw:Includes/NoInclude/End" data-parsoid='{"src":"&lt;/noinclude>"}'/><ul><li> stuff</li>
-<li> here</li></ul>
+<meta typeof="mw:Includes/NoInclude/End" data-parsoid='{"src":"&lt;/noinclude>"}'/><ul><li>stuff</li>
+<li>here</li></ul>
 
-<meta typeof="mw:Includes/IncludeOnly" data-parsoid='{"src":"&lt;includeonly>can have stuff&lt;/includeonly>"}'/><meta typeof="mw:Includes/IncludeOnly/End" data-parsoid='{"src":""}'/><h3 id="here"> here </h3>
+<meta typeof="mw:Includes/IncludeOnly" data-parsoid='{"src":"&lt;includeonly>can have stuff&lt;/includeonly>"}'/><meta typeof="mw:Includes/IncludeOnly/End" data-parsoid='{"src":""}'/><h3 id="here">here</h3>
 
 !! end
 
@@ -12267,10 +12174,10 @@ Preprocessor precedence 5: tplarg takes precedence over template
 !! wikitext
 {{Precedence5|Bullet}}
 !! html/php
-<ul><li> Bar</li></ul>
+<ul><li>Bar</li></ul>
 
 !! html/parsoid
-<ul typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"Precedence5","href":"./Template:Precedence5"},"params":{"1":{"wt":"Bullet"}},"i":0}}]}'><li> Bar</li></ul>
+<ul typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"Precedence5","href":"./Template:Precedence5"},"params":{"1":{"wt":"Bullet"}},"i":0}}]}'><li>Bar</li></ul>
 !! end
 
 !! test
@@ -12360,14 +12267,14 @@ Preprocessor precedence 9: groups of braces
 {{Preprocessor precedence 9|Four|Bullet|1|2}}
 !! html/php
 <dl><dt>4</dt>
-<dd> {Four}</dd>
+<dd>{Four}</dd>
 <dt>5</dt>
-<dd> </dd></dl>
-<ul><li> Bar</li></ul>
+<dd></dd></dl>
+<ul><li>Bar</li></ul>
 <dl><dt>6</dt>
-<dd> Four</dd>
+<dd>Four</dd>
 <dt>7</dt>
-<dd> {Bullet}</dd></dl>
+<dd>{Bullet}</dd></dl>
 
 !! html/parsoid
 <dl about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"Preprocessor precedence 9","href":"./Template:Preprocessor_precedence_9"},"params":{"1":{"wt":"Four"},"2":{"wt":"Bullet"},"3":{"wt":"1"},"4":{"wt":"2"}},"i":0}}]}'>
@@ -12405,21 +12312,21 @@ language=zh
 {{Preprocessor precedence 10|Three|raw2|Bullet|1|2}}
 !! html/php
 <dl><dt>1</dt>
-<dd> raw</dd>
+<dd>raw</dd>
 <dt>2</dt>
-<dd> -</dd></dl>
-<ul><li> Bar-</li></ul>
+<dd>-</dd></dl>
+<ul><li>Bar-</li></ul>
 <dl><dt>3</dt>
-<dd> -Three-</dd>
+<dd>-Three-</dd>
 <dt>4</dt>
-<dd> raw2</dd>
+<dd>raw2</dd>
 <dt>5</dt>
-<dd> -</dd></dl>
-<ul><li> Bar-</li></ul>
+<dd>-</dd></dl>
+<ul><li>Bar-</li></ul>
 <dl><dt>6</dt>
-<dd> -Three-</dd>
+<dd>-Three-</dd>
 <dt>7</dt>
-<dd> raw2</dd></dl>
+<dd>raw2</dd></dl>
 
 !! html/parsoid
 <dl about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"Preprocessor precedence 10","href":"./Template:Preprocessor_precedence_10"},"params":{"1":{"wt":"Three"},"2":{"wt":"raw2"},"3":{"wt":"Bullet"},"4":{"wt":"1"},"5":{"wt":"2"}},"i":0}}]}'>
@@ -12473,43 +12380,34 @@ Preprocessor precedence 12: broken language converter closed by brace.
 parsoid=wt2html
 !! wikitext
 This form breaks the template, which is unfortunate:
-* {{echo|foo-{bar}bat}}
+*{{echo|foo-{bar}bat}}
 
 But if the broken language converter markup is inside an extension
 tag, nothing bad happens:
-* <nowiki>foo-{bar}bat</nowiki>
-* {{echo|<nowiki>foo-{bar}bat</nowiki>}}
-* <pre>foo-{bar}bat</pre>
-* {{echo|<pre>foo-{bar}bat</pre>}}
+*<nowiki>foo-{bar}bat</nowiki>
+*{{echo|<nowiki>foo-{bar}bat</nowiki>}}
+*<pre>foo-{bar}bat</pre>
+*{{echo|<pre>foo-{bar}bat</pre>}}
 
 <tag>foo-{bar}bat</tag>
 {{echo|<tag>foo-{bar}bat</tag>}}
 
 !! html/php+tidy
-<p>This form breaks the template, which is unfortunate:</p>
-<ul>
-<li>{{echo|foo-{bar}bat}}</li>
-</ul>
-<p>But if the broken language converter markup is inside an extension tag, nothing bad happens:</p>
-<ul>
-<li>foo-{bar}bat</li>
-<li>foo-{bar}bat</li>
-<li>
-<pre>
-foo-{bar}bat
-</pre></li>
-<li>
-<pre>
-foo-{bar}bat
-</pre></li>
-</ul>
-<pre>
-'foo-{bar}bat'
+<p>This form breaks the template, which is unfortunate:
+</p>
+<ul><li>{{echo|foo-{bar}bat}}</li></ul>
+<p>But if the broken language converter markup is inside an extension
+tag, nothing bad happens:
+</p>
+<ul><li>foo-&#123;bar}bat</li>
+<li>foo-&#123;bar}bat</li>
+<li><pre>foo-{bar}bat</pre></li>
+<li><pre>foo-{bar}bat</pre></li></ul>
+<pre>'foo-{bar}bat'
 array (
 )
 </pre>
-<pre>
-'foo-{bar}bat'
+<pre>'foo-{bar}bat'
 array (
 )
 </pre>
@@ -12533,40 +12431,36 @@ Preprocessor precedence 13: broken language converter in external link
 !! options
 parsoid=wt2html
 !! wikitext
-* [http://example.com/-{foo Example in URL]
-* [http://example.com Example in -{link} description]
-* {{echo|[http://example.com/-{foo Breaks template, however]}}
+*[http://example.com/-{foo Example in URL]
+*[http://example.com Example in -{link} description]
+*{{echo|[http://example.com/-{foo Breaks template, however]}}
 !! html/php+tidy
-<ul>
-<li><a rel="nofollow" class="external text" href="http://example.com/-{foo">Example in URL</a></li>
+<ul><li><a rel="nofollow" class="external text" href="http://example.com/-{foo">Example in URL</a></li>
 <li><a rel="nofollow" class="external text" href="http://example.com">Example in -{link} description</a></li>
-<li>{{echo|<a rel="nofollow" class="external text" href="http://example.com/-{foo">Breaks template, however</a>}}</li>
-</ul>
+<li>{{echo|<a rel="nofollow" class="external text" href="http://example.com/-{foo">Breaks template, however</a>}}</li></ul>
 !! html/parsoid
 <ul>
-<li><a rel="mw:ExtLink" href="http://example.com/-{foo">Example in URL</a></li>
-<li><a rel="mw:ExtLink" href="http://example.com">Example in -{link} description</a></li>
-<li>{{echo|<a rel="mw:ExtLink" href="http://example.com/-{foo">Breaks template, however</a>}}</li>
+<li><a rel="mw:ExtLink" class="external text" href="http://example.com/-{foo">Example in URL</a></li>
+<li><a rel="mw:ExtLink" class="external text" href="http://example.com">Example in -{link} description</a></li>
+<li>{{echo|<a rel="mw:ExtLink" class="external text" href="http://example.com/-{foo">Breaks template, however</a>}}</li>
 </ul>
 !! end
 
 !! test
 Preprocessor precedence 14: broken language converter in comment
 !! wikitext
-* <!--{{foo}}--> ...should be ok
-* <!---{{foo}}--> ...extra dashes
-* {{echo|foo<!-- -{bar} -->bat}} ...should be ok
+*<!--{{foo}}-->...should be ok
+*<!---{{foo}}-->...extra dashes
+*{{echo|foo<!-- -{bar} -->bat}}...should be ok
 !! html/php+tidy
-<ul>
-<li>...should be ok</li>
+<ul><li>...should be ok</li>
 <li>...extra dashes</li>
-<li>foobat ...should be ok</li>
-</ul>
+<li>foobat...should be ok</li></ul>
 !! html/parsoid
 <ul>
-<li><!--{{foo}}--> ...should be ok</li>
-<li><!--&#x2D;{{foo}}--> ...extra dashes</li>
-<li><span about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo&lt;!-- -{bar} -->bat"}},"i":0}}]}'>foo</span><span about="#mwt1"><!-- &#x2D;{bar} --></span><span about="#mwt1">bat</span> ...should be ok</li>
+<li><!--{{foo}}-->...should be ok</li>
+<li><!--&#x2D;{{foo}}-->...extra dashes</li>
+<li><span about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo&lt;!-- -{bar} -->bat"}},"i":0}}]}'>foo</span><span about="#mwt1"><!-- &#x2D;{bar} --></span><span about="#mwt1">bat</span>...should be ok</li>
 </ul>
 !! end
 
@@ -12592,17 +12486,23 @@ __NOTOC__ __NOEDITSECTION__
 6
 !! html/php+tidy
 <h3><span id="1_foo.5Bbar_1"></span><span class="mw-headline" id="1_foo[bar_1">1 foo[bar 1</span></h3>
-<p>1</p>
+<p>1
+</p>
 <h3><span id="2_foo.5B.5Bbar_2"></span><span class="mw-headline" id="2_foo[[bar_2">2 foo[[bar 2</span></h3>
-<p>2</p>
+<p>2
+</p>
 <h3><span id="3_foo.7Bbar_3"></span><span class="mw-headline" id="3_foo{bar_3">3 foo{bar 3</span></h3>
-<p>3</p>
+<p>3
+</p>
 <h3><span id="4_foo.7B.7Bbar_4"></span><span class="mw-headline" id="4_foo{{bar_4">4 foo{{bar 4</span></h3>
-<p>4</p>
+<p>4
+</p>
 <h3><span id="5_foo.7B.7B.7Bbar_5"></span><span class="mw-headline" id="5_foo{{{bar_5">5 foo{{{bar 5</span></h3>
-<p>5</p>
+<p>5
+</p>
 <h3><span id="6_foo-.7Bbar_6"></span><span class="mw-headline" id="6_foo-{bar_6">6 foo-{bar 6</span></h3>
-<p>6</p>
+<p>6
+</p>
 !! html/parsoid
 <meta property="mw:PageProp/notoc"/> <meta property="mw:PageProp/noeditsection"/>
 <h3 id="1_foo[bar_1"><span id="1_foo.5Bbar_1" typeof="mw:FallbackId"></span>1 foo[bar 1</h3>
@@ -12646,6 +12546,32 @@ parsoid=wt2html
 <p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"hi {{}}"}},"i":0}}]}'>hi {{}}</p>
 !! end
 
+!! test
+Preprocessor precedence 18: another rightmost wins scenario
+!! options
+parsoid=wt2html
+!! wikitext
+{{ -{{{{1|tplarg}}} }} }-
+!! html/php
+<p>{{ -{tplarg }} }-
+</p>
+!! html/parsoid
+<p>{{ -{<span about="#mwt1" typeof="mw:Param" data-mw='{"parts":[{"templatearg":{"target":{"wt":"1"},"params":{"1":{"wt":"tplarg"}},"i":0}}]}'>tplarg</span> }} }-</p>
+!! end
+
+!! test
+Preprocessor precedence 19: break syntax
+!! options
+parsoid=wt2html
+!! wikitext
+-{{
+!! html/php
+<p>-{{
+</p>
+!! html/parsoid
+<p>-{{</p>
+!! end
+
 ###
 ### Token Stream Patcher tests
 ###
@@ -12775,9 +12701,7 @@ Templates: 2. Inside a block tag
 
 !! html+tidy
 <div>Foo</div>
-<blockquote>
-<p>Foo</p>
-</blockquote>
+<blockquote><p>Foo</p></blockquote>
 !!end
 
 !!test
@@ -12816,9 +12740,9 @@ Templates: P-wrapping: 1c. Templates on consecutive lines
 bar <div>baz</div>
 
 !! html+tidy
-<p>Foo</p>
-<p>bar</p>
-<div>baz</div>
+<p>Foo
+</p><p>
+bar </p><div>baz</div>
 !! end
 
 !!test
@@ -13112,17 +13036,17 @@ Templates: Support for templates generating attributes and content
 4. Entities and nowikis inside templated attributes should be handled correctly inside templated tables
 !! wikitext
 {|
-| {{table_attribs_6}} hi
+|{{table_attribs_6}} hi
 |}
 !! html/php
 <table>
 <tr>
-<td style="background: red;"> hi
+<td style="background: red;">hi
 </td></tr></table>
 
 !! html/parsoid
 <table>
-<tbody><tr><td style="background:  red;" typeof="mw:Transclusion" about="#mwt1" data-parsoid='{"autoInsertedEnd":true,"pi":[[]]}' data-mw='{"parts":["| ",{"template":{"target":{"wt":"table_attribs_6","href":"./Template:Table_attribs_6"},"params":{},"i":0}}," hi"]}'> hi</td></tr>
+<tbody><tr><td style="background:  red;" typeof="mw:Transclusion" about="#mwt1" data-parsoid='{"autoInsertedEnd":true,"pi":[[]]}' data-mw='{"parts":["|",{"template":{"target":{"wt":"table_attribs_6","href":"./Template:Table_attribs_6"},"params":{},"i":0}}," hi"]}'> hi</td></tr>
 </tbody></table>
 !! end
 
@@ -13241,12 +13165,13 @@ Templates: Wiki Tables: 1a. Fostering of entire template content
 a
 <tr><td></td></tr></table>
 
-!! html+tidy
-<p>a</p>
-<table>
-<tr>
-<td></td>
-</tr>
+!! html/php+tidy
+
+a
+<table><tbody><tr><td></td></tr></tbody></table>
+!! html/parsoid
+<p about="#mwt2" typeof="mw:Transclusion" data-parsoid='{"fostered":true,"autoInsertedEnd":true,"firstWikitextNode":"TABLE","pi":[[{"k":"1"}]]}' data-mw='{"parts":["{|\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"a"}},"i":0}},"\n|}"]}'>a</p><table about="#mwt2">
+
 </table>
 !! end
 
@@ -13266,14 +13191,18 @@ foo
 </div>
 <tr><td></td></tr></table>
 
-!! html+tidy
+!! html/php+tidy
 <div>
+<p>foo
+</p>
+</div><table>
+
+<tbody><tr><td></td></tr></tbody></table>
+!! html/parsoid
+<div about="#mwt3" typeof="mw:Transclusion" data-parsoid='{"stx":"html","fostered":true,"autoInsertedEnd":true,"firstWikitextNode":"TABLE","pi":[[{"k":"1"}],[{"k":"1"}]]}' data-mw='{"parts":["{|\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;div>"}},"i":0}},"\nfoo\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;/div>"}},"i":1}},"\n|}"]}'>
 <p>foo</p>
-</div>
-<table>
-<tr>
-<td></td>
-</tr>
+</div><table about="#mwt3">
+
 </table>
 !! end
 
 <div>b</div>
 <tr><td></td></tr></table>
 
-!! html+tidy
-<p>a</p>
-<div>b</div>
-<table>
-<tr>
-<td></td>
-</tr>
+!! html/php+tidy
+
+a
+<div>b</div><table>
+<tbody><tr><td></td></tr></tbody></table>
+!! html/parsoid
+<p about="#mwt2" typeof="mw:Transclusion" data-parsoid='{"fostered":true,"autoInsertedEnd":true,"firstWikitextNode":"TABLE","pi":[[{"k":"1"}]]}' data-mw='{"parts":["{|\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"a\n&lt;div>b&lt;/div>"}},"i":0}},"\n|}"]}'>a</p><div about="#mwt2">b</div><table about="#mwt2">
+
+
 </table>
 !! end
 
@@ -13366,7 +13297,7 @@ Templates: Wiki Tables: 7. Fosterable <ref>s should get fostered
 
 <references />
 !!html/parsoid
-<span about="#mwt2" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Transclusion  mw:Extension/ref" data-mw='{"parts":[{"template":{"target":{"wt":"PartialTable","href":"./Template:PartialTable"},"params":{},"i":0}},"&lt;ref>foo&lt;/ref>\n|}"]}'><a href="./Main_Page#cite_note-1"><span class="mw-reflink-text">[1]</span></a></span><table about="#mwt2">
+<sup about="#mwt2" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Transclusion  mw:Extension/ref" data-mw='{"parts":[{"template":{"target":{"wt":"PartialTable","href":"./Template:PartialTable"},"params":{},"i":0}},"&lt;ref>foo&lt;/ref>\n|}"]}'><a href="./Main_Page#cite_note-1"><span class="mw-reflink-text">[1]</span></a></sup><table about="#mwt2">
 <tbody>
 </tbody></table>
 
@@ -13445,78 +13376,10 @@ a<div>b{{echo|c</div>d}}e
 a<div>bc</div>de
 
 !! html+tidy
-<p>a</p>
-<div>bc</div>
-<p>de</p>
+<p>a</p><div>bc</div><p>de
+</p>
 !! end
 
-!!test
-Templates: Ugly templates: 1. Navbox template parses badly leading to table misnesting
-(Parsoid-centric)
-!! options
-parsoid
-!! wikitext
-{|
-|{{echo|foo</table>}}
-|bar
-|}
-!! html
-<table about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":["{|\n|",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo</table>"}},"i":0}},"\n|bar\n|}"]}'>
-
-<tbody>
-<tr>
-<td>foo</td></tr></tbody></table><span about="#mwt1">
-</span><span about="#mwt1">|bar</span><span about="#mwt1">
-|}</span>
-!!end
-
-!!test
-Templates: Ugly templates: 2. Navbox template parses badly leading to table misnesting
-(Parsoid-centric)
-!! options
-parsoid
-!! wikitext
-<table>
-  <tr>
-    <td>
-    <table>
-      <tr>
-        <td>1. {{echo|foo </table>}}</td>
-        <td> bar </td>
-        <td>2. {{echo|baz </table>}}</td>
-      </tr>
-      <tr>
-        <td>abc</td>
-      </tr>
-    </table>
-    </td>
-  </tr>
-  <tr>
-    <td>xyz</td>
-  </tr>
-</table>
-!! html
-<table about="#mwt2" typeof="mw:Transclusion" data-mw='{"parts":["<table>\n  <tr>\n    <td>\n    <table>\n      <tr>\n        <td>1. ",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo </table>"}},"i":0}},"</td>\n        <td> bar </td>\n        <td>2. ",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"baz </table>"}},"i":1}},"</td>\n      </tr>\n      <tr>\n        <td>abc</td>\n      </tr>\n    </table>\n    </td>\n  </tr>\n  <tr>\n    <td>xyz</td>\n  </tr>\n</table>"]}'>
-  <tbody><tr>
-    <td>
-    <table>
-      <tbody><tr>
-        <td>1. foo </td></tr></tbody></table></td>
-        <td> bar </td>
-        <td>2. baz </td></tr></tbody></table><span about="#mwt2">
-      </span><span about="#mwt2">
-      </span><span about="#mwt2">
-        </span><span about="#mwt2">abc</span><span about="#mwt2">
-      </span><span about="#mwt2">
-    </span><span about="#mwt2">
-    </span><span about="#mwt2">
-  </span><span about="#mwt2">
-  </span><span about="#mwt2">
-    </span><span about="#mwt2">xyz</span><span about="#mwt2">
-  </span><span about="#mwt2">
-</span>
-!!end
-
 !! test
 Templates: Ugly templates: 3. newline-only template parameter
 !! wikitext
@@ -14449,6 +14312,8 @@ Titles in unlinked images (T23454)
 !! html/php
 <p><img alt="stuff" src="http://example.com/images/3/3a/Foobar.jpg" title="stuff" width="1941" height="220" />
 </p>
+!! html/parsoid
+<p><figure-inline class="mw-default-size" typeof="mw:Image" data-mw='{"caption":"stuff"}'><span><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"/></span></figure-inline></p>
 !! end
 
 !! test
@@ -14545,18 +14410,11 @@ thumbsize=220
 123<div class="thumb tright"><div class="thumbinner" style="width:222px;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" width="220" height="25" class="thumbimage" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/330px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/440px-Foobar.jpg 2x" /></a>  <div class="thumbcaption"><div class="magnify"><a href="/wiki/File:Foobar.jpg" class="internal" title="Enlarge"></a></div></div></div></div>456
 
 !! html/php+tidy
-<p>123<a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>456</p>
-<p>123</p>
-<div class="floatright"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a></div>
-<p>456 123</p>
-<div class="thumb tright">
-<div class="thumbinner" style="width:222px;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" width="220" height="25" class="thumbimage" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/330px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/440px-Foobar.jpg 2x" /></a>
-<div class="thumbcaption">
-<div class="magnify"><a href="/wiki/File:Foobar.jpg" class="internal" title="Enlarge"></a></div>
-</div>
-</div>
-</div>
-<p>456</p>
+<p>123<a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>456
+</p><p>
+123</p><div class="floatright"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a></div><p>456
+123</p><div class="thumb tright"><div class="thumbinner" style="width:222px;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" width="220" height="25" class="thumbimage" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/330px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/440px-Foobar.jpg 2x" /></a>  <div class="thumbcaption"><div class="magnify"><a href="/wiki/File:Foobar.jpg" class="internal" title="Enlarge"></a></div></div></div></div><p>456
+</p>
 !! html/parsoid
 <p>123<figure-inline class="mw-default-size" typeof="mw:Image"><a 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"/></a></figure-inline>456</p>
 <p>123</p><figure class="mw-default-size mw-halign-right" typeof="mw:Image"><a 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"/></a></figure><p>456</p>
@@ -14703,7 +14561,7 @@ Image with link parameter, wgNoFollowLinks set to false
 [[Image:foobar.jpg|link=http://example.com/]]
 !! config
 wgNoFollowLinks=false
-!! html
+!! html/php
 <p><a href="http://example.com/"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 </p>
 !! end
@@ -14714,7 +14572,7 @@ Image with link parameter, wgNoFollowDomainExceptions
 [[Image:foobar.jpg|link=http://example.com/]]
 !! config
 wgNoFollowDomainExceptions='example.com'
-!! html
+!! html/php
 <p><a href="http://example.com/"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 </p>
 !! end
@@ -15195,7 +15053,7 @@ thumbsize=220
 <div class="thumb tright"><div class="thumbinner" style="width:222px;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="" src="http://example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" width="220" height="25" class="thumbimage" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/330px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/440px-Foobar.jpg 2x" /></a>  <div class="thumbcaption"><div class="magnify"><a href="/wiki/File:Foobar.jpg" class="internal" title="Enlarge"></a></div><a rel="nofollow" class="external free" href="http://example.com">http://example.com</a></div></div></div>
 
 !! html/parsoid
-<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption><a rel="mw:ExtLink" href="http://example.com">http://example.com</a></figcaption></figure>
+<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption><a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a></figcaption></figure>
 !! end
 
 !! test
@@ -15209,7 +15067,7 @@ parsoid=wt2html,wt2wt,html2html
 <div class="thumb tright"><div class="thumbinner" style="width:222px;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Alteration" src="http://example.com/images/thumb/3/3a/Foobar.jpg/220px-Foobar.jpg" width="220" height="25" class="thumbimage" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/330px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/440px-Foobar.jpg 2x" /></a>  <div class="thumbcaption"><div class="magnify"><a href="/wiki/File:Foobar.jpg" class="internal" title="Enlarge"></a></div><a rel="nofollow" class="external free" href="http://example.com">http://example.com</a></div></div></div>
 
 !! html/parsoid
-<figure class="mw-default-size" typeof="mw:Image/Thumb"><a href="./File:Foobar.jpg"><img alt="Alteration" 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"/></a><figcaption><a rel="mw:ExtLink" href="http://example.com">http://example.com</a></figcaption></figure>
+<figure class="mw-default-size" typeof="mw:Image/Thumb"><a href="./File:Foobar.jpg"><img alt="Alteration" 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"/></a><figcaption><a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a></figcaption></figure>
 !! end
 
 !! test
@@ -15269,7 +15127,7 @@ T3887: A RFC with a thumbnail
 <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>This is <a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc12354">RFC 12354</a></div></div></div>
 
 !! html/parsoid
-<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption>This is <a href="https://tools.ietf.org/html/rfc12354" rel="mw:ExtLink">RFC 12354</a></figcaption></figure>
+<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption>This is <a href="https://tools.ietf.org/html/rfc12354" rel="mw:ExtLink" class="external text">RFC 12354</a></figcaption></figure>
 !! end
 
 !! test
@@ -15280,7 +15138,7 @@ T3887: A mailto link with a thumbnail
 <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>Please <a rel="nofollow" class="external free" href="mailto:nobody@example.com">mailto:nobody@example.com</a></div></div></div>
 
 !! html/parsoid
-<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption>Please <a rel="mw:ExtLink" href="mailto:nobody@example.com">mailto:nobody@example.com</a></figcaption></figure>
+<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption>Please <a rel="mw:ExtLink" class="external free" href="mailto:nobody@example.com">mailto:nobody@example.com</a></figcaption></figure>
 !! end
 
 # Pending resolution to T2368
@@ -15430,6 +15288,10 @@ Image: caption containing leading space
 <figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption> bar</figcaption></figure>
 !!end
 
+# html/php output not have newlines after table, td, th, etc. because
+# Linker::makeThumbLink2() replaces the newlines with spaces since
+# the table is inside a caption.
+# FIXME: Verify if that circa 2004 fix is still required.
 !! test
 Image: caption containing a table
 !! options
@@ -15437,21 +15299,21 @@ parsoid=wt2html,wt2wt,html2html
 !! wikitext
 [[Image:Foobar.jpg|thumb|200px|This is an example image thumbnail caption with a table
 {|
-! Foo !! Bar
+!Foo!!Bar
 |-
-| Foo1 || Bar1
+|Foo1||Bar1
 |}
 and some more text.]]
 !! html/php
-<div class="thumb tright"><div class="thumbinner" style="width:202px;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="" src="http://example.com/images/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg" width="200" height="23" class="thumbimage" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/300px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/400px-Foobar.jpg 2x" /></a>  <div class="thumbcaption"><div class="magnify"><a href="/wiki/File:Foobar.jpg" class="internal" title="Enlarge"></a></div>This is an example image thumbnail caption with a table <table> <tr> <th> Foo </th> <th> Bar </th></tr> <tr> <td> Foo1 </td> <td> Bar1 </td></tr></table> and some more text.</div></div></div>
+<div class="thumb tright"><div class="thumbinner" style="width:202px;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="" src="http://example.com/images/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg" width="200" height="23" class="thumbimage" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/300px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/400px-Foobar.jpg 2x" /></a>  <div class="thumbcaption"><div class="magnify"><a href="/wiki/File:Foobar.jpg" class="internal" title="Enlarge"></a></div>This is an example image thumbnail caption with a table <table> <tr> <th>Foo</th> <th>Bar </th></tr> <tr> <td>Foo1</td> <td>Bar1 </td></tr></table> and some more text.</div></div></div>
 
 !! html/parsoid
 <figure typeof="mw:Image/Thumb"><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="23" width="200"/></a><figcaption>This is an example image thumbnail caption with a table
 <table>
 <tbody>
-<tr><th>Foo </th><th>Bar</th></tr>
+<tr><th>Foo</th><th>Bar</th></tr>
 <tr>
-<td>Foo1 </td>
+<td>Foo1</td>
 <td>Bar1</td></tr></tbody></table>and some more text.</figcaption></figure>
 !! end
 
@@ -15463,7 +15325,7 @@ T5090: External links other than http: in image captions
 <div class="thumb tright"><div class="thumbinner" style="width:202px;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="" src="http://example.com/images/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg" width="200" height="23" class="thumbimage" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/300px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/400px-Foobar.jpg 2x" /></a>  <div class="thumbcaption"><div class="magnify"><a href="/wiki/File:Foobar.jpg" class="internal" title="Enlarge"></a></div>This caption has <a rel="nofollow" class="external text" href="irc://example.net">irc</a> and <a rel="nofollow" class="external text" href="https://example.com">Secure</a> ext links in it.</div></div></div>
 
 !! html/parsoid
-<figure typeof="mw:Image/Thumb"><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="23" width="200"/></a><figcaption>This caption has <a rel="mw:ExtLink" href="irc://example.net">irc</a> and <a rel="mw:ExtLink" href="https://example.com">Secure</a> ext links in it.</figcaption></figure>
+<figure typeof="mw:Image/Thumb"><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="23" width="200"/></a><figcaption>This caption has <a rel="mw:ExtLink" class="external text" href="irc://example.net">irc</a> and <a rel="mw:ExtLink" class="external text" href="https://example.com">Secure</a> ext links in it.</figcaption></figure>
 !! end
 
 !! test
@@ -15799,7 +15661,7 @@ T93580: 1. Templated <ref> inside block images
 
 <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: <span 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></span></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="./Main_Page#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>
 !! end
@@ -15811,7 +15673,7 @@ T93580: 2. <ref> inside inline images
 
 <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;span 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\",\"body\":{\"id\":\"mw-reference-text-cite_note-1\"},\"attrs\":{}}&#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;/span>"}'><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\",\"body\":{\"id\":\"mw-reference-text-cite_note-1\"},\"attrs\":{}}&#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>
 
 <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>
 !! end
@@ -15823,7 +15685,7 @@ T93580: 3. Templated <ref> inside inline images
 
 <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;span 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;/span>"}'><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=\"./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>
 
 <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>
 !! end
@@ -15964,7 +15826,7 @@ Render invalid page names as plain text (T53090)
 [[.]]
 [[..]]
 [[foo././bar]]
-[[foo<a rel="mw:ExtLink" href="http://example.com"></a>xyz]]</p>
+[[foo<a rel="mw:ExtLink" class="external autonumber" href="http://example.com"></a>xyz]]</p>
 
 <p>[[<span typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"./../foo"}},"i":0}}]}'>./../foo</span>|bar]]
 [[<span typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"foo/."}},"i":0}}]}'>foo/.</span>|bar]]
@@ -16177,7 +16039,8 @@ Bar
 <p>Foo <link rel="mw:PageProp/Category" href="./Category:Baz"/> Bar</p>
 <p>Foo <link rel="mw:PageProp/Category" href="./Category:Baz"/> Bar</p>
 <p>Foo <link rel="mw:PageProp/Category" href="./Category:Baz"/> Bar</p>
-<p>Foo <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz"/> Bar <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[[Category:Baz]]"}},"i":0}}]}'/></p>
+<p>Foo <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz"/> Bar</p>
+<link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz"/> <link rel="mw:PageProp/Category" href="./Category:Baz" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"[[Category:Baz]]"}},"i":0}}]}'/>
 <link rel="mw:PageProp/Category" href="./Category:Baz"/>
 !! end
 
@@ -16560,7 +16423,7 @@ es:1 fr:1
 !! test
 Basic section headings
 !! wikitext
-== Headline 1 ==
+==Headline 1==
 Some text
 
 ==Headline 2==
@@ -16582,12 +16445,12 @@ Blah blah
 !! test
 Section headings with TOC
 !! wikitext
-== Headline 1 ==
-=== Subheadline 1 ===
-===== Skipping a level =====
-====== Skipping a level ======
+==Headline 1==
+===Subheadline 1===
+=====Skipping a level=====
+======Skipping a level======
 
-== Headline 2 ==
+==Headline 2==
 Some text
 ===Another headline===
 !! html
@@ -16629,10 +16492,10 @@ Some text
 TOC anchors don't collide
 !! wikitext
 __FORCETOC__
-== Headline 2 ==
-== Headline ==
-== Headline 2 ==
-== Headline ==
+==Headline 2==
+==Headline==
+==Headline 2==
+==Headline==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -16657,16 +16520,16 @@ Handling of sections up to level 6 and beyond
 !! options
 parsoid=wt2html
 !! wikitext
-= Level 1 Heading=
-== Level 2 Heading==
-=== Level 3 Heading===
-==== Level 4 Heading====
-===== Level 5 Heading=====
-====== Level 6 Heading======
-======= Level 7 Heading=======
-======== Level 8 Heading========
-========= Level 9 Heading=========
-========== Level 10 Heading==========
+=Level 1 Heading=
+==Level 2 Heading==
+===Level 3 Heading===
+====Level 4 Heading====
+=====Level 5 Heading=====
+======Level 6 Heading======
+=======Level 7 Heading=======
+========Level 8 Heading========
+=========Level 9 Heading=========
+==========Level 10 Heading==========
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -16681,10 +16544,10 @@ parsoid=wt2html
 <li class="toclevel-5 tocsection-5"><a href="#Level_5_Heading"><span class="tocnumber">1.1.1.1.1</span> <span class="toctext">Level 5 Heading</span></a>
 <ul>
 <li class="toclevel-6 tocsection-6"><a href="#Level_6_Heading"><span class="tocnumber">1.1.1.1.1.1</span> <span class="toctext">Level 6 Heading</span></a></li>
-<li class="toclevel-6 tocsection-7"><a href="#.3D_Level_7_Heading.3D"><span class="tocnumber">1.1.1.1.1.2</span> <span class="toctext">= Level 7 Heading=</span></a></li>
-<li class="toclevel-6 tocsection-8"><a href="#.3D.3D_Level_8_Heading.3D.3D"><span class="tocnumber">1.1.1.1.1.3</span> <span class="toctext">== Level 8 Heading==</span></a></li>
-<li class="toclevel-6 tocsection-9"><a href="#.3D.3D.3D_Level_9_Heading.3D.3D.3D"><span class="tocnumber">1.1.1.1.1.4</span> <span class="toctext">=== Level 9 Heading===</span></a></li>
-<li class="toclevel-6 tocsection-10"><a href="#.3D.3D.3D.3D_Level_10_Heading.3D.3D.3D.3D"><span class="tocnumber">1.1.1.1.1.5</span> <span class="toctext">==== Level 10 Heading====</span></a></li>
+<li class="toclevel-6 tocsection-7"><a href="#.3DLevel_7_Heading.3D"><span class="tocnumber">1.1.1.1.1.2</span> <span class="toctext">=Level 7 Heading=</span></a></li>
+<li class="toclevel-6 tocsection-8"><a href="#.3D.3DLevel_8_Heading.3D.3D"><span class="tocnumber">1.1.1.1.1.3</span> <span class="toctext">==Level 8 Heading==</span></a></li>
+<li class="toclevel-6 tocsection-9"><a href="#.3D.3D.3DLevel_9_Heading.3D.3D.3D"><span class="tocnumber">1.1.1.1.1.4</span> <span class="toctext">===Level 9 Heading===</span></a></li>
+<li class="toclevel-6 tocsection-10"><a href="#.3D.3D.3D.3DLevel_10_Heading.3D.3D.3D.3D"><span class="tocnumber">1.1.1.1.1.5</span> <span class="toctext">====Level 10 Heading====</span></a></li>
 </ul>
 </li>
 </ul>
@@ -16704,33 +16567,33 @@ parsoid=wt2html
 <h4><span class="mw-headline" id="Level_4_Heading">Level 4 Heading</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=4" title="Edit section: Level 4 Heading">edit</a><span class="mw-editsection-bracket">]</span></span></h4>
 <h5><span class="mw-headline" id="Level_5_Heading">Level 5 Heading</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=5" title="Edit section: Level 5 Heading">edit</a><span class="mw-editsection-bracket">]</span></span></h5>
 <h6><span class="mw-headline" id="Level_6_Heading">Level 6 Heading</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=6" title="Edit section: Level 6 Heading">edit</a><span class="mw-editsection-bracket">]</span></span></h6>
-<h6><span class="mw-headline" id=".3D_Level_7_Heading.3D">= Level 7 Heading=</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=7" title="Edit section: = Level 7 Heading=">edit</a><span class="mw-editsection-bracket">]</span></span></h6>
-<h6><span class="mw-headline" id=".3D.3D_Level_8_Heading.3D.3D">== Level 8 Heading==</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=8" title="Edit section: == Level 8 Heading==">edit</a><span class="mw-editsection-bracket">]</span></span></h6>
-<h6><span class="mw-headline" id=".3D.3D.3D_Level_9_Heading.3D.3D.3D">=== Level 9 Heading===</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=9" title="Edit section: === Level 9 Heading===">edit</a><span class="mw-editsection-bracket">]</span></span></h6>
-<h6><span class="mw-headline" id=".3D.3D.3D.3D_Level_10_Heading.3D.3D.3D.3D">==== Level 10 Heading====</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=10" title="Edit section: ==== Level 10 Heading====">edit</a><span class="mw-editsection-bracket">]</span></span></h6>
+<h6><span class="mw-headline" id=".3DLevel_7_Heading.3D">=Level 7 Heading=</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=7" title="Edit section: =Level 7 Heading=">edit</a><span class="mw-editsection-bracket">]</span></span></h6>
+<h6><span class="mw-headline" id=".3D.3DLevel_8_Heading.3D.3D">==Level 8 Heading==</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=8" title="Edit section: ==Level 8 Heading==">edit</a><span class="mw-editsection-bracket">]</span></span></h6>
+<h6><span class="mw-headline" id=".3D.3D.3DLevel_9_Heading.3D.3D.3D">===Level 9 Heading===</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=9" title="Edit section: ===Level 9 Heading===">edit</a><span class="mw-editsection-bracket">]</span></span></h6>
+<h6><span class="mw-headline" id=".3D.3D.3D.3DLevel_10_Heading.3D.3D.3D.3D">====Level 10 Heading====</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=10" title="Edit section: ====Level 10 Heading====">edit</a><span class="mw-editsection-bracket">]</span></span></h6>
 
 !! html/parsoid
-<h1 id="Level_1_Heading" data-parsoid='{}'> Level 1 Heading</h1>
-<h2 id="Level_2_Heading" data-parsoid='{}'> Level 2 Heading</h2>
-<h3 id="Level_3_Heading" data-parsoid='{}'> Level 3 Heading</h3>
-<h4 id="Level_4_Heading" data-parsoid='{}'> Level 4 Heading</h4>
-<h5 id="Level_5_Heading" data-parsoid='{}'> Level 5 Heading</h5>
-<h6 id="Level_6_Heading" data-parsoid='{}'> Level 6 Heading</h6>
-<h6 id="=_Level_7_Heading=" data-parsoid='{}'><span id=".3D_Level_7_Heading.3D" typeof="mw:FallbackId"></span>= Level 7 Heading=</h6>
-<h6 id="==_Level_8_Heading==" data-parsoid='{}'><span id=".3D.3D_Level_8_Heading.3D.3D" typeof="mw:FallbackId"></span>== Level 8 Heading==</h6>
-<h6 id="===_Level_9_Heading===" data-parsoid='{}'><span id=".3D.3D.3D_Level_9_Heading.3D.3D.3D" typeof="mw:FallbackId"></span>=== Level 9 Heading===</h6>
-<h6 id="====_Level_10_Heading====" data-parsoid='{}'><span id=".3D.3D.3D.3D_Level_10_Heading.3D.3D.3D.3D" typeof="mw:FallbackId"></span>==== Level 10 Heading====</h6>
+<h1 id="Level_1_Heading" data-parsoid='{}'>Level 1 Heading</h1>
+<h2 id="Level_2_Heading" data-parsoid='{}'>Level 2 Heading</h2>
+<h3 id="Level_3_Heading" data-parsoid='{}'>Level 3 Heading</h3>
+<h4 id="Level_4_Heading" data-parsoid='{}'>Level 4 Heading</h4>
+<h5 id="Level_5_Heading" data-parsoid='{}'>Level 5 Heading</h5>
+<h6 id="Level_6_Heading" data-parsoid='{}'>Level 6 Heading</h6>
+<h6 id="=Level_7_Heading=" data-parsoid='{}'><span id=".3DLevel_7_Heading.3D" typeof="mw:FallbackId"></span>=Level 7 Heading=</h6>
+<h6 id="==Level_8_Heading==" data-parsoid='{}'><span id=".3D.3DLevel_8_Heading.3D.3D" typeof="mw:FallbackId"></span>==Level 8 Heading==</h6>
+<h6 id="===Level_9_Heading===" data-parsoid='{}'><span id=".3D.3D.3DLevel_9_Heading.3D.3D.3D" typeof="mw:FallbackId"></span>===Level 9 Heading===</h6>
+<h6 id="====Level_10_Heading====" data-parsoid='{}'><span id=".3D.3D.3D.3DLevel_10_Heading.3D.3D.3D.3D" typeof="mw:FallbackId"></span>====Level 10 Heading====</h6>
 !! end
 
 !! test
 TOC regression (T11764)
 !! wikitext
-== title 1 ==
-=== title 1.1 ===
-==== title 1.1.1 ====
-=== title 1.2 ===
-== title 2 ==
-=== title 2.1 ===
+==title 1==
+===title 1.1===
+====title 1.1.1====
+===title 1.2===
+==title 2==
+===title 2.1===
 !! html
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -16782,12 +16645,12 @@ TOC with wgMaxTocLevel=3 (T8204)
 !! options
 wgMaxTocLevel=3
 !! wikitext
-== title 1 ==
-=== title 1.1 ===
-==== title 1.1.1 ====
-=== title 1.2 ===
-== title 2 ==
-=== title 2.1 ===
+==title 1==
+===title 1.1===
+====title 1.1.1====
+===title 1.2===
+==title 2==
+===title 2.1===
 !! html
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -16848,8 +16711,8 @@ wgMaxTocLevel=3
 !! test
 Resolving duplicate section names
 !! wikitext
-== Foo bar ==
-== Foo bar ==
+==Foo bar==
+==Foo bar==
 !! html
 <h2><span class="mw-headline" id="Foo_bar">Foo bar</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: Foo bar">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 <h2><span class="mw-headline" id="Foo_bar_2">Foo bar</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=2" title="Edit section: Foo bar">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
@@ -16859,8 +16722,8 @@ Resolving duplicate section names
 !! test
 Resolving duplicate section names with differing case (T12721)
 !! wikitext
-== Foo bar ==
-== Foo Bar ==
+==Foo bar==
+==Foo Bar==
 !! html
 <h2><span class="mw-headline" id="Foo_bar">Foo bar</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: Foo bar">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 <h2><span class="mw-headline" id="Foo_Bar_2">Foo Bar</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=2" title="Edit section: Foo Bar">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
@@ -16914,9 +16777,9 @@ Link inside a section heading
 TOC regression (T14077)
 !! wikitext
 __TOC__
-== title 1 ==
-=== title 1.1 ===
-== title 2 ==
+==title 1==
+===title 1.1===
+==title 2==
 !! html
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -16943,7 +16806,7 @@ http://example.com [[File:Foobar.jpg]]
 <p><a rel="nofollow" class="external free" href="http://example.com">http://example.com</a> <a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com">http://example.com</a> <figure-inline class="mw-default-size" typeof="mw:Image"><a 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"/></a></figure-inline></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a> <figure-inline class="mw-default-size" typeof="mw:Image"><a 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"/></a></figure-inline></p>
 !!end
 
 # Parsoid doesn't wt2wt this cleanly because it adds <nowiki>s.
@@ -16977,19 +16840,19 @@ Header with special characters (T27462)
 !! wikitext
 The tooltips shall not show entities to the user (ie. be double escaped)
 
-== text > text ==
+==text > text==
 section 1
 
-== text < text ==
+==text < text==
 section 2
 
-== text & text ==
+==text & text==
 section 3
 
-== text ' text ==
+==text ' text==
 section 4
 
-== text " text ==
+==text " text==
 section 5
 !! html/php
 <p>The tooltips shall not show entities to the user (ie. be double escaped)
@@ -17022,19 +16885,19 @@ section 5
 !! html/parsoid
 <p>The tooltips shall not show entities to the user (ie. be double escaped)</p>
 
-<h2 id="text_>_text"><span id="text_.3E_text" typeof="mw:FallbackId"></span> text > text </h2>
+<h2 id="text_>_text"><span id="text_.3E_text" typeof="mw:FallbackId"></span>text > text</h2>
 <p>section 1</p>
 
-<h2 id="text_&lt;_text"><span id="text_.3C_text" typeof="mw:FallbackId"></span> text &lt; text </h2>
+<h2 id="text_&lt;_text"><span id="text_.3C_text" typeof="mw:FallbackId"></span>text &lt; text</h2>
 <p>section 2</p>
 
-<h2 id="text_&amp;_text"><span id="text_.26_text" typeof="mw:FallbackId"></span> text &amp; text </h2>
+<h2 id="text_&amp;_text"><span id="text_.26_text" typeof="mw:FallbackId"></span>text &amp; text</h2>
 <p>section 3</p>
 
-<h2 id="text_'_text"><span id="text_.27_text" typeof="mw:FallbackId"></span> text ' text </h2>
+<h2 id="text_'_text"><span id="text_.27_text" typeof="mw:FallbackId"></span>text ' text</h2>
 <p>section 4</p>
 
-<h2 id='text_"_text'><span id="text_.22_text" typeof="mw:FallbackId"></span> text " text </h2>
+<h2 id='text_"_text'><span id="text_.22_text" typeof="mw:FallbackId"></span>text " text</h2>
 <p>section 5</p>
 !! end
 
@@ -17043,22 +16906,22 @@ Header with space, plus and underscore as entity
 !! wikitext
 Id should not contain + for spaces
 
-== Space between Text ==
+==Space between Text==
 section 1
 
-== Space-Entity&#32;between&#32;Text ==
+==Space-Entity&#32;between&#32;Text==
 section 2
 
-== Plus+between+Text ==
+==Plus+between+Text==
 section 3
 
-== Plus-Entity&#43;between&#43;Text ==
+==Plus-Entity&#43;between&#43;Text==
 section 4
 
-== Underscore_between_Text ==
+==Underscore_between_Text==
 section 5
 
-== Underscore-Entity&#95;between&#95;Text ==
+==Underscore-Entity&#95;between&#95;Text==
 section 6
 
 [[#Space between Text]]
@@ -17108,22 +16971,22 @@ section 6
 !! html/parsoid
 <p>Id should not contain + for spaces</p>
 
-<h2 id="Space_between_Text"> Space between Text </h2>
+<h2 id="Space_between_Text">Space between Text</h2>
 <p>section 1</p>
 
-<h2 id="Space-Entity_between_Text"> Space-Entity<span typeof="mw:Entity" data-parsoid='{"src":"&amp;#32;","srcContent":" "}'> </span>between<span typeof="mw:Entity" data-parsoid='{"src":"&amp;#32;","srcContent":" "}'> </span>Text </h2>
+<h2 id="Space-Entity_between_Text">Space-Entity<span typeof="mw:Entity" data-parsoid='{"src":"&amp;#32;","srcContent":" "}'> </span>between<span typeof="mw:Entity" data-parsoid='{"src":"&amp;#32;","srcContent":" "}'> </span>Text</h2>
 <p>section 2</p>
 
-<h2 id="Plus+between+Text"><span id="Plus.2Bbetween.2BText" typeof="mw:FallbackId"></span> Plus+between+Text </h2>
+<h2 id="Plus+between+Text"><span id="Plus.2Bbetween.2BText" typeof="mw:FallbackId"></span>Plus+between+Text</h2>
 <p>section 3</p>
 
-<h2 id="Plus-Entity+between+Text"><span id="Plus-Entity.2Bbetween.2BText" typeof="mw:FallbackId"></span> Plus-Entity<span typeof="mw:Entity" data-parsoid='{"src":"&amp;#43;","srcContent":"+"}'>+</span>between<span typeof="mw:Entity" data-parsoid='{"src":"&amp;#43;","srcContent":"+"}'>+</span>Text </h2>
+<h2 id="Plus-Entity+between+Text"><span id="Plus-Entity.2Bbetween.2BText" typeof="mw:FallbackId"></span>Plus-Entity<span typeof="mw:Entity" data-parsoid='{"src":"&amp;#43;","srcContent":"+"}'>+</span>between<span typeof="mw:Entity" data-parsoid='{"src":"&amp;#43;","srcContent":"+"}'>+</span>Text</h2>
 <p>section 4</p>
 
-<h2 id="Underscore_between_Text"> Underscore_between_Text </h2>
+<h2 id="Underscore_between_Text">Underscore_between_Text</h2>
 <p>section 5</p>
 
-<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>
+<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>
@@ -17172,13 +17035,13 @@ HTML headers vs TOC (T25393)
 (__NOEDITSECTION__ for clearer output, doesn't matter here)
 !! wikitext
 <h1>Header 1</h1>
-== Header 1.1 ==
-== Header 1.2 ==
+==Header 1.1==
+==Header 1.2==
 
 <h1>Header 2
 </h1>
-== Header 2.1 ==
-== Header 2.2 ==
+==Header 2.1==
+==Header 2.2==
 __NOEDITSECTION__
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
@@ -17201,19 +17064,20 @@ __NOEDITSECTION__
 <h1><span class="mw-headline" id="Header_1">Header 1</span></h1>
 <h2><span class="mw-headline" id="Header_1.1">Header 1.1</span></h2>
 <h2><span class="mw-headline" id="Header_1.2">Header 1.2</span></h2>
-<h1><span class="mw-headline" id="Header_2">Header 2</span></h1>
+<h1><span class="mw-headline" id="Header_2">Header 2
+</span></h1>
 <h2><span class="mw-headline" id="Header_2.1">Header 2.1</span></h2>
 <h2><span class="mw-headline" id="Header_2.2">Header 2.2</span></h2>
 
 !! html/parsoid
 <h1 id="Header_1" data-parsoid='{"stx":"html"}'>Header 1</h1>
-<h2 id="Header_1.1" data-parsoid='{}'> Header 1.1 </h2>
-<h2 id="Header_1.2" data-parsoid='{}'> Header 1.2 </h2>
+<h2 id="Header_1.1" data-parsoid='{}'>Header 1.1</h2>
+<h2 id="Header_1.2" data-parsoid='{}'>Header 1.2</h2>
 
 <h1 id="Header_2" data-parsoid='{"stx":"html"}'>Header 2
 </h1>
-<h2 id="Header_2.1" data-parsoid='{}'> Header 2.1 </h2>
-<h2 id="Header_2.2" data-parsoid='{}'> Header 2.2 </h2>
+<h2 id="Header_2.1" data-parsoid='{}'>Header 2.1</h2>
+<h2 id="Header_2.2" data-parsoid='{}'>Header 2.2</h2>
 <meta property="mw:PageProp/noeditsection"/>
 !! end
 
@@ -17248,7 +17112,7 @@ http://example.com[[File:Foobar.jpg]]
 <p><a rel="nofollow" class="external free" href="http://example.com">http://example.com</a><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com">http://example.com</a><figure-inline class="mw-default-size" typeof="mw:Image"><a 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"/></a></figure-inline></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a><figure-inline class="mw-default-size" typeof="mw:Image"><a 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"/></a></figure-inline></p>
 !!end
 
 !! test
@@ -17357,23 +17221,37 @@ parsoid=wt2html,html2html
 <div id="title=" data-parsoid='{"stx":"html"}'>HTML rocks</div>
 !! end
 
+# FIXME Parsoid doesn't actually match PHP here.
+# Probably we should use the synthetic <foo /> or <indicator>
+# extensions for this test, which are enabled when running parser tests.
+!! test
+Extension tag in attribute value
+!! wikitext
+<span title="<translate>123</translate>">ok</span>
+!! html/php+disabled
+<p>&lt;span title="&lt;translate&gt;123&lt;/translate&gt;"&gt;ok&lt;/span&gt;
+</p>
+!! html/parsoid
+<p><span title="123" about="#mwt4" typeof="mw:ExpandedAttrs" data-parsoid='{"stx":"html","a":{"title":"123"},"sa":{"title":"&lt;translate>123&lt;/translate>"}}' data-mw='{"attribs":[[{"txt":"title"},{"html":"&lt;translate typeof=\"mw:Extension/translate\" about=\"#mwt3\" data-parsoid=&apos;{\"dsr\":[13,39,2,2]}&apos; data-mw=&apos;{\"name\":\"translate\",\"attrs\":{},\"body\":{\"extsrc\":\"123\"}}&apos;>123&lt;/translate>"}]]}'>ok</span></p>
+!! end
+
 !! test
 table with multiple empty attribute values
 !! options
 parsoid=wt2html,html2html
 !! wikitext
 {| title= id=
-| hi
+|hi
 |}
 !! html/php
 <table title="id=">
 <tr>
-<td> hi
+<td>hi
 </td></tr></table>
 
 !! html/parsoid
 <table title="id=">
-<tbody><tr><td> hi</td></tr>
+<tbody><tr><td>hi</td></tr>
 </tbody></table>
 !! end
 
@@ -17414,12 +17292,12 @@ HTML multiple attributes correction
 Table multiple attributes correction
 !! wikitext
 {|
-!+ class="error" class="awesome"| status
+!+ class="error" class="awesome"|status
 |}
 !! html
 <table>
 <tr>
-<th class="awesome"> status
+<th class="awesome">status
 </th></tr></table>
 
 !!end
@@ -17464,11 +17342,9 @@ Remember AT&T?
 text with character entity: eacute
 !! wikitext
 I always thought &eacute; was a cute letter.
-!! html
+!! html+tidy
 <p>I always thought &#233; was a cute letter.
 </p>
-!! html+tidy
-<p>I always thought é was a cute letter.</p>
 !! end
 
 !! test
@@ -17546,12 +17422,11 @@ Ensure that HTML adoption agency algorithm is properly implemented.
 !! end
 
 # This was T43545 in the PHP parser.
-# Note that tidy doesn't handle this correctly.
 !! test
 Nesting of <kbd>
 !! wikitext
 <kbd>X<kbd>Y</kbd>Z</kbd>
-!! html
+!! html+tidy
 <p><kbd>X<kbd>Y</kbd>Z</kbd>
 </p>
 !! end
@@ -17560,22 +17435,20 @@ Nesting of <kbd>
 # Note that there are some other nestable tags (b, i, etc) which are
 # not covered; see T53081 for discussion.
 
-# Note that tidy doesn't handle this correctly.
 !! test
 Nesting of <em>
 !! wikitext
 <em>X<em>Y</em>Z</em>
-!! html
+!! html+tidy
 <p><em>X<em>Y</em>Z</em>
 </p>
 !! end
 
-# Note that tidy doesn't handle this correctly.
 !! test
 Nesting of <strong>
 !! wikitext
 <strong>X<strong>Y</strong>Z</strong>
-!! html
+!! html+tidy
 <p><strong>X<strong>Y</strong>Z</strong>
 </p>
 !! end
@@ -17585,10 +17458,10 @@ Nesting of <q>
 !! wikitext
 <q>X<q>Y</q>Z</q>
 !! html+tidy
-<p><q>X<q>Y</q>Z</q></p>
+<p><q>X<q>Y</q>Z</q>
+</p>
 !! end
 
-# Note that tidy doesn't handle this correctly.
 !! test
 Nesting of <ruby>
 !! wikitext
@@ -17598,7 +17471,6 @@ Nesting of <ruby>
 </p>
 !! end
 
-# Note that tidy doesn't handle this correctly.
 !! test
 Nesting of <bdo>
 !! wikitext
@@ -17643,6 +17515,7 @@ Media link with text
 
 # FIXME: this is still bad HTML tag nesting
 # FIXME: doBlockLevels won't wrap this in a paragraph because it contains a div
+# Parsoid & Remex fix the p-wrapping since they operate on the DOM.
 !! test
 Media link with nasty text
 !! wikitext
@@ -17650,9 +17523,8 @@ Media link with nasty text
 !! html/php
 <a href="http://example.com/images/3/3a/Foobar.jpg" class="internal" title="Foobar.jpg">Safe Link&lt;div style="display:none"&gt;" onmouseover="alert(document.cookie)" onfoo="&lt;/div&gt;</a>
 
-!! html+php/tidy
-<p><a href="http://example.com/images/3/3a/Foobar.jpg" class="internal" title="Foobar.jpg">Safe Link</a></p>
-<div style="display:none">" onmouseover="alert(document.cookie)" onfoo="</div>
+!! html/php+tidy
+<p><a href="http://example.com/images/3/3a/Foobar.jpg" class="internal" title="Foobar.jpg">Safe Link</a></p><a href="http://example.com/images/3/3a/Foobar.jpg" class="internal" title="Foobar.jpg"><div style="display:none">" onmouseover="alert(document.cookie)" onfoo="</div></a>
 !! html/parsoid
 <p><a rel="mw:MediaLink" href="//example.com/images/3/3a/Foobar.jpg" title="Foobar.jpg" data-parsoid='{"autoInsertedEnd":true}'>Safe Link</a></p><div style="display:none" data-parsoid='{"stx":"html"}'><a rel="mw:MediaLink" href="//example.com/images/3/3a/Foobar.jpg" title="Foobar.jpg" data-parsoid='{"autoInsertedEnd":true,"autoInsertedStart":true}'>" onmouseover="alert(document.cookie)" onfoo="</a></div>
 
@@ -18176,12 +18048,12 @@ MSIE 6 CSS safety test: Repetition markers (T57332)
 Table attribute legitimate extension
 !! wikitext
 {|
-!+ style="<nowiki>color:blue</nowiki>"| status
+!+ style="<nowiki>color:blue</nowiki>"|status
 |}
 !! html
 <table>
 <tr>
-<th style="color:blue"> status
+<th style="color:blue">status
 </th></tr></table>
 
 !!end
@@ -18190,12 +18062,12 @@ Table attribute legitimate extension
 Table attribute safety
 !! wikitext
 {|
-!+ style="<nowiki>border-width:expression(0+alert(document.cookie))</nowiki>"| status
+!+ style="<nowiki>border-width:expression(0+alert(document.cookie))</nowiki>"|status
 |}
 !! html
 <table>
 <tr>
-<th style="/* insecure input */"> status
+<th style="/* insecure input */">status
 </th></tr></table>
 
 !! end
@@ -18256,11 +18128,12 @@ Expansion of multi-line templates in attribute values (T8255 sanity check 2)
 !! end
 
 !! test
-Tags which are hidden from Tidy cannot pass through the Sanitizer
+Tags which are hidden from tidiers cannot pass through the Sanitizer
 !! wikitext
 <mw:toc><script>alert();</script></mw:toc>
 !! html+tidy
-<p>&lt;mw:toc&gt;&lt;script&gt;alert();&lt;/script&gt;&lt;/mw:toc&gt;</p>
+<p>&lt;mw:toc&gt;&lt;script&gt;alert();&lt;/script&gt;&lt;/mw:toc&gt;
+</p>
 !! end
 
 ###
@@ -18504,6 +18377,27 @@ this is a '''test'''
 <p>this is a <b>test</b></p>
 !! end
 
+!! test
+Parser hook: horizontal rule inside extension tag that outputs <pre>
+!! wikitext
+<tag>
+Hello
+<hr/>
+Goodbye
+</tag>
+!! html/php
+<pre>
+'
+Hello
+<hr/>
+Goodbye
+'
+array (
+)
+</pre>
+
+!! end
+
 ###
 ### (see tests/parser/parserTestsParserHook.php for the <statictag> extension)
 ###
@@ -18565,14 +18459,14 @@ Nested template calls
 ### Sanitizer
 ###
 
-# HTML+Tidy strips out empty tags completely. Parsoid doesn't.
-# FIXME: Wikitext for this first test doesn't match its title.
+# Remex wraps empty tag runs with p-tags.
+# Parsoid strips them out during p-wrapping.
 !! test
 Sanitizer: Closing of open tags
 !! wikitext
 <s></s><table></table>
 !! html/php+tidy
-
+<p><s></s></p><table></table>
 !! html/parsoid
 <s></s><table></table>
 !! end
@@ -18593,6 +18487,8 @@ parsoid=wt2html
 !! wikitext
 </s>
 !! html/php+tidy
+<p class="mw-empty-elt">
+</p>
 !! html/parsoid
 !! end
 
@@ -18602,10 +18498,9 @@ Sanitizer: Closing of closed but not open table tags
 parsoid=wt2html
 !! wikitext
 Table not started</td></tr></table>
-!! html/php+tidy
-<p>Table not started</p>
-!! html/parsoid
-<p>Table not started</p>
+!! html+tidy
+<p>Table not started
+</p>
 !! end
 
 !! test
@@ -18763,7 +18658,7 @@ language=sr variant=sr-el
 !! html/parsoid
 <p><meta typeof="mw:LanguageVariant" data-mw-variant='{"add":true,"oneway":[{"f":"foAjrjvi","l":"sr-el","t":"\" onload=\"alert(1)\" data-foo=\""}]}'/></p>
 
-<p><span class="mw-default-size" typeof="mw:Image"><a href="./Датотека:Foobar.jpg"><img alt="foAjrjvi" resource="./Датотека: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":"foAjrjvi","resource":"./Датотека:Foobar.jpg","height":"220","width":"1941"},"sa":{"alt":"alt=-{}-foAjrjvi-{}-","resource":"File:Foobar.jpg"}}'/></a></span></p>
+<p><figure-inline class="mw-default-size" typeof="mw:Image"><a href="./Датотека:Foobar.jpg"><img alt="foAjrjvi" resource="./Датотека: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":"foAjrjvi","resource":"./Датотека:Foobar.jpg","height":"220","width":"1941"},"sa":{"alt":"alt=-{}-foAjrjvi-{}-","resource":"File:Foobar.jpg"}}'/></a></figure-inline></p>
 !! end
 
 !! test
@@ -18776,10 +18671,6 @@ Self closed html pairs (T7487)
 <div><font id="bug2"></font>In div text</div>
 !! end
 
-#
-#
-#
-
 !! test
 Punctuation: nbsp before exclamation
 !! wikitext
@@ -18837,9 +18728,9 @@ HTML bullet list, unclosed tags (T7497)
 </ul>
 !! html/php+tidy
 <ul>
-<li>One</li>
-<li>Two</li>
-</ul>
+<li>One
+</li><li>Two
+</li></ul>
 !! html/parsoid
 <ul data-parsoid='{"stx":"html"}'>
 <li data-parsoid='{"stx":"html","autoInsertedEnd":true}'>One</li>
@@ -18879,9 +18770,9 @@ HTML ordered list, unclosed tags (T7497)
 </ol>
 !! html/php+tidy
 <ol>
-<li>One</li>
-<li>Two</li>
-</ol>
+<li>One
+</li><li>Two
+</li></ol>
 !! html/parsoid
 <ol data-parsoid='{"stx":"html"}'>
 <li data-parsoid='{"stx":"html","autoInsertedEnd":true}'>One</li>
@@ -18936,30 +18827,15 @@ HTML nested bullet list, open tags (T7497)
 <li>Sub-two
 </ul>
 </ul>
-!! html/php+tidy
-<ul>
-<li>One</li>
-<li>Two:
-<ul>
-<li>Sub-one</li>
-<li>Sub-two</li>
-</ul>
-</li>
-</ul>
-!! html/parsoid
+!! html+tidy
 <ul>
 <li>One
-</li>
-<li>Two:
+</li><li>Two:
 <ul>
 <li>Sub-one
-</li>
-<li>Sub-two
-</li>
-</ul>
-</li>
-</ul>
-
+</li><li>Sub-two
+</li></ul>
+</li></ul>
 !! end
 
 !! test
@@ -19056,11 +18932,11 @@ mailto:inline@mail.tld
 </p><p><a rel="nofollow" class="external free" href="mailto:inline@mail.tld">mailto:inline@mail.tld</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://first/"></a> <a rel="mw:ExtLink" href="http://second"></a> <a rel="mw:ExtLink" href="ftp://ftp"></a></p>
-<p><a rel="mw:ExtLink" href="ftp://inlineftp">ftp://inlineftp</a></p>
-<p><a rel="mw:ExtLink" href="mailto:enclosed@mail.tld">With target</a></p>
-<p><a rel="mw:ExtLink" href="mailto:enclosed@mail.tld"></a></p>
-<p><a rel="mw:ExtLink" href="mailto:inline@mail.tld">mailto:inline@mail.tld</a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://first/"></a> <a rel="mw:ExtLink" class="external autonumber" href="http://second"></a> <a rel="mw:ExtLink" class="external autonumber" href="ftp://ftp"></a></p>
+<p><a rel="mw:ExtLink" class="external free" href="ftp://inlineftp">ftp://inlineftp</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="mailto:enclosed@mail.tld">With target</a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="mailto:enclosed@mail.tld"></a></p>
+<p><a rel="mw:ExtLink" class="external free" href="mailto:inline@mail.tld">mailto:inline@mail.tld</a></p>
 !! end
 
 
@@ -19090,7 +18966,7 @@ Fuzz testing: Parser13
 !! test
 Fuzz testing: Parser14
 !! wikitext
-== onmouseover= ==
+==onmouseover===
 http://__TOC__
 !! html/php
 <h2><span class="mw-headline" id="onmouseover.3D">onmouseover=</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: onmouseover=">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
@@ -19102,20 +18978,15 @@ http://<div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>C
 
 
 !! html/php+tidy
-<h2><span class="mw-headline" id="onmouseover.3D">onmouseover=</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: onmouseover=">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
-<p>http://</p>
-<div id="toc" class="toc">
-<div class="toctitle" lang="en" dir="ltr" xml:lang="en">
-<h2>Contents</h2>
-</div>
+<h2><span class="mw-headline" id="onmouseover.3D">onmouseover=</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: onmouseover=">edit</a><span class="mw-editsection-bracket">]</span></span></h2><p>
+http://</p><div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
 <li class="toclevel-1 tocsection-1"><a href="#onmouseover.3D"><span class="tocnumber">1</span> <span class="toctext">onmouseover=</span></a></li>
 </ul>
 </div>
-<p></p>
 !! html/parsoid
-<h2 id="onmouseover="><span id="onmouseover.3D" typeof="mw:FallbackId"></span> onmouseover= </h2>
-<p><a rel="mw:ExtLink" href="http://__TOC__" data-parsoid='{"stx":"url"}'>http://__TOC__</a></p>
+<h2 id="onmouseover="><span id="onmouseover.3D" typeof="mw:FallbackId"></span>onmouseover=</h2>
+<p><a rel="mw:ExtLink" class="external free" href="http://__TOC__" data-parsoid='{"stx":"url"}'>http://__TOC__</a></p>
 !! end
 
 !! test
@@ -19144,43 +19015,34 @@ parsoid=wt2html,html2html
 !! end
 
 # Known to produce bogus xml (extra </td>)
+# Don't add the html/php section since it generates broken HTML
 !! test
 Fuzz testing: Parser16
 !! wikitext
 {|
 !https://||||||
-!! html
+!! html+tidy
 <table>
-<tr>
+<tbody><tr>
 <th>https://</th>
 <th></th>
 <th></th>
 <th>
-</td>
-</tr>
-</table>
 
-!! html+tidy
-<table>
-<tr>
-<th>https://</th>
-<th></th>
-<th></th>
-<th></th>
-</tr>
-</table>
+</th></tr>
+</tbody></table>
 !! end
 
 !! test
 Fuzz testing: Parser21
 !! wikitext
 {|
-! irc://{{ftp://a" onmouseover="alert('hello world');"
+!irc://{{ftp://a" onmouseover="alert('hello world');"
 |
 !! html
 <table>
 <tr>
-<th> <a rel="nofollow" class="external free" href="irc://{{ftp://a">irc://{{ftp://a</a>" onmouseover="alert('hello world');"
+<th><a rel="nofollow" class="external free" href="irc://{{ftp://a">irc://{{ftp://a</a>" onmouseover="alert('hello world');"
 </th>
 <td>
 </td>
@@ -19273,7 +19135,7 @@ http://example.com <nowiki>junk</nowiki>
 <p><a rel="nofollow" class="external free" href="http://example.com">http://example.com</a> junk
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com" data-parsoid='{"stx":"url"}'>http://example.com</a> <span typeof="mw:Nowiki">junk</span></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com" data-parsoid='{"stx":"url"}'>http://example.com</a> <span typeof="mw:Nowiki">junk</span></p>
 !! end
 
 !!test
@@ -19284,7 +19146,7 @@ http://example.com<nowiki>junk</nowiki>
 <p><a rel="nofollow" class="external free" href="http://example.com">http://example.com</a>junk
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com" data-parsoid='{"stx":"url"}'>http://example.com</a><span typeof="mw:Nowiki">junk</span></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com" data-parsoid='{"stx":"url"}'>http://example.com</a><span typeof="mw:Nowiki">junk</span></p>
 !! end
 
 !! test
@@ -19295,12 +19157,9 @@ http://example.com<pre>junk</pre>
 <a rel="nofollow" class="external free" href="http://example.com">http://example.com</a><pre>junk</pre>
 
 !! html/php+tidy
-<p><a rel="nofollow" class="external free" href="http://example.com">http://example.com</a></p>
-<pre>
-junk
-</pre>
+<p><a rel="nofollow" class="external free" href="http://example.com">http://example.com</a></p><pre>junk</pre>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com" data-parsoid='{"stx":"url"}'>http://example.com</a></p><pre typeof="mw:Extension/pre" about="#mwt2" data-mw='{"name":"pre","attrs":{},"body":{"extsrc":"junk"}}'>junk</pre>
+<p><a rel="mw:ExtLink" class="external free" href="http://example.com" data-parsoid='{"stx":"url"}'>http://example.com</a></p><pre typeof="mw:Extension/pre" about="#mwt2" data-mw='{"name":"pre","attrs":{},"body":{"extsrc":"junk"}}'>junk</pre>
 !! end
 
 !! test
@@ -20354,23 +20213,23 @@ xxx
 !! test
 Handling of &#x0A; in URLs
 !! wikitext
-*irc://&#x0A;a
+*irc://&#x0A;a
 !! html/php
-<ul><li><ul><li> <a rel="nofollow" class="external free" href="irc://%0Aa">irc://%0Aa</a></li></ul></li></ul>
+<ul><li><a rel="nofollow" class="external free" href="irc://%0Aa">irc://%0Aa</a></li></ul>
 
 !! html/parsoid
-<ul><li><ul><li> <a rel="mw:ExtLink" href="irc://%0Aa" data-parsoid='{"stx":"url","a":{"href":"irc://%0Aa"},"sa":{"href":"irc://&amp;#x0A;a"}}'>irc://%0Aa</a></li></ul></li></ul>
+<ul><li><a rel="mw:ExtLink" class="external free" href="irc://%0Aa" data-parsoid='{"stx":"url","a":{"href":"irc://%0Aa"},"sa":{"href":"irc://&amp;#x0A;a"}}'>irc://%0Aa</a></li></ul>
 !! end
 
 !! test
 Handling of %0A in URLs
 !! wikitext
-*irc://%0Aa
+*irc://%0Aa
 !! html/php
-<ul><li><ul><li> <a rel="nofollow" class="external free" href="irc://%0Aa">irc://%0Aa</a></li></ul></li></ul>
+<ul><li><a rel="nofollow" class="external free" href="irc://%0Aa">irc://%0Aa</a></li></ul>
 
 !! html/parsoid
-<ul><li><ul><li> <a rel="mw:ExtLink" href="irc://%0Aa">irc://%0Aa</a></li></ul></li></ul>
+<ul><li><a rel="mw:ExtLink" class="external free" href="irc://%0Aa">irc://%0Aa</a></li></ul>
 !! end
 
 # The PHP parser strips the empty tags out for giggles; parsoid doesn't.
@@ -20440,51 +20299,51 @@ Say the magic word
 !! options
 title=[[Parser test]]
 !! wikitext
-* {{PAGENAME}}
-* {{PAGENAMEE}}
-* {{FULLPAGENAME}}
-* {{FULLPAGENAMEE}}
-* {{BASEPAGENAME}}
-* {{BASEPAGENAMEE}}
-* {{SUBPAGENAME}}
-* {{SUBPAGENAMEE}}
-* {{ROOTPAGENAME}}
-* {{ROOTPAGENAMEE}}
-* {{TALKPAGENAME}}
-* {{TALKPAGENAMEE}}
-* {{SUBJECTPAGENAME}}
-* {{SUBJECTPAGENAMEE}}
-* {{NAMESPACEE}}
-* {{NAMESPACE}}
-* {{NAMESPACENUMBER}}
-* {{TALKSPACE}}
-* {{TALKSPACEE}}
-* {{SUBJECTSPACE}}
-* {{SUBJECTSPACEE}}
-* {{Dynamic|{{NUMBEROFUSERS}}|{{NUMBEROFPAGES}}|{{CURRENTVERSION}}|{{CONTENTLANGUAGE}}|{{DIRECTIONMARK}}|{{CURRENTTIMESTAMP}}|{{NUMBEROFARTICLES}}}}
-!! html
-<ul><li> Parser test</li>
-<li> Parser_test</li>
-<li> Parser test</li>
-<li> Parser_test</li>
-<li> Parser test</li>
-<li> Parser_test</li>
-<li> Parser test</li>
-<li> Parser_test</li>
-<li> Parser test</li>
-<li> Parser_test</li>
-<li> Talk:Parser test</li>
-<li> Talk:Parser_test</li>
-<li> Parser test</li>
-<li> Parser_test</li>
-<li> </li>
-<li> </li>
-<li> 0</li>
-<li> Talk</li>
-<li> Talk</li>
-<li> </li>
-<li> </li>
-<li> <a href="/index.php?title=Template:Dynamic&amp;action=edit&amp;redlink=1" class="new" title="Template:Dynamic (page does not exist)">Template:Dynamic</a></li></ul>
+*{{PAGENAME}}
+*{{PAGENAMEE}}
+*{{FULLPAGENAME}}
+*{{FULLPAGENAMEE}}
+*{{BASEPAGENAME}}
+*{{BASEPAGENAMEE}}
+*{{SUBPAGENAME}}
+*{{SUBPAGENAMEE}}
+*{{ROOTPAGENAME}}
+*{{ROOTPAGENAMEE}}
+*{{TALKPAGENAME}}
+*{{TALKPAGENAMEE}}
+*{{SUBJECTPAGENAME}}
+*{{SUBJECTPAGENAMEE}}
+*{{NAMESPACEE}}
+*{{NAMESPACE}}
+*{{NAMESPACENUMBER}}
+*{{TALKSPACE}}
+*{{TALKSPACEE}}
+*{{SUBJECTSPACE}}
+*{{SUBJECTSPACEE}}
+*{{Dynamic|{{NUMBEROFUSERS}}|{{NUMBEROFPAGES}}|{{CURRENTVERSION}}|{{CONTENTLANGUAGE}}|{{DIRECTIONMARK}}|{{CURRENTTIMESTAMP}}|{{NUMBEROFARTICLES}}}}
+!! html
+<ul><li>Parser test</li>
+<li>Parser_test</li>
+<li>Parser test</li>
+<li>Parser_test</li>
+<li>Parser test</li>
+<li>Parser_test</li>
+<li>Parser test</li>
+<li>Parser_test</li>
+<li>Parser test</li>
+<li>Parser_test</li>
+<li>Talk:Parser test</li>
+<li>Talk:Parser_test</li>
+<li>Parser test</li>
+<li>Parser_test</li>
+<li></li>
+<li></li>
+<li>0</li>
+<li>Talk</li>
+<li>Talk</li>
+<li></li>
+<li></li>
+<li><a href="/index.php?title=Template:Dynamic&amp;action=edit&amp;redlink=1" class="new" title="Template:Dynamic (page does not exist)">Template:Dynamic</a></li></ul>
 
 !! end
 ### Note: Above tests excludes the "{{NUMBEROFADMINS}}" magic word because it generates a MySQL error when included.
@@ -20573,7 +20432,7 @@ image4    |300px| centre
 <li class="gallerybox" style="width: 155px;"><div class="thumb" style="width: 150px; height: 150px;"><figure-inline typeof="mw:Error mw:Image"><a href="./File:Image2.gif"><img resource="./File:Image2.gif" src="./Special:FilePath/Image2.gif" height="120" width="120"/></a></figure-inline></div><div class="gallerytext"></div></li>
 <li class="gallerybox" style="width: 155px;"><div class="thumb" style="width: 150px; height: 150px;"><figure-inline typeof="mw:Error mw:Image"><a href="./File:Image3"><img resource="./File:Image3" src="./Special:FilePath/Image3" height="120" width="120"/></a></figure-inline></div><div class="gallerytext"></div></li>
 <li class="gallerybox" style="width: 155px;"><div class="thumb" style="width: 150px; height: 150px;"><figure-inline typeof="mw:Error mw:Image"><a href="./File:Image4"><img resource="./File:Image4" src="./Special:FilePath/Image4" height="300" width="300"/></a></figure-inline></div><div class="gallerytext"></div></li>
-<li class="gallerybox" style="width: 155px;"><div class="thumb" style="width: 150px; height: 150px;"><figure-inline typeof="mw:Error mw:Image"><a href="./File:Image5.svg"><img resource="./File:Image5.svg" src="./Special:FilePath/Image5.svg" height="120" width="120"/></a></figure-inline></div><div class="gallerytext"> <a rel="mw:ExtLink" href="http://///////">http://///////</a></div></li>
+<li class="gallerybox" style="width: 155px;"><div class="thumb" style="width: 150px; height: 150px;"><figure-inline typeof="mw:Error mw:Image"><a href="./File:Image5.svg"><img resource="./File:Image5.svg" src="./Special:FilePath/Image5.svg" height="120" width="120"/></a></figure-inline></div><div class="gallerytext"> <a rel="mw:ExtLink" class="external free" href="http://///////">http://///////</a></div></li>
 <li class="gallerybox" style="width: 155px;"><div class="thumb" style="width: 150px; height: 150px;"><figure-inline typeof="mw:Error mw:Image"><a href="./File:*_image6"><img resource="./File:*_image6" src="./Special:FilePath/*_image6" height="120" width="120"/></a></figure-inline></div><div class="gallerytext"></div></li>
 </ul>
 !! end
@@ -20701,6 +20560,48 @@ image:foobar.jpg|Blabla|alt=This is a foo-bar.|blabla.
 </ul>
 !! end
 
+!! test
+Gallery (without px units)
+!! wikitext
+<gallery widths="70" heights="40">
+File:Foobar.jpg
+</gallery>
+!! html/php
+<ul class="gallery mw-gallery-traditional">
+               <li class="gallerybox" style="width: 105px"><div style="width: 105px">
+                       <div class="thumb" style="width: 100px;"><div style="margin:31px auto;"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/70px-Foobar.jpg" width="70" height="8" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/105px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/140px-Foobar.jpg 2x" /></a></div></div>
+                       <div class="gallerytext">
+                       </div>
+               </div></li>
+</ul>
+
+!! html/parsoid
+<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" about="#mwt2" data-mw='{"name":"gallery","attrs":{"widths":"70","heights":"40"},"body":{"extsrc":"\nFile:Foobar.jpg\n"}}'>
+<li class="gallerybox" style="width: 105px;"><div class="thumb" style="width: 100px; height: 70px;"><figure-inline typeof="mw:Image"><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/70px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="8" width="70"/></a></figure-inline></div><div class="gallerytext"></div></li>
+</ul>
+!! end
+
+!! test
+Gallery (with invalid units)
+!! wikitext
+<gallery widths="70em" heights="40em">
+File:Foobar.jpg
+</gallery>
+!! html/php
+<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="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
+<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" about="#mwt2" data-mw='{"name":"gallery","attrs":{"widths":"70em","heights":"40em"},"body":{"extsrc":"\nFile:Foobar.jpg\n"}}'>
+<li class="gallerybox" style="width: 155px;"><div class="thumb" style="width: 150px; height: 150px;"><figure-inline typeof="mw:Image"><a href="./File:Foobar.jpg"><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
 Gallery with link that has fragment
 !! options
@@ -20933,7 +20834,7 @@ File:Foobar.jpg|alt=galleryalt|link=Wikilink
 </ul>
 
 !! html/parsoid
-<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" about="#mwt2" data-parsoid='{"dsr":[0,70,2,2]}' data-mw='{"name":"gallery","attrs":{},"body":{}}'>
+<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" about="#mwt2" data-mw='{"name":"gallery","attrs":{},"body":{}}'>
 <li class="gallerybox" style="width: 155px;"><div class="thumb" style="width: 150px; height: 150px;"><figure-inline typeof="mw:Image"><a href="./Wikilink"><img alt="galleryalt" 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
@@ -20967,11 +20868,11 @@ File:Foobar.jpg|alt=galleryalt|link=http://www.example.org
 Gallery override link with absolute external link with LanguageConverter
 !! options
 language=zh
-!! input
+!! wikitext
 <gallery>
 File:foobar.jpg|caption|alt=galleryalt|link=http://www.example.org
 </gallery>
-!! result
+!! html/php
 <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="http://www.example.org"><img alt="galleryalt" 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>
@@ -20982,6 +20883,10 @@ File:foobar.jpg|caption|alt=galleryalt|link=http://www.example.org
                </div></li>
 </ul>
 
+!! html/parsoid
+<ul class="gallery mw-gallery-traditional" typeof="mw:Extension/gallery" about="#mwt2" data-mw='{"name":"gallery","attrs":{},"body":{"extsrc":"\nFile:foobar.jpg|caption|alt=galleryalt|link=http://www.example.org\n"}}'>
+<li class="gallerybox" style="width: 155px;"><div class="thumb" style="width: 150px; height: 150px;"><figure-inline typeof="mw:Image"><a href="http://www.example.org"><img alt="galleryalt" 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
@@ -21114,8 +21019,6 @@ parsoid=wt2html,wt2wt,html2html
 !! html/php
 <p>&#x4a;&#x61;&#x76;&#x61;&#x53;&#x63;&#114;&#x69;&#112;&#x74;
 </p>
-!! html/php+tidy
-<p>JavaScript</p>
 !! html/parsoid
 <p><span typeof="mw:Entity">J</span><span typeof="mw:Entity">a</span><span typeof="mw:Entity">v</span><span typeof="mw:Entity">a</span><span typeof="mw:Entity">S</span><span typeof="mw:Entity">c</span><span typeof="mw:Entity">r</span><span typeof="mw:Entity">i</span><span typeof="mw:Entity">p</span><span typeof="mw:Entity">t</span></p>
 !! end
@@ -21124,11 +21027,9 @@ parsoid=wt2html,wt2wt,html2html
 HTML Hex character encoding bogus encoding (T28437 regression check)
 !! wikitext
 &#xsee;&#XSEE;
-!! html/php
+!! html
 <p>&amp;#xsee;&amp;#XSEE;
 </p>
-!! html/parsoid
-<p>&amp;#xsee;&amp;#XSEE;</p>
 !! end
 
 !! test
@@ -21140,8 +21041,6 @@ parsoid=wt2html,wt2wt,html2html
 !! html/php
 <p>&#xee;&#xee;
 </p>
-!! html/php+tidy
-<p>îî</p>
 !! html/parsoid
 <p><span typeof="mw:Entity">î</span><span typeof="mw:Entity">î</span></p>
 !! end
@@ -21160,8 +21059,7 @@ Illegal character references (T106578)
 ; Surrogate: &#xD83D;&#xDCA9;
 ; This is an okay astral character: &#x1F4A9;
 !! html+tidy
-<dl>
-<dt>Null</dt>
+<dl><dt>Null</dt>
 <dd>&amp;#00;</dd>
 <dt>FF</dt>
 <dd>&amp;#xC;</dd>
@@ -21174,8 +21072,7 @@ Illegal character references (T106578)
 <dt>Surrogate</dt>
 <dd>&amp;#xD83D;&amp;#xDCA9;</dd>
 <dt>This is an okay astral character</dt>
-<dd>💩</dd>
-</dl>
+<dd>&#x1f4a9;</dd></dl>
 !! end
 
 !! test
@@ -21192,11 +21089,9 @@ __FORCETOC__
 ISBN code coverage
 !! wikitext
 ISBN  978-0-1234-56&#x20;789
-!! html
+!! html/php
 <p><a href="/wiki/Special:BookSources/9780123456" class="internal mw-magiclink-isbn">ISBN 978-0-1234-56</a>&#x20;789
 </p>
-!! html+tidy
-<p><a href="/wiki/Special:BookSources/9780123456" class="internal mw-magiclink-isbn">ISBN 978-0-1234-56</a> 789</p>
 !! html/parsoid
 <p><a href="./Special:BookSources/9780123456" rel="mw:WikiLink" data-parsoid='{"stx":"magiclink"}'>ISBN 978-0-1234-56</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#x20;","srcContent":" "}'> </span>789</p>
 !! end
@@ -21267,7 +21162,7 @@ T24905: <abbr> followed by ISBN followed by </a>
 <p><abbr>(fr)</abbr> <a href="/wiki/Special:BookSources/2753300917" class="internal mw-magiclink-isbn">ISBN 2753300917</a> <a rel="nofollow" class="external text" href="http://www.example.com">example.com</a>
 </p>
 !! html/parsoid
-<p><abbr data-parsoid='{"stx":"html"}'>(fr)</abbr> <a href="./Special:BookSources/2753300917" rel="mw:WikiLink" data-parsoid='{"stx":"magiclink"}'>ISBN 2753300917</a> <a rel="mw:ExtLink" href="http://www.example.com">example.com</a></p>
+<p><abbr data-parsoid='{"stx":"html"}'>(fr)</abbr> <a href="./Special:BookSources/2753300917" rel="mw:WikiLink" data-parsoid='{"stx":"magiclink"}'>ISBN 2753300917</a> <a rel="mw:ExtLink" class="external text" href="http://www.example.com">example.com</a></p>
 !! end
 
 !! test
@@ -21292,66 +21187,78 @@ RFC [[RFC 1234]]
 RFC code coverage
 !! wikitext
 RFC   983&#x20;987
-!! html
+!! html/php
 <p><a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc983">RFC 983</a>&#x20;987
 </p>
-!! html+tidy
-<p><a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc983">RFC 983</a> 987</p>
+!! html/parsoid
+<p><a href="https://tools.ietf.org/html/rfc983" rel="mw:ExtLink" class="external text" data-parsoid='{"stx":"magiclink"}'>RFC   983</a><span typeof="mw:Entity" data-parsoid='{"src":"&amp;#x20;","srcContent":" "}'> </span>987</p>
 !! end
 
 !! test
 Centre-aligned image
 !! wikitext
 [[Image:foobar.jpg|centre]]
-!! html
+!! html/php
 <div class="center"><div class="floatnone"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a></div></div>
 
-!!end
+!! html/parsoid
+<figure class="mw-default-size mw-halign-center" typeof="mw:Image" data-parsoid='{"optList":[{"ck":"center","ak":"centre"}]}'><a 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":"Image:foobar.jpg"}}'/></a></figure>
+!! end
 
 !! test
 None-aligned image
 !! wikitext
 [[Image:foobar.jpg|none]]
-!! html
+!! html/php
 <div class="floatnone"><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/3/3a/Foobar.jpg" width="1941" height="220" /></a></div>
 
-!!end
+!! html/parsoid
+<figure class="mw-default-size mw-halign-none" typeof="mw:Image"><a 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":"Image:foobar.jpg"}}'/></a></figure>
+!! end
 
 !! test
 Width + Height sized image (using px) (height is ignored)
 !! wikitext
 [[Image:foobar.jpg|640x480px]]
-!! html
+!! html/php
 <p><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg" width="640" height="73" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/960px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg 2x" /></a>
 </p>
-!!end
+!! html/parsoid
+<p><figure-inline typeof="mw:Image" data-parsoid='{"optList":[{"ck":"width","ak":"640x480px"}]}'><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="73" width="640" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"73","width":"640"},"sa":{"resource":"Image:foobar.jpg"}}'/></a></figure-inline></p>
+!! end
 
 !! test
 Width-sized image (using px, no following whitespace)
 !! wikitext
 [[Image:foobar.jpg|640px]]
-!! html
+!! html/php
 <p><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg" width="640" height="73" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/960px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg 2x" /></a>
 </p>
-!!end
+!! html/parsoid
+<p><figure-inline typeof="mw:Image" data-parsoid='{"optList":[{"ck":"width","ak":"640px"}]}'><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="73" width="640" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"73","width":"640"},"sa":{"resource":"Image:foobar.jpg"}}'/></a></figure-inline></p>
+!! end
 
 !! test
 Width-sized image (using px, with following whitespace - test regression from r39467)
 !! wikitext
 [[Image:foobar.jpg|640px ]]
-!! html
+!! html/php
 <p><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg" width="640" height="73" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/960px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg 2x" /></a>
 </p>
+!! html/parsoid
+<p><figure-inline typeof="mw:Image" data-parsoid='{"optList":[{"ck":"width","ak":"640px "}]}'><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="73" width="640" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"73","width":"640"},"sa":{"resource":"Image:foobar.jpg"}}'/></a></figure-inline></p>
 !!end
 
 !! test
 Width-sized image (using px, with preceding whitespace - test regression from r39467)
 !! wikitext
 [[Image:foobar.jpg| 640px]]
-!! html
+!! html/php
 <p><a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg" width="640" height="73" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/960px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg 2x" /></a>
 </p>
-!!end
+!! html/parsoid
+<p><figure-inline typeof="mw:Image" data-parsoid='{"optList":[{"ck":"width","ak":" 640px"}]}'><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="73" width="640" data-parsoid='{"a":{"resource":"./File:Foobar.jpg","height":"73","width":"640"},"sa":{"resource":"Image:foobar.jpg"}}'/></a></figure-inline></p>
+!! end
 
 !! test
 Image with page parameter
@@ -21397,7 +21304,7 @@ Images with the "|" character in the comment
 <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>An <a rel="nofollow" class="external text" href="http://test/?param1=%7Cleft%7C&amp;param2=%7Cx">external</a> URL</div></div></div>
 
 !! html/parsoid
-<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption>An <a rel="mw:ExtLink" href="http://test/?param1=%7Cleft%7C&amp;param2=%7Cx" data-parsoid='{"a":{"href":"http://test/?param1=%7Cleft%7C&amp;param2=%7Cx"},"sa":{"href":"http://test/?param1=|left|&amp;param2=|x"}}'>external</a> URL</figcaption></figure>
+<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption>An <a rel="mw:ExtLink" class="external text" href="http://test/?param1=%7Cleft%7C&amp;param2=%7Cx" data-parsoid='{"a":{"href":"http://test/?param1=%7Cleft%7C&amp;param2=%7Cx"},"sa":{"href":"http://test/?param1=|left|&amp;param2=|x"}}'>external</a> URL</figcaption></figure>
 !! end
 
 !! test
@@ -21529,20 +21436,20 @@ parsoid=wt2html
 !! test
 Definition list code coverage
 !! wikitext
-; title   : def
-; title : def
+;title   : def
+;title : def
 ;title: def
 !! html/php
-<dl><dt> title  &#160;</dt>
-<dd> def</dd>
-<dt> title&#160;</dt>
-<dd> def</dd>
+<dl><dt>title  &#160;</dt>
+<dd>def</dd>
+<dt>title&#160;</dt>
+<dd>def</dd>
 <dt>title</dt>
-<dd> def</dd></dl>
+<dd>def</dd></dl>
 
 !! html/parsoid
-<dl><dt> title  <span typeof="mw:Placeholder"> </span></dt><dd> def</dd>
-<dt> title<span typeof="mw:Placeholder"> </span></dt><dd> def</dd>
+<dl><dt>title  <span typeof="mw:Placeholder"> </span></dt><dd> def</dd>
+<dt>title<span typeof="mw:Placeholder"> </span></dt><dd> def</dd>
 <dt>title</dt><dd> def</dd></dl>
 !! end
 
@@ -21787,7 +21694,7 @@ anchorencode encodes like the TOC generator: (T20431)
 !! config
 wgFragmentMode=[ 'html5', 'legacy' ]
 !! wikitext
-=== _ +:.3A%3A _ &&amp;]] x ===
+===_ +:.3A%3A _ &&amp;]] x===
 {{anchorencode: _ +:.3A%3A _ &&amp;]] x}}
 __NOEDITSECTION__
 !! html/php
@@ -21795,7 +21702,7 @@ __NOEDITSECTION__
 <p>+:.3A%3A_&amp;&amp;&#93;&#93;_x
 </p>
 !! html/parsoid
-<h3 id="+:.3A%3A_&amp;&amp;]]_x"><span id=".2B:.3A.253A_.26.26.5D.5D_x" typeof="mw:FallbackId"></span> _ +:.3A%3A _ &amp;<span typeof="mw:Entity" data-parsoid='{"src":"&amp;amp;","srcContent":"&amp;","dsr":[18,23,null,null]}'>&amp;</span>]] x </h3>
+<h3 id="+:.3A%3A_&amp;&amp;]]_x"><span id=".2B:.3A.253A_.26.26.5D.5D_x" typeof="mw:FallbackId"></span>_ +:.3A%3A _ &amp;<span typeof="mw:Entity" data-parsoid='{"src":"&amp;amp;","srcContent":"&amp;","dsr":[18,23,null,null]}'>&amp;</span>]] x</h3>
 <p about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"anchorencode: _ +:.3A%3A _ &amp;&amp;amp;]] x","function":"anchorencode"},"params":{},"i":0}}]}'>+:.3A%3A_&amp;&amp;<span typeof="mw:Entity">]</span><span typeof="mw:Entity">]</span>_x</p>
 <meta property="mw:PageProp/noeditsection"/>
 !! end
@@ -21805,7 +21712,7 @@ anchorencode encodes like the TOC generator: (T20431) (legacy)
 !! config
 wgFragmentMode=[ 'legacy' ]
 !! wikitext
-=== _ +:.3A%3A&&amp;]] ===
+===_ +:.3A%3A&&amp;]]===
 {{anchorencode: _ +:.3A%3A&&amp;]] }}
 __NOEDITSECTION__
 !! html/php
@@ -22089,12 +21996,12 @@ wgFragmentMode=[ 'html5', 'legacy' ]
 !! options
 language=sr variant=sr-ec
 !! wikitext
-== -{Naslov}- ==
+==-{Naslov}-==
 
 Note that even an unprotected headline ID is not affected by language
 conversion:
 
-== Latinski ==
+==Latinski==
 !! html/php
 <h2><span id="-.7BNaslov.7D-"></span><span class="mw-headline" id="-{Naslov}-">Naslov</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="Уреди одељак „Naslov“">уреди</a><span class="mw-editsection-bracket">]</span></span></h2>
 <p>Ноте тхат евен ан унпротецтед хеадлине ИД ис нот аффецтед бy лангуаге
@@ -22103,7 +22010,7 @@ conversion:
 <h2><span class="mw-headline" id="Latinski">Латински</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=2" title="Уреди одељак „Латински“">уреди</a><span class="mw-editsection-bracket">]</span></span></h2>
 
 !! html/parsoid
-<h2 id="-{Naslov}-"><span id="-.7BNaslov.7D-" typeof="mw:FallbackId"></span> <span typeof="mw:LanguageVariant" data-mw-variant='{"disabled":{"t":"Naslov"}}'></span> </h2>
+<h2 id="-{Naslov}-"><span id="-.7BNaslov.7D-" typeof="mw:FallbackId"></span><span typeof="mw:LanguageVariant" data-mw-variant='{"disabled":{"t":"Naslov"}}'></span></h2>
 
 <p>Note that even an unprotected headline ID is not affected by language
 conversion:</p>
@@ -22546,7 +22453,7 @@ Nested raw: -{R|nested {{echo|hi}} templates}-
 Strings evaluating false shouldn't be ignored by Language converter (T51072)
 !! options
 language=zh variant=zh-cn
-!! input
+!! wikitext
 -{zh-cn:0;zh-sg:1;zh-tw:2;zh-hk:3}-
 !! html/php
 <p>0
@@ -22559,7 +22466,7 @@ language=zh variant=zh-cn
 Conversion rules from [numeric-only string] to [something else] (T48634)
 !! options
 language=zh variant=zh-cn
-!! input
+!! wikitext
 -{H|0=>zh-cn:B}--{H|0=>zh-cn:C;0=>zh-cn:D}--{H|0=>zh-hans:A}-012345-{A|zh-tw:0;zh-cn:E;}-012345
 !! html/php
 <p>D12345EE12345
@@ -22572,7 +22479,7 @@ language=zh variant=zh-cn
 Two-way converter rule entries with an empty value should be ignored (T53551)
 !! options
 language=zh variant=zh-cn
-!! input
+!! wikitext
 -{H|zh-cn:foo;zh-tw:;}-foobar
 !! html/php
 <p>foobar
@@ -22585,7 +22492,7 @@ language=zh variant=zh-cn
 One-way converter rule entries with an empty "from" string should be ignored (T53551)
 !! options
 language=zh variant=zh-cn
-!! input
+!! wikitext
 -{H|=>zh-cn:foo;}-foobar
 !! html/php
 <p>foobar
@@ -22598,7 +22505,7 @@ language=zh variant=zh-cn
 Empty converter rule entries shouldn't be inserted into the conversion table (T53551)
 !! options
 language=zh variant=zh-cn
-!! input
+!! wikitext
 -{H|}-foobar
 !! html/php
 <p>foobar
@@ -22660,10 +22567,10 @@ language=zh variant=zh-cn
 
 <span>a-{H|0=>zh-cn:x<span>y;0=>zh-tw:b<div>c}-d
 !! html/php+tidy
-<p><span>ab</span></p>
-<div><span>cd <span>ab</span></span>
-<div><span>cd <span>ad</span></span></div>
-</div>
+<span>ab<div>cd
+<span>ab<div>cd
+<span>ad
+</span></div></span></div></span>
 !! html/parsoid
 <p><span data-parsoid='{"stx":"html","autoInsertedEnd":true}'>a</span></p><div typeof="mw:LanguageVariant" data-mw-variant='{"disabled":{"t":"b&lt;div data-parsoid=&#39;{\"stx\":\"html\",\"autoInsertedEnd\":true,\"dsr\":[10,16,5,0]}&#39;>c&lt;/div>"}}'></div><p>d</p>
 
@@ -22782,20 +22689,20 @@ gopher://www.google.com
 !! html/php
 <p><a rel="nofollow" class="external free" href="http://www.google.com">http://www.google.com</a>
 <a rel="nofollow" class="external free" href="gopher://www.google.com">gopher://www.google.com</a>
-<a rel="nofollow" class="external free" href="http://www.google.com">http://www.google.com</a>
-<a rel="nofollow" class="external free" href="gopher://www.google.com">gopher://www.google.com</a>
+<a rel="nofollow" class="external text" href="http://www.google.com">http://www.google.com</a>
+<a rel="nofollow" class="external text" href="gopher://www.google.com">gopher://www.google.com</a>
 <a rel="nofollow" class="external text" href="https://www.google.com">irc://www.google.com</a>
 <a rel="nofollow" class="external text" href="ftp://www.google.com">www.гоогле.цом/фтп://дир</a>
 <a rel="nofollow" class="external text" href="//www.google.com">www.гоогле.цом</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://www.google.com">http://www.google.com</a>
-<a rel="mw:ExtLink" href="gopher://www.google.com">gopher://www.google.com</a>
-<a rel="mw:ExtLink" href="http://www.google.com">http://www.google.com</a>
-<a rel="mw:ExtLink" href="gopher://www.google.com">gopher://www.google.com</a>
-<a rel="mw:ExtLink" href="https://www.google.com">irc://www.google.com</a>
-<a rel="mw:ExtLink" href="ftp://www.google.com">www.google.com/ftp://dir</a>
-<a rel="mw:ExtLink" href="//www.google.com">www.google.com</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="http://www.google.com">http://www.google.com</a>
+<a rel="mw:ExtLink" class="external free" href="gopher://www.google.com">gopher://www.google.com</a>
+<a rel="mw:ExtLink" class="external free" href="http://www.google.com">http://www.google.com</a>
+<a rel="mw:ExtLink" class="external free" href="gopher://www.google.com">gopher://www.google.com</a>
+<a rel="mw:ExtLink" class="external text" href="https://www.google.com">irc://www.google.com</a>
+<a rel="mw:ExtLink" class="external text" href="ftp://www.google.com">www.google.com/ftp://dir</a>
+<a rel="mw:ExtLink" class="external text" href="//www.google.com">www.google.com</a></p>
 !! end
 
 !! test
@@ -22949,10 +22856,9 @@ language=zh variant=zh-cn
 ;<b>foo:bar
 ;-{zh-cn:AAA
 !! html/php+tidy
-<dl>
-<dt><b>foo:bar</b></dt>
-<dt><b>-{zh-cn:AAA</b></dt>
-</dl>
+<dl><dt><b>foo:bar</b></dt><b>
+<dt>-{zh-cn:AAA</dt></b></dl><p><b>
+</b></p>
 !! html/parsoid
 <dl><dt data-parsoid='{"dsr":[0,11,1,0]}'><b data-parsoid='{"stx":"html","autoInsertedEnd":true}'>foo:bar</b></dt><b data-parsoid='{"stx":"html","autoInsertedEnd":true,"autoInsertedStart":true}'>
 <dt data-parsoid='{"dsr":[12,20,1,0]}'>-{zh-cn</dt>
@@ -22991,7 +22897,7 @@ parsoid=wt2html,wt2wt,html2html
 <table>
 
 <tr>
-<td> B
+<td>B
 </td></tr></table>
 
 !! html/parsoid
@@ -23122,26 +23028,20 @@ language=sr
 !! test
 T2529: Uncovered bullet
 !! wikitext
-* Foo {{bullet}}
+*Foo {{bullet}}
 !! html
-<ul><li> Foo </li>
-<li> Bar</li></ul>
+<ul><li>Foo</li>
+<li>Bar</li></ul>
 
 !! end
 
-# Plain MediaWiki does not remove empty lists, but tidy actually does.
-# Templates in Wikipedia rely on this behavior, as tidy has always been
-# enabled there. These tests are normally run *without* tidy, so specify the
-# full output here. 
-# To test realistic parsing behavior, apply a tidy-like transformation to both
-# the expected output and your parser's output.
 !! test
-T2529: Uncovered bullet leaving empty list, normally removed by tidy
+T2529: Uncovered bullet in a deeply nested list
 !! wikitext
-******* Foo {{bullet}}
+*******Foo {{bullet}}
 !! html
-<ul><li><ul><li><ul><li><ul><li><ul><li><ul><li><ul><li> Foo </li></ul></li></ul></li></ul></li></ul></li></ul></li></ul></li>
-<li> Bar</li></ul>
+<ul><li><ul><li><ul><li><ul><li><ul><li><ul><li><ul><li>Foo</li></ul></li></ul></li></ul></li></ul></li></ul></li></ul></li>
+<li>Bar</li></ul>
 
 !! end
 
 </p>
 <table>
 <tr>
-<td> 1 </td>
-<td> 2
+<td>1</td>
+<td>2
 </td></tr>
 <tr>
-<td> 3 </td>
-<td> 4
+<td>3</td>
+<td>4
 </td></tr></table>
 <p>y
 </p>
 !! test
 T2529: Uncovered bullet in parser function result
 !! wikitext
-* Foo {{lc:{{bullet}} }}
+*Foo {{lc:{{bullet}} }}
 !! html
-<ul><li> Foo </li>
-<li> bar</li></ul>
+<ul><li>Foo</li>
+<li>bar</li></ul>
 
 !! end
 
@@ -23327,14 +23227,17 @@ Line two
 
 !! end
 
+# doBlockLevels screws up this output and Remex cleans up as much as it can.
+# Parsoid seems to do a better job here since its p-wrapper is probably smarter.
 !! test
 Nesting tags, paragraphs on lines which begin with <div>
 !! wikitext
 <div></div><strong>A
 B</strong>
 !! html/php+tidy
-<p><strong>A</strong></p>
-<p><strong>B</strong></p>
+<div></div><p><strong>A
+</strong></p><strong></strong><p><strong>B</strong>
+</p>
 !! html/parsoid
 <div></div>
 <p><strong>A
@@ -23354,9 +23257,8 @@ Line two</blockquote>
 Line two</blockquote>
 
 !! html+tidy
-<blockquote>
-<p>Line one Line two</p>
-</blockquote>
+<blockquote><p>Line one
+Line two</p></blockquote>
 !! end
 
 !! test
@@ -23374,10 +23276,12 @@ Line two</blockquote>
 
 !! html+tidy
 <blockquote>
-<p>Line one</p>
-Line two</blockquote>
+<p>Line one
+</p><p>
+Line two</p></blockquote>
 !! end
 
+# Parsoid's output is broken on this because of Tidy-compatibility cruft
 !! test
 T8200: paragraphs inside blockquotes (extra line break on close)
 !! wikitext
@@ -23392,9 +23296,9 @@ Line two
 </blockquote>
 
 !! html+tidy
-<blockquote>
-<p>Line one</p>
-<p>Line two</p>
+<blockquote><p>Line one
+</p><p>Line two
+</p>
 </blockquote>
 !! end
 
@@ -23413,13 +23317,9 @@ Line two
 </p>
 </blockquote>
 
-!! html+tidy
-<blockquote>
-<p>Line one</p>
-<p>Line two</p>
-</blockquote>
 !! end
 
+# FIXME: Why does/should the blockquote+div combo suppress p-wrapping here?
 !! test
 Paragraphs inside blockquotes/divs (no extra line breaks)
 !! wikitext
@@ -23498,9 +23398,11 @@ wgLinkHolderBatchSize=0
 Free external link invading image caption
 !! wikitext
 [[Image:Foobar.jpg|thumb|http://x|hello]]
-!! html
+!! html/php
 <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>hello</div></div></div>
 
+!! html/parsoid
+<figure class="mw-default-size" typeof="mw:Image/Thumb" data-parsoid='{"optList":[{"ck":"thumbnail","ak":"thumb"},{"ck":"bogus","ak":"http://x"},{"ck":"caption","ak":"hello"}]}'><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":"Image:Foobar.jpg"}}'/></a><figcaption>hello</figcaption></figure>
 !! end
 
 !! test
@@ -23513,25 +23415,29 @@ language=fa
 <p><a rel="nofollow" class="external autonumber" href="http://en.wikipedia.org/">[۱]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://en.wikipedia.org/"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="http://en.wikipedia.org/"></a></p>
 !! end
 
 !! test
 Multibyte character in padleft
 !! wikitext
 {{padleft:-Hello|7|Æ}}
-!! html
+!! html/php
 <p>Æ-Hello
 </p>
+!! html/parsoid
+<p typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"padleft:-Hello","function":"padleft"},"params":{"1":{"wt":"7"},"2":{"wt":"Æ"}},"i":0}}]}'>Æ-Hello</p>
 !! end
 
 !! test
 Multibyte character in padright
 !! wikitext
 {{padright:Hello-|7|Æ}}
-!! html
+!! html/php
 <p>Hello-Æ
 </p>
+!! html/parsoid
+<p typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"padright:Hello-","function":"padright"},"params":{"1":{"wt":"7"},"2":{"wt":"Æ"}},"i":0}}]}'>Hello-Æ</p>
 !! end
 
 !!test
@@ -23771,7 +23677,7 @@ comment
 Bad images - basic functionality
 !! wikitext
 [[File:Bad.jpg]]
-!! DISABLED/html/php
+!! html/php+disabled
 !! html/parsoid
 <p><span class="mw-default-size" typeof="mw:Error mw:Image" data-mw='{"errors":[{"key":"bad-image","message":"This image is blacklisted in this context."}]}'><a href="./File:Bad.jpg"><img resource="./File:Bad.jpg" height="220" width="220"/></a></span></p>
 !! end
@@ -23782,7 +23688,7 @@ Bad images - T18039: text after bad image disappears
 Foo bar
 [[File:Bad.jpg]]
 Bar foo
-!! DISABLED/html/php
+!! html/php+disabled
 <p>Foo bar
 </p><p>Bar foo
 </p>
@@ -23960,13 +23866,13 @@ showindicators
 <indicator name="02">[[Main Page]]</indicator>
 <indicator name="03">[[File:Foobar.jpg|25px|link=]]</indicator>
 <indicator name="04">[[File:Foobar.jpg|25px]]</indicator>
-<indicator name="05">* foo
-* bar</indicator>
+<indicator name="05">*foo
+*bar</indicator>
 <indicator name="06"><nowiki>foo</nowiki></indicator>
 <indicator name="07"> Preformatted</indicator>
 <indicator name="08"><div>Broken tag</indicator>
 <indicator name="09">{| class=wikitable
-| cell
+|cell
 |}</indicator>
 <indicator name="10">Two
 
@@ -23976,8 +23882,8 @@ paragraphs</indicator>
 02=<a href="/wiki/Main_Page" title="Main Page">Main Page</a>
 03=<img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/25px-Foobar.jpg" width="25" height="3" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/38px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/50px-Foobar.jpg 2x" />
 04=<a href="/wiki/File:Foobar.jpg" class="image"><img alt="Foobar.jpg" src="http://example.com/images/thumb/3/3a/Foobar.jpg/25px-Foobar.jpg" width="25" height="3" srcset="http://example.com/images/thumb/3/3a/Foobar.jpg/38px-Foobar.jpg 1.5x, http://example.com/images/thumb/3/3a/Foobar.jpg/50px-Foobar.jpg 2x" /></a>
-05=<ul><li> foo</li>
-<li> bar</li></ul>
+05=<ul><li>foo</li>
+<li>bar</li></ul>
 
 06=foo
 07=<pre>Preformatted
@@ -23986,7 +23892,7 @@ paragraphs</indicator>
 
 09=<table class="wikitable">
 <tr>
-<td> cell
+<td>cell
 </td></tr></table>
 
 10=<p>Two
@@ -24253,7 +24159,7 @@ T28375: TOC with italics
 title=[[Main Page]]
 !! wikitext
 __TOC__
-== ''Lost'' episodes ==
+==''Lost'' episodes==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -24265,7 +24171,7 @@ __TOC__
 
 !! html/parsoid
 <meta property="mw:PageProp/toc" data-parsoid='{}'/>
-<h2 id="Lost_episodes" data-parsoid='{}'> <i>Lost</i> episodes </h2>
+<h2 id="Lost_episodes" data-parsoid='{}'><i>Lost</i> episodes</h2>
 !! end
 
 !! test
@@ -24274,7 +24180,7 @@ T28375: TOC with bold
 title=[[Main Page]]
 !! wikitext
 __TOC__
-== '''should be bold''' then normal text ==
+=='''should be bold''' then normal text==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -24286,7 +24192,7 @@ __TOC__
 
 !! html/parsoid
 <meta property="mw:PageProp/toc" data-parsoid='{}'/>
-<h2 id="should_be_bold_then_normal_text" data-parsoid='{}'> <b>should be bold</b> then normal text </h2>
+<h2 id="should_be_bold_then_normal_text" data-parsoid='{}'><b>should be bold</b> then normal text</h2>
 !! end
 
 !! test
@@ -24295,7 +24201,7 @@ T35845: Headings become cursive in TOC when they contain an image
 title=[[Main Page]]
 !! wikitext
 __TOC__
-== Image [[Image:foobar.jpg]] ==
+==Image [[Image:foobar.jpg]]==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -24307,7 +24213,7 @@ __TOC__
 
 !! html/parsoid
 <meta property="mw:PageProp/toc" data-parsoid='{}'/>
-<h2 id="Image" data-parsoid='{}'> Image <figure-inline class="mw-default-size" typeof="mw:Image"><a 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":"Image:foobar.jpg"}}'/></a></figure-inline> </h2>
+<h2 id="Image" data-parsoid='{}'>Image <figure-inline class="mw-default-size" typeof="mw:Image"><a 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":"Image:foobar.jpg"}}'/></a></figure-inline></h2>
 !! end
 
 !! test
@@ -24316,7 +24222,7 @@ T35845 (2): Headings become bold in TOC when they contain a blockquote
 title=[[Main Page]]
 !! wikitext
 __TOC__
-== <blockquote>Quote</blockquote> ==
+==<blockquote>Quote</blockquote>==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -24327,24 +24233,16 @@ __TOC__
 <h2><span class="mw-headline" id="Quote"><blockquote>Quote</blockquote></span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Main_Page&amp;action=edit&amp;section=1" title="Edit section: Quote">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 
 !! html/php+tidy
-<p></p>
-<div id="toc" class="toc">
-<div class="toctitle" lang="en" dir="ltr" xml:lang="en">
-<h2>Contents</h2>
-</div>
+<div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
 <li class="toclevel-1 tocsection-1"><a href="#Quote"><span class="tocnumber">1</span> <span class="toctext">Quote</span></a></li>
 </ul>
 </div>
-<p></p>
-<h2><span class="mw-headline" id="Quote"></span></h2>
-<blockquote>
-<p><span class="mw-headline" id="Quote">Quote</span></p>
-</blockquote>
-<p><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Main_Page&amp;action=edit&amp;section=1" title="Edit section: Quote">edit</a><span class="mw-editsection-bracket">]</span></span></p>
+
+<h2><span class="mw-headline" id="Quote"><blockquote><p>Quote</p></blockquote></span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Main_Page&amp;action=edit&amp;section=1" title="Edit section: Quote">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 !! html/parsoid
 <meta property="mw:PageProp/toc" data-parsoid='{}'/>
-<h2 id="Quote" data-parsoid='{}'> <blockquote>Quote</blockquote> </h2>
+<h2 id="Quote" data-parsoid='{}'><blockquote>Quote</blockquote></h2>
 !! end
 
 !! test
@@ -24355,7 +24253,7 @@ wgFragmentMode=[ 'html5', 'legacy' ]
 title=[[Main Page]]
 !! wikitext
 __TOC__
-== Proof: 2 < 3 ==
+==Proof: 2 < 3==
 <small>Hanc marginis exiguitas non caperet.</small>
 QED
 !! html/php
@@ -24371,7 +24269,7 @@ QED
 </p>
 !! html/parsoid
 <meta property="mw:PageProp/toc" data-parsoid='{}'/>
-<h2 id="Proof:_2_&lt;_3" data-parsoid='{}'><span id="Proof:_2_.3C_3" typeof="mw:FallbackId"></span> Proof: 2 &lt; 3 </h2>
+<h2 id="Proof:_2_&lt;_3" data-parsoid='{}'><span id="Proof:_2_.3C_3" typeof="mw:FallbackId"></span>Proof: 2 &lt; 3</h2>
 <p><small>Hanc marginis exiguitas non caperet.</small>
 QED</p>
 !! end
@@ -24380,9 +24278,9 @@ QED</p>
 Multiple tags in TOC
 !! wikitext
 __TOC__
-== <i>Foo</i> <b>Bar</b> ==
+==<i>Foo</i> <b>Bar</b>==
 
-== <i>Foo</i> <blockquote>Bar</blockquote> ==
+==<i>Foo</i> <blockquote>Bar</blockquote>==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -24395,28 +24293,20 @@ __TOC__
 <h2><span class="mw-headline" id="Foo_Bar_2"><i>Foo</i> <blockquote>Bar</blockquote></span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=2" title="Edit section: Foo Bar">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 
 !! html/php+tidy
-<p></p>
-<div id="toc" class="toc">
-<div class="toctitle" lang="en" dir="ltr" xml:lang="en">
-<h2>Contents</h2>
-</div>
+<div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
 <li class="toclevel-1 tocsection-1"><a href="#Foo_Bar"><span class="tocnumber">1</span> <span class="toctext"><i>Foo</i> <b>Bar</b></span></a></li>
 <li class="toclevel-1 tocsection-2"><a href="#Foo_Bar_2"><span class="tocnumber">2</span> <span class="toctext"><i>Foo</i> Bar</span></a></li>
 </ul>
 </div>
-<p></p>
+
 <h2><span class="mw-headline" id="Foo_Bar"><i>Foo</i> <b>Bar</b></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: Foo Bar">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
-<h2><span class="mw-headline" id="Foo_Bar_2"><i>Foo</i></span></h2>
-<blockquote>
-<p><span class="mw-headline" id="Foo_Bar_2">Bar</span></p>
-</blockquote>
-<p><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=2" title="Edit section: Foo Bar">edit</a><span class="mw-editsection-bracket">]</span></span></p>
+<h2><span class="mw-headline" id="Foo_Bar_2"><i>Foo</i> <blockquote><p>Bar</p></blockquote></span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=2" title="Edit section: Foo Bar">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 !! html/parsoid
 <meta property="mw:PageProp/toc" data-parsoid='{}'/>
-<h2 id="Foo_Bar" data-parsoid='{}'> <i data-parsoid='{"stx":"html"}'>Foo</i> <b data-parsoid='{"stx":"html"}'>Bar</b> </h2>
+<h2 id="Foo_Bar" data-parsoid='{}'><i data-parsoid='{"stx":"html"}'>Foo</i> <b data-parsoid='{"stx":"html"}'>Bar</b></h2>
 
-<h2 id="Foo_Bar_2" data-parsoid='{}'> <i data-parsoid='{"stx":"html"}'>Foo</i> <blockquote>Bar</blockquote> </h2>
+<h2 id="Foo_Bar_2" data-parsoid='{}'><i data-parsoid='{"stx":"html"}'>Foo</i> <blockquote>Bar</blockquote></h2>
 !! end
 
 # Don't expect Parsoid to roundtrip this until the php parser comes closer to
@@ -24427,9 +24317,9 @@ Tags with parameters in TOC
 parsoid=wt2html
 !! wikitext
 __TOC__
-== <sup class="in-h2">Hello</sup> ==
+==<sup class="in-h2">Hello</sup>==
 
-== <sup class="a > b">Evilbye</sup> ==
+==<sup class="a > b">Evilbye</sup>==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -24443,24 +24333,24 @@ __TOC__
 
 !! html/parsoid
 <meta property="mw:PageProp/toc" />
-<h2 id="Hello"> <sup class="in-h2" data-parsoid='{"stx":"html"}'>Hello</sup> </h2>
+<h2 id="Hello"><sup class="in-h2" data-parsoid='{"stx":"html"}'>Hello</sup></h2>
 
-<h2 id='b">Evilbye'><span id="b.22.3EEvilbye" typeof="mw:FallbackId"></span> <sup class="a " data-parsoid='{"stx":"html"}'> b">Evilbye</sup> </h2>
+<h2 id='b">Evilbye'><span id="b.22.3EEvilbye" typeof="mw:FallbackId"></span><sup class="a " data-parsoid='{"stx":"html"}'> b">Evilbye</sup></h2>
 !! end
 
 !! test
 span tags with directionality in TOC
 !! wikitext
 __TOC__
-== <span dir="ltr">C++</span> ==
+==<span dir="ltr">C++</span>==
 
-== <span dir="rtl">זבנג!</span> ==
+==<span dir="rtl">זבנג!</span>==
 
-== <span style="font-style: italic">The attributes on these span tags must be deleted from the TOC</span> ==
+==<span style="font-style: italic">The attributes on these span tags must be deleted from the TOC</span>==
 
-== <span style="font-style: italic" dir="ltr">All attributes on these span tags must be deleted from the TOC</span> ==
+==<span style="font-style: italic" dir="ltr">All attributes on these span tags must be deleted from the TOC</span>==
 
-== <span dir="ltr" style="font-style: italic">Attributes after dir on these span tags must be deleted from the TOC</span> ==
+==<span dir="ltr" style="font-style: italic">Attributes after dir on these span tags must be deleted from the TOC</span>==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -24480,18 +24370,18 @@ __TOC__
 
 !! html/parsoid
 <meta property="mw:PageProp/toc" data-parsoid='{}'/>
-<h2 id="C++" data-parsoid='{}'><span id="C.2B.2B" typeof="mw:FallbackId"></span> <span dir="ltr">C++</span> </h2>
-<h2 id="זבנג!"><span id=".D7.96.D7.91.D7.A0.D7.92.21" typeof="mw:FallbackId"></span> <span dir="rtl">זבנג!</span> </h2>
-<h2 id="The_attributes_on_these_span_tags_must_be_deleted_from_the_TOC"> <span style="font-style: italic">The attributes on these span tags must be deleted from the TOC</span> </h2>
-<h2 id="All_attributes_on_these_span_tags_must_be_deleted_from_the_TOC"> <span style="font-style: italic" dir="ltr">All attributes on these span tags must be deleted from the TOC</span> </h2>
-<h2 id="Attributes_after_dir_on_these_span_tags_must_be_deleted_from_the_TOC"> <span dir="ltr" style="font-style: italic">Attributes after dir on these span tags must be deleted from the TOC</span> </h2>
+<h2 id="C++" data-parsoid='{}'><span id="C.2B.2B" typeof="mw:FallbackId"></span><span dir="ltr">C++</span></h2>
+<h2 id="זבנג!"><span id=".D7.96.D7.91.D7.A0.D7.92.21" typeof="mw:FallbackId"></span><span dir="rtl">זבנג!</span></h2>
+<h2 id="The_attributes_on_these_span_tags_must_be_deleted_from_the_TOC"><span style="font-style: italic">The attributes on these span tags must be deleted from the TOC</span></h2>
+<h2 id="All_attributes_on_these_span_tags_must_be_deleted_from_the_TOC"><span style="font-style: italic" dir="ltr">All attributes on these span tags must be deleted from the TOC</span></h2>
+<h2 id="Attributes_after_dir_on_these_span_tags_must_be_deleted_from_the_TOC"><span dir="ltr" style="font-style: italic">Attributes after dir on these span tags must be deleted from the TOC</span></h2>
 !! end
 
 !! test
 T74884: bdi element in ToC
 !! wikitext
 __TOC__
-== <bdi>test</bdi> ==
+==<bdi>test</bdi>==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -24503,14 +24393,14 @@ __TOC__
 
 !! html/parsoid
 <meta property="mw:PageProp/toc" data-parsoid='{}'/>
-<h2 id="test" data-parsoid='{}'> <bdi>test</bdi> </h2>
+<h2 id="test" data-parsoid='{}'><bdi>test</bdi></h2>
 !! end
 
 !! test
 T35715: s/strike element in ToC
 !! wikitext
 __TOC__
-== <s>test</s> test <strike>test</strike> ==
+==<s>test</s> test <strike>test</strike>==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -24522,17 +24412,14 @@ __TOC__
 
 !! html/parsoid
 <meta property="mw:PageProp/toc" data-parsoid='{}'/>
-<h2 id="test_test_test" data-parsoid='{}'> <s>test</s> test <strike>test</strike> </h2>
+<h2 id="test_test_test" data-parsoid='{}'><s>test</s> test <strike>test</strike></h2>
 !! end
 
-# Note that the html output does not have the <p></p>, but the
-# html+tidy output *does*.  This is because the empty <p></p> is
-# removed by the sanitizer, but only when tidy is *not* enabled (!).
 !! test
 Empty <p> tag in TOC, removed by Sanitizer (T92892)
 !! wikitext
 __TOC__
-== x ==
+==x==
 !! html/php
 <div id="toc" class="toc"><div class="toctitle" lang="en" dir="ltr"><h2>Contents</h2></div>
 <ul>
@@ -24542,21 +24429,9 @@ __TOC__
 
 <h2><span class="mw-headline" id="x">x</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: x">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 
-!! html/php+tidy
-<p></p>
-<div id="toc" class="toc">
-<div class="toctitle" lang="en" dir="ltr" xml:lang="en">
-<h2>Contents</h2>
-</div>
-<ul>
-<li class="toclevel-1 tocsection-1"><a href="#x"><span class="tocnumber">1</span> <span class="toctext">x</span></a></li>
-</ul>
-</div>
-<p></p>
-<h2><span class="mw-headline" id="x">x</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: x">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 !! html/parsoid
 <meta property="mw:PageProp/toc" data-parsoid='{}'/>
-<h2 id="x" data-parsoid='{}'> x </h2>
+<h2 id="x" data-parsoid='{}'>x</h2>
 !! end
 
 !! article
@@ -24708,17 +24583,17 @@ new support for bdi element (T33817)
 Ignore pipe between table row attributes
 !! wikitext
 {|
-| quux
+|quux
 |- id=foo | style='color: red'
-| bar
+|bar
 |}
 !! html
 <table>
 <tr>
-<td> quux
+<td>quux
 </td></tr>
 <tr id="foo" style="color: red">
-<td> bar
+<td>bar
 </td></tr></table>
 
 !! end
@@ -24732,15 +24607,46 @@ Language parser function
 </p>
 !! end
 
+!!test
+Padleft and padright (default 0-padding)
+!! wikitext
+{{padleft:xyz|5}}
+{{padright:xyz|5}}
+!! html/php
+<p>00xyz
+xyz00
+</p>
+!! html/parsoid
+<p><span typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"padleft:xyz","function":"padleft"},"params":{"1":{"wt":"5"}},"i":0}}]}'>00xyz</span>
+<span typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"padright:xyz","function":"padright"},"params":{"1":{"wt":"5"}},"i":0}}]}'>xyz00</span></p>
+!! end
+
+!!test
+Padleft and padright (partial fill)
+!! wikitext
+{{padleft:xyz|6|ab}}
+{{padright:xyz|6|ab}}
+!! html/php
+<p>abaxyz
+xyzaba
+</p>
+!! html/parsoid
+<p><span typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"padleft:xyz","function":"padleft"},"params":{"1":{"wt":"6"},"2":{"wt":"ab"}},"i":0}}]}'>abaxyz</span>
+<span typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"padright:xyz","function":"padright"},"params":{"1":{"wt":"6"},"2":{"wt":"ab"}},"i":0}}]}'>xyzaba</span></p>
+!! end
+
 !!test
 Padleft and padright as substr
 !! wikitext
 {{padleft:|3|abcde}}
 {{padright:|3|abcde}}
-!! html
+!! html/php
 <p>abc
 abc
 </p>
+!! html/parsoid
+<p><span typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"padleft:","function":"padleft"},"params":{"1":{"wt":"3"},"2":{"wt":"abcde"}},"i":0}}]}'>abc</span>
+<span typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"padright:","function":"padright"},"params":{"1":{"wt":"3"},"2":{"wt":"abcde"}},"i":0}}]}'>abc</span></p>
 !! end
 
 !!test
@@ -24764,7 +24670,7 @@ T36939 - Case insensitive link parsing ([HttP://])
 <p><a rel="nofollow" class="external autonumber" href="HttP://MediaWiki.Org/">[1]</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="HttP://MediaWiki.Org/"></a></p>
+<p><a rel="mw:ExtLink" class="external autonumber" href="HttP://MediaWiki.Org/"></a></p>
 !! end
 
 !!test
@@ -24784,7 +24690,7 @@ HttP://MediaWiki.Org/
 <p><a rel="nofollow" class="external free" href="HttP://MediaWiki.Org/">HttP://MediaWiki.Org/</a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="HttP://MediaWiki.Org/">HttP://MediaWiki.Org/</a></p>
+<p><a rel="mw:ExtLink" class="external free" href="HttP://MediaWiki.Org/">HttP://MediaWiki.Org/</a></p>
 !! end
 
 !!test
@@ -24793,11 +24699,11 @@ Disable TOC
 notoc
 !! wikitext
 Lead
-== Section 1 ==
-== Section 2 ==
-== Section 3 ==
-== Section 4 ==
-== Section 5 ==
+==Section 1==
+==Section 2==
+==Section 3==
+==Section 4==
+==Section 5==
 !! html
 <p>Lead
 </p>
@@ -24967,6 +24873,7 @@ Empty LI and TR nodes should not be stripped from top-level content
 * a
 *
 * b
+
 {|
 |-
 |-
 
 !! end
 
+!! test
+Headings: Used as horizontal rule
+!! config
+wgFragmentMode=[ 'html5', 'legacy' ]
+!! options
+parsoid=wt2html
+!! wikitext
+===============
+!! html/php
+<h6><span id=".3D.3D.3D"></span><span class="mw-headline" id="===">===</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: ===">edit</a><span class="mw-editsection-bracket">]</span></span></h6>
+
+!! html/parsoid
+<h6 id="==="><span id=".3D.3D.3D" typeof="mw:FallbackId"></span>===</h6>
+!! end
+
 #### --------------- Lists ---------------
 #### 0. Outside nests (*foo, etc.)
 #### 1. Nested inside html <ul><li>*foo</li></ul>
@@ -25627,15 +25549,12 @@ parsoid=html2wt
 |}
 !! html/php+tidy
 <table>
+<tbody><tr>
+<td>foo|bar
+</td></tr>
 <tr>
-<td>foo|bar</td>
-</tr>
-<tr>
-<td>x
-<div>a|b</div>
-</td>
-</tr>
-</table>
+<td>x<div>a|b</div>
+</td></tr></tbody></table>
 !! end
 
 !! test
@@ -25910,9 +25829,9 @@ parsoid=html2wt
 !! html/php
 <table>
 <tr>
-<td> &lt;foo
+<td>&lt;foo
 </td>
-<td> bar&gt;
+<td>bar&gt;
 </td></tr></table>
 
 !! end
@@ -26744,8 +26663,8 @@ parsoid=wt2html,html2html
 !! wikitext
 <div title="Hello world />Foo
 !! html/php+tidy
-<div title="Hello world"></div>
-<p>Foo</p>
+<div title="Hello world"></div><p>Foo
+</p>
 !! html/parsoid
 <div title="Hello world " data-parsoid='{"stx":"html","selfClose":true}'></div><p>Foo</p>
 !! end
@@ -26802,12 +26721,12 @@ parsoid=wt2html,html2html
 Accept empty td cell attribute
 !! wikitext
 {|
-| align="center" | foo ||  |
+| align="center" |foo||  |
 |}
 !! html
 <table>
 <tr>
-<td align="center"> foo </td>
+<td align="center">foo</td>
 <td>
 </td></tr></table>
 
@@ -26817,13 +26736,13 @@ Accept empty td cell attribute
 Non-empty attributes in th-cells
 !! wikitext
 {|
-! Foo !! style="color: red" | Bar
+!Foo!! style="color: red" |Bar
 |}
 !! html
 <table>
 <tr>
-<th> Foo </th>
-<th style="color: red"> Bar
+<th>Foo</th>
+<th style="color: red">Bar
 </th></tr></table>
 
 !!end
@@ -26832,13 +26751,13 @@ Non-empty attributes in th-cells
 Accept empty attributes in th-cells
 !! wikitext
 {|
-!| foo !!| bar
+!|foo!!|bar
 |}
 !! html
 <table>
 <tr>
-<th> foo </th>
-<th> bar
+<th>foo</th>
+<th>bar
 </th></tr></table>
 
 !!end
@@ -26847,17 +26766,17 @@ Accept empty attributes in th-cells
 Empty table rows go away
 !! wikitext
 {|
-| Hello
-| there
+|Hello
+|there
 |- class="foo"
 |-
 |}
 !! html
 <table>
 <tr>
-<td> Hello
+<td>Hello
 </td>
-<td> there
+<td>there
 </td></tr>
 
 </table>
@@ -26887,11 +26806,9 @@ RT-ed inter-element separators should be valid separators
 </tbody></table>
 !!end
 
-# Parsoid-only since PHP parser relies on Tidy for correct output
+# Parsoid-only test of a DOM pass
 !!test
 Trailing newlines in a deep dom-subtree that ends a wikitext line should be migrated out
-!!options
-parsoid
 !! wikitext
 {|
 |<small>foo
@@ -26901,7 +26818,7 @@ bar
 {|
 |<small>foo<small>
 |}
-!! html
+!! html/parsoid
 <table>
 <tbody><tr data-parsoid='{"autoInsertedEnd":true,"autoInsertedStart":true}'><td data-parsoid='{"autoInsertedEnd":true}'><small data-parsoid='{"stx":"html","autoInsertedEnd":true}'>foo
 <p>bar</p></small></td></tr>
@@ -27001,13 +26918,13 @@ Indent and comment before table row
 !! wikitext
 {|
  <!--hi-->|-
- | there
+ |there
 |}
 !! html/php
 <table>
 
 <tr>
-<td> there
+<td>there
 </td></tr></table>
 
 !! html/parsoid
@@ -27940,7 +27857,7 @@ parsoid=html2wt
 !! html/parsoid
 <ul><li>a<br>b</li><li>c</li></ul>
 !! wikitext
-* a<br>b
+* a<br />b
 * c
 !! end
 
@@ -28473,7 +28390,7 @@ WTS of autolinks with nowikis (round-trip)
 !! wikitext
 x<nowiki/>http://cscott.net<nowiki/>x
 !! html/parsoid
-<p>x<a rel="mw:ExtLink" href="http://cscott.net">http://cscott.net</a>x</p>
+<p>x<a rel="mw:ExtLink" class="external free" href="http://cscott.net">http://cscott.net</a>x</p>
 !! end
 
 # this is the "easy" test because it leaves in place all the
@@ -28541,18 +28458,25 @@ Magic links inside links (not autolinked)
 [http://foo.com PMID 1234]
 [http://foo.com ISBN 123456789x]
 !! html+tidy
-<p><a href="/wiki/Foo" title="Foo">http://example.com</a> <a href="/wiki/Foo" title="Foo">RFC 1234</a> <a href="/wiki/Foo" title="Foo">PMID 1234</a> <a href="/wiki/Foo" title="Foo">ISBN 123456789x</a></p>
-<p><a rel="nofollow" class="external text" href="http://foo.com">http://example.com</a> <a rel="nofollow" class="external text" href="http://foo.com">RFC 1234</a> <a rel="nofollow" class="external text" href="http://foo.com">PMID 1234</a> <a rel="nofollow" class="external text" href="http://foo.com">ISBN 123456789x</a></p>
+<p><a href="/wiki/Foo" title="Foo">http://example.com</a>
+<a href="/wiki/Foo" title="Foo">RFC 1234</a>
+<a href="/wiki/Foo" title="Foo">PMID 1234</a>
+<a href="/wiki/Foo" title="Foo">ISBN 123456789x</a>
+</p><p><a rel="nofollow" class="external text" href="http://foo.com">http://example.com</a>
+<a rel="nofollow" class="external text" href="http://foo.com">RFC 1234</a>
+<a rel="nofollow" class="external text" href="http://foo.com">PMID 1234</a>
+<a rel="nofollow" class="external text" href="http://foo.com">ISBN 123456789x</a>
+</p>
 !! html/parsoid
 <p><a rel="mw:WikiLink" href="./Foo" title="Foo">http://example.com</a>
 <a rel="mw:WikiLink" href="./Foo" title="Foo">RFC 1234</a>
 <a rel="mw:WikiLink" href="./Foo" title="Foo">PMID 1234</a>
 <a rel="mw:WikiLink" href="./Foo" title="Foo">ISBN 123456789x</a></p>
 
-<p><a rel="mw:ExtLink" href="http://foo.com">http://example.com</a>
-<a rel="mw:ExtLink" href="http://foo.com">RFC 1234</a>
-<a rel="mw:ExtLink" href="http://foo.com">PMID 1234</a>
-<a rel="mw:ExtLink" href="http://foo.com">ISBN 123456789x</a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://foo.com">http://example.com</a>
+<a rel="mw:ExtLink" class="external text" href="http://foo.com">RFC 1234</a>
+<a rel="mw:ExtLink" class="external text" href="http://foo.com">PMID 1234</a>
+<a rel="mw:ExtLink" class="external text" href="http://foo.com">ISBN 123456789x</a></p>
 !! end
 
 !! test
@@ -28563,38 +28487,14 @@ Magic links inside image captions (autolinked)
 [[File:Foobar.jpg|thumb|PMID 1234]]
 [[File:Foobar.jpg|thumb|ISBN 123456789x]]
 !! html+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>
-<a rel="nofollow" class="external free" href="http://example.com">http://example.com</a></div>
-</div>
-</div>
-<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>
-<a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc1234">RFC 1234</a></div>
-</div>
-</div>
-<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>
-<a class="external mw-magiclink-pmid" rel="nofollow" href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract">PMID 1234</a></div>
-</div>
-</div>
-<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>
-<a href="/wiki/Special:BookSources/123456789X" class="internal mw-magiclink-isbn">ISBN 123456789x</a></div>
-</div>
-</div>
-!! html/parsoid
-<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption><a rel="mw:ExtLink" href="http://example.com">http://example.com</a></figcaption></figure>
-<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption><a href="https://tools.ietf.org/html/rfc1234" rel="mw:ExtLink">RFC 1234</a></figcaption></figure>
-<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption><a href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract" rel="mw:ExtLink">PMID 1234</a></figcaption></figure>
+<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><a rel="nofollow" class="external free" href="http://example.com">http://example.com</a></div></div></div>
+<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><a class="external mw-magiclink-rfc" rel="nofollow" href="https://tools.ietf.org/html/rfc1234">RFC 1234</a></div></div></div>
+<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><a class="external mw-magiclink-pmid" rel="nofollow" href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract">PMID 1234</a></div></div></div>
+<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><a href="/wiki/Special:BookSources/123456789X" class="internal mw-magiclink-isbn">ISBN 123456789x</a></div></div></div>
+!! html/parsoid
+<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption><a rel="mw:ExtLink" class="external free" href="http://example.com">http://example.com</a></figcaption></figure>
+<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption><a href="https://tools.ietf.org/html/rfc1234" rel="mw:ExtLink" class="external text">RFC 1234</a></figcaption></figure>
+<figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption><a href="//www.ncbi.nlm.nih.gov/pubmed/1234?dopt=Abstract" rel="mw:ExtLink" class="external text">PMID 1234</a></figcaption></figure>
 <figure class="mw-default-size" typeof="mw:Image/Thumb"><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"/></a><figcaption><a href="./Special:BookSources/123456789X" rel="mw:WikiLink">ISBN 123456789x</a></figcaption></figure>
 !! end
 
@@ -28657,31 +28557,34 @@ parsoid=html2wt,wt2wt
 |<nowiki>- </nowiki>
 |-
 |<small>-</small>
-|<br>
+|<br />
 -
-|<br>
+|<br />
 -
 |}
 !! html/php+tidy
 <table>
+<tbody><tr>
+<th>-
+</th>
+<th>-
+</th></tr>
 <tr>
-<th>-</th>
-<th>-</th>
-</tr>
-<tr>
-<td>-</td>
-<td>-</td>
-</tr>
+<td>-
+</td>
+<td>- 
+</td></tr>
 <tr>
-<td><small>-</small></td>
-<td><br />
-<p>-</p>
+<td><small>-</small>
 </td>
 <td><br />
-<p>-</p>
+<p>-
+</p>
 </td>
-</tr>
-</table>
+<td><br />
+<p>-
+</p>
+</td></tr></tbody></table>
 !! end
 
 !! test
@@ -28865,7 +28768,7 @@ parsoid=wt2html
 !! wikitext
 {{echo|hi}}[http://example.com [[ho]]]
 !! html/parsoid
-<p><span about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"hi"}},"i":0}}]}'>hi</span><a rel="mw:ExtLink" href="http://example.com"></a><a rel="mw:WikiLink" href="./Ho" title="Ho" data-parsoid='{"misnested":true}'>ho</a></p>
+<p><span about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"hi"}},"i":0}}]}'>hi</span><a rel="mw:ExtLink" class="external autonumber" href="http://example.com"></a><a rel="mw:WikiLink" href="./Ho" title="Ho" data-parsoid='{"misnested":true}'>ho</a></p>
 !! end
 
 !! test
@@ -28966,7 +28869,7 @@ foo {{echo|<span>bar</span> [[Category:baz]]}} bar
 # of the categories in wikitext
 # Do not remove these characters in edits.
 #
-# As part of the serialization, these bidi characters will get stripped.
+# As part of the serialization, these Unicode directional formatting characters will get stripped.
 !! test
 RTL (\u200f) and LTR (\u200e) markers around category tags should be stripped
 !! options
@@ -29109,7 +29012,7 @@ parsoid={
 !! html/parsoid
 <h2>foo<br/>bar</h2>
 !! wikitext
-== foo<br> bar ==
+== foo<br /> bar ==
 !! end
 
 !! test
 }}
 !! end
 
+!! test
+New list is serialized on newlines
+!! options
+parsoid=html2wt
+!! html/parsoid
+<p>The quick brown fox jumps over the lazy dog.</p><ul>
+<li>Yesterday</li>
+<li>Today</li>
+<li>Tomorrow</li>
+</ul><p>The quick onyx goblin jumps over the lazy dwarf.</p>
+!! wikitext
+The quick brown fox jumps over the lazy dog.
+
+* Yesterday
+* Today
+* Tomorrow
+
+The quick onyx goblin jumps over the lazy dwarf.
+!! end
+
+!! test
+New lists in formatting elements serialized w/o newlines
+!! options
+parsoid=html2wt
+!! html/parsoid
+<small>
+
+<ul>
+<li>123</li>
+</ul>
+
+</small>
+
+<small><ul><li>hi</li></ul></small>
+!! wikitext
+<small>
+* 123
+</small>
+
+<small>
+* hi
+</small>
+!! end
+
+!! test
+New list in table doesn't need newlines
+!! options
+parsoid=html2wt
+!! html/parsoid
+<table><tr><td><ul><li>test</li><li>123</li></td></tr></table>
+!! wikitext
+{|
+|
+* test
+* 123
+|}
+!! end
+
 # ---------------------------------------------------
 # End of tests spec'ing wikitext serialization norms |
 # ---------------------------------------------------
@@ -29819,17 +29780,15 @@ parsoid={
 !! test
 Empty LI (T49673)
 !! wikitext
-* a
+*a
 * 
 *
-* b
-!! html/php+tidy
-<ul>
-<li>a</li>
+*b
+!! html+tidy
+<ul><li>a</li>
 <li class="mw-empty-elt"></li>
 <li class="mw-empty-elt"></li>
-<li>b</li>
-</ul>
+<li>b</li></ul>
 !! end
 
 !! test
@@ -29837,13 +29796,9 @@ Thumbnail output
 !! wikitext
 [[File:Thumb.png|thumb]]
 !! html/php+tidy
-<div class="thumb tright">
-<div class="thumbinner" style="width:137px;"><a href="/wiki/File:Thumb.png" class="image"><img alt="Thumb.png" src="http://example.com/images/e/ea/Thumb.png" width="135" height="135" class="thumbimage" /></a>
-<div class="thumbcaption">
-<div class="magnify"><a href="/wiki/File:Thumb.png" class="internal" title="Enlarge"></a></div>
-</div>
-</div>
-</div>
+<div class="thumb tright"><div class="thumbinner" style="width:137px;"><a href="/wiki/File:Thumb.png" class="image"><img alt="Thumb.png" src="http://example.com/images/e/ea/Thumb.png" width="135" height="135" class="thumbimage" /></a>  <div class="thumbcaption"><div class="magnify"><a href="/wiki/File:Thumb.png" class="internal" title="Enlarge"></a></div></div></div></div>
+!! html/parsoid
+<figure class="mw-default-size" typeof="mw:Image/Thumb"><a href="./File:Thumb.png"><img resource="./File:Thumb.png" src="//example.com/images/e/ea/Thumb.png" data-file-width="135" data-file-height="135" data-file-type="bitmap" height="135" width="135"/></a></figure>
 !! end
 
 !! test
@@ -29857,16 +29812,14 @@ unclosed internal link XSS (T137264)
 <p>[[#%3Cscript%3Ealert(1)%3C/script%3E|</p>
 !! end
 
-# Use $wgRawHtml to inject a <style> tag, since you normally can't in wikitext
-# (Parsoid doesn't support $wgRawHtml==true)
 !! test
 Validating that <style> isn't eaten by tidy (T167349)
 !! options
-wgRawHtml=1
+styletag=1
 !! wikitext
 <div class="foo">
-<html><style>.foo::before { content: "<foo>"; }</style></html>
-<html><style data-mw-foobar="baz">.foo::after { content: "<bar>"; }</style></html>
+<style>.foo::before { content: "<foo>"; }</style>
+<style data-mw-foobar="baz">.foo::after { content: "<bar>"; }</style>
 </div>
 !! html/php+tidy
 <div class="foo">
@@ -29875,19 +29828,99 @@ wgRawHtml=1
 </div>
 !! end
 
+!! test
+Validating that <style> isn't wrapped in a paragraph (T186965)
+!! options
+styletag=1
+!! wikitext
+A style tag, by itself or with other style/link tags, shouldn't be wrapped in a paragraph
+
+<style>.foo::before { content: "<foo>"; }</style>
+
+<style>.foo::before { content: "<foo>"; }</style> <link rel="foo" href="bar"/><style>.foo::before { content: "<foo>"; }</style>
+
+But if it's on a line with other content, let it be wrapped.
+
+<style>.foo::before { content: "<foo>"; }</style> bar
+
+foo <style>.foo::before { content: "<foo>"; }</style>
+
+foo <style>.foo::before { content: "<foo>"; }</style> bar
+
+And the same if we have non-paragraph-breaking whitespace
+
+foo
+<style>.foo::before { content: "<foo>"; }</style>
+bar
+!! html/php
+<p>A style tag, by itself or with other style/link tags, shouldn't be wrapped in a paragraph
+</p>
+<style>.foo::before { content: "<foo>"; }</style>
+<style>.foo::before { content: "<foo>"; }</style> <link rel="foo" href="bar"/><style>.foo::before { content: "<foo>"; }</style>
+<p>But if it's on a line with other content, let it be wrapped.
+</p><p><style>.foo::before { content: "<foo>"; }</style> bar
+</p><p>foo <style>.foo::before { content: "<foo>"; }</style>
+</p><p>foo <style>.foo::before { content: "<foo>"; }</style> bar
+</p><p>And the same if we have non-paragraph-breaking whitespace
+</p><p>foo
+<style>.foo::before { content: "<foo>"; }</style>
+bar
+</p>
+!! end
+
+!! test
+Validating that <link> isn't wrapped in a paragraph (T186965)
+!! options
+styletag=1
+!! wikitext
+A link tag, by itself or with other style/link tags, shouldn't be wrapped in a paragraph
+
+<link rel="foo" href="bar"/>
+
+<link rel="foo" href="bar"/> <style>.foo::before { content: "<foo>"; }</style><link rel="foo" href="bar"/>
+
+But if it's on a line with other content, let it be wrapped.
+
+<link rel="foo" href="bar"/> bar
+
+foo <link rel="foo" href="bar"/>
+
+foo <link rel="foo" href="bar"/> bar
+
+And the same if we have non-paragraph-breaking whitespace
+
+foo
+<link rel="foo" href="bar"/>
+bar
+!! html/php
+<p>A link tag, by itself or with other style/link tags, shouldn't be wrapped in a paragraph
+</p>
+<link rel="foo" href="bar"/>
+<link rel="foo" href="bar"/> <style>.foo::before { content: "<foo>"; }</style><link rel="foo" href="bar"/>
+<p>But if it's on a line with other content, let it be wrapped.
+</p><p><link rel="foo" href="bar"/> bar
+</p><p>foo <link rel="foo" href="bar"/>
+</p><p>foo <link rel="foo" href="bar"/> bar
+</p><p>And the same if we have non-paragraph-breaking whitespace
+</p><p>foo
+<link rel="foo" href="bar"/>
+bar
+</p>
+!! end
+
 !! test
 Decoding of HTML entities in headings and links for IDs and link fragments (T103714)
 !! config
 wgFragmentMode=[ 'html5', 'legacy' ]
 !! wikitext
-== A&B&amp;C&amp;amp;D&amp;amp;amp;E ==
+==A&B&amp;C&amp;amp;D&amp;amp;amp;E==
 [[#A&B&amp;C&amp;amp;D&amp;amp;amp;E]]
 !! html/php
 <h2><span id="A.26B.26C.26amp.3BD.26amp.3Bamp.3BE"></span><span class="mw-headline" id="A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E">A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E</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&amp;B&amp;C&amp;amp;D&amp;amp;amp;E">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 <p><a href="#A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E">#A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E</a>
 </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>
+<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>
 !! end
 
@@ -29896,7 +29929,7 @@ Decoding of HTML entities in headings and links for IDs and link fragments (T103
 !! config
 wgFragmentMode=[ 'legacy' ]
 !! wikitext
-== A&B&amp;C&amp;amp;D&amp;amp;amp;E ==
+==A&B&amp;C&amp;amp;D&amp;amp;amp;E==
 [[#A&B&amp;C&amp;amp;D&amp;amp;amp;E]]
 !! html/php
 <h2><span class="mw-headline" id="A.26B.26C.26amp.3BD.26amp.3Bamp.3BE">A&amp;B&amp;C&amp;amp;D&amp;amp;amp;E</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&amp;B&amp;C&amp;amp;D&amp;amp;amp;E">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
@@ -29949,17 +29982,17 @@ HTML5 ids: fallback to legacy
 !! config
 wgFragmentMode=[ 'html5', 'legacy' ]
 !! wikitext
-== Foo bar ==
+==Foo bar==
 
-== foo Bar ==
+==foo Bar==
 
-== Тест ==
+==Тест==
 
-== Тест ==
+==Тест==
 
-== тест ==
+==тест==
 
-== Hey < # " > % : ' ==
+==Hey < # " > % : '==
 [[#Foo bar]] [[#foo Bar]] [[#Тест]] [[#тест]] [[#Hey < # " > % : ']]
 
 {{anchorencode:💩}} <span id="{{anchorencode:💩}}"></span>
@@ -29990,20 +30023,20 @@ wgFragmentMode=[ 'html5', 'legacy' ]
 </p><p><a href="#啤酒">#啤酒</a> <a href="#啤酒">#啤酒</a>
 </p>
 !! html/parsoid
-<h2 id="Foo_bar"> Foo bar </h2>
+<h2 id="Foo_bar">Foo bar</h2>
 
-<h2 id="foo_Bar_2"> foo Bar </h2>
+<h2 id="foo_Bar_2">foo Bar</h2>
 
-<h2 id="Тест"><span id=".D0.A2.D0.B5.D1.81.D1.82" typeof="mw:FallbackId"></span> Тест </h2>
+<h2 id="Тест"><span id=".D0.A2.D0.B5.D1.81.D1.82" typeof="mw:FallbackId"></span>Тест</h2>
 
-<h2 id="Тест_2"><span id=".D0.A2.D0.B5.D1.81.D1.82_2" typeof="mw:FallbackId"></span> Тест </h2>
+<h2 id="Тест_2"><span id=".D0.A2.D0.B5.D1.81.D1.82_2" typeof="mw:FallbackId"></span>Тест</h2>
 
-<h2 id="тест"><span id=".D1.82.D0.B5.D1.81.D1.82" typeof="mw:FallbackId"></span> тест </h2>
+<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>
+<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><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\":[190,209,null,null]}&#39; data-mw=&#39;{\"parts\":[{\"template\":{\"target\":{\"wt\":\"anchorencode:💩\",\"function\":\"anchorencode\"},\"params\":{},\"i\":0}}]}&#39;>💩&lt;/span>"}]]}'></span></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>
@@ -30015,17 +30048,17 @@ HTML5 ids: legacy with a fallback to modern
 !! config
 wgFragmentMode=[ 'legacy', 'html5' ]
 !! wikitext
-== Foo bar ==
+==Foo bar==
 
-== foo Bar ==
+==foo Bar==
 
-== Тест ==
+==Тест==
 
-== Тест ==
+==Тест==
 
-== тест ==
+==тест==
 
-== Hey < # " > % : ' ==
+==Hey < # " > % : '==
 [[#Foo bar]] [[#foo Bar]] [[#Тест]] [[#тест]] [[#Hey < # " > % : ']]
 
 {{anchorencode:💩}} <span id="{{anchorencode:💩}}"></span>
@@ -30063,17 +30096,17 @@ HTML5 ids: no legacy
 !! config
 wgFragmentMode=[ 'html5' ]
 !! wikitext
-== Foo bar ==
+==Foo bar==
 
-== foo Bar ==
+==foo Bar==
 
-== Тест ==
+==Тест==
 
-== Тест ==
+==Тест==
 
-== тест ==
+==тест==
 
-== Hey < # " > % : ' ==
+==Hey < # " > % : '==
 [[#Foo bar]] [[#foo Bar]] [[#Тест]] [[#тест]] [[#Hey < # " > % : ']]
 
 {{anchorencode:💩}} <span id="{{anchorencode:💩}}"></span>
@@ -30110,13 +30143,16 @@ T90902: Normalize weird characters in section IDs
 !! config
 wgFragmentMode=[ 'html5', 'legacy' ]
 !! wikitext
-== Foo&nbsp;bar ==
+==Foo&nbsp;bar==
 [[#Foo&nbsp;bar]]
 
 !! html/php
 <h2><span class="mw-headline" id="Foo_bar">Foo&#160;bar</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: Foo bar">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 <p><a href="#Foo_bar">#Foo&#160;bar</a>
 </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>
 !! end
 
 !! test
@@ -30130,8 +30166,8 @@ T51672: Test for brackets in attributes of elements in external link texts
 <a rel="nofollow" class="external text" href="http://example.com/">link <span title="title with &#91;brackets&#93;">span</span></a>
 </p>
 !! html/parsoid
-<p><a rel="mw:ExtLink" href="http://example.com/">link <span title="title with [brackets]">span</span></a>
-<a rel="mw:ExtLink" href="http://example.com/">link <span title="title with [brackets]" data-parsoid='{"stx":"html","a":{"title":"title with [brackets]"},"sa":{"title":"title with &amp;#91;brackets&amp;#93;"}}'>span</span></a></p>
+<p><a rel="mw:ExtLink" class="external text" href="http://example.com/">link <span title="title with [brackets]">span</span></a>
+<a rel="mw:ExtLink" class="external text" href="http://example.com/">link <span title="title with [brackets]" data-parsoid='{"stx":"html","a":{"title":"title with [brackets]"},"sa":{"title":"title with &amp;#91;brackets&amp;#93;"}}'>span</span></a></p>
 !! end
 
 !! test
@@ -30172,40 +30208,40 @@ parsoid={
   "wrapSections": true
 }
 !! wikitext
-= 1 =
+=1=
 a
 
-= 2 =
+=2=
 b
 
-== 2.1 ==
+==2.1==
 c
 
-== 2.2 ==
+==2.2==
 d
 
-=== 2.2.1 ===
+===2.2.1===
 e
 
-= 3 =
+=3=
 f
 !! html/parsoid
-<section data-mw-section-id="0"></section><section data-mw-section-id="1"><h1 id="1"> 1 </h1>
+<section data-mw-section-id="0"></section><section data-mw-section-id="1"><h1 id="1">1</h1>
 <p>a</p>
 
-</section><section data-mw-section-id="2"><h1 id="2"> 2 </h1>
+</section><section data-mw-section-id="2"><h1 id="2">2</h1>
 <p>b</p>
 
-<section data-mw-section-id="3"><h2 id="2.1"> 2.1 </h2>
+<section data-mw-section-id="3"><h2 id="2.1">2.1</h2>
 <p>c</p>
 
-</section><section data-mw-section-id="4"><h2 id="2.2"> 2.2 </h2>
+</section><section data-mw-section-id="4"><h2 id="2.2">2.2</h2>
 <p>d</p>
 
-<section data-mw-section-id="5"><h3 id="2.2.1"> 2.2.1 </h3>
+<section data-mw-section-id="5"><h3 id="2.2.1">2.2.1</h3>
 <p>e</p>
 
-</section></section></section><section data-mw-section-id="6"><h1 id="3"> 3 </h1>
+</section></section></section><section data-mw-section-id="6"><h1 id="3">3</h1>
 <p>f</p>
 
 </section>
@@ -30224,13 +30260,13 @@ Para 2 with a <div>nested in it</div>
 
 Para 3.
 
-= 1 =
+=1=
 a
 
-= 2 =
+=2=
 b
 
-== 2.1 ==
+==2.1==
 c
 !! html/parsoid
 <section data-mw-section-id="0"><p>Para 1.</p>
 
 <p>Para 3.</p>
 
-</section><section data-mw-section-id="1"><h1 id="1"> 1 </h1>
+</section><section data-mw-section-id="1"><h1 id="1">1</h1>
 <p>a</p>
 
-</section><section data-mw-section-id="2"><h1 id="2"> 2 </h1>
+</section><section data-mw-section-id="2"><h1 id="2">2</h1>
 <p>b</p>
 
-<section data-mw-section-id="3"><h2 id="2.1"> 2.1 </h2>
+<section data-mw-section-id="3"><h2 id="2.1">2.1</h2>
 <p>c</p>
 
 </section></section>
@@ -30258,29 +30294,29 @@ parsoid={
   "wrapSections": true
 }
 !! wikitext
-= 1 =
+=1=
 a
 
 {{echo|1=
-== 1.1 ==
+==1.1==
 b
 }}
 
-== 1.2 ==
+==1.2==
 c
 
-= 2 =
+=2=
 d
 !! html/parsoid
-<section data-mw-section-id="0"></section><section data-mw-section-id="1"><h1 id="1"> 1 </h1>
+<section data-mw-section-id="0"></section><section data-mw-section-id="1"><h1 id="1">1</h1>
 <p>a</p>
 
-<section data-mw-section-id="-1"><h2 about="#mwt1" typeof="mw:Transclusion" id="1.1" data-parsoid='{"dsr":[9,33,null,null],"pi":[[{"k":"1","named":true,"spc":["","","\n","\n"]}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"== 1.1 ==\nb"}},"i":0}}]}'> 1.1 </h2><span about="#mwt1">
+<section data-mw-section-id="-1"><h2 about="#mwt1" typeof="mw:Transclusion" id="1.1" data-parsoid='{"dsr":[9,33,null,null],"pi":[[{"k":"1","named":true,"spc":["","","\n","\n"]}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"==1.1==\nb"}},"i":0}}]}'>1.1</h2><span about="#mwt1">
 </span><p about="#mwt1">b</p>
-</section><section data-mw-section-id="3"><h2 id="1.2"> 1.2 </h2>
+</section><section data-mw-section-id="3"><h2 id="1.2">1.2</h2>
 <p>c</p>
 
-</section></section><section data-mw-section-id="4"><h1 id="2"> 2 </h1>
+</section></section><section data-mw-section-id="4"><h1 id="2">2</h1>
 <p>d</p></section>
 !! end
 
@@ -30294,26 +30330,26 @@ parsoid={
   "modes": ["wt2html", "wt2wt"]
 }
 !! wikitext
-= 1 =
+=1=
 a
 
 {{echo|1=
-== 1.1 ==
+==1.1==
 b
-=== 1.1.1 ===
+===1.1.1===
 d
 }}
-= 2 =
+=2=
 e
 !! html/parsoid
-<section data-mw-section-id="0"></section><section data-mw-section-id="1"><h1 id="1"> 1 </h1>
+<section data-mw-section-id="0"></section><section data-mw-section-id="1"><h1 id="1">1</h1>
 <p>a</p>
 
-<section data-mw-section-id="-1"><h2 about="#mwt1" typeof="mw:Transclusion" id="1.1" data-parsoid='{"dsr":[9,50,null,null],"pi":[[{"k":"1","named":true,"spc":["","","\n","\n"]}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"== 1.1 ==\nb\n=== 1.1.1 ===\nd"}},"i":0}},"\n"]}'> 1.1 </h2><span about="#mwt1">
+<section data-mw-section-id="-1"><h2 about="#mwt1" typeof="mw:Transclusion" id="1.1" data-parsoid='{"dsr":[9,50,null,null],"pi":[[{"k":"1","named":true,"spc":["","","\n","\n"]}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"==1.1==\nb\n===1.1.1===\nd"}},"i":0}},"\n"]}'>1.1</h2><span about="#mwt1">
 </span><p about="#mwt1">b</p><span about="#mwt1">
-</span><section data-mw-section-id="-1" about="#mwt1"><h3 about="#mwt1" id="1.1.1"> 1.1.1 </h3><span about="#mwt1">
+</span><section data-mw-section-id="-1" about="#mwt1"><h3 about="#mwt1" id="1.1.1">1.1.1</h3><span about="#mwt1">
 </span><p about="#mwt1">d</p><span about="#mwt1">
-</span></section></section></section><section data-mw-section-id="4" data-parsoid="{}"><h1 id="2"> 2 </h1>
+</span></section></section></section><section data-mw-section-id="4" data-parsoid="{}"><h1 id="2">2</h1>
 <p>e</p></section>
 !! end
 
@@ -30327,32 +30363,32 @@ parsoid={
   "modes": ["wt2html", "wt2wt"]
 }
 !! wikitext
-= 1 =
+=1=
 a
 
 {{echo|1=
 x
-== 1.1 ==
+==1.1==
 b
 ==1.2==
 c
 ===1.2.1===
 d
 }}
-= 2 =
+=2=
 e
 !! html/parsoid
 <section data-mw-section-id="0"></section><section data-mw-section-id="1" data-parsoid="{}"><h1 id="1"> 1 </h1>
 <p>a</p>
 
-<p about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"dsr":[9,60,0,0],"pi":[[{"k":"1","named":true,"spc":["","","\n","\n"]}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"x\n== 1.1 ==\nb\n==1.2==\nc\n===1.2.1===\nd"}},"i":0}},"\n"]}'>x</p><span about="#mwt1">
-</span><section data-mw-section-id="-1" about="#mwt1"><h2 about="#mwt1" id="1.1"> 1.1 </h2><span about="#mwt1">
+<p about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"dsr":[9,60,0,0],"pi":[[{"k":"1","named":true,"spc":["","","\n","\n"]}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"x\n==1.1==\nb\n==1.2==\nc\n===1.2.1===\nd"}},"i":0}},"\n"]}'>x</p><span about="#mwt1">
+</span><section data-mw-section-id="-1" about="#mwt1"><h2 about="#mwt1" id="1.1">1.1</h2><span about="#mwt1">
 </span><p about="#mwt1">b</p><span about="#mwt1">
 </span></section><section data-mw-section-id="-1" about="#mwt1"><h2 about="#mwt1" id="1.2">1.2</h2><span about="#mwt1">
 </span><p about="#mwt1">c</p><span about="#mwt1">
 </span><section data-mw-section-id="-1" about="#mwt1"><h3 about="#mwt1" id="1.2.1">1.2.1</h3><span about="#mwt1">
 </span><p about="#mwt1">d</p><span about="#mwt1">
-</span></section></section></section><section data-mw-section-id="5"><h1 id="2"> 2 </h1>
+</span></section></section></section><section data-mw-section-id="5"><h1 id="2">2</h1>
 <p>e</p></section>
 !! end
 
@@ -30371,7 +30407,7 @@ parsoid={
 a
 
 {{echo|
-= 1 =
+=1=
 b
 }}
 
 <section data-mw-section-id="-1"></section><section data-mw-section-id="-2"><div data-parsoid='{"stx":"html"}'>
 <p>a</p>
 
-<span about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"\n= 1 =\nb\n"}},"i":0}},"\n\nc\n"]}'>
-</span><section data-mw-section-id="-1" about="#mwt1"><h1 about="#mwt1" id="1"> 1 </h1><span about="#mwt1">
+<span about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"\n=1=\nb\n"}},"i":0}},"\n\nc\n"]}'>
+</span><section data-mw-section-id="-1" about="#mwt1"><h1 about="#mwt1" id="1">1</h1><span about="#mwt1">
 </span><p about="#mwt1">b
 </p><span about="#mwt1">
 
@@ -30401,32 +30437,32 @@ parsoid={
   "wrapSections": true
 }
 !! wikitext
-= 1 =
+=1=
 a
 
 {{echo|1=
-= 2 =
+=2=
 b
-== 2.1 ==
+==2.1==
 c
 }}
 
 d
 
-= 3 =
+=3=
 e
 !! html/parsoid
-<section data-mw-section-id="0"></section><section data-mw-section-id="1"><h1 id="1"> 1 </h1>
+<section data-mw-section-id="0"></section><section data-mw-section-id="1"><h1 id="1">1</h1>
 <p>a</p>
 
-</section><section data-mw-section-id="-1"><h1 about="#mwt1" typeof="mw:Transclusion" id="2" data-parsoid='{"dsr":[9,45,null,null],"pi":[[{"k":"1","named":true,"spc":["","","\n","\n"]}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"= 2 =\nb\n== 2.1 ==\nc"}},"i":0}},"\n\nd\n\n"]}'> 2 </h1><span about="#mwt1">
+</section><section data-mw-section-id="-1"><h1 about="#mwt1" typeof="mw:Transclusion" id="2" data-parsoid='{"dsr":[9,45,null,null],"pi":[[{"k":"1","named":true,"spc":["","","\n","\n"]}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"=2=\nb\n==2.1==\nc"}},"i":0}},"\n\nd\n\n"]}'>2</h1><span about="#mwt1">
 </span><p about="#mwt1">b</p><span about="#mwt1">
-</span><section data-mw-section-id="-1" about="#mwt1"><h2 about="#mwt1" id="2.1"> 2.1 </h2><span about="#mwt1">
+</span><section data-mw-section-id="-1" about="#mwt1"><h2 about="#mwt1" id="2.1">2.1</h2><span about="#mwt1">
 </span><p about="#mwt1">c</p><span about="#mwt1">
 
 </span><p about="#mwt1">d</p><span about="#mwt1">
 
-</span></section></section><section data-mw-section-id="4"><h1 id="3"> 3 </h1>
+</span></section></section><section data-mw-section-id="4"><h1 id="3">3</h1>
 <p>e</p></section>
 !! end
 
@@ -30444,31 +30480,31 @@ parsoid={
   "modes": ["wt2html", "wt2wt"]
 }
 !! wikitext
-= 1 =
+=1=
 a
 
 {{echo|1=
-== 1.2 ==
+==1.2==
 b
-= 2 =
+=2=
 c
 }}
 
 d
 
-= 3 =
+=3=
 e
 !! html/parsoid
-<section data-mw-section-id="0"></section><section data-mw-section-id="1" about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":["= 1 =\na\n\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"== 1.2 ==\nb\n= 2 =\nc"}},"i":0}},"\n\nd\n\n"]}'><h1 id="1"> 1 </h1>
+<section data-mw-section-id="0"></section><section data-mw-section-id="1" about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":["=1=\na\n\n",{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"==1.2==\nb\n=2=\nc"}},"i":0}},"\n\nd\n\n"]}'><h1 id="1">1</h1>
 <p>a</p>
 
-<section data-mw-section-id="-1"><h2 about="#mwt1" typeof="mw:Transclusion" id="1.2" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"== 1.2 ==\nb\n= 2 =\nc"}},"i":0}}]}'> 1.2 </h2><span about="#mwt1">
+<section data-mw-section-id="-1"><h2 about="#mwt1" typeof="mw:Transclusion" id="1.2" data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"==1.2==\nb\n=2=\nc"}},"i":0}}]}'>1.2</h2><span about="#mwt1">
 </span><p about="#mwt1">b</p><span about="#mwt1">
-</span></section></section><section data-mw-section-id="-1" about="#mwt1"><h1 about="#mwt1" id="2"> 2 </h1><span about="#mwt1">
+</span></section></section><section data-mw-section-id="-1" about="#mwt1"><h1 about="#mwt1" id="2">2</h1><span about="#mwt1">
 </span><p about="#mwt1">c</p>
 
 <p>d</p>
-</section><section data-mw-section-id="4" data-parsoid="{}"><h1 id="3"> 3 </h1>
+</section><section data-mw-section-id="4" data-parsoid="{}"><h1 id="3">3</h1>
 <p>e</p></section>
 !! end
 
@@ -30482,39 +30518,39 @@ parsoid={
 foo
 
 <div style="border:1px solid red;">
-= 1 =
+=1=
 a
 
-== 1.1 ==
+==1.1==
 b
 
-= 2 =
+=2=
 c
 </div>
 
-= 3 =
+=3=
 d
 
-== 3.1 ==
+==3.1==
 e
 !! html/parsoid
 <section data-mw-section-id="-1"><p>foo</p>
 
 </section><section data-mw-section-id="-2"><div style="border:1px solid red;">
-<section data-mw-section-id="1"><h1 id="1"> 1 </h1>
+<section data-mw-section-id="1"><h1 id="1">1</h1>
 <p>a</p>
 
-<section data-mw-section-id="2"><h2 id="1.1"> 1.1 </h2>
+<section data-mw-section-id="2"><h2 id="1.1">1.1</h2>
 <p>b</p>
 
-</section></section><section data-mw-section-id="-1"><h1 id="2"> 2 </h1>
+</section></section><section data-mw-section-id="-1"><h1 id="2">2</h1>
 <p>c</p>
 </section></div>
 
-</section><section data-mw-section-id="4"><h1 id="3"> 3 </h1>
+</section><section data-mw-section-id="4"><h1 id="3">3</h1>
 <p>d</p>
 
-<section data-mw-section-id="5"><h2 id="3.1"> 3.1 </h2>
+<section data-mw-section-id="5"><h2 id="3.1">3.1</h2>
 <p>e</p>
 </section></section>
 !! end
@@ -30528,44 +30564,44 @@ parsoid={
 !! wikitext
 foo
 
-= 1 =
+=1=
 a
 <div style="border:1px solid red;">
 b
 
-== 1.1 ==
+==1.1==
 c
 
-= 2 =
+=2=
 d
 </div>
 e
 
-= 3 =
+=3=
 f
 
-== 3.1 ==
+==3.1==
 g
 !! html/parsoid
 <section data-mw-section-id="0"><p>foo</p>
 
-</section><section data-mw-section-id="-1"><h1 id="1"> 1 </h1>
+</section><section data-mw-section-id="-1"><h1 id="1">1</h1>
 <p>a</p>
 </section><section data-mw-section-id="-2"><div style="border:1px solid red;">
 <p>b</p>
 
-<section data-mw-section-id="2"><h2 id="1.1"> 1.1 </h2>
+<section data-mw-section-id="2"><h2 id="1.1">1.1</h2>
 <p>c</p>
 
-</section><section data-mw-section-id="-1"><h1 id="2"> 2 </h1>
+</section><section data-mw-section-id="-1"><h1 id="2">2</h1>
 <p>d</p>
 </section></div>
 <p>e</p>
 
-</section><section data-mw-section-id="4"><h1 id="3"> 3 </h1>
+</section><section data-mw-section-id="4"><h1 id="3">3</h1>
 <p>f</p>
 
-<section data-mw-section-id="5"><h2 id="3.1"> 3.1 </h2>
+<section data-mw-section-id="5"><h2 id="3.1">3.1</h2>
 <p>g</p>
 </section></section>
 !! end
@@ -30581,21 +30617,21 @@ foo
 
 <h1>a</h1>
 
-= b =
+=b=
 
 <h1>c</h1>
 
-= d =
+=d=
 !! html/parsoid
 <section data-mw-section-id="0"><p>foo</p>
 
 <h1 id="a" data-parsoid='{"stx":"html"}'>a</h1>
 
-</section><section data-mw-section-id="1"><h1 id="b"> b </h1>
+</section><section data-mw-section-id="1"><h1 id="b">b</h1>
 
 <h1 id="c" data-parsoid='{"stx":"html"}'>c</h1>
 
-</section><section data-mw-section-id="2"><h1 id="d"> d </h1></section>
+</section><section data-mw-section-id="2"><h1 id="d">d</h1></section>
 !! end
 
 !! test
@@ -30607,18 +30643,18 @@ parsoid={
 !! wikitext
 
 <!-- this is a comment, presumably significant to editors -->
-= 1 =
+=1=
 a
 
-= 2 =
+=2=
 b
 !! html/parsoid
 <section data-mw-section-id="0" data-parsoid="{}">
 <!-- this is a comment, presumably significant to editors -->
-</section><section data-mw-section-id="1"><h1 id="1"> 1 </h1>
+</section><section data-mw-section-id="1"><h1 id="1">1</h1>
 <p>a</p>
 
-</section><section data-mw-section-id="2"><h1 id="2"> 2 </h1>
+</section><section data-mw-section-id="2"><h1 id="2">2</h1>
 <p>b</p></section>
 !! end
 
@@ -30631,15 +30667,235 @@ parsoid={
 !! wikitext
 foo
 {{echo|<div>
-== a ==
-== b ==
+==a==
+==b==
 </div>
 }}
 !! html/parsoid
 <section data-mw-section-id="-1"><p>foo</p>
-</section><section data-mw-section-id="-2"><div about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"stx":"html","pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;div>\n== a ==\n== b ==\n&lt;/div>\n"}},"i":0}}]}'>
-<section data-mw-section-id="-1"><h2 id="a"> a </h2>
-</section><section data-mw-section-id="-1"><h2 id="b"> b </h2>
+</section><section data-mw-section-id="-2"><div about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"stx":"html","pi":[[{"k":"1"}]]}' data-mw='{"parts":[{"template":{"target":{"wt":"echo","href":"./Template:Echo"},"params":{"1":{"wt":"&lt;div>\n==a==\n==b==\n&lt;/div>\n"}},"i":0}}]}'>
+<section data-mw-section-id="-1"><h2 id="a">a</h2>
+</section><section data-mw-section-id="-1"><h2 id="b">b</h2>
 </section></div><span about="#mwt1">
 </span></section>
 !! end
+
+##########################################################################
+Tests demonstrating white-space insensitivity in input wikitext
+for wikitext headings, wikitext list items, and wikitext table captions,
+headings, and cells. HTML versions of the same should preserve whitespace.
+##########################################################################
+!! test
+Trim whitespace in wikitext headings, list items, table captions, headings, and cells
+!! wikitext
+__NOTOC__
+==    <!--c1-->  <!--c2--> Spaces   <!--c3--> <!--c4-->  ==
+==             <!--c2-->       <!--c2--> Tabs          <!--c3--><!--c4-->      ==
+*     <!--c1-->   <!--c2-->  List item <!--c3--> <!--c4-->
+; <!--term to define--> term : <!--term's definition--> definition
+{|
+|+ <!--c1--> <!--c2--> Table Caption <!--c3--> <!--c4-->
+|-
+!  <!--c1--> <!--c2--> Table Heading 1 <!--c3--> <!--c4--> !!   Table Heading 2 <!--c5-->
+|-
+| <!--c1--> <!--c2--> Table Cell 1 <!--c3--> <!--c4--> ||   Table Cell 2 <!--c5-->
+|-
+| class="foo" || <!--c1--> <!--c2--> Table Cell 3 <!--c3--> <!--c4-->
+|-
+| <!--c1--> testing [[one|two]] <!--c2--> | <!--c3--> some content
+|}
+: {|
+  |  <!--c1--> <!--c2--> Table Cell 1 <!--c3--> <!--c4--> ||   Table Cell 2 <!--c5-->
+  |} foo   <!--c1-->
+!! html/php+tidy
+<h2><span class="mw-headline" id="Spaces">Spaces</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: Spaces">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<h2><span class="mw-headline" id="Tabs">Tabs</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=2" title="Edit section: Tabs">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+<ul><li>List item</li></ul>
+<dl><dt>term&#160;</dt>
+<dd>definition</dd></dl>
+<table>
+<caption>Table Caption
+</caption>
+<tbody><tr>
+<th>Table Heading 1</th>
+<th>Table Heading 2
+</th></tr>
+<tr>
+<td>Table Cell 1</td>
+<td>Table Cell 2
+</td></tr>
+<tr>
+<td>class="foo"</td>
+<td>Table Cell 3
+</td></tr>
+<tr>
+<td>testing <a href="/index.php?title=One&amp;action=edit&amp;redlink=1" class="new" title="One (page does not exist)">two</a>  |  some content
+</td></tr></tbody></table>
+<dl><dd><table>
+<tbody><tr>
+<td>Table Cell 1</td>
+<td>Table Cell 2
+</td></tr></tbody></table> foo</dd></dl>
+!! end
+
+# Looks like <caption> is not accepted in HTML
+!! test
+Do not trim whitespace in HTML headings, list items, table captions, headings, and cells
+!! wikitext
+__NOTOC__
+<h2>    <!--c1-->   <!--c2--> Heading    <!--c3--> <!--c4-->  </h2>
+<ul><li>     <!--c1-->   <!--c2-->  List item <!--c3--> <!--c4-->  </li></ul>
+<table>
+<tr><th> <!--c1--> <!--c2--> Table Heading <!--c3--> <!--c4--> <th></tr>
+<tr><td> <!--c1--> <!--c2--> Table Cell <!--c3--> <!--c4--> <th></tr>
+</table>
+!! html/php+tidy
+<h2><span class="mw-headline" id="Heading">        Heading       </span></h2>
+<ul><li>          List item    </li></ul>
+<table>
+<tbody><tr><th>   Table Heading   </th><th></th></tr>
+<tr><td>   Table Cell   </td><th></th></tr>
+</tbody></table>
+!! end
+
+!! test
+Do not trim whitespace in links and quotes
+!! wikitext
+foo ''  <!--c1--> italic <!--c2-->   '' and '''  <!--c3-->  bold  <!--c4-->  '''
+[[Foo|  some text  ]]
+!! html/php+tidy
+<p>foo <i>   italic    </i> and <b>    bold    </b>
+<a href="/wiki/Foo" title="Foo">  some text  </a>
+</p>
+!! end
+
+!! test
+Remove p tags surrounding a single element in a figcaption
+!! options
+parsoid=html2wt
+!! wikitext
+[[File:Foobar.jpg|right|200x200px|Caption]]
+!! html/parsoid
+<figure class="mw-halign-right" typeof="mw:Image"><a href="./File:Foobar.jpg"><img resource="./File:Foobar.jpg" src="//example.com/images/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg" data-file-width="1941" data-file-height="220" data-file-type="bitmap" height="23" width="200"/></a><figcaption><p>Caption</p></figcaption></figure>
+!! end
+
+!! test
+Selser preserves lack of newline before list and allows newline after the list
+!! options
+parsoid={
+  "modes": ["selser"],
+  "scrubWikitext": true,
+  "changes": [
+    [ "ul", "after", "<p>footer</p>" ]
+  ]
+}
+!! wikitext
+header
+*foo
+*bar
+!! wikitext/edited
+header
+*foo
+*bar
+
+footer
+!! end
+
+
+!! test
+Selser does not introduce newlines between unedited paragraph preceding the list
+!! options
+parsoid={
+  "modes": ["selser"],
+  "changes": [
+    [ "table tbody tr td p:last-child", "empty" ]
+  ]
+}
+!! wikitext
+{|
+|
+header
+*foo
+*bar
+footer
+|}
+!! wikitext/edited
+{|
+|
+header
+*foo
+*bar
+
+|}
+!! end
+
+!! test
+Selser does not introduce newlines between unedited paragraph following the list
+!! options
+parsoid={
+  "modes": ["selser"],
+  "changes": [
+    [ "table tbody tr td p:first-child", "empty" ]
+  ]
+}
+!! wikitext
+{|
+|
+header
+*foo
+*bar
+footer
+|}
+!! wikitext/edited
+{|
+|
+
+*foo
+*bar
+footer
+|}
+!! end
+
+!! test
+Remove a list item but do not insert newline above list
+!! options
+parsoid={
+  "modes": ["selser"],
+  "changes": [
+     [ "ul li:last-child", "remove" ]
+  ]
+}
+!! wikitext
+header
+*foo
+*bar
+footer
+!! wikitext/edited
+header
+*foo
+footer
+!! end
+
+!! test
+Check soft hyphens as entities (&shy;) in displaytitle (T66528)
+!! options
+showtitle
+title=[[Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygon]]
+!! wikitext
+{{DISPLAYTITLE:Lopado&shy;temacho&shy;selacho&shy;galeo&shy;kranio&shy;leipsano&shy;drim&shy;hypo&shy;trimmato&shy;silphio&shy;parao&shy;melito&shy;katakechy&shy;meno&shy;kichl&shy;epi&shy;kossypho&shy;phatto&shy;perister&shy;alektryon&shy;opte&shy;kephallio&shy;kigklo&shy;peleio&shy;lagoio&shy;siraio&shy;baphe&shy;tragano&shy;pterygon}}
+!! html/php
+Lopado&#173;temacho&#173;selacho&#173;galeo&#173;kranio&#173;leipsano&#173;drim&#173;hypo&#173;trimmato&#173;silphio&#173;parao&#173;melito&#173;katakechy&#173;meno&#173;kichl&#173;epi&#173;kossypho&#173;phatto&#173;perister&#173;alektryon&#173;opte&#173;kephallio&#173;kigklo&#173;peleio&#173;lagoio&#173;siraio&#173;baphe&#173;tragano&#173;pterygon
+
+!! end
+
+!! test
+Check soft hyphens as Unicode characters (U+00AD) in displaytitle (T66528)
+!! options
+showtitle
+title=[[Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygon]]
+!! wikitext
+{{DISPLAYTITLE:Lopado­temacho­selacho­galeo­kranio­leipsano­drim­hypo­trimmato­silphio­parao­melito­katakechy­meno­kichl­epi­kossypho­phatto­perister­alektryon­opte­kephallio­kigklo­peleio­lagoio­siraio­baphe­tragano­pterygon}}
+!! html/php
+Lopado­temacho­selacho­galeo­kranio­leipsano­drim­hypo­trimmato­silphio­parao­melito­katakechy­meno­kichl­epi­kossypho­phatto­perister­alektryon­opte­kephallio­kigklo­peleio­lagoio­siraio­baphe­tragano­pterygon
+
+!! end