From 7ab2418e48ae6765451d2caf8727b7c68f604bd9 Mon Sep 17 00:00:00 2001 From: sshikaree Date: Tue, 18 Jan 2022 17:40:58 +0300 Subject: [PATCH] fix #78 --- parse.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/parse.go b/parse.go index e3b7a14..d93b190 100644 --- a/parse.go +++ b/parse.go @@ -79,10 +79,34 @@ func createParser(r io.Reader) *parser { return p } +func (p *parser) getPrefix() (string, error) { + buf, err := p.reader.buffer.Peek(50) + if err != nil && (err != bufio.ErrBufferFull && err != io.EOF) { + return "", err + } + var start int = -1 + for i := range buf { + switch buf[i] { + case '<': + start = i + case ':': + return string(buf[start+1 : i]), nil + case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0, '>': + return "", nil + } + } + + return "", nil +} + func (p *parser) parse() (*Node, error) { var streamElementNodeCounter int for { + prefix, err := p.getPrefix() + if err != nil { + return nil, err + } tok, err := p.decoder.Token() if err != nil { return nil, err @@ -128,11 +152,15 @@ func (p *parser) parse() (*Node, error) { node := &Node{ Type: ElementNode, Data: tok.Name.Local, - Prefix: p.space2prefix[tok.Name.Space], NamespaceURI: tok.Name.Space, Attr: attributes, level: p.level, } + if prefix != "" { + node.Prefix = prefix + } else { + node.Prefix = p.space2prefix[tok.Name.Space] + } if p.level == p.prev.level { AddSibling(p.prev, node)