~samwhited/xmpp

ref: d4b835bf2b08a22e6a13cd9b33e62c88f9854677 xmpp/internal/decl/decl.go -rw-r--r-- 994 bytes
d4b835bfSam Whited internal/marshal: prevent duplicate xmlns attr 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2019 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

// Package decl contains functionality related to XML declarations.
package decl // import "mellium.im/xmpp/internal/decl"

import (
	"encoding/xml"
)

const (
	// XMLHeader is an XML header like the one in encoding/xml but without a
	// newline at the end.
	XMLHeader = `<?xml version="1.0" encoding="UTF-8"?>`
)

type skipper struct {
	r       xml.TokenReader
	started bool
}

// Token implements xml.TokenReader for Reader.
func (r *skipper) Token() (xml.Token, error) {
	tok, err := r.r.Token()
	if tok != nil && !r.started {
		r.started = true
		if proc, ok := tok.(xml.ProcInst); ok && proc.Target == "xml" {
			if err != nil {
				return nil, err
			}
			return r.r.Token()
		}
	}
	return tok, err
}

// Skip wraps a token reader and skips any XML declaration.
func Skip(r xml.TokenReader) xml.TokenReader {
	return &skipper{r: r}
}