Skip to content

More compact XML escaping #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,6 @@ Strophe = {
text = text.replace(/\&/g, "&");
text = text.replace(/</g, "&lt;");
text = text.replace(/>/g, "&gt;");
text = text.replace(/'/g, "&apos;");
text = text.replace(/"/g, "&quot;");
return text;
},

Expand Down Expand Up @@ -948,12 +946,16 @@ Strophe = {
result = "<" + nodeName;
for (i = 0; i < elem.attributes.length; i++) {
if(elem.attributes[i].nodeName != "_realname") {
result += " " + elem.attributes[i].nodeName.toLowerCase() +
"='" + elem.attributes[i].value
.replace(/&/g, "&amp;")
.replace(/\'/g, "&apos;")
.replace(/>/g, "&gt;")
.replace(/</g, "&lt;") + "'";
result += " " + elem.attributes[i].nodeName.toLowerCase() + "=";
var escaped = elem.attributes[i].value
.replace(/\&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
if (escaped.indexOf('"') > -1) {
result += "'" + escaped.replace(/\'/g, "&apos;") + "'";
} else {
result += '"' + escaped.replace(/\"/g, "&quot;") + '"';
}
}
}

Expand Down
16 changes: 8 additions & 8 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ $(document).ready(function () {

test("Builder with XML attribute escaping test", function () {
var text = "<b>";
var expected = "<presence to='&lt;b&gt;' xmlns='jabber:client'/>";
var expected = '<presence to="&lt;b&gt;" xmlns="jabber:client"/>';
var pres = $pres({to: text});
equal(pres.toString(), expected, "< should be escaped");

text = "foo&bar";
expected = "<presence to='foo&amp;bar' xmlns='jabber:client'/>";
expected = '<presence to="foo&amp;bar" xmlns="jabber:client"/>';
pres = $pres({to: text});
equal(pres.toString(), expected, "& should be escaped");
});

test("c() accepts text and passes it to xmlElement", function () {
var pres = $pres({from: "[email protected]", to: "[email protected]"})
.c("nick", {xmlns: "http://jabber.org/protocol/nick"}, "Darcy");
var expected = "<presence from='[email protected]' to='[email protected]' xmlns='jabber:client'><nick xmlns='http://jabber.org/protocol/nick'>Darcy</nick></presence>";
var expected = '<presence from="[email protected]" to="[email protected]" xmlns="jabber:client"><nick xmlns="http://jabber.org/protocol/nick">Darcy</nick></presence>';
equal(pres.toString(), expected, "'Darcy' should be a child of <presence>");
});

Expand All @@ -125,7 +125,7 @@ $(document).ready(function () {
equal(Strophe.getText(textNode0), "s &lt; &amp; &gt; p", "should be escaped");
var text1 = "s's or \"p\"";
var textNode1 = Strophe.xmlTextNode(text1);
equal(Strophe.getText(textNode1), "s&apos;s or &quot;p&quot;", "should be escaped");
equal(Strophe.getText(textNode1), "s's or \"p\"", "should be unchanged");
var text2 = "<![CDATA[<foo>]]>";
var textNode2 = Strophe.xmlTextNode(text2);
equal(Strophe.getText(textNode2), "&lt;![CDATA[&lt;foo&gt;]]&gt;", "should be escaped");
Expand All @@ -152,15 +152,15 @@ $(document).ready(function () {
var parser = new DOMParser();
// Attributes
var element1 = parser.parseFromString("<foo attr1='abc' attr2='edf'>bar</foo>","text/xml").documentElement;
equal(Strophe.serialize(element1), "<foo attr1='abc' attr2='edf'>bar</foo>", "should be serialized");
equal(Strophe.serialize(element1), '<foo attr1="abc" attr2="edf">bar</foo>', "should be serialized");
var element2 = parser.parseFromString("<foo attr1=\"abc\" attr2=\"edf\">bar</foo>","text/xml").documentElement;
equal(Strophe.serialize(element2), "<foo attr1='abc' attr2='edf'>bar</foo>", "should be serialized");
equal(Strophe.serialize(element2), '<foo attr1="abc" attr2="edf">bar</foo>', "should be serialized");
// Escaping values
var element3 = parser.parseFromString("<foo>a &gt; &apos;b&apos; &amp; &quot;b&quot; &lt; c</foo>","text/xml").documentElement;
equal(Strophe.serialize(element3), "<foo>a &gt; &apos;b&apos; &amp; &quot;b&quot; &lt; c</foo>", "should be serialized");
equal(Strophe.serialize(element3), "<foo>a &gt; 'b' &amp; \"b\" &lt; c</foo>", "should be serialized");
// Escaping attributes
var element4 = parser.parseFromString("<foo attr='&lt;a> &apos;b&apos;'>bar</foo>","text/xml").documentElement;
equal(Strophe.serialize(element4), "<foo attr='&lt;a&gt; &apos;b&apos;'>bar</foo>", "should be serialized");
equal(Strophe.serialize(element4), "<foo attr=\"&lt;a&gt; 'b'\">bar</foo>", "should be serialized");
var element5 = parser.parseFromString("<foo attr=\"&lt;a> &quot;b&quot;\">bar</foo>","text/xml").documentElement;
equal(Strophe.serialize(element5), "<foo attr='&lt;a&gt; \"b\"'>bar</foo>", "should be serialized");
// Empty elements
Expand Down