From cd0be2174222c0e5e3ee08fc1ac60db7df346c9a Mon Sep 17 00:00:00 2001 From: leavesster <11785335+leavesster@users.noreply.github.com> Date: Mon, 25 Oct 2021 22:04:02 +0800 Subject: [PATCH 1/2] fix: when node is root, not output unnecessary string --- node.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node.go b/node.go index be6e31a..cc8be4b 100644 --- a/node.go +++ b/node.go @@ -142,7 +142,7 @@ func outputXML(buf *bytes.Buffer, n *Node, preserveSpaces bool) { func (n *Node) OutputXML(self bool) string { preserveSpaces := calculatePreserveSpaces(n, false) var buf bytes.Buffer - if self { + if self && n.Type != DocumentNode { outputXML(&buf, n, preserveSpaces) } else { for n := n.FirstChild; n != nil; n = n.NextSibling { From eca2fa02d3647d14430f80c7ed89b5f3fe158fcd Mon Sep 17 00:00:00 2001 From: zhengchun Date: Tue, 26 Oct 2021 00:34:42 +0800 Subject: [PATCH 2/2] fix #72 test cases --- parse_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/parse_test.go b/parse_test.go index 8982f29..fc90984 100644 --- a/parse_test.go +++ b/parse_test.go @@ -370,7 +370,7 @@ func TestStreamParser_Success1(t *testing.T) { } testOutputXML(t, "first call result", `b1`, n) testOutputXML(t, "doc after first call", - `<>c1b1`, findRoot(n)) + `c1b1`, findRoot(n)) // Second `` read n, err = sp.Read() @@ -379,7 +379,7 @@ func TestStreamParser_Success1(t *testing.T) { } testOutputXML(t, "second call result", `b2z1`, n) testOutputXML(t, "doc after second call", - `<>d1b2z1`, findRoot(n)) + `d1b2z1`, findRoot(n)) // Third `` read (Note we will skip 'b3' since the streamElementFilter excludes it) n, err = sp.Read() @@ -391,7 +391,7 @@ func TestStreamParser_Success1(t *testing.T) { // been filtered out and is not our target node, thus it is considered just like any other // non target nodes such as ``` or `` testOutputXML(t, "doc after third call", - `<>b4`, + `b4`, findRoot(n)) // Fourth `` read @@ -401,7 +401,7 @@ func TestStreamParser_Success1(t *testing.T) { } testOutputXML(t, "fourth call result", `b5`, n) testOutputXML(t, "doc after fourth call", - `<>b5`, + `b5`, findRoot(n)) _, err = sp.Read() @@ -431,7 +431,7 @@ func TestStreamParser_Success2(t *testing.T) { t.Fatal(err.Error()) } testOutputXML(t, "first call result", `c1`, n) - testOutputXML(t, "doc after first call", `<>c1`, findRoot(n)) + testOutputXML(t, "doc after first call", `c1`, findRoot(n)) // Second Read() should return d1 n, err = sp.Read() @@ -440,7 +440,7 @@ func TestStreamParser_Success2(t *testing.T) { } testOutputXML(t, "second call result", `d1`, n) testOutputXML(t, "doc after second call", - `<>b1d1`, findRoot(n)) + `b1d1`, findRoot(n)) // Third call should return c2 n, err = sp.Read() @@ -449,7 +449,7 @@ func TestStreamParser_Success2(t *testing.T) { } testOutputXML(t, "third call result", `c2`, n) testOutputXML(t, "doc after third call", - `<>b2c2`, findRoot(n)) + `b2c2`, findRoot(n)) _, err = sp.Read() if err != io.EOF {