Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7595

Show
Ignore:
Timestamp:
09/23/07 18:09:46 (10 months ago)
Author:
rick
Message:

move TextHelper#sanitize config options to the TextHelper module so it can be included and used with any class, not just ActionView::Base

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_view/base.rb

    r7592 r7595  
    200200    cattr_accessor :erb_variable 
    201201     
    202     # A regular expression of the valid characters used to separate protocols like 
    203     # the ':' in 'http://foo.com' 
    204     @@sanitized_protocol_separator = /:|(&#0*58)|(&#x70)|(%|%)3A/ 
    205     cattr_accessor :sanitized_protocol_separator 
    206  
    207     # Specifies a Set of HTML attributes that can have URIs. 
    208     @@sanitized_uri_attributes = Set.new(%w(href src cite action longdesc xlink:href lowsrc)) 
    209     cattr_reader :sanitized_uri_attributes 
    210  
    211     # Adds valid HTML attributes that the #sanitize helper checks for URIs. 
    212     # 
    213     #   Rails::Initializer.run do |config| 
    214     #     config.action_view.sanitized_uri_attributes = 'lowsrc', 'target' 
    215     #   end 
    216     # 
    217     def self.sanitized_uri_attributes=(attributes) 
    218       @@sanitized_uri_attributes.merge(attributes) 
    219     end 
    220  
    221     # Specifies a Set of 'bad' tags that the #sanitize helper will remove completely, as opposed 
    222     # to just escaping harmless tags like <font> 
    223     @@sanitized_bad_tags = Set.new('script') 
    224     cattr_reader :sanitized_bad_tags 
    225      
    226     # Adds to the Set of 'bad' tags for the #sanitize helper. 
    227     # 
    228     #   Rails::Initializer.run do |config| 
    229     #     config.action_view.sanitized_bad_tags = 'embed', 'object' 
    230     #   end 
    231     # 
    232     def self.sanitized_bad_tags=(attributes) 
    233       @@sanitized_bad_tags.merge(attributes) 
    234     end 
    235      
    236     # Specifies the default Set of tags that the #sanitize helper will allow unscathed. 
    237     @@sanitized_allowed_tags = Set.new(%w(strong em b i p code pre tt output samp kbd var sub  
    238       sup dfn cite big small address hr br div span h1 h2 h3 h4 h5 h6 ul ol li dt dd abbr  
    239       acronym a img blockquote del ins fieldset legend)) 
    240     cattr_reader :sanitized_allowed_tags 
    241  
    242     # Adds to the Set of allowed tags for the #sanitize helper. 
    243     # 
    244     #   Rails::Initializer.run do |config| 
    245     #     config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td' 
    246     #   end 
    247     # 
    248     def self.sanitized_allowed_tags=(attributes) 
    249       @@sanitized_allowed_tags.merge(attributes) 
    250     end 
    251  
    252     # Specifies the default Set of html attributes that the #sanitize helper will leave  
    253     # in the allowed tag. 
    254     @@sanitized_allowed_attributes = Set.new(%w(href src width height alt cite datetime title class name xml:lang abbr)) 
    255     cattr_reader :sanitized_allowed_attributes 
    256  
    257     # Adds to the Set of allowed html attributes for the #sanitize helper. 
    258     # 
    259     #   Rails::Initializer.run do |config| 
    260     #     config.action_view.sanitized_allowed_attributes = 'onclick', 'longdesc' 
    261     #   end 
    262     # 
    263     def self.sanitized_allowed_attributes=(attributes) 
    264       @@sanitized_allowed_attributes.merge(attributes) 
    265     end 
    266  
    267     # Specifies the default Set of acceptable css properties that #sanitize and #sanitize_css will accept. 
    268     @@sanitized_allowed_css_properties = Set.new(%w(azimuth background-color border-bottom-color border-collapse  
    269       border-color border-left-color border-right-color border-top-color clear color cursor direction display  
    270       elevation float font font-family font-size font-style font-variant font-weight height letter-spacing line-height 
    271       overflow pause pause-after pause-before pitch pitch-range richness speak speak-header speak-numeral speak-punctuation 
    272       speech-rate stress text-align text-decoration text-indent unicode-bidi vertical-align voice-family volume white-space 
    273       width)) 
    274     cattr_reader :sanitized_allowed_css_properties 
    275  
    276     # Adds to the Set of allowed css properties for the #sanitize and #sanitize_css heleprs. 
    277     # 
    278     #   Rails::Initializer.run do |config| 
    279     #     config.action_view.sanitized_allowed_css_properties = 'expression' 
    280     #   end 
    281     # 
    282     def self.sanitized_allowed_css_properties=(attributes) 
    283       @@sanitized_allowed_css_properties.merge(attributes) 
    284     end 
    285      
    286     # Specifies the default Set of acceptable css keywords that #sanitize and #sanitize_css will accept. 
    287     @@sanitized_allowed_css_keywords = Set.new(%w(auto aqua black block blue bold both bottom brown center 
    288       collapse dashed dotted fuchsia gray green !important italic left lime maroon medium none navy normal 
    289       nowrap olive pointer purple red right solid silver teal top transparent underline white yellow)) 
    290     cattr_reader :sanitized_allowed_css_keywords 
    291  
    292     # Adds to the Set of allowed css keywords for the #sanitize and #sanitize_css helpers. 
    293     # 
    294     #   Rails::Initializer.run do |config| 
    295     #     config.action_view.sanitized_allowed_css_keywords = 'expression' 
    296     #   end 
    297     # 
    298     def self.sanitized_allowed_css_keywords=(attributes) 
    299       @@sanitized_allowed_css_keywords.merge(attributes) 
    300     end 
    301      
    302     # Specifies the default Set of allowed shorthand css properties for the #sanitize and #sanitize_css helpers. 
    303     @@sanitized_shorthand_css_properties = Set.new(%w(background border margin padding)) 
    304     cattr_reader :sanitized_shorthand_css_properties 
    305  
    306     # Adds to the Set of allowed shorthand css properties for the #sanitize and #sanitize_css helpers. 
    307     # 
    308     #   Rails::Initializer.run do |config| 
    309     #     config.action_view.sanitized_shorthand_css_properties = 'expression' 
    310     #   end 
    311     # 
    312     def self.sanitized_shorthand_css_properties=(attributes) 
    313       @@sanitized_shorthand_css_properties.merge(attributes) 
    314     end 
    315  
    316     # Specifies the default Set of protocols that the #sanitize helper will leave in 
    317     # protocol attributes. 
    318     @@sanitized_allowed_protocols = Set.new(%w(ed2k ftp http https irc mailto news gopher nntp telnet webcal xmpp callto feed svn urn aim rsync tag ssh sftp rtsp afs)) 
    319     cattr_reader :sanitized_allowed_protocols 
    320  
    321     # Adds to the Set of allowed protocols for the #sanitize helper. 
    322     # 
    323     #   Rails::Initializer.run do |config| 
    324     #     config.action_view.sanitized_allowed_protocols = 'ssh', 'feed' 
    325     #   end 
    326     # 
    327     def self.sanitized_allowed_protocols=(attributes) 
    328       @@sanitized_allowed_protocols.merge(attributes) 
    329     end 
    330  
    331202    delegate :request_forgery_protection_token, :to => :controller 
    332203 
  • trunk/actionpack/lib/action_view/helpers/text_helper.rb

    r7592 r7595  
    88    # your views. These helper methods extend ActionView making them callable  
    99    # within your template files. 
    10     module TextHelper       
     10    module TextHelper 
     11      def self.included(base) 
     12        base.extend(ClassMethods) 
     13      end 
     14       
    1115      # The preferred method of outputting text in your views is to use the  
    1216      # <%= "text" %> eRuby syntax. The regular _puts_ and _print_ methods  
     
    563567        end 
    564568      end 
    565        
     569 
     570      # A regular expression of the valid characters used to separate protocols like 
     571      # the ':' in 'http://foo.com' 
     572      @@sanitized_protocol_separator = /:|(&#0*58)|(&#x70)|(%|&#37;)3A/ 
     573      mattr_accessor :sanitized_protocol_separator, :instance_writer => false 
     574       
     575      # Specifies a Set of HTML attributes that can have URIs. 
     576      @@sanitized_uri_attributes = Set.new(%w(href src cite action longdesc xlink:href lowsrc)) 
     577      mattr_reader :sanitized_uri_attributes 
     578       
     579      # Specifies a Set of 'bad' tags that the #sanitize helper will remove completely, as opposed 
     580      # to just escaping harmless tags like &lt;font&gt; 
     581      @@sanitized_bad_tags = Set.new('script') 
     582      mattr_reader :sanitized_bad_tags 
     583       
     584      # Specifies the default Set of tags that the #sanitize helper will allow unscathed. 
     585      @@sanitized_allowed_tags = Set.new(%w(strong em b i p code pre tt output samp kbd var sub  
     586        sup dfn cite big small address hr br div span h1 h2 h3 h4 h5 h6 ul ol li dt dd abbr  
     587        acronym a img blockquote del ins fieldset legend)) 
     588      mattr_reader :sanitized_allowed_tags 
     589       
     590      # Specifies the default Set of html attributes that the #sanitize helper will leave  
     591      # in the allowed tag. 
     592      @@sanitized_allowed_attributes = Set.new(%w(href src width height alt cite datetime title class name xml:lang abbr)) 
     593      mattr_reader :sanitized_allowed_attributes 
     594       
     595      # Specifies the default Set of acceptable css properties that #sanitize and #sanitize_css will accept. 
     596      @@sanitized_allowed_css_properties = Set.new(%w(azimuth background-color border-bottom-color border-collapse  
     597        border-color border-left-color border-right-color border-top-color clear color cursor direction display  
     598        elevation float font font-family font-size font-style font-variant font-weight height letter-spacing line-height 
     599        overflow pause pause-after pause-before pitch pitch-range richness speak speak-header speak-numeral speak-punctuation 
     600        speech-rate stress text-align text-decoration text-indent unicode-bidi vertical-align voice-family volume white-space 
     601        width)) 
     602      mattr_reader :sanitized_allowed_css_properties 
     603       
     604      # Specifies the default Set of acceptable css keywords that #sanitize and #sanitize_css will accept. 
     605      @@sanitized_allowed_css_keywords = Set.new(%w(auto aqua black block blue bold both bottom brown center 
     606        collapse dashed dotted fuchsia gray green !important italic left lime maroon medium none navy normal 
     607        nowrap olive pointer purple red right solid silver teal top transparent underline white yellow)) 
     608      mattr_reader :sanitized_allowed_css_keywords 
     609       
     610      # Specifies the default Set of allowed shorthand css properties for the #sanitize and #sanitize_css helpers. 
     611      @@sanitized_shorthand_css_properties = Set.new(%w(background border margin padding)) 
     612      mattr_reader :sanitized_shorthand_css_properties 
     613       
     614      # Specifies the default Set of protocols that the #sanitize helper will leave in 
     615      # protocol attributes. 
     616      @@sanitized_allowed_protocols = Set.new(%w(ed2k ftp http https irc mailto news gopher nntp telnet webcal xmpp callto feed svn urn aim rsync tag ssh sftp rtsp afs)) 
     617      mattr_reader :sanitized_allowed_protocols 
     618 
     619      module ClassMethods #:nodoc: 
     620        def self.extended(base) 
     621          class << base 
     622            # we want these to be class methods on ActionView::Base, they'll get mattr_readers for these below. 
     623            [:sanitized_protocol_separator, :sanitized_uri_attributes, :sanitized_bad_tags, :sanitized_allowed_tags, 
     624                :sanitized_allowed_attributes, :sanitized_allowed_css_properties, :sanitized_allowed_css_keywords, 
     625                :sanitized_shorthand_css_properties, :sanitized_allowed_protocols, :sanitized_protocol_separator=].each do |prop| 
     626              delegate prop, :to => TextHelper 
     627            end 
     628          end 
     629        end 
     630 
     631        # Adds valid HTML attributes that the #sanitize helper checks for URIs. 
     632        # 
     633        #   Rails::Initializer.run do |config| 
     634        #     config.action_view.sanitized_uri_attributes = 'lowsrc', 'target' 
     635        #   end 
     636        # 
     637        def sanitized_uri_attributes=(attributes) 
     638          Helpers::TextHelper.sanitized_uri_attributes.merge(attributes) 
     639        end 
     640 
     641        # Adds to the Set of 'bad' tags for the #sanitize helper. 
     642        # 
     643        #   Rails::Initializer.run do |config| 
     644        #     config.action_view.sanitized_bad_tags = 'embed', 'object' 
     645        #   end 
     646        # 
     647        def sanitized_bad_tags=(attributes) 
     648          Helpers::TextHelper.sanitized_bad_tags.merge(attributes) 
     649        end 
     650        # Adds to the Set of allowed tags for the #sanitize helper. 
     651        # 
     652        #   Rails::Initializer.run do |config| 
     653        #     config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td' 
     654        #   end 
     655        # 
     656        def sanitized_allowed_tags=(attributes) 
     657          Helpers::TextHelper.sanitized_allowed_tags.merge(attributes) 
     658        end 
     659 
     660        # Adds to the Set of allowed html attributes for the #sanitize helper. 
     661        # 
     662        #   Rails::Initializer.run do |config| 
     663        #     config.action_view.sanitized_allowed_attributes = 'onclick', 'longdesc' 
     664        #   end 
     665        # 
     666        def sanitized_allowed_attributes=(attributes) 
     667          Helpers::TextHelper.sanitized_allowed_attributes.merge(attributes) 
     668        end 
     669 
     670        # Adds to the Set of allowed css properties for the #sanitize and #sanitize_css heleprs. 
     671        # 
     672        #   Rails::Initializer.run do |config| 
     673        #     config.action_view.sanitized_allowed_css_properties = 'expression' 
     674        #   end 
     675        # 
     676        def sanitized_allowed_css_properties=(attributes) 
     677          Helpers::TextHelper.sanitized_allowed_css_properties.merge(attributes) 
     678        end 
     679 
     680        # Adds to the Set of allowed css keywords for the #sanitize and #sanitize_css helpers. 
     681        # 
     682        #   Rails::Initializer.run do |config| 
     683        #     config.action_view.sanitized_allowed_css_keywords = 'expression' 
     684        #   end 
     685        # 
     686        def sanitized_allowed_css_keywords=(attributes) 
     687          Helpers::TextHelper.sanitized_allowed_css_keywords.merge(attributes) 
     688        end 
     689 
     690        # Adds to the Set of allowed shorthand css properties for the #sanitize and #sanitize_css helpers. 
     691        # 
     692        #   Rails::Initializer.run do |config| 
     693        #     config.action_view.sanitized_shorthand_css_properties = 'expression' 
     694        #   end 
     695        # 
     696        def sanitized_shorthand_css_properties=(attributes) 
     697          Helpers::TextHelper.sanitized_shorthand_css_properties.merge(attributes) 
     698        end 
     699 
     700        # Adds to the Set of allowed protocols for the #sanitize helper. 
     701        # 
     702        #   Rails::Initializer.run do |config| 
     703        #     config.action_view.sanitized_allowed_protocols = 'ssh', 'feed' 
     704        #   end 
     705        # 
     706        def sanitized_allowed_protocols=(attributes) 
     707          Helpers::TextHelper.sanitized_allowed_protocols.merge(attributes) 
     708        end 
     709      end 
     710 
    566711      private 
    567712        # The cycle helpers need to store the cycles in a place that is