Skip to content

Commit

Permalink
Don't break compatibility for pre-rails_xss rails
Browse files Browse the repository at this point in the history
  • Loading branch information
gtd committed Feb 16, 2011
1 parent cb8df05 commit d930fd1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/acts_as_sanitiled.rb
Expand Up @@ -14,6 +14,10 @@ def self.included(klass)
klass.extend ClassMethods
end

def self.html_safe_available?
"".respond_to?(:html_safe)
end

module ClassMethods
def acts_as_textiled(*attributes)
raise "only acts_as_sanitized or acts_as_sanitiled can take an options hash" if attributes.last.is_a?(Hash)
Expand Down Expand Up @@ -58,7 +62,7 @@ def acts_as_sanitiled(*attributes)
string = Sanitize.clean(string, sanitize_options) unless skip_sanitize
textiled[attribute.to_s] = string
end
textiled[attribute.to_s].html_safe
ActsAsSanitiled.html_safe_available? ? textiled[attribute.to_s].html_safe : textiled[attribute.to_s]
elsif type.nil? && self[attribute].nil?
nil
elsif type_options.include?(type.to_s)
Expand All @@ -68,7 +72,7 @@ def acts_as_sanitiled(*attributes)
end
end

define_method("#{attribute}_plain", proc { strip_html(__send__(attribute)).html_safe if __send__(attribute) } )
define_method("#{attribute}_plain", proc { ActsAsSanitiled.html_safe_available? ? strip_html(__send__(attribute)).html_safe : strip_html(__send__(attribute)) if __send__(attribute) } )
define_method("#{attribute}_source", proc { __send__("#{attribute}_before_type_cast") } )

@textiled_attributes << attribute
Expand Down
6 changes: 3 additions & 3 deletions spec/sanitiled_spec.rb
Expand Up @@ -49,7 +49,7 @@
it "should mark textilized and sanitized output as html safe" do
@story.description.should.be :html_safe?
@story.body.should.be :html_safe?
end
end if ActsAsSanitiled.html_safe_available?

it "should properly strip html when given the 'plain' option" do
@story.description(:plain).should.equal @desc_plain
Expand All @@ -59,7 +59,7 @@
it "should mark output stripped of html as html safe" do
@story.description(:plain).should.be :html_safe?
@story.body(:plain).should.be :html_safe?
end
end if ActsAsSanitiled.html_safe_available?

it "should leave unchanged when given the 'source' option" do
@story.description(:source).should.equal @desc_textile
Expand All @@ -69,7 +69,7 @@
it "should not mark raw source as html safe" do
@story.description(:source).should.not.be :html_safe?
@story.body(:source).should.not.be :html_safe?
end
end if ActsAsSanitiled.html_safe_available?

it "should raise when given a non-sensical option" do
proc{ @story.description(:cassadaga) }.should.raise
Expand Down

0 comments on commit d930fd1

Please sign in to comment.