WordPress.org

Make WordPress Core

Changeset 17730


Ignore:
Timestamp:
04/27/2011 09:50:39 PM (9 years ago)
Author:
ryan
Message:

kses optimizations. Props duck_. fixes #17045

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/kses.php

    r17228 r17730  
    555555    $pass_allowed_html = $allowed_html;
    556556    $pass_allowed_protocols = $allowed_protocols;
    557     return preg_replace_callback( '%((<!--.*?(-->|$))|(<[^>]*(>|$)|>))%', '_wp_kses_split_callback', $string );
     557    return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $string );
    558558}
    559559
     
    566566function _wp_kses_split_callback( $match ) {
    567567    global $pass_allowed_html, $pass_allowed_protocols;
    568     return wp_kses_split2( $match[1], $pass_allowed_html, $pass_allowed_protocols );
     568    return wp_kses_split2( $match[0], $pass_allowed_html, $pass_allowed_protocols );
    569569}
    570570
     
    597597    # It matched a ">" character
    598598
    599     if (preg_match('%^<!--(.*?)(-->)?$%', $string, $matches)) {
    600         $string = str_replace(array('<!--', '-->'), '', $matches[1]);
    601         while ( $string != $newstring = wp_kses($string, $allowed_html, $allowed_protocols) )
     599    if ( '<!--' == substr( $string, 0, 4 ) ) {
     600        $string = str_replace( array('<!--', '-->'), '', $string );
     601        while ( $string != ($newstring = wp_kses($string, $allowed_html, $allowed_protocols)) )
    602602            $string = $newstring;
    603603        if ( $string == '' )
     
    619619    $attrlist = $matches[3];
    620620
    621     if (!@isset($allowed_html[strtolower($elem)]))
     621    if ( ! isset($allowed_html[strtolower($elem)]) )
    622622        return '';
    623623    # They are using a not allowed HTML element
    624624
    625625    if ($slash != '')
    626         return "<$slash$elem>";
     626        return "</$elem>";
    627627    # No attributes are allowed for closing elements
    628628
    629     return wp_kses_attr("$slash$elem", $attrlist, $allowed_html, $allowed_protocols);
     629    return wp_kses_attr( $elem, $attrlist, $allowed_html, $allowed_protocols );
    630630}
    631631
     
    655655
    656656    # Are any attributes allowed at all for this element?
    657 
    658     if (@ count($allowed_html[strtolower($element)]) == 0)
     657    if ( ! isset($allowed_html[strtolower($element)]) || count($allowed_html[strtolower($element)]) == 0 )
    659658        return "<$element$xhtml_slash>";
    660659
    661660    # Split it
    662 
    663661    $attrarr = wp_kses_hair($attr, $allowed_protocols);
    664662
    665663    # Go through $attrarr, and save the allowed attributes for this element
    666664    # in $attr2
    667 
    668665    $attr2 = '';
    669666
     667    $allowed_attr = $allowed_html[strtolower($element)];
    670668    foreach ($attrarr as $arreach) {
    671         if (!@ isset ($allowed_html[strtolower($element)][strtolower($arreach['name'])]))
     669        if ( ! isset( $allowed_attr[strtolower($arreach['name'])] ) )
    672670            continue; # the attribute is not allowed
    673671
    674         $current = $allowed_html[strtolower($element)][strtolower($arreach['name'])];
    675         if ($current == '')
     672        $current = $allowed_attr[strtolower($arreach['name'])];
     673        if ( $current == '' )
    676674            continue; # the attribute is not allowed
    677675
    678         if (!is_array($current))
     676        if ( ! is_array($current) ) {
    679677            $attr2 .= ' '.$arreach['whole'];
    680678        # there are no checks
    681679
    682         else {
     680        } else {
    683681            # there are some checks
    684682            $ok = true;
    685             foreach ($current as $currkey => $currval)
    686                 if (!wp_kses_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval)) {
     683            foreach ($current as $currkey => $currval) {
     684                if ( ! wp_kses_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval) ) {
    687685                    $ok = false;
    688686                    break;
    689687                }
     688            }
    690689
    691690            if ( strtolower($arreach['name']) == 'style' ) {
    692691                $orig_value = $arreach['value'];
    693 
    694692                $value = safecss_filter_attr($orig_value);
    695693
     
    698696
    699697                $arreach['value'] = $value;
    700 
    701698                $arreach['whole'] = str_replace($orig_value, $value, $arreach['whole']);
    702699            }
     
    708705
    709706    # Remove any "<" or ">" characters
    710 
    711707    $attr2 = preg_replace('/[<>]/', '', $attr2);
    712708
Note: See TracChangeset for help on using the changeset viewer.