-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamednodemap.go
63 lines (54 loc) · 1.81 KB
/
namednodemap.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package dom
import "fmt"
type domNamedNodeMap struct {
nodes map[string]Node
}
func newNamedNodeMap() NamedNodeMap {
nnm := &domNamedNodeMap{}
nnm.nodes = make(map[string]Node)
return nnm
}
// TODO: GetItems()... not according to spec though.
func (nnm *domNamedNodeMap) GetItems() map[string]Node {
return nnm.nodes
}
func (nnm *domNamedNodeMap) GetNamedItem(name string) Node {
return nnm.nodes[name]
}
// TODO: Return proper errors. The current implementation is not entirely correct.
// The spec mentions that a NamedNodeMap can accept Nodes other than Attr, except
// when the 'parent' is an Element. We'll need specializations of the NamedNodeMap
// interface, for ex. attrNamedNodeMap, entityNamedNodeMap.
//
// See: https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-1780488922
//
// Errors:
//
// NUSE_ATTRIBUTE_ERR: Raised if arg is an Attr that is already an attribute of
// another Element object. The DOM user must explicitly clone Attr nodes to
// re-use them in other elements.
//
// HIERARCHY_REQUEST_ERR: Raised if an attempt is made to add a node doesn't
// belong in this NamedNodeMap. Examples would include trying to insert something
// other than an Attr node into an Element's map of attributes, or a non-Entity
// node into the DocumentType's map of Entities.
func (nnm *domNamedNodeMap) SetNamedItem(n Node) error {
if _, ok := n.(Attr); ok {
nnm.nodes[n.GetNodeName()] = n
return nil
}
return fmt.Errorf("%v: can not set a non-Attr node as a named item", ErrorHierarchyRequest)
}
func (nnm *domNamedNodeMap) RemoveNamedItem(name string) {
delete(nnm.nodes, name)
}
func (nnm *domNamedNodeMap) Length() int {
return len(nnm.nodes)
}
func (nnm *domNamedNodeMap) String() string {
s := ""
for k, v := range nnm.GetItems() {
s += fmt.Sprintf("%v=%v,", k, v.GetNodeValue())
}
return s
}