Changeset 131

Show
Ignore:
Timestamp:
01/17/2007 14:37:42 (22 months ago)
Author:
jgarber
Message:

Fix :filter_html restriction. Closes #7 and fixes the bug exposed in [115].

Location:
trunk
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/redcloth/base.rb

    r129 r131  
    123123        no_textile text 
    124124        rip_offtags text 
     125        clean_html text if filter_html 
    125126        hard_break text 
    126127        unless @lite_mode 
     
    129130        end 
    130131        inline text 
     132 
     133        escape_html_except_tags text if filter_html 
     134 
    131135        smooth_offtags text 
    132136        retrieve text 
     
    134138        post_process text 
    135139        DEFAULT_RULES.each {|ruleset| send("#{ruleset}_post_process", text) if private_methods.include? "#{ruleset}_post_process"} 
    136  
    137         clean_html text if filter_html 
    138140 
    139141        return text.strip 
     
    383385    # Flexible HTML escaping 
    384386    # 
    385     def htmlesc( str, mode ) 
     387    def htmlesc( str, mode=nil ) 
    386388        str.gsub!( '&', '&' ) 
    387389        str.gsub!( '"', '"' ) if mode != :NoQuotes 
     
    627629        'blockquote' => ['cite'] 
    628630    } 
    629  
    630     def clean_html( text, tags = BASIC_TAGS ) 
     631     
     632    # Which tags to accept as input when :filter_html is on 
     633    ALLOWED_INCOMING_TAGS = { 
     634        'kbd' => nil, 
     635        'code' => ['lang'], 
     636        'notextile' => nil,  
     637        'pre' => nil 
     638    } 
     639 
     640    # Escape unauthorized tags 
     641    def clean_html( text, allowed_tags = ALLOWED_INCOMING_TAGS ) 
    631642        text.gsub!( /<!\[CDATA\[/, '' ) 
    632         text.gsub!( /<(\/*)(\w+)([^>]*)>/ ) do 
     643        text.gsub!( /<(\/*)([A-Za-z]\w*)([^>]*)>/ ) do |m| 
    633644            raw = $~ 
    634645            tag = raw[2].downcase 
    635             if tags.has_key? tag 
     646            if m =~ /<redpre#\d+>/ 
     647              m # return internal pre markers untouched 
     648            elsif allowed_tags.has_key? tag 
    636649                pcs = [tag] 
    637                 tags[tag].each do |prop| 
     650                allowed_tags[tag].each do |prop| 
    638651                    ['"', "'", ''].each do |q| 
    639652                        q2 = ( q != '' ? q : '\s' ) 
     
    645658                        end 
    646659                    end 
    647                 end if tags[tag] 
     660                end if allowed_tags[tag] 
    648661                "<#{raw[1]}#{pcs.join " "}>" 
    649662            else 
    650                 " " 
     663                htmlesc(m) # gsub!s m 
     664                m 
    651665            end 
    652666        end 
     667    end 
     668     
     669    def escape_html_except_tags(text) 
     670      text.gsub!(/ 
     671          ( <!-- (?m:.*?) --> 
     672            | <\/?  
     673              [A-Za-z]\w*\b                               # Tags start with a letter and 
     674              (?:<\d+>|[^>"']|"[^"]*"|'[^']*')* >         # can have shelved items or attributes. 
     675            | &(?:[a-zA-Z0-9]+|\#[0-9]+|\#x[0-9a-fA-F]+); # Existing entity. 
     676          ) 
     677          |([^<&]+|[<&]) 
     678 
     679        /x) do |m| 
     680          if $2 
     681            htmlesc(m) 
     682            m 
     683          else 
     684            m 
     685          end 
     686      end 
    653687    end 
    654688     
  • trunk/run-tests.rb

    r130 r131  
    1111    errors = [] 
    1212    tests = 0 
    13     options = (testfile =~ /hardbreaks/) ? [:hard_breaks] : [] 
     13    options = [] 
     14    options << :hard_breaks if testfile =~ /hardbreaks/ 
     15    options << :filter_html if testfile =~ /filter_html/ 
    1416     
    1517    print File.basename(testfile)+":\n\t" 
  • trunk/test/textism.yml

    r115 r131  
    373373        <li>We must act</li> 
    374374  </ul> 
    375 --- 
    376 in: /me <3 beer 
    377 out: <p>/me &lt;3 beer</p>