Class: AntiSamy::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/antisamy/html/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy, output) ⇒ Handler

:nodoc:



6
7
8
9
10
11
12
13
# File 'lib/antisamy/html/handler.rb', line 6

def initialize(policy,output) #:nodoc:
  @document = Nokogiri::HTML::DocumentFragment.parse("")
  @current_node = @document
  @policy = policy
  @preserve_whitespace = @policy.directive(Policy::PRESERVE_SPACE)
  @errors = []
  @output_encoding = output
end

Instance Attribute Details

#errorsObject

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



22
23
24
25
# File 'lib/antisamy/html/handler.rb', line 22

def cdata(text)
  node = Nokogiri::XML::CDATA.new(@document,text)
  @current_node.add_child(node)
end

#characters(text) ⇒ Object

create a text node



33
34
35
36
37
38
39
40
# File 'lib/antisamy/html/handler.rb', line 33

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



28
29
30
# File 'lib/antisamy/html/handler.rb', line 28

def comment(text) #:nodoc:
  @current_node.add_child(Nokogiri::XML::Comment.new(@document, text))
end

#documentObject

format the output applying any policy rules



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/antisamy/html/handler.rb', line 76

def document
  # check some directives
  indent = 0
  options = Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS
  if @policy.directive(Policy::FORMAT_OUTPUT)
    options |= Nokogiri::XML::Node::SaveOptions::FORMAT
    indent = 2
  end
  if @policy.directive(Policy::OMIT_DOC_TYPE) || @policy.directive(Policy::OMIT_XML_DECL)
    options |= Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  end

  clean = ""
  if @policy.directive(Policy::USE_XHTML)
    options |= Nokogiri::XML::Node::SaveOptions::AS_XHTML
    clean = @document.to_xhtml(:encoding => @output_encoding, :indent=>indent,:save_with=>options)
  else
    clean = @document.to_html(:encoding => @output_encoding, :indent=>indent,:save_with=>options)
  end
  return clean
end

#encode_text(text) ⇒ Object

HTML entity encode some text



16
17
18
19
# File 'lib/antisamy/html/handler.rb', line 16

def encode_text(text)
  return "" if text.nil?
  @document.encode_special_chars(text)
end

#end_element(name) ⇒ Object

end an element



68
69
70
71
72
73
# File 'lib/antisamy/html/handler.rb', line 68

def end_element(name)
  if @current_node.nil? or !@current_node.name.eql?(name)
    return
  end
  @current_node = @current_node.parent if @current_node.parent
end

#start_element(name, attributes) ⇒ Object

start an element



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/antisamy/html/handler.rb', line 43

def start_element(name,attributes)
  if name.eql?("head") or name.eql?("body") or name.eql?("html")
    return
  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