[Pkg-javascript-commits] [ltx] 185/469: skip serializing null/undefined children
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:01:47 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 7289d44a3e639d400f4fbc73e1de0ffe9e02a0c8
Author: Astro <astro at spaceboyz.net>
Date: Thu Dec 1 22:34:26 2011 +0100
skip serializing null/undefined children
---
lib/element.js | 6 +++++-
test/test_element.js | 18 ++++++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/lib/element.js b/lib/element.js
index 37c2058..d14f016 100644
--- a/lib/element.js
+++ b/lib/element.js
@@ -211,11 +211,15 @@ Element.prototype.write = function(writer) {
} else {
writer(">");
this.children.forEach(function(child) {
+ if (!child)
+ /* Skip null/undefined */
+ return;
+
if (child.write)
child.write(writer);
else if (typeof child === 'string')
writer(escapeXmlText(child));
- else
+ else if (child.toString)
writer(escapeXmlText(child.toString()));
});
writer("</");
diff --git a/test/test_element.js b/test/test_element.js
index 7f9a059..5330c4f 100644
--- a/test/test_element.js
+++ b/test/test_element.js
@@ -33,6 +33,24 @@ vows.describe('ltx').addBatch({
'serialize an element with number contents': function() {
var e = new ltx.Element('e').t(23);
assert.equal(e.toString(), '<e>23</e>');
+ },
+ 'serialize with undefined attribute': function() {
+ var e = new ltx.Element('e', { foo: undefined });
+ assert.equal(e.toString(), '<e/>');
+ },
+ 'serialize with null attribute': function() {
+ var e = new ltx.Element('e', { foo: null });
+ assert.equal(e.toString(), '<e/>');
+ },
+ 'serialize with undefined child': function() {
+ var e = new ltx.Element('e');
+ e.children = [undefined];
+ assert.equal(e.toString(), '<e></e>');
+ },
+ 'serialize with null child': function() {
+ var e = new ltx.Element('e');
+ e.children = [null];
+ assert.equal(e.toString(), '<e></e>');
}
},
--
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