Skip to content

Commit

Permalink
Merge pull request #79 from jupenur/canonicalPrep
Browse files Browse the repository at this point in the history
Fix deletion of attributes in-place
  • Loading branch information
russellhaering authored Mar 2, 2022
2 parents 9a278d1 + ef8174b commit b317f5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
24 changes: 13 additions & 11 deletions canonicalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,21 @@ func canonicalPrep(el *etree.Element, seenSoFar map[string]struct{}, strip bool,

ne := el.Copy()
sort.Sort(etreeutils.SortedAttrs(ne.Attr))
if len(ne.Attr) != 0 {
for _, attr := range ne.Attr {
if attr.Space != nsSpace {
continue
}
key := attr.Space + ":" + attr.Key
if _, seen := _seenSoFar[key]; seen {
ne.RemoveAttr(attr.Space + ":" + attr.Key)
} else {
_seenSoFar[key] = struct{}{}
}
n := 0
for _, attr := range ne.Attr {
if attr.Space != nsSpace {
ne.Attr[n] = attr
n++
continue
}
key := attr.Space + ":" + attr.Key
if _, seen := _seenSoFar[key]; !seen {
ne.Attr[n] = attr
n++
_seenSoFar[key] = struct{}{}
}
}
ne.Attr = ne.Attr[:n]

if !comments {
c := 0
Expand Down
6 changes: 6 additions & 0 deletions canonicalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func TestXmldocC14N11(t *testing.T) {
runCanonicalizationTest(t, MakeC14N11Canonicalizer(), xmldoc, xmldocC14N11Canonicalized)
}

func TestNestedExcC14N11(t *testing.T) {
input := `<X xmlns:x="x" xmlns:y="y"><Y xmlns:x="x" xmlns:y="y" xmlns:z="z"/></X>`
expected := `<X xmlns:x="x" xmlns:y="y"><Y xmlns:z="z"></Y></X>`
runCanonicalizationTest(t, MakeC14N11Canonicalizer(), input, expected)
}

func TestExcC14nDefaultNamespace(t *testing.T) {
input := `<foo:Foo xmlns="urn:baz" xmlns:foo="urn:foo"><foo:Bar></foo:Bar></foo:Foo>`
expected := `<foo:Foo xmlns:foo="urn:foo"><foo:Bar></foo:Bar></foo:Foo>`
Expand Down

0 comments on commit b317f5f

Please sign in to comment.