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:


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.

Parameters:

  • paths (Array<String>)

    the XPath to match with

Returns:


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.

Parameters:

  • path (String)

    the CSS selector

Returns:


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.

Parameters:

  • expression (String)

    a CSS selector

Returns:


89
90
91
# File 'opal/browser/dom/node_set.rb', line 89

def filter(expression)
  NodeSet[@literal.select { |node| node =~ expression }]
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (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_aryObject


109
110
111
# File 'opal/browser/dom/node_set.rb', line 109

def to_ary
  @literal
end

#xpath(path) ⇒ NodeSet

Query for children matching the given XPath.

Parameters:

Returns:


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

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