Found another bug caused by Template:Selfref as used on Wikimedia Foundation. (Previously: T182349)
The template produces different HTML via different API actions, both containing buggy-looking nested div.hatnote elements (just with the nesting relationship reordered).
Wikitext: {{selfref|{{for|the project page on the foundation itself|:Wikipedia:Wikimedia Foundation}}|hat}}
In MW HTML (and via api.php?action=parse):
<div role="note" class="hatnote navigation-not-searchable">\n <div role="note" class="hatnote navigation-not-searchable"> <span class="plainlinks selfreference noprint"> For the project page on the foundation itself, see <a href="/wiki/Wikipedia:Wikimedia_Foundation" title="Wikipedia:Wikimedia Foundation">Wikipedia:Wikimedia Foundation</a>. </span> </div> </div>
In Parsoid HTML (via api.php?action=expandtemplates):
<div role="note" class="hatnote navigation-not-searchable" about="#mwt1" typeof="mw:Transclusion" data-mw='{"parts":[{"template":{"target":{"wt":"selfref","href":"./Template:Selfref"},"params":{"1":{"wt":"{{for|the project page on the foundation itself|:Wikipedia:Wikimedia Foundation}}"},"2":{"wt":"hat"}},"i":0}}]}' id="mwAg"> <span class="plainlinks selfreference noprint"> <div role="note" class="hatnote navigation-not-searchable"> For the project page on the foundation itself, see <a rel="mw:WikiLink" href="./Wikipedia:Wikimedia_Foundation" title="Wikipedia:Wikimedia Foundation">Wikipedia:Wikimedia Foundation</a>. </div> </span> </div>
I noticed this because the duplicate hatnote divs are tripping up MCS when it queries for hatnotes with querySelectorAll('.hatnote'):
hatnotes: [ "<span class=\"plainlinks selfreference noprint\"><div role=\"note\" class=\"hatnote navigation-not-searchable\">For the project page on the foundation itself, see <a href=\"/wiki/Wikipedia:Wikimedia_Foundation\" title=\"Wikipedia:Wikimedia Foundation\">Wikipedia:Wikimedia Foundation</a>.</div></span>", "For the project page on the foundation itself, see <a href=\"/wiki/Wikipedia:Wikimedia_Foundation\" title=\"Wikipedia:Wikimedia Foundation\">Wikipedia:Wikimedia Foundation</a>." ]
(https://en.wikipedia.org/api/rest_v1/page/mobile-sections-lead/Wikimedia_Foundation)
Note: This doesn't affect hatnotes produced with the regular Hatnote template (e.g., on https://en.wikipedia.org/api/rest_v1/page/html/Wikipedia.)