diff --git a/pkg/xixo/driver_test.go b/pkg/xixo/driver_test.go index 8799aea..da584a2 100644 --- a/pkg/xixo/driver_test.go +++ b/pkg/xixo/driver_test.go @@ -59,7 +59,7 @@ func TestFuncDriverEditEmptyElement(t *testing.T) { assert.True(t, called) - expected := "" + expected := "" assert.Equal(t, expected, writer.String()) } diff --git a/pkg/xixo/element.go b/pkg/xixo/element.go index d01da1b..973f639 100644 --- a/pkg/xixo/element.go +++ b/pkg/xixo/element.go @@ -5,15 +5,6 @@ import ( "strings" ) -type CommentElement struct { - OuterTextBefore string - Comment string -} - -func (c CommentElement) String() string { - return fmt.Sprintf("%s", c.OuterTextBefore, c.Comment) -} - type XMLElement struct { Name string Attrs map[string]string @@ -30,7 +21,6 @@ type XMLElement struct { prefix string outerTextBefore string - comments []CommentElement autoClosable bool } @@ -115,21 +105,15 @@ func (n *XMLElement) String() string { attributes = strings.Trim(attributes, " ") - commentsString := "" - for _, comment := range n.comments { - commentsString += comment.String() - } - - if n.autoClosable && n.InnerText == "" && commentsString == "" && xmlChilds == "" { + if n.autoClosable && n.InnerText == "" && xmlChilds == "" { return fmt.Sprintf("%s<%s/>", n.outerTextBefore, attributes) } - return fmt.Sprintf("%s<%s>%s%s%s", + return fmt.Sprintf("%s<%s>%s%s", n.outerTextBefore, attributes, - commentsString, xmlChilds, n.InnerText, n.Name) @@ -148,10 +132,6 @@ func (n *XMLElement) AddAttribute(name string, value string) { n.Attrs[name] = value } -func (n *XMLElement) AddComment(comment CommentElement) { - n.comments = append(n.comments, comment) -} - func NewXMLElement() *XMLElement { return &XMLElement{ Name: "", diff --git a/pkg/xixo/parser.go b/pkg/xixo/parser.go index 1d17d70..fb65992 100644 --- a/pkg/xixo/parser.go +++ b/pkg/xixo/parser.go @@ -147,7 +147,7 @@ func (x *XMLParser) parse() error { continue } - iscomment, _, err = x.readComment() + iscomment, err = x.readComment() if err != nil { return err @@ -184,6 +184,8 @@ func (x *XMLParser) parse() error { x.resultChannel <- element if callback, ok := x.loopElements[element.Name]; ok { + element.outerTextBefore = "" + mutatedElement, err := callback(element) if err != nil { return err @@ -231,10 +233,9 @@ func (x *XMLParser) getElementTree(result *XMLElement) *XMLElement { element *XMLElement tagClosed bool iscomment bool - comment CommentElement ) - result.outerTextBefore = string(x.scratchOuterText.bytes()) + result.outerTextBefore = string(x.scratch2.bytes()) x.scratchOuterText.reset() x.scratch2.reset() // this hold the inner text @@ -263,7 +264,7 @@ func (x *XMLParser) getElementTree(result *XMLElement) *XMLElement { continue } - iscomment, comment, err = x.readComment() + iscomment, err = x.readComment() if err != nil { result.Err = err @@ -272,8 +273,6 @@ func (x *XMLParser) getElementTree(result *XMLElement) *XMLElement { } if iscomment { - result.AddComment(comment) - continue } @@ -439,7 +438,7 @@ func (x *XMLParser) startElement() (*XMLElement, bool, error) { if prev == '/' { result.Name = string(x.scratch.bytes()[:len(x.scratch.bytes())-1]) result.autoClosable = true - result.outerTextBefore = string(x.scratchOuterText.bytes()) + result.outerTextBefore = string(x.scratch2.bytes()) x.scratchOuterText.reset() x.scratch2.reset() @@ -511,6 +510,8 @@ search_close_tag: if cur == '>' { // if tag name not found if prev == '/' { // tag special close + result.autoClosable = true + return result, true, nil } @@ -522,26 +523,25 @@ search_close_tag: } } -func (x *XMLParser) readComment() (bool, CommentElement, error) { +func (x *XMLParser) readComment() (bool, error) { var ( - c byte - err error - result CommentElement + c byte + err error ) c, err = x.readByte() if err != nil { - return false, result, err + return false, err } if c != '!' { err := x.unreadByte() if err != nil { - return false, result, err + return false, err } - return false, result, nil + return false, nil } var d, e byte @@ -549,19 +549,19 @@ func (x *XMLParser) readComment() (bool, CommentElement, error) { d, err = x.readByte() if err != nil { - return false, result, err + return false, err } e, err = x.readByte() if err != nil { - return false, result, err + return false, err } if d != '-' || e != '-' { err = x.defaultError() - return false, result, err + return false, err } // skip part @@ -571,23 +571,29 @@ func (x *XMLParser) readComment() (bool, CommentElement, error) { c, err = x.readByte() if err != nil { - return false, result, err + return false, err } if c == '>' && len(x.scratch.bytes()) > 1 && x.scratch.bytes()[len(x.scratch.bytes())-1] == '-' && x.scratch.bytes()[len(x.scratch.bytes())-2] == '-' { - result = CommentElement{ - string(x.scratchOuterText.bytes()), - string(x.scratch.bytes())[:len(x.scratch.bytes())-2], + x.scratch2.add('<') + x.scratch2.add('!') + x.scratch2.add('-') + x.scratch2.add('-') + + for _, c := range x.scratch.bytes() { + x.scratch2.add(c) } + x.scratch2.add('>') + x.scratchOuterText.reset() - x.scratch2.reset() + // x.scratch2.reset() x.scratch.reset() - return true, result, nil + return true, nil } x.scratch.add(c) diff --git a/pkg/xixo/parser_test.go b/pkg/xixo/parser_test.go index a77dd32..5475d96 100644 --- a/pkg/xixo/parser_test.go +++ b/pkg/xixo/parser_test.go @@ -244,10 +244,11 @@ func TestStreamWithoutModifications(t *testing.T) { {input: " ", element: "a"}, {input: "", element: "b"}, + {input: "", element: "a"}, {input: " ", element: "b"}, {input: " ", element: "a"}, {input: " ", element: "b"}, - // {input: " ", element: "a"}, + {input: " ", element: "a"}, {input: "i", element: "a"}, {input: "", element: "a"}, @@ -258,8 +259,9 @@ func TestStreamWithoutModifications(t *testing.T) { {input: "ijk", element: "a"}, {input: "ijk", element: "a"}, - // {input: "", element: "b"}, - {input: "", element: "b"}, + {input: "", element: "b"}, + {input: "", element: "b"}, + {input: "", element: "a"}, } for _, testCase := range tests {