Skip to content

Commit

Permalink
Merge pull request #51 from laurenkt/issue-50/adjacent-comments-not-r…
Browse files Browse the repository at this point in the history
…emoved

Fix issue where adjacent comments were skipped during canonicalization
  • Loading branch information
adamdecaf authored Mar 20, 2024
2 parents 9757d76 + fa75a13 commit eff650e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions examples/canonicalization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ var example37SubsetExpression = `<!-- Evaluate with declaration xmlns:ietf="http

var example37Output = `<e1 xmlns="http://www.ietf.org" xmlns:w3c="http://www.w3.org"><e3 xmlns="" id="E3" xml:space="preserve"></e3></e1>`

var exampleGHIssue50Input = `<a><!-- comment0 --><!-- comment1 --><!-- comment2 --></a>`
var exampleGHIssue50Output = `<a></a>`

type exampleXML struct {
input string
output string
Expand All @@ -190,6 +193,7 @@ func TestCanonicalizationExamples(t *testing.T) {
// http://stackoverflow.com/questions/6002619/unmarshal-an-iso-8859-1-xml-input-in-go
// "(Example 3.6)": {input: example36Input, output: example36Output},
// "(Example 3.7)": {input: example37Input, output: example37Output, expression: example37SubsetExpression},
"(Example from GitHub Issue #50)": {input: exampleGHIssue50Input, output: exampleGHIssue50Output},
}
for description, test := range cases {
Convey(fmt.Sprintf("When transformed %s", description), func() {
Expand Down
5 changes: 4 additions & 1 deletion exclusivecanonicalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,17 @@ func (e ExclusiveCanonicalization) processRecursive(node *etree.Element,

newDefaultNS, newPrefixesInScope := e.renderAttributes(node, prefixesInScope, defaultNS)

for _, child := range node.Child {
for i := 0; i < len(node.Child); i++ {
child := node.Child[i]

oldNamespaces := e.namespaces
e.namespaces = copyNamespace(oldNamespaces)

switch child := child.(type) {
case *etree.Comment:
if !e.WithComments {
removeTokenFromElement(etree.Token(child), node)
i--
}
case *etree.Element:
e.processRecursive(child, newPrefixesInScope, newDefaultNS)
Expand Down

0 comments on commit eff650e

Please sign in to comment.