[Pkg-javascript-commits] [ltx] 335/469: Element.getChildElements() method. Fixes #39

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:03: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 fff07d7ac87605a16bfa200d223968b7649d5bd7
Author: Thom Nichols <tmnichols at gmail.com>
Date:   Thu Apr 3 11:05:38 2014 -0400

    Element.getChildElements() method.  Fixes #39
---
 lib/element.js       | 11 +++++++++++
 test/element-test.js | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/lib/element.js b/lib/element.js
index 5376fa3..3c8161d 100644
--- a/lib/element.js
+++ b/lib/element.js
@@ -177,6 +177,17 @@ Element.prototype.getChildText = function(name, xmlns) {
     return child ? child.getText() : null
 }
 
+/**
+ * Return all direct descendents that are Elements.
+ * This differs from `getChildren` in that it will exclude text nodes,
+ * processing instructions, etc.
+ */
+Element.prototype.getChildElements = function() {
+    return this.getChildrenByFilter( function(child) {
+        return child instanceof Element
+    })
+}
+
 /*** Builder ***/
 
 /** returns uppermost parent */
diff --git a/test/element-test.js b/test/element-test.js
index 8171897..5320f0c 100644
--- a/test/element-test.js
+++ b/test/element-test.js
@@ -146,6 +146,38 @@ vows.describe('ltx').addBatch({
             assert.equal(clone.getChildText('description'), 'foobar')
         }
     },
+    'children': {
+        'getChildren': function() {
+            var el = new ltx.Element('a')
+            .c('b')
+            .c('b2').up().up()
+            .t('foo')
+            .c('c').t('cbar').up()
+            .t('bar')
+            .root()
+            
+            var children = el.children
+            assert.equal( children.length, 4 )
+            assert.equal( children[0].name, 'b')
+            assert.equal( children[1], 'foo')
+            assert.equal( children[2].name, 'c')
+            assert.equal( children[3], 'bar')
+        },
+        'getChildElements': function() {
+            var el = new ltx.Element('a')
+            .c('b')
+            .c('b2').up().up()
+            .t('foo')
+            .c('c').t('cbar').up()
+            .t('bar')
+            .root()
+
+            var children = el.getChildElements()
+            assert.equal( children.length, 2 )
+            assert.equal( children[0].name, 'b')
+            assert.equal( children[1].name, 'c')
+        }
+    },
 
     'recursive': {
         'getChildrenByAttr': function() {

-- 
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