From 427ed6078260d610e616d319888639ca0c83dcc4 Mon Sep 17 00:00:00 2001 From: egginabucket Date: Tue, 7 Mar 2023 11:08:04 -0800 Subject: [PATCH] add func newXMLName for attrs --- node.go | 74 ++++++++++++++++++++------------------------------------ query.go | 9 ++----- 2 files changed, 28 insertions(+), 55 deletions(-) diff --git a/node.go b/node.go index 2f77457..fb4912d 100644 --- a/node.go +++ b/node.go @@ -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) @@ -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. diff --git a/query.go b/query.go index 0bd45dd..9f2493f 100644 --- a/query.go +++ b/query.go @@ -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 } }