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.
-
#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.
43 44 45 46 47 48 49 50 51 |
# File 'opal/browser/dom/node_set.rb', line 43 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.
58 59 60 61 62 63 64 65 66 |
# File 'opal/browser/dom/node_set.rb', line 58 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.
73 74 75 76 77 |
# File 'opal/browser/dom/node_set.rb', line 73 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.
85 86 87 |
# File 'opal/browser/dom/node_set.rb', line 85 def filter(expression) @literal.select { |node| node =~ expression } end |
#search(*what) ⇒ Object
Search for multiple selectors
90 91 92 |
# File 'opal/browser/dom/node_set.rb', line 90 def search(*what) NodeSet[@literal.map { |node| node.search(*what) }] end |
#to_ary ⇒ Object
105 106 107 |
# File 'opal/browser/dom/node_set.rb', line 105 def to_ary @literal end |