7

I am working on an XMPP client and having an issue with messages being sent/received by Strophe (javascript version).

The issue is messages that contain "special" characters. For instance, if I send:

I'm here.

An external client (i.e. iChat) will display

I&ampapos;m here.

A strophe client doesn't display anything at all.

If I send that same message from iChat to the strophe client, it displays properly.

Here is the most basic sample code I could come up with:

<html>
<head>
  <script type='text/javascript' src='strophe.min.js'></script>
  <script type='text/javascript'>
    function onConnect(status) {
        if (status == Strophe.Status.CONNECTED) {
            var message = $msg({to: CONTACT_JID, from: JID, type: 'chat'}).c('body').t("I'm here."); ;
            connection.send(message.tree());
        }
    }
    var connection = new Strophe.Connection('http://bosh.metajack.im:5280/xmpp-httpbind');
    connection.connect(JID, PASS, onConnect);
  </script>
</head>
<body></body>
</html>

Thanks in advance for any help.

Edit:

Outbound, it seems Strophe is double encoding. When I type

I'm

it is sending

<body>I&amp;apos;m</body>

Inbound, it appears to not be handling CDATA properly. Any guidance or ideas are appreciated.

jopke
  • 1,186
  • 6
  • 18
  • Are you able to look at the data sent by js and check whether this conversion is done by the strophe.js or on the server side? – Cheery Feb 05 '12 at 03:27
  • I'm digging into that right now... will update question – jopke Feb 05 '12 at 03:32
  • One "idea": Remember that `'` isn't valid when the message goes from XML to HTML - it's only a valid entity in XML (as opposed to `'`) - which is likely why some HTML-based clients won't display it at all. – JimmiTh Feb 05 '12 at 03:41
  • @jopke it is doing XML escaping. `Function: xmlescape Excapes invalid xml characters.` Line 822 of non-minimized version of the js file. – Cheery Feb 05 '12 at 03:42
  • @Cheery - I'm not calling xmlexcape directly and seems to be double escaping it. I will dig into though, that might be a clue. – jopke Feb 05 '12 at 03:44
  • @jopke Add `alert(message.tree().textContent);` before `connection.send`. Is it double escaped? My example is not. And I following the flow of the code - do not see any double escaping. – Cheery Feb 05 '12 at 04:01
  • using jquery's .text() instead of Strophe.getText() has allowed me to display the strophe->strophe inbound stuff, but interacting with desktop clients is still problematic. I think i've just managed to mask the issue... – jopke Feb 05 '12 at 04:05
  • @Cheery - at that point it is not double escaped, I see I'm – jopke Feb 05 '12 at 04:07
  • @Cheery - so I think what might be happening is when I do .t(msg) Strophe is escaping it (' -> ') and then when it is sent, it is escaped again, because if I log Strophe's raw output, it is I&apos;m – jopke Feb 05 '12 at 04:09
  • @jopke I see what happens. It serializes the DOM structure to the String, making second escape. – Cheery Feb 05 '12 at 04:21
  • @Cheery I've been digging into it some but haven't figured out a way to avoid that 2nd escape. I'm open to creating/tweaking the message via jquery if necessary. – jopke Feb 05 '12 at 04:30
  • @jopke Try this way c('body', '', "I'm here."); – Cheery Feb 05 '12 at 04:34
  • @Cheery that didn't seem to do it, still getting the double escape when i log the raw outbound data – jopke Feb 05 '12 at 04:44
  • 1
    @jopke Yes, I see it. In general it supposes that the server should unserialize the string back, changing html entities. Is it your server? You can modify js file, preventing the second escaping, but I do not know how valid the XML structure could be in this case. Remove escaping on line 1163 of non-minimized js file. – Cheery Feb 05 '12 at 04:49
  • @Cheery The BOSH server I'm using right now is the test one at metajack.im, but I will eventually be connecting to my own. Removing that line seemed to do the trick, I think I'm happy with that for now. Thanks for all the help, feel free to write up a formal answer and I'll accept it. – jopke Feb 05 '12 at 05:11
  • @joke read my answer. as people are saying - it is better to remove the original escape of the text node instead of the serialization escape. – Cheery Feb 05 '12 at 05:12

1 Answers1

7

Ok, based on

https://github.com/metajack/strophejs/issues/54

https://github.com/metajack/strophejs/pull/59

you should remove escaping of the text node. Open non-minimized version of strophe.js file and comment line #846

//text = Strophe.xmlescape(text);
Ando
  • 11,199
  • 2
  • 30
  • 46
Cheery
  • 16,063
  • 42
  • 57