[wp-trac] [WordPress Trac] #37672: wpautop adds a closing p-tag without an opening p-tag
WordPress Trac
noreply at wordpress.org
Tue Aug 16 15:52:09 UTC 2016
#37672: wpautop adds a closing p-tag without an opening p-tag
--------------------------+------------------------------
Reporter: TBarregren | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Formatting | Version: 4.5.3
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+------------------------------
Comment (by TBarregren):
If it is to any help, this is my function.php code to solve this (and
related) problem(s).
{{{#!php
<?php
add_filter('the_content', function ($html) {
$html_pos = 0;
$last_html_pos = strlen($html) - 1;
$opening_pos = null;
$text_pos = null;
// See http://w3c.github.io/html/single-page.html#elementdef-p
// and http://w3c.github.io/html/single-page.html#kinds-of-content-
phrasing-content
$self_closing_tags_allowed_in_p = explode(' ', 'area br embed img input
link wbr');
$not_self_closing_tags_allowed_in_p = explode(' ', 'a abbr audio b bdi
bdo button canvas cite code data datalist del dfn em i iframe ins kbd
label map mark math meter noscript object output picture progress q ruby s
samp script select small span strong sub sup svg template textarea time u
var video text');
while (($html_pos = strpos($html, '<', $html_pos)) !== false) {
if (substr($html, $html_pos, 4) === '<!--') {
$html_pos = strpos($html, '-->', $html_pos + 4);
if ($html_pos === false) return $html; // Too messy, don't do
anything further.
$html_pos += 3;
}
elseif ($opening_pos === null) {
if (substr($html, $html_pos, 4) === '<pre') {
$html_pos = strpos($html, '</pre>', $html_pos + 6);
if ($html_pos === false) return $html; // Too messy, don't do
anything further.
$html_pos += 6;
}
elseif (strtolower(substr($html, $html_pos, 2)) === '<p') {
$opening_pos = $html_pos;
$html_pos = strpos($html, '>', $html_pos + 2);
if ($html_pos === false) return $html; // Too messy, don't do
anything further.
$text_pos = ++$html_pos;
}
elseif (strtolower(substr($html, $html_pos, 4)) === '</p>') {
// See https://core.trac.wordpress.org/ticket/37672
$html = substr($html, 0, $html_pos) . substr($html, $html_pos +
4);
$last_html_pos -= 4;
}
else {
$html_pos += 1;
}
}
else {
if (strtolower(substr($html, $html_pos, 4)) === '</p>') {
if (trim(substr($html, $text_pos, $html_pos - $text_pos)) === '')
{
$html = substr($html, 0, $opening_pos) . substr($html, $html_pos
+ 4);
$html_pos = $opening_pos;
$last_html_pos = strlen($html) - 1;
}
else {
$html_pos += 4;
}
$opening_pos = null;
}
else {
$tag_end_pos = $html_pos + 1;
while ($html{$tag_end_pos} !== '>' &&
!ctype_space($html{$tag_end_pos})) {
++$tag_end_pos;
}
$tag = strtolower(substr($html, $html_pos + 1, $tag_end_pos -
$html_pos - 1));
if (in_array($tag, $self_closing_tags_allowed_in_p)) {
$html_pos = strpos($html, '>', $tag_end_pos);
if ($html_pos === false) return $html; // Too messy, don't do
anything further.
$html_pos += 1;
}
elseif (in_array($tag, $not_self_closing_tags_allowed_in_p)) {
$html_pos = strpos($html, "</$tag", $tag_end_pos);
if ($html_pos === false) return $html; // Too messy, don't do
anything further.
$html_pos += 1;
}
else {
$tag_end_pos = strpos($html, "</$tag>", $tag_end_pos);
if ($tag_end_pos === false) return $html; // Too messy, don't do
anything further.
$tag_end_pos += strlen("</$tag>");
$html = substr($html, 0, $html_pos) . '</p>' . substr($html,
$html_pos, $tag_end_pos - $html_pos) . '<p>' . substr($html,
$tag_end_pos);
}
}
}
}
if ($opening_pos !== null) {
$html .= '</p>';
}
return $html;
});
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/37672#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list