-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathXMLNamedNodeMap.coffee
More file actions
42 lines (33 loc) · 1.33 KB
/
XMLNamedNodeMap.coffee
File metadata and controls
42 lines (33 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Represents a map of nodes accessed by a string key
module.exports = class XMLNamedNodeMap
# Initializes a new instance of `XMLNamedNodeMap`
# This is just a wrapper around an ordinary
# JS object.
#
# `nodes` the object containing nodes.
constructor: (@nodes) ->
# DOM level 1
Object.defineProperty @::, 'length', get: () -> Object.keys(@nodes).length or 0
# Creates and returns a deep clone of `this`
#
clone: () ->
# this class should not be cloned since it wraps
# around a given object. The calling function should check
# whether the wrapped object is null and supply a new object
# (from the clone).
@nodes = null
# DOM Level 1
getNamedItem: (name) -> @nodes[name]
setNamedItem: (node) ->
oldNode = @nodes[node.nodeName]
@nodes[node.nodeName] = node
return oldNode or null
removeNamedItem: (name) ->
oldNode = @nodes[name]
delete @nodes[name]
return oldNode or null
item: (index) -> @nodes[Object.keys(@nodes)[index]] or null
# DOM level 2 functions to be implemented later
getNamedItemNS: (namespaceURI, localName) -> throw new Error "This DOM method is not implemented."
setNamedItemNS: (node) -> throw new Error "This DOM method is not implemented."
removeNamedItemNS: (namespaceURI, localName) -> throw new Error "This DOM method is not implemented."