Class: Nori::Parser::Nokogiri::Document

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/nori/parser/nokogiri.rb

Defined Under Namespace

Classes: CDataText

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



21
22
23
# File 'lib/nori/parser/nokogiri.rb', line 21

def options
  @options
end

Instance Method Details

#cdata_block(string) ⇒ Object

Adds the CDATA section content verbatim.



68
69
70
71
# File 'lib/nori/parser/nokogiri.rb', line 68

def cdata_block(string)
  last = stack.last
  last.add_node(CDataText.new(string)) if last
end

#characters(string) ⇒ Object

If this node is a successive character then add it as is. First child being a space-only text node will not be added because there is no previous characters, unless xml:space="preserve" is in effect.



60
61
62
63
64
65
# File 'lib/nori/parser/nokogiri.rb', line 60

def characters(string)
  last = stack.last
  if last and (preserve_whitespace? or last.children.last.is_a?(String) or string.strip.size > 0)
    last.add_node(string)
  end
end

#end_element(name) ⇒ Object

To keep backward behaviour compatibility delete last child if it is a space-only text node. CDATA content is literal and is never stripped, and under xml:space="preserve" whitespace-only text is kept too.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nori/parser/nokogiri.rb', line 43

def end_element(name)
  preserve = preserve_whitespace?
  space_stack.pop
  if stack.size > 1
    last = stack.pop
    maybe_string = last.children.last
    if !preserve and maybe_string.is_a?(String) and !maybe_string.is_a?(CDataText) and maybe_string.strip.empty?
      last.children.pop
    end
    stack.last.add_node last
  end
end

#space_stackObject

Tracks the xml:space value in effect for each open element so #preserve_whitespace? can honor XML 1.0 §2.10 inheritance.



29
30
31
# File 'lib/nori/parser/nokogiri.rb', line 29

def space_stack
  @space_stack ||= []
end

#stackObject



23
24
25
# File 'lib/nori/parser/nokogiri.rb', line 23

def stack
  @stack ||= []
end

#start_element(name, attrs = []) ⇒ Object



33
34
35
36
37
# File 'lib/nori/parser/nokogiri.rb', line 33

def start_element(name, attrs = [])
  attributes = Hash[*attrs.flatten]
  space_stack.push xml_space(attributes)
  stack.push Nori::XMLUtilityNode.new(options, name, attributes)
end