Module: Scrapetor::Dom::NodeMethods

Included in:
Comment, Doctype, Document, Element, Text
Defined in:
lib/scrapetor/dom.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



18
19
20
# File 'lib/scrapetor/dom.rb', line 18

def parent
  @parent
end

Instance Method Details

#add_next_sibling(node_or_html) ⇒ Object Also known as: after



64
65
66
67
68
69
70
71
72
# File 'lib/scrapetor/dom.rb', line 64

def add_next_sibling(node_or_html)
  return self unless @parent
  nodes = Dom.normalize_replacement(node_or_html, parent: @parent)
  idx = @parent.children.index(self)
  return self unless idx
  nodes.each { |n| n.parent = @parent }
  @parent.children.insert(idx + 1, *nodes)
  nodes.last
end

#add_previous_sibling(node_or_html) ⇒ Object Also known as: before



53
54
55
56
57
58
59
60
61
# File 'lib/scrapetor/dom.rb', line 53

def add_previous_sibling(node_or_html)
  return self unless @parent
  nodes = Dom.normalize_replacement(node_or_html, parent: @parent)
  idx = @parent.children.index(self)
  return self unless idx
  nodes.each { |n| n.parent = @parent }
  @parent.children.insert(idx, *nodes)
  nodes.last
end

#comment?Boolean

Returns:

  • (Boolean)


28
# File 'lib/scrapetor/dom.rb', line 28

def comment?; false; end

#doctype?Boolean

Returns:

  • (Boolean)


29
# File 'lib/scrapetor/dom.rb', line 29

def doctype?; false; end

#documentObject



20
21
22
23
24
# File 'lib/scrapetor/dom.rb', line 20

def document
  cur = self
  cur = cur.parent while cur.respond_to?(:parent) && cur.parent
  cur
end

#element?Boolean

Returns:

  • (Boolean)


26
# File 'lib/scrapetor/dom.rb', line 26

def element?; false; end

#next_element_siblingObject



89
90
91
92
93
# File 'lib/scrapetor/dom.rb', line 89

def next_element_sibling
  cur = next_sibling
  cur = cur.next_sibling while cur && !cur.element?
  cur
end

#next_siblingObject



75
76
77
78
79
80
# File 'lib/scrapetor/dom.rb', line 75

def next_sibling
  return nil unless @parent
  sibs = @parent.children
  idx = sibs.index(self)
  idx && sibs[idx + 1]
end

#previous_element_siblingObject



95
96
97
98
99
# File 'lib/scrapetor/dom.rb', line 95

def previous_element_sibling
  cur = previous_sibling
  cur = cur.previous_sibling while cur && !cur.element?
  cur
end

#previous_siblingObject



82
83
84
85
86
87
# File 'lib/scrapetor/dom.rb', line 82

def previous_sibling
  return nil unless @parent
  sibs = @parent.children
  idx = sibs.index(self)
  idx && idx > 0 ? sibs[idx - 1] : nil
end

#removeObject Also known as: unlink, delete



31
32
33
34
35
36
# File 'lib/scrapetor/dom.rb', line 31

def remove
  return unless @parent
  @parent.children.delete(self)
  @parent = nil
  self
end

#replace(node_or_html) ⇒ Object Also known as: swap, replace_with



40
41
42
43
44
45
46
47
48
49
# File 'lib/scrapetor/dom.rb', line 40

def replace(node_or_html)
  replacements = Dom.normalize_replacement(node_or_html, parent: @parent)
  return self unless @parent
  idx = @parent.children.index(self)
  return self unless idx
  replacements.each { |r| r.parent = @parent }
  @parent.children[idx, 1] = replacements
  @parent = nil
  replacements.last
end

#text?Boolean

Returns:

  • (Boolean)


27
# File 'lib/scrapetor/dom.rb', line 27

def text?;    false; end