Module: Scrapetor::Dom::NodeMethods
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #add_next_sibling(node_or_html) ⇒ Object (also: #after)
- #add_previous_sibling(node_or_html) ⇒ Object (also: #before)
- #comment? ⇒ Boolean
- #doctype? ⇒ Boolean
- #document ⇒ Object
- #element? ⇒ Boolean
- #next_element_sibling ⇒ Object
- #next_sibling ⇒ Object
- #previous_element_sibling ⇒ Object
- #previous_sibling ⇒ Object
- #remove ⇒ Object (also: #unlink, #delete)
- #replace(node_or_html) ⇒ Object (also: #swap, #replace_with)
- #text? ⇒ Boolean
Instance Attribute Details
#parent ⇒ Object
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
28 |
# File 'lib/scrapetor/dom.rb', line 28 def comment?; false; end |
#doctype? ⇒ Boolean
29 |
# File 'lib/scrapetor/dom.rb', line 29 def doctype?; false; end |
#document ⇒ Object
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
26 |
# File 'lib/scrapetor/dom.rb', line 26 def element?; false; end |
#next_element_sibling ⇒ Object
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_sibling ⇒ Object
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_sibling ⇒ Object
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_sibling ⇒ Object
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 |
#remove ⇒ Object 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
27 |
# File 'lib/scrapetor/dom.rb', line 27 def text?; false; end |