Use RegEx instead of DOMDocument when protecting <pre> tags in WP_oEmbed::_strip_newlines(). It is incredibly difficult to maintain character encoding and whitespace when parsing via DOMDocument.

See #31214.

Built from https://develop.svn.wordpress.org/trunk@31423


git-svn-id: http://core.svn.wordpress.org/trunk@31404 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2015-02-11 22:18:53 +00:00
parent bcda24004a
commit 641f95155b
2 changed files with 18 additions and 19 deletions

View File

@ -563,28 +563,27 @@ class WP_oEmbed {
return $html;
}
$pre = array();
$tokens = array();
if ( class_exists( 'DOMDocument' ) ) {
$token = '__PRE__';
$replace = array();
$count = 1;
$count = 1;
$found = array();
$token = '__PRE__';
$search = array( "\t", "\n", "\r", ' ' );
$replace = array( '__TAB__', '__NL__', '__CR__', '__SPACE__' );
$tokenized = str_replace( $search, $replace, $html );
$dom = new DOMDocument();
$dom->loadHTML( $html );
$tags = $dom->getElementsByTagName( 'pre' );
foreach ( $tags as $i => $tag ) {
$tag_html = $dom->saveHTML( $tag );
$tag_token = $token . $i;
$replace[ $tag_token ] = $tag_html;
preg_match_all( '#(<pre[^>]*>.+?</pre>)#i', $tokenized, $matches, PREG_SET_ORDER );
foreach ( $matches as $i => $match ) {
$tag_html = str_replace( $replace, $search, $match[0] );
$tag_token = $token . $i;
$html = str_replace( $tag_html, $tag_token, $html, $count );
}
$pre = array_values( $replace );
$tokens = array_keys( $replace );
$found[ $tag_token ] = $tag_html;
$html = str_replace( $tag_html, $tag_token, $html, $count );
}
$stripped = str_replace( array( "\r\n", "\n" ), '', $html );
$replaced = str_replace( $replace, $search, $html );
$stripped = str_replace( array( "\r\n", "\n" ), '', $replaced );
$pre = array_values( $found );
$tokens = array_keys( $found );
return str_replace( $tokens, $pre, $stripped );
}
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.2-alpha-31422';
$wp_version = '4.2-alpha-31423';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.