Skip to content

Commit

Permalink
encoding/xml: gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Sep 27, 2021
1 parent aba6f79 commit b113c04
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/encoding/xml/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,12 @@ func (p *printer) popPrefix() {
p.deleteAttrPrefix(prefix)
}
}

// No prefix is returned if the first prefix of the list for this tag is not assigned to a namespace
func (p *printer) tagPrefix() string {
prefix := p.prefixes[len(p.prefixes)-1] // last prefix relates to current tag
i := len(p.prefixes) - 1
for i >= 0 && i < len(p.prefixes){
for i >= 0 && i < len(p.prefixes) {
if prefix == "" { // end of list of prefixes for this tag is reached
// check that previous prefix is the one of the tag
if i+1 < len(p.prefixes) { // list is not empty
Expand Down Expand Up @@ -763,7 +764,7 @@ func (p *printer) writeStart(start *StartElement) error {
}
}
if prefixToPrint == "" { // no prefix was found for the space of the tag name
// the tag name Space is then considered as the default xmlns=".Space"
// the tag name Space is then considered as the default xmlns=".Space"
for _, attr := range start.Attr {
// attributes values which are namespaces are searched to avoid reprinting the default
if start.Name.Space == attr.Value && attr.Name.Space == "" && attr.Name.Local == xmlnsPrefix {
Expand All @@ -779,7 +780,7 @@ func (p *printer) writeStart(start *StartElement) error {
p.WriteString(start.Name.Local) // tag name
} else {
p.WriteString(start.Name.Local) // no prefix found
p.WriteString(` xmlns="`) // printing the namespace taken as default without prefix
p.WriteString(` xmlns="`) // printing the namespace taken as default without prefix
p.EscapeString(start.Name.Space)
p.WriteByte('"')
}
Expand All @@ -798,7 +799,7 @@ func (p *printer) writeStart(start *StartElement) error {
p.WriteByte(':')
} else if attr.Name.Space != "" { // not a name space {.Space}:{.Local}={.Value} and .Local is not xmlns
p.WriteString(p.createAttrPrefix(attr.Name.Space)) // name.Space is not a prefix but nothing should be done
p.WriteByte(':') // about it if another one exists
p.WriteByte(':') // about it if another one exists
}
// When space is empty, only writing .Local=.Value which will also be xmlns=".Value"
p.WriteString(attr.Name.Local)
Expand Down
16 changes: 8 additions & 8 deletions src/encoding/xml/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,7 @@ var encodeTokenTests = []struct {
{Name{"xmlns", "x"}, "space"},
{Name{"space", "foo"}, "value"},
}},
},// #16 <x:local xmlns:x="space" space:foo="value">
}, // #16 <x:local xmlns:x="space" space:foo="value">
want: `<x:local xmlns:x="space" xmlns:space="space" space:foo="value">`,
}, {
desc: "start element with explicit namespace and colliding prefix",
Expand Down Expand Up @@ -2136,7 +2136,7 @@ var encodeTokenTests = []struct {
{Name{"xmlns", "b"}, "space"}, // xmlns:b="space"
{Name{"space", "x"}, "value"}, // space:x="value"
}},
},// #20
}, // #20
want: `<a:foo xmlns:a="space" xmlns:b="space" xmlns:space="space" space:x="value">`,
}, {
desc: "nested element redefines name space",
Expand Down Expand Up @@ -2172,7 +2172,7 @@ var encodeTokenTests = []struct {
{Name{"", "xmlns"}, "space"}, // xlmns="space"
{Name{"space", "a"}, "value"}, // space:a="value"
}},
},// #23 Keeps its oddity
}, // #23 Keeps its oddity
want: `<foo xmlns:x="space"><foo xmlns="space" xmlns:space="space" space:a="value">`,
}, {
desc: "nested element uses empty attribute name space when default ns defined",
Expand All @@ -2183,7 +2183,7 @@ var encodeTokenTests = []struct {
StartElement{Name{"space", "foo"}, []Attr{
{Name{"", "attr"}, "value"},
}},
},// #24 Purpose is to produce valid xml
}, // #24 Purpose is to produce valid xml
want: `<foo xmlns="space"><foo xmlns="space" attr="value">`,
}, {
desc: "redefine xmlns",
Expand Down Expand Up @@ -2240,8 +2240,8 @@ var encodeTokenTests = []struct {
{Name{"", "xmlns"}, "space"},
}},
StartElement{Name{"", "foo"}, []Attr{
{Name{"", "xmlns"}, ""}, // xmlns=""
{Name{"", "x"}, "value"}, // x="value"
{Name{"", "xmlns"}, ""}, // xmlns=""
{Name{"", "x"}, "value"}, // x="value"
{Name{"space", "x"}, "value"}, // space:x="value"
}},
}, // #31 xmlns:space="space" oddity still appears
Expand Down Expand Up @@ -2281,8 +2281,8 @@ var encodeTokenTests = []struct {
desc: "default name space not used by attributes, not explicitly defined",
toks: []Token{
StartElement{Name{"space", "foo"}, []Attr{
{Name{"", "xmlns"}, "space"}, // xmlns="space"
{Name{"space", "baz"}, "foo"}, // space:baz="foo"
{Name{"", "xmlns"}, "space"}, // xmlns="space"
{Name{"space", "baz"}, "foo"}, // space:baz="foo"
}},
StartElement{Name{"space", "baz"}, nil},
EndElement{Name{"space", "baz"}},
Expand Down

0 comments on commit b113c04

Please sign in to comment.