forked from JoshVarga/svgparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
40 lines (37 loc) · 1.1 KB
/
find.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
package svgparser
// FindID finds the first child with the specified ID.
func (e *Element) FindID(id string) *Element {
for _, child := range e.Children {
for _, attr := range child.Attributes {
if attr.Name.Local == "id" && attr.Value == id {
return child
}
}
if element := child.FindID(id); element != nil {
return element
}
}
return nil
}
// FindAll finds all children with the given name.
func (e *Element) FindAll(name string) []*Element {
var elements []*Element
for _, child := range e.Children {
if child.Name.Local == name {
elements = append(elements, child)
}
elements = append(elements, child.FindAll(name)...)
}
return elements
}
// FindAllBySpaceAndLocalName finds all children with the specific space and local name.
func (e *Element) FindAllBySpaceAndLocalName(space, localName string) []*Element {
var elements []*Element
for _, child := range e.Children {
if child.Name.Space == space && child.Name.Local == localName {
elements = append(elements, child)
}
elements = append(elements, child.FindAllBySpaceAndLocalName(space, localName)...)
}
return elements
}