Changeset 34747
- Timestamp:
- 10/01/2015 06:04:13 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/formatting.php
r34727 r34747 217 217 // Look for shortcodes and HTML elements. 218 218 219 $tagnames = array_keys( $shortcode_tags ); 220 $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) ); 221 $tagregexp = "(?:$tagregexp)(?![\\w-])"; // Excerpt of get_shortcode_regex(). 219 preg_match_all( '@\[/?([^<>&/\[\]\x00-\x20]++)@', $text, $matches ); 220 $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] ); 221 $found_shortcodes = ! empty( $tagnames ); 222 if ( $found_shortcodes ) { 223 $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) ); 224 $tagregexp = "(?:$tagregexp)(?![\\w-])"; // Excerpt of get_shortcode_regex(). 225 $shortcode_regex = 226 '\[' // Find start of shortcode. 227 . '[\/\[]?' // Shortcodes may begin with [/ or [[ 228 . $tagregexp // Only match registered shortcodes, because performance. 229 . '(?:' 230 . '[^\[\]<>]+' // Shortcodes do not contain other shortcodes. Quantifier critical. 231 . '|' 232 . '<[^\[\]>]*>' // HTML elements permitted. Prevents matching ] before >. 233 . ')*+' // Possessive critical. 234 . '\]' // Find end of shortcode. 235 . '\]?'; // Shortcodes may end with ]] 236 } 222 237 223 238 $comment_regex = … … 229 244 . '(?:-->)?'; // End of comment. If not found, match all input. 230 245 231 $shortcode_regex = 232 '\[' // Find start of shortcode. 233 . '[\/\[]?' // Shortcodes may begin with [/ or [[ 234 . $tagregexp // Only match registered shortcodes, because performance. 235 . '(?:' 236 . '[^\[\]<>]+' // Shortcodes do not contain other shortcodes. Quantifier critical. 246 $html_regex = // Needs replaced with wp_html_split() per Shortcode API Roadmap. 247 '<' // Find start of element. 248 . '(?(?=!--)' // Is this a comment? 249 . $comment_regex // Find end of comment. 237 250 . '|' 238 . '<[^\[\]>]*>' // HTML elements permitted. Prevents matching ] before >. 239 . ')*+' // Possessive critical. 240 . '\]' // Find end of shortcode. 241 . '\]?'; // Shortcodes may end with ]] 242 243 $regex = 244 '/(' // Capture the entire match. 245 . '<' // Find start of element. 246 . '(?(?=!--)' // Is this a comment? 247 . $comment_regex // Find end of comment. 248 . '|' 249 . '[^>]*>' // Find end of element. 250 . ')' 251 . '|' 252 . $shortcode_regex // Find shortcodes. 253 . ')/s'; 251 . '[^>]*>?' // Find end of element. If not found, match all input. 252 . ')'; 253 254 if ( $found_shortcodes ) { 255 $regex = '/(' . $html_regex . '|' . $shortcode_regex . ')/s'; 256 } else { 257 $regex = '/(' . $html_regex . ')/s'; 258 } 254 259 255 260 $textarr = preg_split( $regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); … … 258 263 // Only call _wptexturize_pushpop_element if $curl is a delimiter. 259 264 $first = $curl[0]; 260 if ( '<' === $first && '<!--' === substr( $curl, 0, 4 ) ) { 261 // This is an HTML comment delimiter. 262 263 continue; 264 265 } elseif ( '<' === $first && '>' === substr( $curl, -1 ) ) { 266 // This is an HTML element delimiter. 267 268 _wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags ); 265 if ( '<' === $first ) { 266 if ( '<!--' === substr( $curl, 0, 4 ) ) { 267 // This is an HTML comment delimeter. 268 continue; 269 } else { 270 // This is an HTML element delimiter. 271 _wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags ); 272 } 269 273 270 274 } elseif ( '' === trim( $curl ) ) { 271 275 // This is a newline between delimiters. Performance improves when we check this. 272 273 276 continue; 274 277 275 } elseif ( '[' === $first && 1 === preg_match( '/^' . $shortcode_regex . '$/', $curl ) ) {278 } elseif ( '[' === $first && $found_shortcodes && 1 === preg_match( '/^' . $shortcode_regex . '$/', $curl ) ) { 276 279 // This is a shortcode delimiter. 277 280 -
trunk/src/wp-includes/shortcodes.php
r34745 r34747 209 209 return $content; 210 210 211 $tagnames = array_keys($shortcode_tags); 212 $tagregexp = join( '|', array_map('preg_quote', $tagnames) ); 213 $pattern = "/\\[($tagregexp)/s"; 214 215 if ( 1 !== preg_match( $pattern, $content ) ) { 216 // Avoids parsing HTML when there are no shortcodes or embeds anyway. 211 // Find all registered tag names in $content. 212 preg_match_all( '@\[([^<>&/\[\]\x00-\x20]++)@', $content, $matches ); 213 $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] ); 214 215 if ( empty( $tagnames ) ) { 217 216 return $content; 218 217 } 219 218 220 $content = do_shortcodes_in_html_tags( $content, $ignore_html );221 222 $pattern = get_shortcode_regex( );219 $content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ); 220 221 $pattern = get_shortcode_regex( $tagnames ); 223 222 $content = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content ); 224 223 … … 248 247 * @global array $shortcode_tags 249 248 * 249 * @param array $tagnames List of shortcodes to find. Optional. Defaults to all registered shortcodes. 250 250 * @return string The shortcode search regular expression 251 251 */ 252 function get_shortcode_regex() { 253 global $shortcode_tags; 254 $tagnames = array_keys($shortcode_tags); 252 function get_shortcode_regex( $tagnames = null ) { 253 global $shortcode_tags; 254 255 if ( empty( $tagnames ) ) { 256 $tagnames = array_keys( $shortcode_tags ); 257 } 255 258 $tagregexp = join( '|', array_map('preg_quote', $tagnames) ); 256 259 … … 338 341 * @param string $content Content to search for shortcodes 339 342 * @param bool $ignore_html When true, all square braces inside elements will be encoded. 343 * @param array $tagnames List of shortcodes to find. 340 344 * @return string Content with shortcodes filtered out. 341 345 */ 342 function do_shortcodes_in_html_tags( $content, $ignore_html ) {346 function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) { 343 347 // Normalize entities in unfiltered HTML before adding placeholders. 344 348 $trans = array( '[' => '[', ']' => ']' ); … … 346 350 $trans = array( '[' => '[', ']' => ']' ); 347 351 348 $pattern = get_shortcode_regex( );352 $pattern = get_shortcode_regex( $tagnames ); 349 353 $textarr = wp_html_split( $content ); 350 354 … … 558 562 return $content; 559 563 560 $content = do_shortcodes_in_html_tags( $content, true ); 561 562 $pattern = get_shortcode_regex(); 564 // Find all registered tag names in $content. 565 preg_match_all( '@\[([^<>&/\[\]\x00-\x20]++)@', $content, $matches ); 566 $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] ); 567 568 if ( empty( $tagnames ) ) { 569 return $content; 570 } 571 572 $content = do_shortcodes_in_html_tags( $content, true, $tagnames ); 573 574 $pattern = get_shortcode_regex( $tagnames ); 563 575 $content = preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content ); 564 576 -
trunk/tests/phpunit/tests/formatting/WPTexturize.php
r32863 r34747 375 375 ), 376 376 array( 377 "word <'word word", // Invalid HTML input triggers the apos in a word pattern.378 "word < ’word word",377 "word <'word word", // Invalid HTML 378 "word <'word word", 379 379 ), 380 380 array( … … 404 404 array( 405 405 "word<'word word", 406 "word< ’word word",406 "word<'word word", 407 407 ), 408 408 array( … … 432 432 array( 433 433 "word <' word word", 434 "word < ’word word",434 "word <' word word", 435 435 ), 436 436 array( … … 460 460 array( 461 461 "word<' word word", 462 "word< ’word word",462 "word<' word word", 463 463 ), 464 464 array( … … 611 611 ), 612 612 array( 613 'word <"word word', // Invalid HTML input triggers the closing quote pattern.614 'word < ”word word',613 'word <"word word', // Invalid HTML 614 'word <"word word', 615 615 ), 616 616 array( … … 644 644 array( 645 645 'word<"word word', 646 'word< ”word word',646 'word<"word word', 647 647 ), 648 648 array( … … 1313 1313 array( 1314 1314 '<br [gallery ...] ... /', 1315 '<br [gallery ...] …/',1315 '<br [gallery ...] ... /', 1316 1316 ), 1317 1317 array( … … 1353 1353 array( 1354 1354 '<br [[gallery ...]] ... /', 1355 '<br [[gallery ...]] …/',1355 '<br [[gallery ...]] ... /', 1356 1356 ), 1357 1357 array(
Note: See TracChangeset
for help on using the changeset viewer.