[Pkg-javascript-commits] [ltx] 55/469: xml.js: fix entity serialization + tests

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:00:59 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository ltx.

commit 7d683ea1b986bc8a58fc957ca7a5bef10ab3ee5d
Author: Astro <astro at spaceboyz.net>
Date:   Sat Aug 14 00:50:35 2010 +0200

    xml.js: fix entity serialization + tests
---
 lib/xmpp/xml.js  | 17 +++++++----------
 test/test_xml.js | 30 ++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/lib/xmpp/xml.js b/lib/xmpp/xml.js
index 6a84497..765cd0f 100644
--- a/lib/xmpp/xml.js
+++ b/lib/xmpp/xml.js
@@ -2,10 +2,7 @@
 function Element(name, attrs) {
     this.name = name;
     this.parent = null;
-    if (attrs)
-	this.attrs = attrs;
-    else
-	this.attrs = {};
+    this.attrs = attrs || {};
     this.children = [];
 }
 
@@ -151,7 +148,7 @@ Element.prototype.write = function(writer) {
 	writer(">");
 	this.children.forEach(function(child) {
 	    if (typeof child == 'string')
-		writer(child);
+		writer(escapeXml(child));
 	    else
 		child.write(writer);
 	});
@@ -163,11 +160,11 @@ Element.prototype.write = function(writer) {
 
 function escapeXml(s) {
     return s.
-	replace('&', '&').
-	replace('<', '<').
-	replace('>', '>').
-	replace('"', '"').
-	replace('\'', ''');
+	replace(/\&/g, '&').
+	replace(/</g, '<').
+	replace(/>/g, '>').
+	replace(/"/g, '"').
+	replace(/'/g, ''');
 };
 
 exports.Element = Element;
diff --git a/test/test_xml.js b/test/test_xml.js
new file mode 100644
index 0000000..52ec0be
--- /dev/null
+++ b/test/test_xml.js
@@ -0,0 +1,30 @@
+var vows = require('vows'),
+assert = require('assert'),
+XML = require('./../lib/xmpp/xml');
+
+vows.describe('XML').addBatch({
+    'serialization': {
+	'serialize an element': function() {
+	    var e = new XML.Element('e');
+	    assert.equal(e.toString(), '<e/>');
+	},
+	'serialize an element with attributes': function() {
+	    var e = new XML.Element('e',
+				    { a1: 'foo' });
+	    assert.equal(e.toString(), '<e a1="foo"/>');
+	},
+	'serialize an element with attributes to entities': function() {
+	    var e = new XML.Element('e',
+				    { a1: '"well"' });
+	    assert.equal(e.toString(), '<e a1=""well""/>');
+	},
+	'serialize an element with text': function() {
+	    var e = new XML.Element('e').t('bar').root();
+	    assert.equal(e.toString(), '<e>bar</e>');
+	},
+	'serialize an element with text to entities': function() {
+	    var e = new XML.Element('e').t('1 < 2').root();
+	    assert.equal(e.toString(), '<e>1 < 2</e>');
+	},
+    }
+}).run();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git



More information about the Pkg-javascript-commits mailing list