Class: Gammo::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/gammo/node.rb

Overview

Direct Known Subclasses

Document, Element, Text

Defined Under Namespace

Classes: Document, Element, Text

Constant Summary collapse

Error =

Represents the error token.

Class.new(Node)
Comment =

Represents the comment token like “<!– foo –>”.

Class.new(Node)
Doctype =

Represents the document type token.

Class.new(Node)
ScopeMarker =

Represents the marker defined in 12.2.4.3. html.spec.whatwg.org/multipage/parsing.html#tokenization

Class.new(Node)
DEFAULT_SCOPE_MARKER =

Default scope marker is inserted when entering applet, object, marquee, template, td, th, and caption elements, and are used to prevent formatting from “leaking” into applet, object, marquee, template, td, th, and caption elements“

Node::ScopeMarker.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag: nil, data: nil, namespace: nil, attributes: Attributes.new([])) ⇒ Gammo::Node

Constructs a node which represents HTML element node.

Parameters:

  • tag (String) (defaults to: nil)
  • data (String) (defaults to: nil)
  • namespace (String, NilClass) (defaults to: nil)
  • attributes (Gammo::Attributes) (defaults to: Attributes.new([]))


114
115
116
117
118
119
# File 'lib/gammo/node.rb', line 114

def initialize(tag: nil, data: nil, namespace: nil, attributes: Attributes.new([]))
  @tag        = tag
  @data       = data
  @namespace  = namespace
  @attributes = Attributes.new(attributes, owner_element: self)
end

Instance Attribute Details

#attributesObject

Reader for attributes associated with this node.



30
31
32
# File 'lib/gammo/node.rb', line 30

def attributes
  @attributes
end

#dataObject

Properties required to represent node.



27
28
29
# File 'lib/gammo/node.rb', line 27

def data
  @data
end

#first_childObject

‘first_child` and `last_child` are pointers for the first and the last nodes.



21
22
23
# File 'lib/gammo/node.rb', line 21

def first_child
  @first_child
end

#last_childObject

‘first_child` and `last_child` are pointers for the first and the last nodes.



21
22
23
# File 'lib/gammo/node.rb', line 21

def last_child
  @last_child
end

#namespaceObject

Properties required to represent node.



27
28
29
# File 'lib/gammo/node.rb', line 27

def namespace
  @namespace
end

#next_siblingObject

‘previous_sibling` and `next_sibling` are pointers for the previous and next sibling nodes.



24
25
26
# File 'lib/gammo/node.rb', line 24

def next_sibling
  @next_sibling
end

#parentObject

‘parent` is the pointer for the parent node.



18
19
20
# File 'lib/gammo/node.rb', line 18

def parent
  @parent
end

#previous_siblingObject

‘previous_sibling` and `next_sibling` are pointers for the previous and next sibling nodes.



24
25
26
# File 'lib/gammo/node.rb', line 24

def previous_sibling
  @previous_sibling
end

#tagObject

Properties required to represent node.



27
28
29
# File 'lib/gammo/node.rb', line 27

def tag
  @tag
end

Instance Method Details

#append_child(child) ⇒ Gammo::Node

Appends given ‘child` into self node.

Parameters:

Returns:

  • (Gammo::Node)

    A node appended into the self node.

Raises:

  • (HierarchyRequestError)

    Raised if given node is already attached to the self node.



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gammo/node.rb', line 162

def append_child(child)
  raise HierarchyRequestError,
    'append_child called for an attached child node' if attached?(child)
  if last = last_child
    last.next_sibling = child
  else
    @first_child = child
  end
  @last_child = child
  child.parent = self
  child.previous_sibling = last
  child
end

#childrenObject



221
222
223
224
225
226
227
228
229
# File 'lib/gammo/node.rb', line 221

def children
  ret = []
  child = first_child
  while child
    ret << child
    child = child.next_sibling
  end
  ret
end

#document?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/gammo/node.rb', line 237

def document?
  self.instance_of?(Document)
end

#each_descendantObject



43
44
45
46
47
48
49
50
51
# File 'lib/gammo/node.rb', line 43

def each_descendant
  stack = [self]
  until stack.empty?
    node = stack.pop
    yield node unless node == self
    stack << node.next_sibling if node != self && node.next_sibling
    stack << node.first_child if node.first_child
  end
end

#get_attribute_node(key, namespace: nil) ⇒ Object



39
40
41
# File 'lib/gammo/node.rb', line 39

def get_attribute_node(key, namespace: nil)
  attributes.find { |attr| attr.key == key && attr.namespace == namespace }
end

#insert_before(node, ref) ⇒ Gammo::Node

Inserts a node before a reference node as a child of a specified parent node.

Parameters:

Returns:

  • (Gammo::Node)

    A node inserted before the reference node.

Raises:

  • (HierarchyRequestError)

    Raised if given node is already attached to the self node.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/gammo/node.rb', line 134

def insert_before(node, ref)
  raise HierarchyRequestError,
    'insert_before called for an attached child node' if attached?(node)
  if ref
    previous_sibling, next_sibling = ref.previous_sibling, ref
  else
    previous_sibling = last_child
  end
  if previous_sibling
    previous_sibling.next_sibling = node
  else
    @first_child = node
  end
  if next_sibling
    next_sibling.previous_sibling = node
  else
    @last_child = node
  end
  node.parent = self
  node.previous_sibling = previous_sibling
  node.next_sibling = next_sibling
  node
end

#owner_documentObject



231
232
233
234
235
# File 'lib/gammo/node.rb', line 231

def owner_document
  node = self
  node = node.parent until node.document?
  node
end

#remove_child(child) ⇒ Gammo::Node

Removes given ‘child` from self node.

Parameters:

Returns:

Raises:

  • (UncaughtTypeError)

    Raised unless given node is not child of the self node.



180
181
182
183
184
185
186
187
188
189
# File 'lib/gammo/node.rb', line 180

def remove_child(child)
  raise UncaughtTypeError,
    'remove_child called for a non-child node' unless child?(child)
  @first_child = child.next_sibling if first_child == child
  child.next_sibling.previous_sibling = child.previous_sibling if child.next_sibling
  @last_child = child.previous_sibling if last_child == child
  child.previous_sibling.next_sibling = child.next_sibling if child.previous_sibling
  child.parent = child.previous_sibling = child.next_sibling = nil
  child
end

#select(&block) ⇒ Object

Select all nodes whose the evaluation of a given block is true.



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/gammo/node.rb', line 209

def select(&block)
  nodes = []
  stack = [self]
  until stack.empty?
    node = stack.pop
    nodes << node if block.call(node)
    stack << node.next_sibling if node.next_sibling
    stack << node.first_child if node.first_child
  end
  nodes
end

#text_contentObject



35
36
37
# File 'lib/gammo/node.rb', line 35

def text_content
  nil
end