Class: Browser::DOM::NodeSet
Overview
Allows manipulation of a set of Nodes.
Class Method Summary collapse
-
.[](*nodes) ⇒ Object
Create a new NodeSet from the given nodes.
Instance Method Summary collapse
-
#at_css(*rules) ⇒ Node?
Get the first node matching the given CSS selectors.
-
#at_xpath(*paths) ⇒ Node?
Get the first node matching the given XPath.
-
#css(path) ⇒ NodeSet
Query for children matching the given CSS selector.
-
#filter(expression) ⇒ NodeSet
Create another NodeSet with all the nodes that match the given expression.
-
#initialize(literal) ⇒ NodeSet
constructor
A new instance of NodeSet.
-
#method_missing(name, *args, &block) ⇒ Object
Any other method will be called on every node in the set.
-
#outer_html ⇒ Object
Outer HTML of the entire nodeset.
- #respond_to_missing?(name) ⇒ Boolean
-
#search(*what) ⇒ Object
Search for multiple selectors.
- #to_ary ⇒ Object
-
#xpath(path) ⇒ NodeSet
Query for children matching the given XPath.
Constructor Details
#initialize(literal) ⇒ NodeSet
Returns a new instance of NodeSet.
13 14 15 |
# File 'opal/browser/dom/node_set.rb', line 13 def initialize(literal) @literal = literal end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Any other method will be called on every node in the set.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'opal/browser/dom/node_set.rb', line 18 def method_missing(name, *args, &block) unless @literal.respond_to? name each {|el| el.__send__(name, *args, &block) } return self end result = @literal.__send__ name, *args, &block if `result === #@literal` self elsif Array === result NodeSet.new(result) else result end end |
Class Method Details
.[](*nodes) ⇒ Object
Create a new Browser::DOM::NodeSet from the given nodes.
Note that the nodes are flattened and converted with DOM automatically, this means you can pass Browser::DOM::NodeSets and Native::Arrays as well.
9 10 11 |
# File 'opal/browser/dom/node_set.rb', line 9 def self.[](*nodes) new(nodes.flatten.map { |x| DOM(Native.convert(x)) }.uniq) end |
Instance Method Details
#at_css(*rules) ⇒ Node?
Get the first node matching the given CSS selectors.
47 48 49 50 51 52 53 54 55 |
# File 'opal/browser/dom/node_set.rb', line 47 def at_css(*rules) each {|node| if node = node.at_css(*rules) return node end } nil end |
#at_xpath(*paths) ⇒ Node?
Get the first node matching the given XPath.
62 63 64 65 66 67 68 69 70 |
# File 'opal/browser/dom/node_set.rb', line 62 def at_xpath(*paths) each {|node| if node = node.at_xpath(*paths) return node end } nil end |
#css(path) ⇒ NodeSet
Query for children matching the given CSS selector.
77 78 79 80 81 |
# File 'opal/browser/dom/node_set.rb', line 77 def css(path) NodeSet[@literal.map {|node| node.css(path) }] end |
#filter(expression) ⇒ NodeSet
Create another Browser::DOM::NodeSet with all the nodes that match the given expression.
89 90 91 |
# File 'opal/browser/dom/node_set.rb', line 89 def filter(expression) NodeSet[@literal.select { |node| node =~ expression }] end |
#outer_html ⇒ Object
Outer HTML of the entire nodeset
99 100 101 |
# File 'opal/browser/dom/node_set.rb', line 99 def outer_html @literal.map(&:outer_html).join end |
#respond_to_missing?(name) ⇒ Boolean
38 39 40 |
# File 'opal/browser/dom/node_set.rb', line 38 def respond_to_missing?(name, *) @literal.respond_to?(name) end |
#search(*what) ⇒ Object
Search for multiple selectors
94 95 96 |
# File 'opal/browser/dom/node_set.rb', line 94 def search(*what) NodeSet[@literal.map { |node| node.search(*what) }] end |
#to_ary ⇒ Object
114 115 116 |
# File 'opal/browser/dom/node_set.rb', line 114 def to_ary @literal end |