Skip to content

Commit bd1773d

Browse files
committed
Add ParentNode and fix children implementation
1 parent 1336a86 commit bd1773d

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

opal/browser/dom.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'browser/dom/node_set'
22
require 'browser/dom/node'
3+
require 'browser/dom/parent_node'
34
require 'browser/dom/attribute'
45
require 'browser/dom/character_data'
56
require 'browser/dom/text'

opal/browser/dom/element.rb

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
module Browser; module DOM
1515

1616
class Element < Node
17+
include ParentNode
18+
1719
def self.create(*args)
1820
$document.create_element(*args)
1921
end

opal/browser/dom/node.rb

+9-32
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,6 @@ def remove
190190
parent.remove_child(self) if parent
191191
end
192192

193-
# Remove all the children of the node.
194-
def clear
195-
children.remove
196-
end
197-
198193
# @!attribute content
199194
# @return [String] the inner text content of the node
200195
if Browser.supports? 'Element.textContent'
@@ -235,17 +230,19 @@ def cdata?
235230
# @!attribute [r] child
236231
# @return [Node?] the first child of the node
237232
def child
238-
children.first
233+
first_child
239234
end
240235

241-
# @!attribute children
242-
# @return [NodeSet] the children of the node
243-
def children
244-
NodeSet[Native::Array.new(`#@native.childNodes`)]
236+
# @!attribute [r] first_child
237+
# @return [Node?] the first child of the node
238+
def first_child
239+
DOM(`#@native.firstChild`)
245240
end
246241

247-
def children=(node)
248-
raise NotImplementedError
242+
# @!attribute [r] last_child
243+
# @return [Node?] the last child of the node
244+
def last_child
245+
DOM(`#@native.lastChild`)
249246
end
250247

251248
# Return true if the node is a comment.
@@ -271,20 +268,6 @@ def elem?
271268

272269
alias element? elem?
273270

274-
# @!attribute [r] element_children
275-
# @return [NodeSet] all the children which are elements
276-
def element_children
277-
children.select(&:element?)
278-
end
279-
280-
alias elements element_children
281-
282-
# @!attribute [r] first_element_child
283-
# @return [Element?] the first element child
284-
def first_element_child
285-
element_children.first
286-
end
287-
288271
# Return true if the node is a document fragment.
289272
def fragment?
290273
node_type == DOCUMENT_FRAGMENT_NODE
@@ -303,12 +286,6 @@ def inner_html=(value)
303286
alias inner_text content
304287
alias inner_text= content=
305288

306-
# @!attribute [r] last_element_child
307-
# @return [Element?] the last element child
308-
def last_element_child
309-
element_children.last
310-
end
311-
312289
# @!attribute name
313290
# @return [String] the name of the node
314291
def name

opal/browser/dom/parent_node.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Browser; module DOM
2+
3+
# Abstract class for all Nodes that can have children.
4+
#
5+
# @see https://developer.mozilla.org/en-US/docs/Web/API/ParentNode
6+
class ParentNode
7+
8+
# @!attribute [r] children
9+
# @return [NodeSet] the children of the node
10+
def children
11+
NodeSet[Native::Array.new(`#@native.children`)]
12+
end
13+
14+
alias elements children
15+
16+
# @!attribute [r] first_element_child
17+
# @return [Element?] the first element child
18+
def first_element_child
19+
DOM(`#@native.firstElementChild`)
20+
end
21+
22+
# @!attribute [r] last_element_child
23+
# @return [Element?] the last element child
24+
def last_element_child
25+
DOM(`#@native.lastElementChild`)
26+
end
27+
28+
# @!attribute [r] child_element_count
29+
# @return [Element?] the last element child
30+
def child_element_count
31+
`#@native.childElementCount`
32+
end
33+
34+
end
35+
36+
end; end

0 commit comments

Comments
 (0)