WordPress.org

Plugin Directory

Changeset 545262


Ignore:
Timestamp:
05/17/12 07:04:21 (2 years ago)
Author:
nbachiyski
Message:

Sepaarte the ICE_Span_Filter class in its own file

Location:
ice/trunk
Files:
3 added
1 edited

Legend:

Unmodified
Added
Removed
  • ice/trunk/mce-revisions.php

    r545254 r545262  
    2222*/ 
    2323 
     24require_once dirname( __FILE__ ) . '/class-ice-span-filter.php'; 
    2425 
    2526// Add plugins to TinyMCE 
     
    219220} 
    220221 
    221 /** 
    222  * Filters <span> elements, generated by ICE 
    223  */ 
    224 class ICE_Span_Filter { 
    225     private $text; 
    226  
    227     const TOKEN_TEXT = 'text'; 
    228     const TOKEN_SPAN = 'span'; 
    229     const TOKEN_DEL = 'del'; 
    230     const TOKEN_INS = 'ins'; 
    231     const TOKEN_CLOSING = 'close'; 
    232  
    233     const STATE_TOP_LEVEL = 'top'; 
    234     const STATE_IN_SPAN = 'in-span'; 
    235     const STATE_IN_INS = 'in-ins'; 
    236     const STATE_IN_DEL = 'in-del'; 
    237  
    238     const CLASS_INS = 'ice-wp-ins'; 
    239     const CLASS_DEL = 'ice-wp-del'; 
    240  
    241     function __construct( $text ) { 
    242         $this->text = $text; 
    243     } 
    244  
    245     /** 
    246      * Filters the text of the object 
    247      * 
    248      * - Removes both tags and their contents for <code>del</code> span tags 
    249      * - Removes tags, but leaves the content for <code>ins</code> span tags 
    250      * - Leaves the rest of the span tags alone 
    251      * 
    252      * Works as a simple state machine. 
    253      * 
    254      * @todo Deal with invalid span nesting 
    255      * @todo See if can break the method up, getting long 
    256      * 
    257      */ 
    258     function filter() { 
    259         $state = self::STATE_TOP_LEVEL; 
    260         $state_stack = array(); 
    261         $result = ''; 
    262  
    263         foreach ( $this->get_tokens() as $key => $token ) { 
    264             switch ( $token->id ) { 
    265             case self::TOKEN_DEL: 
    266                 $state_stack[] = self::STATE_IN_DEL; 
    267                 break; 
    268             case self::TOKEN_INS: 
    269                 $state_stack[] = self::STATE_IN_INS; 
    270                 break; 
    271             case self::TOKEN_SPAN: 
    272                 $state_stack[] = self::STATE_IN_SPAN; 
    273                 if ( !in_array( self::STATE_IN_DEL, $state_stack, true ) ) { 
    274                     $result .= $token->text; 
    275                 } 
    276                 break; 
    277             case self::TOKEN_CLOSING: 
    278                 if ( self::STATE_IN_SPAN == array_pop( $state_stack ) && !in_array( self::STATE_IN_DEL, $state_stack, true ) ) { 
    279                     $result .= $token->text; 
    280                 } 
    281                 break; 
    282             case self::TOKEN_TEXT: 
    283                 if ( !in_array( self::STATE_IN_DEL, $state_stack, true ) ) { 
    284                     $result .= $token->text; 
    285                 } 
    286                 break; 
    287             } 
    288         } 
    289  
    290         // Remove empty left over tags 
    291         while ( preg_match( '/<(p|div|span|li|blockquote|h[1-6])\b[^>]*>[ \r\n\t]*<\/\\1>/', $result, $matches ) ) { 
    292             $result = str_replace( $matches[0], '', $result ); 
    293         } 
    294  
    295         return $result; 
    296     } 
    297  
    298     /** 
    299      * Tokenizes (splits) the HTML into: 
    300      *  - normal spans: opening span tag, which is neither an <code>ins</code>, not a <code>del</code> 
    301      *  - ins spans: opening span tag with class ice-wp-ins 
    302      *  - del spans: opening span tag with class ice-wp-del 
    303      *  - closing: a closing span tag 
    304      *  - text: any other text or tags, which aren't relevant to our parsing 
    305      */ 
    306     function get_tokens() { 
    307         $tokens = array(); 
    308         $parts = preg_split( '|(</?span[^>]*>)|', $this->text, -1, PREG_SPLIT_DELIM_CAPTURE ); 
    309         foreach( $parts as $token_text ) { 
    310             if ( empty( $token_text ) ) { 
    311                 continue; 
    312             } 
    313             if ( '</span>' == $token_text ) { 
    314                 $id = self::TOKEN_CLOSING; 
    315             } elseif ( 0 === strpos( $token_text, '<span' ) ) { 
    316                 $css_classes = self::get_css_classes($token_text); 
    317  
    318                 if ( in_array( self::CLASS_INS, $css_classes, true ) ) 
    319                     $id = self::TOKEN_INS; 
    320                 elseif ( in_array( self::CLASS_DEL, $css_classes, true ) ) 
    321                     $id = self::TOKEN_DEL; 
    322                 else 
    323                     $id = self::TOKEN_SPAN; 
    324             } else { 
    325                 $id = self::TOKEN_TEXT; 
    326             } 
    327             $tokens[] = (object) array( 'id' => $id, 'text' => $token_text ); 
    328         } 
    329         return $tokens; 
    330     } 
    331  
    332     static function get_css_classes( $tag ) { 
    333         if ( preg_match( '/class\s*=\s*[\'"]([^\'"]+)[\'"]/', $tag, $matches ) ) 
    334             return preg_split( '/[ \r\n\t]+/', trim($matches[1]) ); 
    335  
    336         return array(); 
    337     } 
    338 } 
    339  
Note: See TracChangeset for help on using the changeset viewer.