diff --git a/opal/browser/dom.rb b/opal/browser/dom.rb index 39fd6ee..e9bc679 100644 --- a/opal/browser/dom.rb +++ b/opal/browser/dom.rb @@ -1,5 +1,6 @@ require 'browser/dom/node_set' require 'browser/dom/node' +require 'browser/dom/parent_node' require 'browser/dom/attribute' require 'browser/dom/character_data' require 'browser/dom/text' diff --git a/opal/browser/dom/element.rb b/opal/browser/dom/element.rb index a9087f2..5f3095e 100644 --- a/opal/browser/dom/element.rb +++ b/opal/browser/dom/element.rb @@ -14,6 +14,8 @@ module Browser; module DOM class Element < Node + include ParentNode + def self.create(*args) $document.create_element(*args) end diff --git a/opal/browser/dom/node.rb b/opal/browser/dom/node.rb index a300300..c30a20a 100644 --- a/opal/browser/dom/node.rb +++ b/opal/browser/dom/node.rb @@ -190,11 +190,6 @@ def remove parent.remove_child(self) if parent end - # Remove all the children of the node. - def clear - children.remove - end - # @!attribute content # @return [String] the inner text content of the node if Browser.supports? 'Element.textContent' @@ -235,17 +230,19 @@ def cdata? # @!attribute [r] child # @return [Node?] the first child of the node def child - children.first + first_child end - # @!attribute children - # @return [NodeSet] the children of the node - def children - NodeSet[Native::Array.new(`#@native.childNodes`)] + # @!attribute [r] first_child + # @return [Node?] the first child of the node + def first_child + DOM(`#@native.firstChild`) end - def children=(node) - raise NotImplementedError + # @!attribute [r] last_child + # @return [Node?] the last child of the node + def last_child + DOM(`#@native.lastChild`) end # Return true if the node is a comment. @@ -271,20 +268,6 @@ def elem? alias element? elem? - # @!attribute [r] element_children - # @return [NodeSet] all the children which are elements - def element_children - children.select(&:element?) - end - - alias elements element_children - - # @!attribute [r] first_element_child - # @return [Element?] the first element child - def first_element_child - element_children.first - end - # Return true if the node is a document fragment. def fragment? node_type == DOCUMENT_FRAGMENT_NODE @@ -303,12 +286,6 @@ def inner_html=(value) alias inner_text content alias inner_text= content= - # @!attribute [r] last_element_child - # @return [Element?] the last element child - def last_element_child - element_children.last - end - # @!attribute name # @return [String] the name of the node def name diff --git a/opal/browser/dom/parent_node.rb b/opal/browser/dom/parent_node.rb new file mode 100644 index 0000000..81045c8 --- /dev/null +++ b/opal/browser/dom/parent_node.rb @@ -0,0 +1,36 @@ +module Browser; module DOM + +# Abstract class for all Nodes that can have children. +# +# @see https://developer.mozilla.org/en-US/docs/Web/API/ParentNode +class ParentNode + + # @!attribute [r] children + # @return [NodeSet] the children of the node + def children + NodeSet[Native::Array.new(`#@native.children`)] + end + + alias elements children + + # @!attribute [r] first_element_child + # @return [Element?] the first element child + def first_element_child + DOM(`#@native.firstElementChild`) + end + + # @!attribute [r] last_element_child + # @return [Element?] the last element child + def last_element_child + DOM(`#@native.lastElementChild`) + end + + # @!attribute [r] child_element_count + # @return [Element?] the last element child + def child_element_count + `#@native.childElementCount` + end + +end + +end; end