Skip to content

Fix issue where adjacent comments were skipped during canonicalization #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading