Class: AntiSamy::Handler
- Inherits:
-
Object
- Object
- AntiSamy::Handler
- Defined in:
- lib/antisamy/html/handler.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
Instance Method Summary collapse
-
#cdata(text) ⇒ Object
create a cdata section.
-
#characters(text) ⇒ Object
create a text node.
-
#comment(text) ⇒ Object
create a comment.
-
#document ⇒ Object
format the output applying any policy rules.
-
#encode_text(text) ⇒ Object
HTML entity encode some text.
-
#end_element(name) ⇒ Object
end an element.
-
#initialize(policy, output, fragment = true) ⇒ Handler
constructor
:nodoc:.
-
#start_element(name, attributes) ⇒ Object
start an element.
Constructor Details
#initialize(policy, output, fragment = true) ⇒ Handler
:nodoc:
6 7 8 9 10 11 12 13 14 |
# File 'lib/antisamy/html/handler.rb', line 6 def initialize(policy,output,fragment = true) #:nodoc: @document = Nokogiri::HTML::DocumentFragment.parse("") @current_node = @document @policy = policy @preserve_whitespace = @policy.directive(Policy::PRESERVE_SPACE) @errors = [] @output_encoding = output @fragment = fragment end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
5 6 7 |
# File 'lib/antisamy/html/handler.rb', line 5 def errors @errors end |
Instance Method Details
#cdata(text) ⇒ Object
create a cdata section
23 24 25 26 |
# File 'lib/antisamy/html/handler.rb', line 23 def cdata(text) node = Nokogiri::XML::CDATA.new(@document,text) @current_node.add_child(node) end |
#characters(text) ⇒ Object
create a text node
34 35 36 37 38 39 40 41 |
# File 'lib/antisamy/html/handler.rb', line 34 def characters(text) node = @current_node.children.last if node and node.text? node.content += text else @current_node.add_child(Nokogiri::XML::Text.new(text, @document)) end end |
#comment(text) ⇒ Object
create a comment
29 30 31 |
# File 'lib/antisamy/html/handler.rb', line 29 def comment(text) #:nodoc: @current_node.add_child(Nokogiri::XML::Comment.new(@document, text)) end |
#document ⇒ Object
format the output applying any policy rules
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/antisamy/html/handler.rb', line 89 def document # check some directives indent = 0 = Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS if @policy.directive(Policy::FORMAT_OUTPUT) |= Nokogiri::XML::Node::SaveOptions::FORMAT indent = 2 end if @policy.directive(Policy::OMIT_DOC_TYPE) || @policy.directive(Policy::OMIT_XML_DECL) |= Nokogiri::XML::Node::SaveOptions::NO_DECLARATION end clean = "" if @policy.directive(Policy::USE_XHTML) |= Nokogiri::XML::Node::SaveOptions::AS_XHTML clean = @document.to_xhtml(:encoding => @output_encoding, :indent=>indent,:save_with=>) else clean = @document.to_html(:encoding => @output_encoding, :indent=>indent,:save_with=>) end return clean end |
#encode_text(text) ⇒ Object
HTML entity encode some text
17 18 19 20 |
# File 'lib/antisamy/html/handler.rb', line 17 def encode_text(text) return "" if text.nil? @document.encode_special_chars(text) end |
#end_element(name) ⇒ Object
end an element
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/antisamy/html/handler.rb', line 71 def end_element(name) if @current_node.nil? or !@current_node.name.eql?(name) return end if @current_node.children.empty? if @policy.allow_empty?(@current_node.name) @current_node = @current_node.parent if @current_node.parent else tnode = @current_node @current_node = @current_node.parent if @current_node.parent tnode.remove end else @current_node = @current_node.parent if @current_node.parent end end |
#start_element(name, attributes) ⇒ Object
start an element
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/antisamy/html/handler.rb', line 44 def start_element(name,attributes) if @fragment if name.eql?("head") or name.eql?("body") or name.eql?("html") return end end elem = Nokogiri::XML::Element.new(name, @document) attributes.each do |attrib_pair| elem[attrib_pair.first] = attrib_pair.last end # Special param tag hacking, as libxml/nokogiri doesnt generate an end tag # for param tags it seems if name.eql?("param") inner_html = "<param" attributes.each do |attrib_pair| inner_html<< " #{attrib_pair.first}=\"#{attrib_pair.last}\"" end inner_html << "/>" # we create a fake cdata node, add it *and* dont move our parent yet elem = Nokogiri::XML::CDATA.new(@document,inner_html) @current_node.add_child(elem) return end @current_node = @current_node.add_child(elem) end |