Class: Browser::DOM::NodeSet

Inherits:
Object
  • Object
show all
Defined in:
opal/browser/dom/node_set.rb

Overview

Allows manipulation of a set of Nodes.

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • rules (Array<String>)

    the CSS selectors to match with

Returns:



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.

Parameters:

  • paths (Array<String>)

    the XPath to match with

Returns:



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.

Parameters:

  • selector (String)

    the CSS selector

Returns:



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.

Parameters:

  • expression (String)

    a CSS selector

Returns:



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_aryObject



105
106
107
# File 'opal/browser/dom/node_set.rb', line 105

def to_ary
  @literal
end

#xpath(path) ⇒ NodeSet

Query for children matching the given XPath.

Parameters:

Returns:



99
100
101
102
103
# File 'opal/browser/dom/node_set.rb', line 99

def xpath(path)
  NodeSet[@literal.map {|node|
    node.xpath(path)
  }]
end