Class: HTML5::TreeWalkers::NonRecursiveTreeWalker

Inherits:
Base
  • Object
show all
Defined in:
lib/html5/treewalkers/base.rb

Direct Known Subclasses

Hpricot::TreeWalker, REXML::TreeWalker

Instance Method Summary collapse

Methods inherited from Base

#initialize, #to_ary

Methods included from TokenConstructor

#_, #comment, #doctype, #empty_tag, #end_tag, #error, #normalize_attrs, #start_tag, #text, #unknown

Constructor Details

This class inherits a constructor from HTML5::TreeWalkers::Base

Instance Method Details

#eachObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/html5/treewalkers/base.rb', line 98

def each
  current_node = @tree
  while current_node != nil
    details = node_details(current_node)
    has_children = false

    case details.shift
    when :DOCTYPE
      yield doctype(*details)

    when :TEXT
      text(*details) {|token| yield token}

    when :ELEMENT
      name, attributes, has_children = details
      if VOID_ELEMENTS.include?(name)
        yield empty_tag(name, attributes.to_a, has_children)
        has_children = false
      else
        yield start_tag(name, attributes.to_a)
      end

    when :COMMENT
      yield comment(details[0])

    when :DOCUMENT, :DOCUMENT_FRAGMENT
      has_children = true

    when nil
      # ignore (REXML::XMLDecl is an example)

    else
      yield unknown(details[0])
    end

    first_child = has_children ? first_child(current_node) : nil
    if first_child != nil
      current_node = first_child
    else
      while current_node != nil
        details = node_details(current_node)
        if details.shift == :ELEMENT
          name, attributes, has_children = details
          yield end_tag(name) if !VOID_ELEMENTS.include?(name)
        end

        if @tree == current_node
          current_node = nil
        else
          next_sibling = next_sibling(current_node)
          if next_sibling != nil
            current_node = next_sibling
            break
          end

          current_node = parent(current_node)
        end
      end
    end
  end
end

#first_child(node) ⇒ Object

Raises:

  • (NotImplementedError)


86
87
88
# File 'lib/html5/treewalkers/base.rb', line 86

def first_child(node)
  raise NotImplementedError
end

#next_sibling(node) ⇒ Object

Raises:

  • (NotImplementedError)


90
91
92
# File 'lib/html5/treewalkers/base.rb', line 90

def next_sibling(node)
  raise NotImplementedError
end

#node_details(node) ⇒ Object

Raises:

  • (NotImplementedError)


82
83
84
# File 'lib/html5/treewalkers/base.rb', line 82

def node_details(node)
  raise NotImplementedError
end

#parent(node) ⇒ Object

Raises:

  • (NotImplementedError)


94
95
96
# File 'lib/html5/treewalkers/base.rb', line 94

def parent(node)
  raise NotImplementedError
end