Skip to content

Commit

Permalink
add func newXMLName for attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
egginabucket committed Mar 7, 2023
1 parent 847b2f6 commit 427ed60
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 55 deletions.
74 changes: 26 additions & 48 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ func WithoutComments() OutputOption {
}
}

func newXMLName(name string) xml.Name {
if i := strings.IndexByte(name, ':'); i > 0 {
return xml.Name{
Space: name[:i],
Local: name[i+1:],
}
}
return xml.Name{
Local: name,
}
}

// InnerText returns the text between the start and end tags of the object.
func (n *Node) InnerText() string {
var output func(*strings.Builder, *Node)
Expand Down Expand Up @@ -220,69 +232,35 @@ func (n *Node) OutputXMLWithOptions(opts ...OutputOption) string {

// AddAttr adds a new attribute specified by 'key' and 'val' to a node 'n'.
func AddAttr(n *Node, key, val string) {
var attr Attr
if i := strings.Index(key, ":"); i > 0 {
attr = Attr{
Name: xml.Name{Space: key[:i], Local: key[i+1:]},
Value: val,
}
} else {
attr = Attr{
Name: xml.Name{Local: key},
Value: val,
}
attr := Attr{
Name: newXMLName(key),
Value: val,
}

n.Attr = append(n.Attr, attr)
}

// SetAttr allows an attribute value with the specified name to be changed.
// If the attribute did not previously exist, it will be created.
func (n *Node) SetAttr(key, value string) {
if i := strings.Index(key, ":"); i > 0 {
space := key[:i]
local := key[i+1:]
for idx := 0; idx < len(n.Attr); idx++ {
if n.Attr[idx].Name.Space == space && n.Attr[idx].Name.Local == local {
n.Attr[idx].Value = value
return
}
}

AddAttr(n, key, value)
} else {
for idx := 0; idx < len(n.Attr); idx++ {
if n.Attr[idx].Name.Local == key {
n.Attr[idx].Value = value
return
}
name := newXMLName(key)
for i, attr := range n.Attr {
if attr.Name == name {
n.Attr[i].Value = value
return
}

AddAttr(n, key, value)
}
AddAttr(n, key, value)
}

// RemoveAttr removes the attribute with the specified name.
func (n *Node) RemoveAttr(key string) {
removeIdx := -1
if i := strings.Index(key, ":"); i > 0 {
space := key[:i]
local := key[i+1:]
for idx := 0; idx < len(n.Attr); idx++ {
if n.Attr[idx].Name.Space == space && n.Attr[idx].Name.Local == local {
removeIdx = idx
}
}
} else {
for idx := 0; idx < len(n.Attr); idx++ {
if n.Attr[idx].Name.Local == key {
removeIdx = idx
}
name := newXMLName(key)
for i, attr := range n.Attr {
if attr.Name == name {
n.Attr = append(n.Attr[:i], n.Attr[i+1:]...)
return
}
}
if removeIdx != -1 {
n.Attr = append(n.Attr[:removeIdx], n.Attr[removeIdx+1:]...)
}
}

// AddChild adds a new node 'n' to a node 'parent' as its last child.
Expand Down
9 changes: 2 additions & 7 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,9 @@ func (n *Node) SelectAttr(name string) string {
}
return ""
}
var local, space string
local = name
if i := strings.Index(name, ":"); i > 0 {
space = name[:i]
local = name[i+1:]
}
xmlName := newXMLName(name)
for _, attr := range n.Attr {
if attr.Name.Local == local && attr.Name.Space == space {
if attr.Name == xmlName {
return attr.Value
}
}
Expand Down

0 comments on commit 427ed60

Please sign in to comment.