From cb3ef90d240baf74fa621fbc758f7b27a3333ba2 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 11 Feb 2015 19:11:27 +0000 Subject: [PATCH] Protect `
` tags when parsing oEmbed responses in
 `WP_oEmbed::_strip_newlines()` in `DOMDocument` is available.

Fixes #31214.

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


git-svn-id: http://core.svn.wordpress.org/trunk@31396 1a063a9b-81f0-0310-95a4-ce76da25c4cd
---
 wp-includes/class-oembed.php | 26 +++++++++++++++++++++++---
 wp-includes/version.php      |  2 +-
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/wp-includes/class-oembed.php b/wp-includes/class-oembed.php
index f5b558b322..8891d2e2a9 100644
--- a/wp-includes/class-oembed.php
+++ b/wp-includes/class-oembed.php
@@ -559,10 +559,30 @@ class WP_oEmbed {
 	 * @return string Possibly modified $html
 	 */
 	public function _strip_newlines( $html, $data, $url ) {
-		if ( false !== strpos( $html, "\n" ) )
-			$html = str_replace( array( "\r\n", "\n" ), '', $html );
+		if ( false === strpos( $html, "\n" ) ) {
+			return $html;
+		}
 
-		return $html;
+		$pre = array();
+		$tokens = array();
+		if ( class_exists( 'DOMDocument' ) ) {
+			$token = '__PRE__';
+			$replace = array();
+			$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;
+				$html = str_replace( $tag_html, $tag_token, $html );
+			}
+			$pre = array_values( $replace );
+			$tokens = array_keys( $replace );
+		}
+
+		$stripped = str_replace( array( "\r\n", "\n" ), '', $html );
+		return str_replace( $tokens, $pre, $stripped );
 	}
 }
 
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 55e8a9aebd..37bd8283ca 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -4,7 +4,7 @@
  *
  * @global string $wp_version
  */
-$wp_version = '4.2-alpha-31414';
+$wp_version = '4.2-alpha-31415';
 
 /**
  * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.