[Pkg-javascript-commits] [ltx] 175/469: Element.prototype.remove()

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:01:23 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 8ae9b752a6ab24a2615379af1e7f6e4370f93c3a
Author: Astro <astro at spaceboyz.net>
Date:   Wed Mar 30 00:08:58 2011 +0200

    Element.prototype.remove()
---
 lib/element.js       | 27 +++++++++++++++++++++++++++
 test/test_element.js | 21 +++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/lib/element.js b/lib/element.js
index c583375..8e0e981 100644
--- a/lib/element.js
+++ b/lib/element.js
@@ -137,6 +137,33 @@ Element.prototype.t = function(text) {
     return this;
 };
 
+/*** Manipulation ***/
+
+/**
+ * Either:
+ *   el.remove(childEl);
+ *   el.remove('author', 'urn:...');
+ */
+Element.prototype.remove = function(el, xmlns) {
+    var filter;
+    if (typeof el === 'string') {
+	/* 1st parameter is tag name */
+	filter = function(child) {
+	    return !(child.is &&
+		     child.is(el, xmlns));
+	};
+    } else {
+	/* 1st parameter is element */
+	filter = function(child) {
+	    return child !== el;
+	};
+    }
+
+    this.children = this.children.filter(filter);
+
+    return this;
+};
+
 /*** Serialization ***/
 
 Element.prototype.toString = function() {
diff --git a/test/test_element.js b/test/test_element.js
index 0535cff..27bed1a 100644
--- a/test/test_element.js
+++ b/test/test_element.js
@@ -26,5 +26,26 @@ vows.describe('ltx').addBatch({
 	    var e = new ltx.Element('e').t('1 < 2').root();
 	    assert.equal(e.toString(), '<e>1 < 2</e>');
 	}
+    },
+
+    'remove': {
+	'by element': function() {
+	    var el = new ltx.Element('e').
+		c('c').c('x').up().up().
+		c('c2').up().
+		c('c').up();
+	    el.remove(el.getChild('c'));
+	    assert.equal(el.getChildren('c').length, 1);
+	    assert.equal(el.getChildren('c2').length, 1);
+	},
+	'by name': function() {
+	    var el = new ltx.Element('e').
+		c('c').up().
+		c('c2').up().
+		c('c').up();
+	    el.remove('c');
+	    assert.equal(el.getChildren('c').length, 0);
+	    assert.equal(el.getChildren('c2').length, 1);
+	}
     }
 }).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