/usr/lib/nodejs/ltx/element.js is in node-ltx 0.2.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | /**
* This cheap replica of DOM/Builder puts me to shame :-)
*
* Attributes are in the element.attrs object. Children is a list of
* either other Elements or Strings for text content.
**/
function Element(name, attrs) {
this.name = name;
this.parent = null;
this.attrs = attrs || {};
this.children = [];
}
/*** Accessors ***/
/**
* if (element.is('message', 'jabber:client')) ...
**/
Element.prototype.is = function(name, xmlns) {
return this.getName() == name &&
(!xmlns || this.getNS() == xmlns);
};
/* without prefix */
Element.prototype.getName = function() {
if (this.name.indexOf(":") >= 0)
return this.name.substr(this.name.indexOf(":") + 1);
else
return this.name;
};
/**
* retrieves the namespace of the current element, upwards recursively
**/
Element.prototype.getNS = function() {
if (this.name.indexOf(":") >= 0) {
var prefix = this.name.substr(0, this.name.indexOf(":"));
return this.findNS(prefix);
} else {
return this.findNS();
}
};
/**
* find the namespace to the given prefix, upwards recursively
**/
Element.prototype.findNS = function(prefix) {
if (!prefix) {
/* default namespace */
if (this.attrs.xmlns)
return this.attrs.xmlns;
else if (this.parent)
return this.parent.findNS();
} else {
/* prefixed namespace */
var attr = 'xmlns:' + prefix;
if (this.attrs[attr])
return this.attrs[attr];
else if (this.parent)
return this.parent.findNS(prefix);
}
};
/**
* xmlns can be null
**/
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];
if (child.getName &&
child.getName() == name &&
(!xmlns || child.getNS() == xmlns))
result.push(child);
}
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++) {
var child = this.children[i];
if (typeof child == 'string')
text += child;
}
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;
};
/*** Builder ***/
/** returns uppermost parent */
Element.prototype.root = function() {
if (this.parent)
return this.parent.root();
else
return this;
};
Element.prototype.tree = Element.prototype.root;
/** just parent or itself */
Element.prototype.up = function() {
if (this.parent)
return this.parent;
else
return this;
};
/** create child node and return it */
Element.prototype.c = function(name, attrs) {
return this.cnode(new Element(name, attrs));
};
Element.prototype.cnode = function(child) {
this.children.push(child);
child.parent = this;
return child;
};
/** add text node and return element */
Element.prototype.t = function(text) {
this.children.push(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;
};
/**
* To use in case you want the same XML data for separate uses.
* Please refrain from this practise unless you know what you are
* doing. Building XML with ltx is easy!
*/
Element.prototype.clone = function() {
var clone = new Element(this.name, {});
for(var k in this.attrs) {
if (this.attrs.hasOwnProperty(k))
clone.attrs[k] = this.attrs[k];
}
for(var i = 0; i < this.children.length; i++) {
var child = this.children[i];
clone.cnode(child.clone ? child.clone() : child);
}
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() {
var s = "";
this.write(function(c) {
s += c;
});
return s;
};
Element.prototype.write = function(writer) {
writer("<");
writer(this.name);
for(var k in this.attrs) {
var v = this.attrs[k];
if (v || v === '' || v === 0) {
writer(" ");
writer(k);
writer("=\"");
if (typeof v != 'string')
v = v.toString();
writer(escapeXml(v));
writer("\"");
}
}
if (this.children.length == 0) {
writer("/>");
} else {
writer(">");
for(var i = 0; i < this.children.length; i++) {
var child = this.children[i];
/* Skip null/undefined */
if (child || child === 0) {
if (child.write)
child.write(writer);
else if (typeof child === 'string')
writer(escapeXmlText(child));
else if (child.toString)
writer(escapeXmlText(child.toString()));
}
}
writer("</");
writer(this.name);
writer(">");
}
};
function escapeXml(s) {
return s.
replace(/\&/g, '&').
replace(/</g, '<').
replace(/>/g, '>').
replace(/"/g, '"').
replace(/'/g, ''');
}
function escapeXmlText(s) {
return s.
replace(/\&/g, '&').
replace(/</g, '<').
replace(/>/g, '>');
}
exports.Element = Element;
exports.escapeXml = escapeXml;
|