Skip to content

Commit

Permalink
Node
Browse files Browse the repository at this point in the history
  • Loading branch information
duckpilot committed May 25, 2010
1 parent 257d832 commit 75247f8
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 253 deletions.
4 changes: 2 additions & 2 deletions src/ojox/dOMImpl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ val getInnerHTML : element -> string
val getInnerText : element -> string
val getNextSiblingElement : element -> element
val getNodeType : node -> int
val getParentElement : node -> element
val getParentElement : #node -> #element
val getScrollLeft : element -> int
val getScrollLeft_document : document -> int
val getScrollTop : document -> int
val getTagName : element -> string
val hasAttribute : element -> string -> bool
val imgGetSrc : imageElement -> string
val imgSetSrc : imageElement -> string -> unit
val isOrHasChild : node -> node -> bool
val isOrHasChild : #node -> #node -> bool
val scrollIntoView : element -> unit
val selectAdd : selectElement -> optionElement -> optionElement -> unit
val selectClear : selectElement -> unit
Expand Down
138 changes: 13 additions & 125 deletions src/ojox/dOMTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,138 +60,26 @@ class type node =
object
inherit javaScriptObject

(**
Adds the node newChild to the end of the list of children of this node. If
the newChild is already in the tree, it is first removed.
@param newChild The node to add
@return The node added
*)
method appendChild : 'a.'a -> 'a

(**
Returns a duplicate of this node, i.e., serves as a generic copy
constructor for nodes. The duplicate node has no parent; (parentNode is
null.).
Cloning an Element copies all attributes and their values, including those
generated by the XML processor to represent defaulted attributes, but this
method does not copy any text it contains unless it is a deep clone, since
the text is contained in a child Text node. Cloning an Attribute directly,
as opposed to be cloned as part of an Element cloning operation, returns a
specified attribute (specified is true). Cloning any other type of node
simply returns a copy of this node.
@param deep If true, recursively clone the subtree under the specified
node; if false, clone only the node itself (and its attributes, if
it is an {!Element})
@return The duplicate node
*)
method cloneNode : bool -> node

method appendChild : 'a. 'a -> 'a
method cloneNode : 'a. bool -> 'a
method contains : node -> bool

method dispatchEvent : nativeEvent -> unit

(**
A NodeList that contains all children of this node. If there are no
children, this is a NodeList containing no nodes.
*)
method _get_childNodes : node array

(**
The first child of this node. If there is no such node, this returns null.
*)
method _get_firstChild : node

(**
The last child of this node. If there is no such node, this returns null.
*)
method _get_lastChild : node

(**
The node immediately following this node. If there is no such node, this
returns null.
*)
method _get_nextSibling : node

(**
The name of this node, depending on its type; see the table above.
*)
method _get_childNodes : 'a. 'a array
method _get_firstChild : 'a. 'a
method _get_lastChild : 'a. 'a
method _get_nextSibling : 'a. 'a
method _get_nodeName : string

(**
A code representing the type of the underlying object, as defined above.
*)
method _get_nodeType : int

(**
The value of this node, depending on its type; see the table above. When it
is defined to be null, setting it has no effect.
*)
method _get_nodeValue : string

(**
The Document object associated with this node. This is also the
{!Document} object used to create new nodes.
*)
method _get_ownerDocument : 'a.'a

(**
The parent of this node. All nodes except Document may have a parent.
However, if a node has just been created and not yet added to the tree, or
if it has been removed from the tree, this is null.
*)
method _get_parentNode : node

(**
The node immediately preceding this node. If there is no such node, this
returns null.
*)
method _get_previousSibling : node

(**
Returns whether this node has any children.
*)
method _get_hasChildNodes : bool

(**
Inserts the node newChild before the existing child node refChild. If
refChild is [null], insert newChild at the end of the list of children.
@param newChild The node to insert
@param refChild The reference node (that is, the node before which the new
node must be inserted), or [null]
@return The node being inserted
*)
method insertBefore : node -> node -> node

(**
Removes the child node indicated by oldChild from the list of children, and
returns it.
@param oldChild The node being removed
@return The node removed
*)
method removeChild : node -> node

(**
Replaces the child node oldChild with newChild in the list of children, and
returns the oldChild node.
@param newChild The new node to put in the child list
@param oldChild The node being replaced in the list
@return The node replaced
*)
method replaceChild : node -> node -> node

(**
The value of this node, depending on its type; see the table above. When it
is defined to be null, setting it has no effect.
*)
method _get_ownerDocument : 'a. 'a
method _get_parentNode : 'a. 'a
method _get_previousSibling : 'a. 'a
method hasChildNodes : bool
method insertBefore : 'a 'b. 'a -> 'b -> 'a
method removeChild : 'a. 'a -> 'a
method replaceChild : 'a 'b. 'a -> 'b -> 'b
method _set_nodeValue : string -> unit

constraint 'a = #node
end

class type element =
Expand Down
139 changes: 13 additions & 126 deletions src/ojox/dOMTypes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -62,138 +62,25 @@ class type node =
object
inherit javaScriptObject

(**
Adds the node newChild to the end of the list of children of this node. If
the newChild is already in the tree, it is first removed.
@param newChild The node to add
@return The node added
*)
method appendChild : 'a.'a -> 'a

(**
Returns a duplicate of this node, i.e., serves as a generic copy
constructor for nodes. The duplicate node has no parent; (parentNode is
null.).
Cloning an Element copies all attributes and their values, including those
generated by the XML processor to represent defaulted attributes, but this
method does not copy any text it contains unless it is a deep clone, since
the text is contained in a child Text node. Cloning an Attribute directly,
as opposed to be cloned as part of an Element cloning operation, returns a
specified attribute (specified is true). Cloning any other type of node
simply returns a copy of this node.
@param deep If true, recursively clone the subtree under the specified
node; if false, clone only the node itself (and its attributes, if
it is an {!Element})
@return The duplicate node
*)
method cloneNode : bool -> node

