[wp-trac] [WordPress Trac] #31695: Extra unclosed <p> tag before <script> breaking the_content()
WordPress Trac
noreply at wordpress.org
Thu Mar 19 16:26:14 UTC 2015
#31695: Extra unclosed <p> tag before <script> breaking the_content()
--------------------------+-----------------------------
Reporter: vaurdan | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Formatting | Version: trunk
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
This bug was detected when trying to preview a post that had a Twitter
oembed URL. There was no error on the log, and the previewed content was
empty ( `the_content()` was printing an empty string ).
After some debugging, I found that something was adding an extra `<p>` and
never closing that tag, on the oembed returned markup, right before
`<script>`:
{{{
<blockquote class="twitter-tweet" width="550">
<p>Lisbon with #WordPressVIP @ Terreiro do Paço
http://instagram.com/p/vGw5IchocB/ </p>
<p>— Henrique Mouta (@vaurdan) <a
href="https://twitter.com/vaurdan/status/530762912462557185">November 7,
2014</a></blockquote>
<p><script async src="//platform.twitter.com/widgets.js"
charset="utf-8"></script></div>
}}}
I did some tracing and found out that it was `wpautop` fault. Even though
`WP_oEmbed::_strip_newlines()` was successfully stripping the new lines
from the returned oEmbed HTML, something was adding an extra newline
before the `<script>` tag.
{{{
$allblocks =
'(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|details|menu|summary)';
$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
$pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
}}}
And that was happening because `<blockquote>` is on the `$allblocks`,
making a newline between `blockquote` and `script`.
To fix this, I used the same logic that `wpautop` uses for `<object>`,
`<option>`, `<source>`, and `<track>`: have no `<p>` or `<br/>` around
the `<script>` tag:
{{{
if ( strpos( $pee, '<script' ) !== false ) {
// no P/BR around script
$pee = preg_replace( '|\s*<script|', '<script', $pee );
$pee = preg_replace( '|</script>\s*|', '</script>', $pee );
}
}}}
Now the oembed is fully functional and doesn't have that unclosed `<p>`
tag:
{{{
<blockquote class="twitter-tweet" width="550">
<p>Lisbon with #WordPressVIP @ Terreiro do Paço
http://instagram.com/p/vGw5IchocB/ </p>
<p>— Henrique Mouta (@vaurdan) <a
href="https://twitter.com/vaurdan/status/530762912462557185">November 7,
2014</a></blockquote><script async src="//platform.twitter.com/widgets.js"
charset="utf-8"></script></div>
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/31695>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list