[Pkg-javascript-commits] [ltx] 42/80: Imported Upstream version 0.1.3
Jonas Smedegaard
dr at jones.dk
Sun Feb 28 10:50:11 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 e96dc7f566648b69e490bff04a29242e972c0811
Author: Jonas Smedegaard <dr at jones.dk>
Date: Sun Jan 15 17:07:38 2012 +0100
Imported Upstream version 0.1.3
---
README.markdown | 11 ++++++++++-
index.js | 1 +
lib/element.js | 44 +++++++++++++++++++++++++++++++++++++++++++-
package.json | 4 ++--
4 files changed, 56 insertions(+), 4 deletions(-)
diff --git a/README.markdown b/README.markdown
index 741dd88..b4ac945 100644
--- a/README.markdown
+++ b/README.markdown
@@ -11,8 +11,10 @@
* `getName()`: name without ns prefix
* `getNS()`: element's xmlns, respects prefixes and searches upwards
* `findNS(prefix?)`: search for xmlns of a prefix upwards
-* `getChild(name, xmlns?)`: find one children
+* `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
* `getText()`: appends all text nodes recursively
* `getChildText(name)`: a child's text contents
* `root()`: uppermost parent in the tree
@@ -27,6 +29,13 @@
children
* `children` is an Array of Strings and Elements
+## Modifying XML Elements
+
+* `remove(child)`: remove child by reference
+* `remove(name, xmlns)`: remove child by tag name and xmlns
+* `attr(attrName, value?)`: modify or get an attribute's value
+* `text(value?)`: modify or get the inner text
+* `clone()`: clones an element that is detached from the document
## Building XML Elements
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..bf4d18a
--- /dev/null
+++ b/index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/index.js');
diff --git a/lib/element.js b/lib/element.js
index 14d0c11..010e358 100644
--- a/lib/element.js
+++ b/lib/element.js
@@ -67,13 +67,14 @@ Element.prototype.findNS = function(prefix) {
Element.prototype.getChild = function(name, xmlns) {
return this.getChildren(name, xmlns)[0];
};
+
/**
* xmlns can be null
**/
Element.prototype.getChildren = function(name, xmlns) {
var result = [];
for(var i = 0; i < this.children.length; i++) {
- var child = this.children[i];
+ var child = this.children[i];
if (child.getName &&
child.getName() == name &&
(!xmlns || child.getNS() == xmlns))
@@ -82,6 +83,28 @@ Element.prototype.getChildren = function(name, xmlns) {
return result;
};
+/**
+ * xmlns can be null
+ **/
+Element.prototype.getChildByAttr = function(attr, val, xmlns) {
+ return this.getChildrenByAttr(attr, val, xmlns)[0];
+};
+
+/**
+ * xmlns can be null
+ **/
+Element.prototype.getChildrenByAttr = function(attr, val, xmlns) {
+ var result = [];
+ for(var i = 0; i < this.children.length; i++) {
+ var child = this.children[i];
+ if (child.attrs &&
+ child.attrs[attr] == val &&
+ (!xmlns || child.getNS() == xmlns))
+ result.push(child);
+ }
+ return result;
+};
+
Element.prototype.getText = function() {
var text = "";
for(var i = 0; i < this.children.length; i++) {
@@ -184,6 +207,25 @@ Element.prototype.clone = function() {
return clone;
};
+Element.prototype.text = function(val) {
+ if(val && this.children.length == 1){
+ this.children[0] = val;
+ return this;
+ }
+ return this.getText();
+};
+
+Element.prototype.attr = function(attr, val) {
+ if(val){
+ if(!this.attrs){
+ this.attrs = {};
+ }
+ this.attrs[attr] = val;
+ return this;
+ }
+ return this.attrs[attr];
+};
+
/*** Serialization ***/
Element.prototype.toString = function() {
diff --git a/package.json b/package.json
index e530e3e..bc5e9b0 100644
--- a/package.json
+++ b/package.json
@@ -1,5 +1,5 @@
{ "name": "ltx"
-,"version": "0.1.2"
+,"version": "0.1.3"
,"main": "./lib/index"
,"description": "<xml for=\"node.js\">"
,"author": "Stephan Maka"
@@ -14,7 +14,7 @@
,"email": "astro at spaceboyz.net"
,"web": "http://spaceboyz.net/~astro/"
}]
-,"contributors": ["Stephan Maka"]
+,"contributors": ["Stephan Maka", "Will Fife", "Markus Kohlhase"]
,"licenses": [{"type": "MIT"}]
,"engine": "node"
,"devDependencies": {"vows": ">=0.5.12"}
--
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