~samwhited/xmpp

6250a10b98385b558528f8f93b8bd5f0b3f5578f — Sam Whited 11 months ago 708e458
styling: use new xmlstream.Insert for Disable

This functionality will be generally useful throughout this library as
well as for others so I've split it out into the xmlstream module in a
generalized way. This patch uses the new xmlstream version for its
original use in the styling package.

Signed-off-by: Sam Whited <sam@samwhited.com>
4 files changed, 15 insertions(+), 47 deletions(-)

M go.mod
M go.sum
M styling/disable.go
M styling/disable_test.go
M go.mod => go.mod +1 -1
@@ 8,5 8,5 @@ require (
	golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7
	golang.org/x/text v0.3.2
	mellium.im/sasl v0.2.1
	mellium.im/xmlstream v0.15.1
	mellium.im/xmlstream v0.15.2-0.20201217125941-a994c65b8415
)

M go.sum => go.sum +2 -2
@@ 17,5 17,5 @@ mellium.im/reader v0.1.0 h1:UUEMev16gdvaxxZC7fC08j7IzuDKh310nB6BlwnxTww=
mellium.im/reader v0.1.0/go.mod h1:F+X5HXpkIfJ9EE1zHQG9lM/hO946iYAmU7xjg5dsQHI=
mellium.im/sasl v0.2.1 h1:nspKSRg7/SyO0cRGY71OkfHab8tf9kCts6a6oTDut0w=
mellium.im/sasl v0.2.1/go.mod h1:ROaEDLQNuf9vjKqE1SrAfnsobm2YKXT1gnN1uDp1PjQ=
mellium.im/xmlstream v0.15.1 h1:qqv+SUESVFPf3NZUGFldWY4KesQVpamnRVxwceK2PYA=
mellium.im/xmlstream v0.15.1/go.mod h1:7SUlP7f2qnMczK+Cu/OFgqaIhldMolVjo8np7xG41D0=
mellium.im/xmlstream v0.15.2-0.20201217125941-a994c65b8415 h1:MUDmwN48+7kNNTH8X02YES5p8WyQxD3DeEeUHXFuaS0=
mellium.im/xmlstream v0.15.2-0.20201217125941-a994c65b8415/go.mod h1:7SUlP7f2qnMczK+Cu/OFgqaIhldMolVjo8np7xG41D0=

M styling/disable.go => styling/disable.go +11 -43
@@ 6,7 6,6 @@ package styling

import (
	"encoding/xml"
	"io"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/ns"


@@ 17,10 16,10 @@ const (
	NS = "urn:xmpp:styling:0"
)

// Unstyled is a type that can be embedded in messages to add a hint that will
// Unstyled is a type that can be added to messages to add a hint that will
// disable styling.
// When unmarshaled its value indicates whether the unstyled hint was present in
// the message.
// When unmarshaled or marshaled its value indicates whether the unstyled hint
// was or will be present in the message.
type Unstyled struct {
	XMLName xml.Name `xml:"urn:xmpp:styling:0 unstyled"`
	Value   bool


@@ 54,44 53,13 @@ func (u *Unstyled) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	return d.Skip()
}

// Disable inserts a hint into any message read through r that disables styling
// for the body of the message.
func Disable(r xml.TokenReader) xml.TokenReader {
	var inner xml.TokenReader
	return xmlstream.ReaderFunc(func() (xml.Token, error) {
		if inner != nil {
			tok, err := inner.Token()
			switch {
			case tok != nil && err == io.EOF:
				inner = nil
				return tok, nil
				// We don't need this case here because the Wrap/Token calls in the
				// multireader are optimized for early EOF and the multireader respects
				// that.
				// case tok == nil && err == io.EOF:
				//	inner = nil
			default:
				return tok, err
			}
		}

		tok, err := r.Token()
		if err != nil {
			return tok, err
		}

		if end, ok := tok.(xml.EndElement); ok && end.Name.Local == "message" && (end.Name.Space == ns.Client || end.Name.Space == ns.Server) {
			inner = xmlstream.MultiReader(
				xmlstream.Wrap(nil,
					xml.StartElement{
						Name: xml.Name{Space: NS, Local: "unstyled"},
					},
				),
				xmlstream.Token(end),
			)
			return inner.Token()
		}
var (
	clientInserter = xmlstream.Insert(xml.Name{Space: ns.Client, Local: "message"}, Unstyled{Value: true})
	serverInserter = xmlstream.Insert(xml.Name{Space: ns.Server, Local: "message"}, Unstyled{Value: true})
)

		return tok, err
	})
// Disable is an xmlstream.Transformer that inserts a hint into any message read
// through r that disables styling for the body of the message.
func Disable(r xml.TokenReader) xml.TokenReader {
	return serverInserter(clientInserter(r))
}

M styling/disable_test.go => styling/disable_test.go +1 -1
@@ 48,7 48,7 @@ func TestDisable(t *testing.T) {
			r := styling.Disable(xml.NewDecoder(strings.NewReader(tc.in)))
			// Prevent duplicate xmlns attributes. See https://mellium.im/issue/75
			r = xmlstream.RemoveAttr(func(start xml.StartElement, attr xml.Attr) bool {
				return start.Name.Local == "message" && attr.Name.Local == "xmlns"
				return (start.Name.Local == "message" || start.Name.Local == "iq") && attr.Name.Local == "xmlns"
			})(r)
			var buf strings.Builder
			e := xml.NewEncoder(&buf)