]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/formatting.php
WordPress 4.2.4
[autoinstalls/wordpress.git] / wp-includes / formatting.php
index 08c367c645d26b7336eb7df4303b430682d8d4c9..0151e1fc64ee3073600a485ed14af5cc6251b629 100644 (file)
@@ -419,10 +419,10 @@ function wpautop($pee, $br = true) {
        $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
 
        // Standardize newline characters to "\n".
        $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
 
        // Standardize newline characters to "\n".
-       $pee = str_replace(array("\r\n", "\r"), "\n", $pee); 
+       $pee = str_replace(array("\r\n", "\r"), "\n", $pee);
 
 
-       // Strip newlines from all elements.
-       $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
+       // Find newlines in all elements and add placeholders.
+       $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) );
 
        // Collapse line breaks before and after <option> elements so they don't get autop'd.
        if ( strpos( $pee, '<option' ) !== false ) {
 
        // Collapse line breaks before and after <option> elements so they don't get autop'd.
        if ( strpos( $pee, '<option' ) !== false ) {
@@ -509,9 +509,59 @@ function wpautop($pee, $br = true) {
        if ( !empty($pre_tags) )
                $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
 
        if ( !empty($pre_tags) )
                $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
 
+       // Restore newlines in all elements.
+       $pee = str_replace( " <!-- wpnl --> ", "\n", $pee );
+
        return $pee;
 }
 
        return $pee;
 }
 
+/**
+ * Separate HTML elements and comments from the text.
+ *
+ * @since 4.2.4
+ *
+ * @param string $input The text which has to be formatted.
+ * @return array The formatted text.
+ */
+function wp_html_split( $input ) {
+       static $regex;
+
+       if ( ! isset( $regex ) ) {
+               $comments =
+                         '!'           // Start of comment, after the <.
+                       . '(?:'         // Unroll the loop: Consume everything until --> is found.
+                       .     '-(?!->)' // Dash not followed by end of comment.
+                       .     '[^\-]*+' // Consume non-dashes.
+                       . ')*+'         // Loop possessively.
+                       . '(?:-->)?';   // End of comment. If not found, match all input.
+
+               $cdata =
+                         '!\[CDATA\['  // Start of comment, after the <.
+                       . '[^\]]*+'     // Consume non-].
+                       . '(?:'         // Unroll the loop: Consume everything until ]]> is found.
+                       .     '](?!]>)' // One ] not followed by end of comment.
+                       .     '[^\]]*+' // Consume non-].
+                       . ')*+'         // Loop possessively.
+                       . '(?:]]>)?';   // End of comment. If not found, match all input.
+
+               $regex =
+                         '/('              // Capture the entire match.
+                       .     '<'           // Find start of element.
+                       .     '(?(?=!--)'   // Is this a comment?
+                       .         $comments // Find end of comment.
+                       .     '|'
+                       .         '(?(?=!\[CDATA\[)' // Is this a comment?
+                       .             $cdata // Find end of comment.
+                       .         '|'
+                       .             '[^>]*>?' // Find end of element. If not found, match all input.
+                       .         ')'
+                       .     ')'
+                       . ')/s';
+       }
+
+       return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE );
+}
+
 /**
  * Replace characters or phrases within HTML elements only.
  *
 /**
  * Replace characters or phrases within HTML elements only.
  *
@@ -523,25 +573,7 @@ function wpautop($pee, $br = true) {
  */
 function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
        // Find all elements.
  */
 function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
        // Find all elements.
-       $comments =
-                 '!'           // Start of comment, after the <.
-               . '(?:'         // Unroll the loop: Consume everything until --> is found.
-               .     '-(?!->)' // Dash not followed by end of comment.
-               .     '[^\-]*+' // Consume non-dashes.
-               . ')*+'         // Loop possessively.
-               . '(?:-->)?';   // End of comment. If not found, match all input.
-
-       $regex =
-                 '/('              // Capture the entire match.
-               .     '<'           // Find start of element.
-               .     '(?(?=!--)'   // Is this a comment?
-               .         $comments // Find end of comment.
-               .     '|'
-               .         '[^>]*>?' // Find end of element. If not found, match all input.
-               .     ')'
-               . ')/s';
-
-       $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
+       $textarr = wp_html_split( $haystack );
        $changed = false;
 
        // Optimize when searching for one item.
        $changed = false;
 
        // Optimize when searching for one item.