Filing in response to discussion at https://www.mediawiki.org/wiki/Topic:U7c45mq2js1912mq.
When encountering <style>...</style> by itself on a line, as might be produced from TemplateStyles, the parser produces
<p><style>...</style> </p>
The <p> tags are unnecessary, and may result in unwanted display depending on what styles are being applied to <p> tags.
Currently our configuration of HTML Tidy treats <style> as a block-level element, which causes it to remove wrapping <p> tags in the situation above. But RemexHtml doesn't remove the <p> tags, presumably since HTML5 allows <style> to be either flow or phrasing.
Parsoid's output also contains these <p> tags; I don't know whether it's getting this from the PHP Parser or doing it itself.
On the other hand, our current configuration of HTML Tidy will turn <p>foo <style>...</style> bar</p> into <p>foo</p><style>...</style><p>bar</p>, which probably isn't good either. But since HTML Tidy is on its way out, that's probably not worth worrying about.
Desired results:
<style>...</style> foo <style>...</style> bar
<style>...</style> <p>foo <style>...</style> bar</p>
<style>...</style>foo bar as a standalone paragraph could be validly output in either of two ways, <style>...</style><p>foo bar</p> or <p><style>...</style>foo bar</p>. And similarly for foo bar<style>...</style>.
If BlockLevelPass avoids either starting or ending a paragraph for a line beginning with <style, similar to how it doesn't start a paragraph for block-level tags, it looks like RemexHtml will do the right thing.
Or RemexHtml could remove the <p> tags when the only content is one or more <style> tags, although that seems less likely to be appropriate since HTML5 doesn't specify that.