nxkit
Version:
This is a collection of tools, independent of any other libraries
140 lines (139 loc) • 5.31 kB
JavaScript
"use strict";
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2015, xuewen.chu
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of xuewen.chu nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL xuewen.chu BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
Object.defineProperty(exports, "__esModule", { value: true });
const node_1 = require("./node");
const parser = require("./parser");
const named_node_map_1 = require("./named_node_map");
// NamedNodeMap
function visitNode(node, callback) {
if (!callback(node))
return false;
var next = node.firstChild;
if (next) {
if (!visitNode(next, callback))
return false;
}
if (next = node.nextSibling)
return visitNode(next, callback);
return true;
}
exports.visitNode = visitNode;
class Element extends node_1.Node {
constructor(doc, tagName) {
super(doc);
this.nodeType = node_1.NODE_TYPE.ELEMENT_NODE;
this.childNodes = new node_1.NodeList();
this.attributes = new named_node_map_1.NamedNodeMap(this);
this.tagName = tagName;
}
get nodeName() {
return this.tagName;
}
hasAttribute(name) {
return this.getAttributeNode(name) != null;
}
getAttribute(name) {
var attr = this.getAttributeNode(name);
return attr && attr.value || '';
}
setAttribute(name, value) {
var attr = this.ownerDocument.createAttribute(name, value);
this.setAttributeNode(attr);
}
getAttributeNode(name) {
return this.attributes.getNamedItem(name);
}
setAttributeNode(newAttr) {
this.attributes.setNamedItem(newAttr);
}
removeAttributeNode(oldAttr) {
this.attributes.removeItem(oldAttr);
}
removeAttribute(name) {
var attr = this.getAttributeNode(name);
attr && this.removeAttributeNode(attr);
}
hasAttributeNS(namespaceURI, localName) {
return this.getAttributeNodeNS(namespaceURI, localName) != null;
}
getAttributeNS(namespaceURI, localName) {
var attr = this.getAttributeNodeNS(namespaceURI, localName);
return attr && attr.value || '';
}
setAttributeNS(namespaceURI, qualifiedName, value) {
var attr = this.ownerDocument.createAttributeNS(namespaceURI, qualifiedName, value);
this.setAttributeNode(attr);
}
getAttributeNodeNS(namespaceURI, localName) {
return this.attributes.getNamedItemNS(namespaceURI, localName);
}
setAttributeNodeNS(newAttr) {
this.attributes.setNamedItemNS(newAttr);
}
removeAttributeNS(namespaceURI, localName) {
var attr = this.getAttributeNodeNS(namespaceURI, localName);
attr && this.removeAttributeNode(attr);
}
getElementsByTagName(name) {
return new node_1.LiveNodeList(this, function (node) {
var ls = [];
visitNode(node, function (node) {
if (node.nodeType == node_1.NODE_TYPE.ELEMENT_NODE && node.tagName == name)
ls.push(node);
return true;
});
return ls;
});
}
getElementsByTagNameNS(namespaceURI, localName) {
return new node_1.LiveNodeList(this, function (node) {
var ls = [];
visitNode(node, function (node) {
if (node.nodeType == node_1.NODE_TYPE.ELEMENT_NODE &&
node.namespaceURI == namespaceURI &&
node.localName == localName)
ls.push(node);
return true;
});
return ls;
});
}
get innerXml() {
return Array.toArray(this.childNodes).join('');
}
set innerXml(xml) {
this.removeAllChild();
if (xml) {
new parser.Parser().fragment(this.ownerDocument, xml, this);
}
}
}
exports.Element = Element;