method appendChild : 'a. 'a -> 'a
method cloneNode : 'a. bool -> 'a
method contains : node -> bool

method dispatchEvent : nativeEvent -> unit

(**
A NodeList that contains all children of this node. If there are no
children, this is a NodeList containing no nodes.
*)
method _get_childNodes : node array

(**
The first child of this node. If there is no such node, this returns null.
*)
method _get_firstChild : node

(**
The last child of this node. If there is no such node, this returns null.
*)
method _get_lastChild : node

(**
The node immediately following this node. If there is no such node, this
returns null.
*)
method _get_nextSibling : node

(**
The name of this node, depending on its type; see the table above.
*)
method _get_childNodes : 'a. 'a array
method _get_firstChild : 'a. 'a
method _get_lastChild : 'a. 'a
method _get_nextSibling : 'a. 'a
method _get_nodeName : string

(**
A code representing the type of the underlying object, as defined above.
*)
method _get_nodeType : int

(**
The value of this node, depending on its type; see the table above. When it
is defined to be null, setting it has no effect.
*)
method _get_nodeValue : string

(**
The Document object associated with this node. This is also the
{!Document} object used to create new nodes.
*)
method _get_ownerDocument : 'a.'a

(**
The parent of this node. All nodes except Document may have a parent.
However, if a node has just been created and not yet added to the tree, or
if it has been removed from the tree, this is null.
*)
method _get_parentNode : node

(**
The node immediately preceding this node. If there is no such node, this
returns null.
*)
method _get_previousSibling : node

(**
Returns whether this node has any children.
*)
method _get_hasChildNodes : bool

(**
Inserts the node newChild before the existing child node refChild. If
refChild is [null], insert newChild at the end of the list of children.
@param newChild The node to insert
@param refChild The reference node (that is, the node before which the new
node must be inserted), or [null]
@return The node being inserted
*)
method insertBefore : node -> node -> node

(**
Removes the child node indicated by oldChild from the list of children, and
returns it.
@param oldChild The node being removed
@return The node removed
*)
method removeChild : node -> node

(**
Replaces the child node oldChild with newChild in the list of children, and
returns the oldChild node.
@param newChild The new node to put in the child list
@param oldChild The node being replaced in the list
@return The node replaced
*)
method replaceChild : node -> node -> node

(**
The value of this node, depending on its type; see the table above. When it
is defined to be null, setting it has no effect.
*)
method _get_ownerDocument : 'a. 'a
method _get_parentNode : 'a. 'a
method _get_previousSibling : 'a. 'a
method hasChildNodes : bool
method insertBefore : 'a 'b. 'a -> 'b -> 'a
method removeChild : 'a. 'a -> 'a
method replaceChild : 'a 'b. 'a -> 'b -> 'b
method _set_nodeValue : string -> unit

constraint 'a = #node
end

class type element =
Expand Down
65 changes: 65 additions & 0 deletions src/ojox/node.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*)

open Ocamljs.Inline
open DOMTypes

let eLEMENT_NODE = 1
let tEXT_NODE = 3
Expand All @@ -28,3 +29,67 @@ let is o = << (!!$o$) && (!!$o$.nodeType) >>
let as_ o =
assert (is o);
(Obj.magic o : DOMTypes.node)

let appendChild (this : #node) newChild = this#appendChild newChild

let cloneNode (this : #node) deep = this#cloneNode deep

let getChildNodes (this : #node) = this#_get_childNodes

let getChildCount this = Array.length (getChildNodes this)

let getChild (this : #node) index =
if index < 0 || index < getChildCount this
then failwith "Child index out of bounds";
(getChildNodes this).(index)

let getFirstChild (this : #node) = this#_get_firstChild

let getLastChild (this : #node) = this#_get_lastChild

let getNextSibling (this : #node) = this#_get_nextSibling

let getNodeName this = this#_get_nodeName

let getNodeType this = this#_get_nodeType

let getNodeValue this = this#_get_nodeValue

let getOwnerDocument (this : #node) = this#_get_ownerDocument

let getParentElement this = DOMImpl.getParentElement this

let getParentNode (this : #node) = this#_get_parentNode

let getPreviousSibling (this : #node) = this#_get_previousSibling

let hasChildNodes this = this#hasChildNodes

let hasParentElement this = Ocamljs.is_null (getParentElement this)

let insertBefore (this : #node) ?refChild newChild =
let refChild = match refChild with None -> << null >> | Some rc -> rc in
this#insertBefore newChild refChild

let insertAfter this ?refChild newChild =
let next = match refChild with None -> << null >> | Some rc -> getNextSibling rc in
if Ocamljs.is_null next
then appendChild this newChild
else insertBefore this ~refChild:next newChild

let insertFirst this child =
let refChild = Ocamljs.option_of_nullable (getFirstChild this) in
insertBefore this ?refChild child

let isOrHasChild this child = DOMImpl.isOrHasChild this child

let removeChild (this : #node) oldChild = this#removeChild oldChild

let removeFromParent this =
let parent = getParentElement this in
if not (Ocamljs.is_null parent)
then ignore(removeChild parent this)

let replaceChild (this : #node) newChild oldChild = this#replaceChild newChild oldChild

let setNodeValue this nodeValue = this#_set_nodeValue nodeValue
Loading

0 comments on commit 75247f8

Please sign in to comment.