[Pkg-javascript-commits] [ltx] 62/80: Imported Upstream version 0.2.2

Jonas Smedegaard dr at jones.dk
Sun Feb 28 10:50:13 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 6f6a07d81e574358556e9e683d17f930d3a5ef29
Author: Jonas Smedegaard <dr at jones.dk>
Date:   Thu Nov 15 01:40:01 2012 +0100

    Imported Upstream version 0.2.2
---
 .travis.yml          |   1 +
 README.markdown      |   4 +-
 lib/element.js       |  25 +++--
 package.json         |   6 +-
 test/element-test.js | 269 +++++++++++++++++++++++++++++----------------------
 5 files changed, 170 insertions(+), 135 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index f1d0f13..2a9b9e6 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,3 +2,4 @@ language: node_js
 node_js:
   - 0.4
   - 0.6
+  - 0.8
diff --git a/README.markdown b/README.markdown
index 2886ec1..38ab1f4 100644
--- a/README.markdown
+++ b/README.markdown
@@ -47,8 +47,8 @@ Refer to `lib/parse.js` for the interface.
 * `findNS(prefix?)`: search for xmlns of a prefix upwards
 * `getChild(name, xmlns?)`: find first child
 * `getChildren(name, xmlns?)`: find all children
-* `getChildByAttr(attr, value, xmlns?)`: find first child by a specific attribute
-* `getChildrenByAttr(attr, value, xmlns?)`: find all children by a specific attribute
+* `getChildByAttr(attr, value, xmlns?, recursive?)`: find first child by a specific attribute
+* `getChildrenByAttr(attr, value, xmlns?, recursive?)`: find all children by a specific attribute
 * `getText()`: appends all text nodes recursively
 * `getChildText(name)`: a child's text contents
 * `root()`: uppermost parent in the tree
diff --git a/lib/element.js b/lib/element.js
index 12b2367..7189847 100644
--- a/lib/element.js
+++ b/lib/element.js
@@ -84,16 +84,16 @@ Element.prototype.getChildren = function(name, xmlns) {
 };
 
 /**
- * xmlns can be null
+ * xmlns and recursive can be null
  **/
