diff --git a/parse.go b/parse.go index 32dfedc..853ea01 100644 --- a/parse.go +++ b/parse.go @@ -169,7 +169,11 @@ func (p *parser) parse() (*Node, error) { if p.streamElementFilter == nil || QuerySelector(p.doc, p.streamElementFilter) != nil { return p.streamNode, nil } - // otherwise, this isn't our target node. clean things up. + // otherwise, this isn't our target node, clean things up. + // note we also remove the underlying *Node from the node tree, to prevent + // future stream node candidate selection error. + RemoveFromTree(p.streamNode) + p.prev = p.streamNodePrev p.streamNode = nil p.streamNodePrev = nil } diff --git a/parse_test.go b/parse_test.go index 6099741..903ee36 100644 --- a/parse_test.go +++ b/parse_test.go @@ -278,18 +278,22 @@ func testOutputXML(t *testing.T, msg string, expectedXML string, n *Node) { func TestStreamParser_Success1(t *testing.T) { s := ` - - c1 - b1 - d1 - b2z1 - b3 - b4 - b5 - c3 - ` - - sp, err := CreateStreamParser(strings.NewReader(s), "/AAA/BBB", "/AAA/BBB[. != 'b3']") + + + c1 + b1 + d1 + b2z1 + b3 + + + b4 + b5 + c3 + + ` + + sp, err := CreateStreamParser(strings.NewReader(s), "/ROOT/*/BBB", "/ROOT/*/BBB[. != 'b3']") if err != nil { t.Fatal(err.Error()) } @@ -300,7 +304,8 @@ func TestStreamParser_Success1(t *testing.T) { t.Fatal(err.Error()) } testOutputXML(t, "first call result", `b1`, n) - testOutputXML(t, "doc after first call", `<>c1b1`, findRoot(n)) + testOutputXML(t, "doc after first call", + `<>c1b1`, findRoot(n)) // Second `` read n, err = sp.Read() @@ -309,7 +314,7 @@ func TestStreamParser_Success1(t *testing.T) { } testOutputXML(t, "second call result", `b2z1`, n) testOutputXML(t, "doc after second call", - `<>c1d1b2z1`, findRoot(n)) + `<>c1d1b2z1`, findRoot(n)) // Third `` read (Note we will skip 'b3' since the streamElementFilter excludes it) n, err = sp.Read() @@ -321,7 +326,8 @@ 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", - `<>c1d1b3b4`, findRoot(n)) + `<>c1d1b4`, + findRoot(n)) // Fourth `` read n, err = sp.Read() @@ -329,9 +335,9 @@ func TestStreamParser_Success1(t *testing.T) { t.Fatal(err.Error()) } testOutputXML(t, "fourth call result", `b5`, n) - // Note the inclusion of `b3` in the document. testOutputXML(t, "doc after fourth call", - `<>c1d1b3b5`, findRoot(n)) + `<>c1d1b5`, + findRoot(n)) _, err = sp.Read() if err != io.EOF {