-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicnamednodemap.go
119 lines (104 loc) · 2.88 KB
/
basicnamednodemap.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package dom
import (
"encoding/xml"
)
type basicNamedNodeMap struct {
attrs []*BasicAttr
mapAttrs map[xml.Name]*BasicAttr
}
func (m *basicNamedNodeMap) GetLength() int {
return len(m.attrs)
}
// Returns a Attr identified by a namespace and related local name.
func (m *basicNamedNodeMap) GetNamedItemNS(uri string, name string) Attr {
if m.mapAttrs == nil {
return nil
}
item, ok := m.mapAttrs[xml.Name{Local: name, Space: uri}]
if !ok {
return nil
}
return item
}
// Returns the Attr at the given index, or null if the index is higher or equal to the number of nodes
func (m *basicNamedNodeMap) Item(index int) Attr {
if index < 0 || index >= len(m.attrs) {
return nil
}
return m.attrs[index]
}
// RemoveNamedItemNS removes the Attr identified by the given name
func (m *basicNamedNodeMap) RemoveNamedItemNS(uri string, name string) {
if m.mapAttrs == nil {
return
}
attr, exists := m.mapAttrs[xml.Name{Local: name, Space: uri}]
if !exists {
return
}
m.removeAttr(attr)
}
func (m *basicNamedNodeMap) removeAttr(attr Attr) {
qname := attr.(*BasicAttr).name.Name
delete(m.mapAttrs, qname)
w := 0
for k := range m.attrs {
if m.attrs[k] != attr {
m.attrs[w] = m.attrs[k]
w++
}
}
m.attrs = m.attrs[:w]
}
// Replaces, or adds, the Attr identified in the map by the given namespace and related local name.
func (m *basicNamedNodeMap) setNamedItemNS(owner Node, attr Attr) {
if attr.GetOwnerElement() != nil && attr.GetOwnerElement() != owner {
panic(ErrDOM{
Typ: INUSE_ATTRIBUTE_ERR,
Msg: "Attribute already in use",
Op: "SetNamedItem",
})
}
if m.mapAttrs == nil {
m.mapAttrs = make(map[xml.Name]*BasicAttr)
}
ba := attr.(*BasicAttr)
qname := ba.name.Name
existing := m.mapAttrs[qname]
if existing != nil {
if existing == attr {
return
}
m.mapAttrs[qname] = ba
for k := range m.attrs {
if m.attrs[k] == existing {
m.attrs[k] = ba
ba.parent = owner
return
}
}
}
m.mapAttrs[qname] = ba
m.attrs = append(m.attrs, ba)
ba.parent = owner
}
type BasicNamedNodeMap struct {
owner *BasicElement
}
var _ NamedNodeMap = &BasicNamedNodeMap{}
func (b *BasicNamedNodeMap) GetLength() int { return b.owner.attributes.GetLength() }
// Returns the Attr at the given index, or null if the index is higher or equal to the number of nodes
func (b *BasicNamedNodeMap) Item(i int) Attr {
return b.owner.attributes.Item(i)
}
// Returns a Attr identified by a namespace and related local name.
func (b *BasicNamedNodeMap) GetNamedItemNS(uri string, name string) Attr {
return b.owner.attributes.GetNamedItemNS(uri, name)
}
// Replaces, or adds, the Attr identified in the map by the given namespace and related local name.
func (b *BasicNamedNodeMap) SetNamedItemNS(a Attr) {
b.owner.attributes.setNamedItemNS(b.owner, a)
}
func (b *BasicNamedNodeMap) RemoveNamedItemNS(uri string, name string) {
b.owner.attributes.RemoveNamedItemNS(uri, name)
}