Changeset 131 for trunk/lib/redcloth/base.rb
- Timestamp:
- 01/17/2007 14:37:42 (22 months ago)
- Files:
-
- 1 modified
-
trunk/lib/redcloth/base.rb (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/redcloth/base.rb
r129 r131 123 123 no_textile text 124 124 rip_offtags text 125 clean_html text if filter_html 125 126 hard_break text 126 127 unless @lite_mode … … 129 130 end 130 131 inline text 132 133 escape_html_except_tags text if filter_html 134 131 135 smooth_offtags text 132 136 retrieve text … … 134 138 post_process text 135 139 DEFAULT_RULES.each {|ruleset| send("#{ruleset}_post_process", text) if private_methods.include? "#{ruleset}_post_process"} 136 137 clean_html text if filter_html138 140 139 141 return text.strip … … 383 385 # Flexible HTML escaping 384 386 # 385 def htmlesc( str, mode )387 def htmlesc( str, mode=nil ) 386 388 str.gsub!( '&', '&' ) 387 389 str.gsub!( '"', '"' ) if mode != :NoQuotes … … 627 629 'blockquote' => ['cite'] 628 630 } 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 ) 631 642 text.gsub!( /<!\[CDATA\[/, '' ) 632 text.gsub!( /<(\/*)( \w+)([^>]*)>/ ) do643 text.gsub!( /<(\/*)([A-Za-z]\w*)([^>]*)>/ ) do |m| 633 644 raw = $~ 634 645 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 636 649 pcs = [tag] 637 tags[tag].each do |prop|650 allowed_tags[tag].each do |prop| 638 651 ['"', "'", ''].each do |q| 639 652 q2 = ( q != '' ? q : '\s' ) … … 645 658 end 646 659 end 647 end if tags[tag]660 end if allowed_tags[tag] 648 661 "<#{raw[1]}#{pcs.join " "}>" 649 662 else 650 " " 663 htmlesc(m) # gsub!s m 664 m 651 665 end 652 666 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 653 687 end 654 688