-Element.prototype.getChildByAttr = function(attr, val, xmlns) {
-    return this.getChildrenByAttr(attr, val, xmlns)[0];
+Element.prototype.getChildByAttr = function(attr, val, xmlns, recursive) {
+    return this.getChildrenByAttr(attr, val, xmlns, recursive)[0];
 };
 
 /**
- * xmlns can be null
+ * xmlns and recursive can be null
  **/
-Element.prototype.getChildrenByAttr = function(attr, val, xmlns) {
+Element.prototype.getChildrenByAttr = function(attr, val, xmlns, recursive) {
     var result = [];
     for(var i = 0; i < this.children.length; i++) {
 	      var child = this.children[i];
@@ -101,7 +101,10 @@ Element.prototype.getChildrenByAttr = function(attr, val, xmlns) {
             child.attrs[attr] == val &&
             (!xmlns || child.getNS() == xmlns))
             result.push(child);
+        if (recursive && child.getChildrenByAttr)
+            result.push(child.getChildrenByAttr(attr, val, xmlns, true));
     }
+    if (recursive) result = [].concat.apply([], result);
     return result;
 };
 
@@ -115,15 +118,9 @@ Element.prototype.getText = function() {
     return text;
 };
 
-Element.prototype.getChildText = function(name) {
-    var text = null;
-    for(var i = 0; i < this.children.length; i++) {
-	var child = this.children[i];
-        if (!text && child.name == name) {
-            text = child.getText();
-        }
-    }
-    return text;
+Element.prototype.getChildText = function(name, xmlns) {
+    var child = this.getChild(name, xmlns);
+    return child ? child.getText() : null;
 };
 
 /*** Builder ***/
diff --git a/package.json b/package.json
index 59b7838..af6d01e 100644
--- a/package.json
+++ b/package.json
@@ -1,9 +1,9 @@
 { "name": "ltx"
-,"version": "0.2.0"
+,"version": "0.2.2"
 ,"main": "./lib/index"
 ,"browserify": "./lib/index-browserify.js"
-,"description": "<xml for=\"node.js\">"
-,"author": "Stephan Maka"
+,"description": "<xml for=\"node.js\" browserify=\"too\">"
+,"author": "Astro"
 ,"dependencies": {"node-expat": ">=1.2.0"
 		 ,"sax": ">=0.3.5"
 		 }
diff --git a/test/element-test.js b/test/element-test.js
index 2de09e9..e3c94fa 100644
--- a/test/element-test.js
+++ b/test/element-test.js
@@ -4,130 +4,167 @@ ltx = require('./../lib/index');
 
 vows.describe('ltx').addBatch({
     'serialization': {
-	'serialize an element': function() {
-	    var e = new ltx.Element('e');
-	    assert.equal(e.toString(), '<e/>');
-	},
-	'serialize an element with attributes': function() {
-	    var e = new ltx.Element('e',
-				    { a1: 'foo' });
-	    assert.equal(e.toString(), '<e a1="foo"/>');
-	},
-	'serialize an element with attributes to entities': function() {
-	    var e = new ltx.Element('e',
-				    { a1: '"well"' });
-	    assert.equal(e.toString(), '<e a1=""well""/>');
-	},
-	'serialize an element with text': function() {
-	    var e = new ltx.Element('e').t('bar').root();
-	    assert.equal(e.toString(), '<e>bar</e>');
-	},
-	'serialize an element with text to entities': function() {
-	    var e = new ltx.Element('e').t('1 < 2').root();
-	    assert.equal(e.toString(), '<e>1 < 2</e>');
-	},
-	'serialize an element with a number attribute': function() {
-	    var e = new ltx.Element('e', { a: 23 });
-	    assert.equal(e.toString(), '<e a="23"/>');
-	},
-	'serialize an element with number contents': function() {
-	    var e = new ltx.Element('e');
-	    e.c('foo').t(23);
-	    e.c('bar').t(0);
-	    assert.equal(e.toString(), '<e><foo>23</foo><bar>0</bar></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 number attribute': function() {
-	    var e = new ltx.Element('e', { foo: 23, bar: 0 });
-	    var s = e.toString();
-	    assert.ok(s.match(/foo="23"/));
-	    assert.ok(s.match(/bar="0"/));
-	},
-	'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>');
-	}
+        'serialize an element': function() {
+            var e = new ltx.Element('e');
+            assert.equal(e.toString(), '<e/>');
+        },
+        'serialize an element with attributes': function() {
+            var e = new ltx.Element('e',
+                  { a1: 'foo' });
+            assert.equal(e.toString(), '<e a1="foo"/>');
+        },
+        'serialize an element with attributes to entities': function() {
+            var e = new ltx.Element('e',
+                  { a1: '"well"' });
+            assert.equal(e.toString(), '<e a1=""well""/>');
+        },
+        'serialize an element with text': function() {
+            var e = new ltx.Element('e').t('bar').root();
+            assert.equal(e.toString(), '<e>bar</e>');
+        },
+        'serialize an element with text to entities': function() {
+            var e = new ltx.Element('e').t('1 < 2').root();
+            assert.equal(e.toString(), '<e>1 < 2</e>');
+        },
+        'serialize an element with a number attribute': function() {
+            var e = new ltx.Element('e', { a: 23 });
+            assert.equal(e.toString(), '<e a="23"/>');
+        },
+        'serialize an element with number contents': function() {
+            var e = new ltx.Element('e');
+            e.c('foo').t(23);
+            e.c('bar').t(0);
+            assert.equal(e.toString(), '<e><foo>23</foo><bar>0</bar></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 number attribute': function() {
+            var e = new ltx.Element('e', { foo: 23, bar: 0 });
+            var s = e.toString();
+            assert.ok(s.match(/foo="23"/));
+            assert.ok(s.match(/bar="0"/));
+        },
+        '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>');
+        }
     },
 
     '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);
-	}
+        '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);
+        }
     },
 
-    'clone': {
-	'clones': function() {
-	    var orig = new ltx.Element('msg', { type: 'get' }).
-		c('content').t('foo').root();
-	    var clone = orig.clone();
-	    assert.equal(clone.name, orig.name);
-	    assert.equal(clone.attrs.type, orig.attrs.type);
-	    assert.equal(clone.attrs.to, orig.attrs.to);
-	    assert.equal(clone.children.length, orig.children.length);
-	    assert.equal(clone.getChildText('content'), orig.getChildText('content'));
+  'clone': {
+      'clones': function() {
+          var orig = new ltx.Element('msg', { type: 'get' }).
+              c('content').t('foo').root();
+          var clone = orig.clone();
+          assert.equal(clone.name, orig.name);
+          assert.equal(clone.attrs.type, orig.attrs.type);
+          assert.equal(clone.attrs.to, orig.attrs.to);
+          assert.equal(clone.children.length, orig.children.length);
+          assert.equal(clone.getChildText('content'), orig.getChildText('content'));
 
-	    assert.equal(orig.getChild('content').up(), orig);
-	    assert.equal(clone.getChild('content').up(), clone);
-	},
-	'mod attr': function() {
-	    var orig = new ltx.Element('msg', { type: 'get' });
-	    var clone = orig.clone();
-	    clone.attrs.type += '-result';
+          assert.equal(orig.getChild('content').up(), orig);
+          assert.equal(clone.getChild('content').up(), clone);
+    },
+    'mod attr': function() {
+        var orig = new ltx.Element('msg', { type: 'get' });
+        var clone = orig.clone();
+        clone.attrs.type += '-result';
 
-	    assert.equal(orig.attrs.type, 'get');
-	    assert.equal(clone.attrs.type, 'get-result');
-	},
-	'rm attr': function() {
-	    var orig = new ltx.Element('msg', { from: 'me' });
-	    var clone = orig.clone();
-	    delete clone.attrs.from;
-	    clone.attrs.to = 'you';
+        assert.equal(orig.attrs.type, 'get');
+        assert.equal(clone.attrs.type, 'get-result');
+    },
+    'rm attr': function() {
+        var orig = new ltx.Element('msg', { from: 'me' });
+        var clone = orig.clone();
+        delete clone.attrs.from;
+        clone.attrs.to = 'you';
 
-	    assert.equal(orig.attrs.from, 'me');
-	    assert.equal(orig.attrs.to, undefined);
-	    assert.equal(clone.attrs.from, undefined);
-	    assert.equal(clone.attrs.to, 'you');
-	},
-	'mod child': function() {
-	    var orig = new ltx.Element('msg', { type: 'get' }).
-		c('content').t('foo').root();
-	    var clone = orig.clone();
-	    clone.getChild('content').
-		t('bar').
-		name = 'description';
+        assert.equal(orig.attrs.from, 'me');
+        assert.equal(orig.attrs.to, undefined);
+        assert.equal(clone.attrs.from, undefined);
+        assert.equal(clone.attrs.to, 'you');
+    },
+    'mod child': function() {
+        var orig = new ltx.Element('msg', { type: 'get' }).
+            c('content').t('foo').root();
+        var clone = orig.clone();
+        clone.getChild('content').t('bar').name = 'description';
 
-	    assert.equal(orig.children[0].name, 'content');
-	    assert.equal(orig.getChildText('content'), 'foo');
-	    assert.equal(clone.children[0].name, 'description');
-	    assert.equal(clone.getChildText('description'), 'foobar');
-	}
+        assert.equal(orig.children[0].name, 'content');
+        assert.equal(orig.getChildText('content'), 'foo');
+        assert.equal(clone.children[0].name, 'description');
+        assert.equal(clone.getChildText('description'), 'foobar');
     }
+  },
+
+  'recursive': {
+      'getChildrenByAttr': function() {
+        var el = new ltx.Element('a')
+            .c('b')
+            .c('c', {myProperty:'x'}).t('bar').up().up().up()
+            .c('d', {id: 'x'})
+            .c('e', {myProperty:'x'}).root();
+
+        var results = el.getChildrenByAttr('myProperty', 'x', null, true);
+        assert.equal( results[0].toString(), '<c myProperty="x">bar</c>');
+        assert.equal( results[1].toString(), '<e myProperty="x"/>');
+      },
+      'getChildByAttr': function() {
+        var el = new ltx.Element('a')
+            .c('b')
+            .c('c', {id:'x'})
+            .t('bar').root();
+        assert.equal(el.getChildByAttr('id', 'x', null, true).toString(), '<c id="x">bar</c>');
+      }
+  },
+
+  "issue #15: Inconsistency with prefixed elements": {
+      topic: function() {
+	  return ltx.parse('<root><x:foo>bar</x:foo></root>');
+      },
+      "getChildText prefixed": function(el) {
+	  assert.equal(el.getChildText('x:foo'), null);
+      },
+      "getChildText unprefixed": function(el) {
+	  assert.equal(el.getChildText('foo'), 'bar');
+      },
+      "getChild prefixed": function(el) {
+	  assert.equal(el.getChild('x:foo'), null);
+      },
+      "getChild unprefixed": function(el) {
+	  assert.equal(el.getChild('foo').getText(), 'bar');
+      }
+  }
 }).export(module);

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