Skip to content

Commit 0d38243

Browse files
committed
属性支持顺序性输出不再是因map导致乱序
1 parent 5eaed9d commit 0d38243

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

tinydom.go

+27-41
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"io"
99
"unicode/utf8"
10+
"container/list"
1011
)
1112

1213
// XMLAttribute 是一个元素的属性的接口.
@@ -513,7 +514,8 @@ type xmlElementImpl struct {
513514
xmlNodeImpl
514515

515516
// rootAttribute XMLAttribute
516-
attributes map[string]XMLAttribute
517+
attrlist *list.List
518+
attrsmap map[string]*list.Element
517519
}
518520

519521
func (e *xmlElementImpl) ToElement() XMLElement {
@@ -542,63 +544,49 @@ func (e *xmlElementImpl) SetName(name string) {
542544
}
543545

544546
func (e *xmlElementImpl) FindAttribute(name string) XMLAttribute {
545-
if nil == e.attributes {
546-
return nil
547-
}
548-
549-
attr, ok := e.attributes[name]
547+
elem, ok := e.attrsmap[name]
550548
if !ok {
551549
return nil
552550
}
553551

554-
return attr
552+
return elem.Value.(*xmlAttributeImpl)
555553
}
556554

557555
func (e *xmlElementImpl) AttributeCount() int {
558-
if nil == e.attributes {
559-
return 0
560-
}
561-
return len(e.attributes)
556+
return len(e.attrsmap)
562557
}
563558

564559
func (e *xmlElementImpl) Attribute(name string, def string) string {
565-
if nil == e.attributes {
566-
return def
567-
}
568-
569-
attr, ok := e.attributes[name]
560+
attr, ok := e.attrsmap[name]
570561
if !ok {
571562
return def
572563
}
573564

574-
return attr.Value()
565+
return attr.Value.(*xmlAttributeImpl).Value()
575566
}
576567

577568
func (e *xmlElementImpl) SetAttribute(name string, value string) XMLAttribute {
578-
if nil == e.attributes {
579-
e.attributes = make(map[string]XMLAttribute)
580-
attr := newAttribute(name, value)
581-
e.attributes[name] = attr
582-
return attr
583-
}
584-
585-
attr, ok := e.attributes[name]
569+
elem, ok := e.attrsmap[name]
586570
if ok {
587-
attr.SetValue(value)
588-
return attr
571+
elem.Value.(*xmlAttributeImpl).SetValue(value)
572+
return elem.Value.(*xmlAttributeImpl)
589573
}
590574

591-
attr = newAttribute(name, value)
592-
e.attributes[name] = attr
575+
attr := newAttribute(name, value)
576+
e.attrsmap[name] = e.attrlist.PushBack(attr)
593577
return attr
594578
}
595579

596580
func (e *xmlElementImpl) DeleteAttribute(name string) XMLAttribute {
597-
attr := e.FindAttribute(name)
598-
if nil == attr {
581+
elem, ok := e.attrsmap[name]
582+
if !ok {
599583
return nil
600584
}
601-
delete(e.attributes, name)
585+
586+
attr := elem.Value.(*xmlAttributeImpl)
587+
588+
e.attrlist.Remove(elem)
589+
delete(e.attrsmap, name)
602590
return attr
603591
}
604592

@@ -620,12 +608,8 @@ func (e *xmlElementImpl) SetText(inText string) {
620608
}
621609

622610
func (e *xmlElementImpl) ForeachAttribute(callback func(attribute XMLAttribute) int) int {
623-
if nil == e.attributes {
624-
return 0
625-
}
626-
627-
for _, value := range e.attributes {
628-
if ret := callback(value); 0 != ret {
611+
for elem := e.attrlist.Front(); nil != elem; elem = elem.Next() {
612+
if ret := callback(elem.Value.(*xmlAttributeImpl)); 0 != ret {
629613
return ret
630614
}
631615
}
@@ -634,7 +618,8 @@ func (e *xmlElementImpl) ForeachAttribute(callback func(attribute XMLAttribute)
634618
}
635619

636620
func (e *xmlElementImpl) ClearAttributes() {
637-
e.attributes = nil
621+
e.attrlist = list.New()
622+
e.attrsmap = make(map[string]*list.Element)
638623
}
639624

640625
// ------------------------------------------------------------------
@@ -762,7 +747,8 @@ func NewElement(name string) XMLElement {
762747
node := new(xmlElementImpl)
763748
node.implobj = node
764749
node.value = name
765-
node.attributes = make(map[string]XMLAttribute)
750+
node.attrsmap = make(map[string]*list.Element)
751+
node.attrlist = list.New()
766752
return node
767753
}
768754

@@ -785,7 +771,7 @@ func NewDirective(directive string) XMLDirective {
785771

786772
// newAttribute 创建一个新的XMLAttribute对象.
787773
// name和value分别用于指定属性的名称和值
788-
func newAttribute(name string, value string) XMLAttribute {
774+
func newAttribute(name string, value string) *xmlAttributeImpl {
789775
attr := new(xmlAttributeImpl)
790776
attr.name = name
791777
attr.value = value

tinydom_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -868,3 +868,19 @@ func Test_inhert(t *testing.T) {
868868
Call(c)
869869
//Call(d)
870870
}
871+
872+
func Test_Attr_Order(t *testing.T) {
873+
s := `<node attr5="55"/>`
874+
doc, _ := LoadDocument(bytes.NewBufferString(s))
875+
node := doc.FirstChildElement("node")
876+
node.SetAttribute("attr2", "22")
877+
node.SetAttribute("attr3", "33")
878+
node.SetAttribute("attr4", "44")
879+
node.SetAttribute("attr6", "66")
880+
node.SetAttribute("attr9", "99")
881+
node.SetAttribute("attr", "")
882+
buf := bytes.NewBufferString("")
883+
doc.Accept(NewSimplePrinter(buf, PrintStream))
884+
expect(t, "属性的顺序就是添加的顺序,不会应为key的不断变化而导致属性输出时,属性间的相对位置发生不断变化",
885+
buf.String() == `<node attr5="55" attr2="22" attr3="33" attr4="44" attr6="66" attr9="99" attr=""/>`)
886+
}

0 commit comments

Comments
 (0)