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.



14
15
16
# File 'opal/browser/dom/node_set.rb', line 14

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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'opal/browser/dom/node_set.rb', line 19

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.



10
11
12
# File 'opal/browser/dom/node_set.rb', line 10

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:



48
49
50
51
52
53
54
55
56
# File 'opal/browser/dom/node_set.rb', line 48

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:



63
64
65
66
67
68
69
70
71
# File 'opal/browser/dom/node_set.rb', line 63

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:



78
79
80
81
82
# File 'opal/browser/dom/node_set.rb', line 78

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:



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

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

#outer_htmlObject

Outer HTML of the entire nodeset



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

def outer_html
  @literal.map(&:outer_html).join
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'opal/browser/dom/node_set.rb', line 39

def respond_to_missing?(name, *)
  @literal.respond_to?(name)
end

#search(*what) ⇒ Object

Search for multiple selectors



95
96
97
# File 'opal/browser/dom/node_set.rb', line 95

def search(*what)
  NodeSet[@literal.map { |node| node.search(*what) }]
end

#to_aryObject Also known as: to_a



115
116
117
# File 'opal/browser/dom/node_set.rb', line 115

def to_ary
  @literal
end

#xpath(path) ⇒ NodeSet

Query for children matching the given XPath.

Parameters:

Returns:



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

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