[Pkg-javascript-commits] [ltx] 198/469: added methods to modify an elements text and its attribute values

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:02:56 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 b42c7db43994fcbaf084c256d38d1fa840c3a3f3
Author: Markus Kohlhase <mail at markus-kohlhase.de>
Date:   Sun Dec 18 17:06:48 2011 +0100

    added methods to modify an elements text and its attribute values
---
 README.markdown |  7 +++++++
 lib/element.js  | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/README.markdown b/README.markdown
index 741dd88..5d33a3b 100644
--- a/README.markdown
+++ b/README.markdown
@@ -27,6 +27,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/lib/element.js b/lib/element.js
index 14d0c11..8ded66c 100644
--- a/lib/element.js
+++ b/lib/element.js
@@ -184,6 +184,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() {

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