()
| 63 | } |
| 64 | |
| 65 | func (d *Decoder) Token() (Token, error) { |
| 66 | if d.err != nil { |
| 67 | return nil, d.err |
| 68 | } |
| 69 | |
| 70 | b, ok := d.peek(1) |
| 71 | if !ok { |
| 72 | return nil, d.err |
| 73 | } |
| 74 | |
| 75 | // If the next byte is not '<', this is plain text. |
| 76 | if b[0] != '<' { |
| 77 | text := d.readText() |
| 78 | if d.err != nil && !errors.Is(d.err, io.EOF) { |
| 79 | return nil, d.err |
| 80 | } |
| 81 | return &Text{Data: text, CData: false}, nil |
| 82 | } |
| 83 | |
| 84 | // Read the next 3 byte to determine the type of the tag. |
| 85 | // Any possible valid tag has at least 2 bytes after '<'. |
| 86 | b, ok = d.mustPeek(3) |
| 87 | if !ok { |
| 88 | return nil, d.err |
| 89 | } |
| 90 | |
| 91 | switch { |
| 92 | case bytes.HasPrefix(b, endTagStart): |
| 93 | // End element |
| 94 | name, nameOk := d.readEndTag() |
| 95 | if !nameOk { |
| 96 | return nil, d.err |
| 97 | } |
| 98 | return &EndElement{Name: name}, nil |
| 99 | |
| 100 | case bytes.HasPrefix(b, procInstStart): |
| 101 | // Processing instruction |
| 102 | target, data, procInstOk := d.readProcInst() |
| 103 | if !procInstOk { |
| 104 | return nil, d.err |
| 105 | } |
| 106 | return &ProcInst{ |
| 107 | Target: target, |
| 108 | Inst: data, |
| 109 | }, nil |
| 110 | |
| 111 | // Check only the first 3 bytes for comment, |
| 112 | // we will check the full sequence in readComment. |
| 113 | case bytes.HasPrefix(b, commentStart[:3]): |
| 114 | data, commentOk := d.readComment() |
| 115 | if !commentOk { |
| 116 | return nil, d.err |
| 117 | } |
| 118 | return &Comment{Data: data}, nil |
| 119 | |
| 120 | // Check only the first 3 bytes for CDATA, |
| 121 | // we will check the full sequence in readCData. |
| 122 | case bytes.HasPrefix(b, cdataStart[:3]): |
no test coverage detected