From 3521c488ea4a5e9879d8a0cbc032505eff470ec3 Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Sun, 23 Jun 2019 15:52:20 +0200 Subject: [PATCH] Initial HTML message support --- msg_html.go | 20 ++++++++++++++++++++ msg_html_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 msg_html.go create mode 100644 msg_html_test.go diff --git a/msg_html.go b/msg_html.go new file mode 100644 index 0000000..fbb4aae --- /dev/null +++ b/msg_html.go @@ -0,0 +1,20 @@ +package xmpp + +import "encoding/xml" + +type HTML struct { + MsgExtension + XMLName xml.Name `xml:"http://jabber.org/protocol/xhtml-im html"` + Body HTMLBody + Lang string `xml:"xml:lang,attr,omitempty"` +} + +type HTMLBody struct { + XMLName xml.Name `xml:"http://www.w3.org/1999/xhtml body"` + // InnerXML MUST be valid xhtml. We do not check if it is valid when generating the XMPP stanza. + InnerXML string `xml:",innerxml"` +} + +func init() { + TypeRegistry.MapExtension(PKTMessage, xml.Name{"http://jabber.org/protocol/xhtml-im", "html"}, HTML{}) +} diff --git a/msg_html_test.go b/msg_html_test.go new file mode 100644 index 0000000..b88104a --- /dev/null +++ b/msg_html_test.go @@ -0,0 +1,44 @@ +package xmpp_test + +import ( + "encoding/xml" + "testing" + + "gosrc.io/xmpp" +) + +func TestHTMLGen(t *testing.T) { + htmlBody := "

Hello World

" + msg := xmpp.NewMessage(xmpp.Attrs{To: "test@localhost"}) + msg.Body = "Hello World" + body := xmpp.HTMLBody{ + InnerXML: htmlBody, + } + html := xmpp.HTML{Body: body} + msg.Extensions = append(msg.Extensions, html) + + result := msg.XMPPFormat() + str := `Hello World

Hello World

` + if result != str { + t.Errorf("incorrect serialize message:\n%s", result) + } + + parsedMessage := xmpp.Message{} + if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil { + t.Errorf("message HTML unmarshall error: %v", err) + return + } + + if parsedMessage.Body != msg.Body { + t.Errorf("incorrect parsed body: '%s'", parsedMessage.Body) + } + + var h xmpp.HTML + if ok := parsedMessage.Get(&h); !ok { + t.Error("could not extract HTML body") + } + + if h.Body.InnerXML != htmlBody { + t.Errorf("could not extract html body: '%s'", h.Body.InnerXML) + } +}