Skip to content

Commit

Permalink
Add NamespaceModifier.PreserveOriginalPrefixes
Browse files Browse the repository at this point in the history
to preserve original namespace binding prefixes
and not introduce potentially shorter aliases.
Duplicate prefixes (prefixes binding to the same
namespace) will still be cleaned-up.
  • Loading branch information
kaiburjack committed Feb 10, 2022
1 parent 20d915c commit e4fe85f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail_on_error: true
- run: |
gofmt -l . && test -z $(gofmt -l .)
go vet -unsafeptr=false .
go vet .
go install github.com/mgechev/revive@latest
revive -config revive.toml .
- run: go build -v .
Expand Down
30 changes: 30 additions & 0 deletions combined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,36 @@ soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
"</a:Envelope>", w.String())
}

func TestAttributesWithPrefixesPreserve(t *testing.T) {
// given
input := `
<ns1:a xmlns:ns1="http://ns1" ns1:attr1="val1" ns2:attr2="val2" xmlns:ns2="http://ns2">
<ns1:b>
<b:c xmlns:b="http://ns2" ns2:attr3="val3">
<b:d ns1:attr4="val4">Test</b:d>
</b:c>
</ns1:b>
</ns1:a>`
dec := gosaxml.NewDecoder(strings.NewReader(input))
w := &bytes.Buffer{}
namespaceModifier := gosaxml.NewNamespaceModifier()
namespaceModifier.PreserveOriginalPrefixes = true
enc := gosaxml.NewEncoder(w, namespaceModifier)
var tk gosaxml.Token

// when
decodeEncode(t, dec, enc, &tk)

// then
assert.Equal(t, "<ns1:a xmlns:ns1=\"http://ns1\" ns1:attr1=\"val1\" ns2:attr2=\"val2\" xmlns:ns2=\"http://ns2\">"+
"<ns1:b>"+
"<ns2:c ns2:attr3=\"val3\">"+
"<ns2:d ns1:attr4=\"val4\">Test</ns2:d>"+
"</ns2:c>"+
"</ns1:b>"+
"</ns1:a>", w.String())
}

func TestAttributesWithPrefixes(t *testing.T) {
// given
input := `
Expand Down
20 changes: 13 additions & 7 deletions namespaceModifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type NamespaceModifier struct {
prefixAliases [][]byte

top byte

PreserveOriginalPrefixes bool
}

// NewNamespaceModifier creates a new NamespaceModifier and returns a pointer to it.
Expand Down Expand Up @@ -148,13 +150,17 @@ func (thiz *NamespaceModifier) processNamespaces(t *Token) {
// we don't need the attribute anymore because we already had a prefix
continue
}
// wo don't know the prefix, but we want to rewrite it
nextPrefixAlias := len(thiz.prefixAliases) / 2
c := namespaceAliases[nextPrefixAlias : nextPrefixAlias+1]
bsc := bs(c)
thiz.addPrefixRewrite(attr.Name.Local, bsc)
thiz.addNamespaceBinding(bsc, attr.Value)
attr.Name.Local = bsc
if !thiz.PreserveOriginalPrefixes {
// wo don't know the prefix, but we want to rewrite it
nextPrefixAlias := len(thiz.prefixAliases) / 2
c := namespaceAliases[nextPrefixAlias : nextPrefixAlias+1]
bsc := bs(c)
thiz.addPrefixRewrite(attr.Name.Local, bsc)
thiz.addNamespaceBinding(bsc, attr.Value)
attr.Name.Local = bsc
} else {
thiz.addNamespaceBinding(attr.Name.Local, attr.Value)
}
} else if attr.Name.Prefix == nil && bytes.Equal(attr.Name.Local, bs("xmlns")) {
// check if the element is already in that namespace, in which case
// we can simply omit the namespace.
Expand Down

0 comments on commit e4fe85f

Please sign in to comment.