User:Jacobolus/math block example

Mediawiki generates incorrect markup for block math elements.
Bug report, https://phabricator.wikimedia.org/T331242 was a duplicate of https://phabricator.wikimedia.org/T182041

For example:

Here is a formula, $$x^2 + y^2 = 1,$$ and here is text afterward.

Rendered:

Here is a formula, $$x^2 + y^2 = 1,$$ and here is text afterward.

Generated markup:

Here is a formula, ...   

and here is text afterward.

Treated by browsers as:

Here is a formula, ...   

and here is text afterward.

The issue here is that a div is not "phrasing content" (see HTML spec: phrasing content) and therefore is not allowed inside a paragraph. When the browser sees a div inside a paragraph, it will implicitly close the paragraph immediately before the div.

Later, the text after the div is put into a bare text node, not inside any top-level element. This is a problem because it is impossible for CSS to target such elements to be styled. As a result, any style intended to apply to paragraphs or other text content will not affect these text nodes.

Finally, when the browser sees the closing paragraph tag not corresponding to any opening tag, it will implicitly open it, leaving an empty paragraph at the end.

As a workaround, authors can add explicit blank lines:

Here is another formula,

$$e = mc^2,$$

and here is text afterward.

Rendered:

Here is another formula,

$$e = mc^2,$$

and here is text afterward.

Generated markup:

Here is another formula, ...    and here is text afterward.

Treated by browsers as:

Here is another formula, ...    and here is text afterward.

This is still not a perfect solution, as it results in 2 extra empty paragraphs. But it works better in practice than the previous one because at least css stylesheets can target the trailing content, and the empty paragraphs don’t affect page rendering.

In my opinion both of the above markup examples should instead output:

Desired markup:

Here is another formula, ...    and here is text afterward.