UNPKG

ajsfw

Version:
201 lines (200 loc) 7.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var NodeListOf_1 = require("./NodeListOf"); var NamedNodeMap_1 = require("./NamedNodeMap"); var Node_1 = require("./Node"); var Attr_1 = require("./Attr"); var HTMLCollectionOf_1 = require("./HTMLCollectionOf"); var DomParser_1 = require("./DomParser"); var Exceptions = require("./Exceptions"); var Element = (function (_super) { __extends(Element, _super); function Element(name, ownerDocument) { var _this = _super.call(this, Node_1.default.ELEMENT_NODE, name.toUpperCase(), ownerDocument) || this; _this.__attributes = new NamedNodeMap_1.default(); return _this; } Object.defineProperty(Element.prototype, "attributes", { get: function () { return this.__attributes; }, enumerable: true, configurable: true }); Element.prototype.getAttribute = function (name) { var attr = this.__attributes.getNamedItem(name); if (attr === null) { return null; } return attr.value; }; Element.prototype.setAttribute = function (name, value) { var attr = new Attr_1.default(this, name, value); this.__attributes.setNamedItem(attr); }; Element.prototype.getAttributeNode = function (name) { return this.__attributes.getNamedItem(name); }; Element.prototype.setAttributeNode = function (attribute) { return this.__attributes.setNamedItem(attribute); }; Element.prototype.hasAttribute = function (name) { return this.__attributes.getNamedItem(name) !== null; }; Object.defineProperty(Element.prototype, "innerHTML", { get: function () { var html = ""; var node = this.childNodes.item(0); while (node !== null) { switch (node.nodeType) { case Node_1.default.COMMENT_NODE: html += "<!--" + node.nodeValue + "-->"; break; case Node_1.default.DOCUMENT_NODE: html += node.textContent; break; case Node_1.default.ELEMENT_NODE: html += node.__html; break; case Node_1.default.TEXT_NODE: html += node.textContent; break; default: throw new Exceptions.InvalidNodeTypeException(); } node = node.nextSibling; } return html; }, set: function (html) { var parser = new DomParser_1.default(this.ownerDocument); var parsed = parser.parse(html); if (parsed instanceof Array) { for (var _i = 0, parsed_1 = parsed; _i < parsed_1.length; _i++) { var o = parsed_1[_i]; this.appendChild(o); } } else { this.appendChild(parsed); } }, enumerable: true, configurable: true }); Object.defineProperty(Element.prototype, "outerHTML", { get: function () { return this.__html; }, enumerable: true, configurable: true }); Element.prototype.getElementById = function (id) { if (this.hasAttribute("id") && this.getAttribute("id") === id) { return this; } var node = this.childNodes.item(0); while (node !== null) { if (node.nodeType !== Node_1.default.ELEMENT_NODE) { node = node.nextSibling; continue; } var e = node.getElementById(id); if (e !== null) { return e; } node = node.nextSibling; } return null; }; Element.prototype.getElementsByClassName = function (name) { return this.__getElementsByClassName(name); }; Element.prototype.getElementsByName = function (name) { return this.__getElementsByName(name); }; Element.prototype.getElementsByTagName = function (name) { return this.__getElementsByTagName(name.toUpperCase()); }; Object.defineProperty(Element.prototype, "__html", { get: function () { var UnpairNodeNames = ["IMG", "BR", "INPUT", "META", "LINK"]; var html = "<" + this.nodeName.toLowerCase(); for (var i = 0, j = this.__attributes.length; i < j; i++) { var atr = this.__attributes.item(i); html += " " + atr.name; if (atr.value) { html += "=\"" + atr.value + "\""; } } if (UnpairNodeNames.indexOf(this.nodeName) !== -1) { html += " /"; } else { html += ">"; html += this.innerHTML; html += "</" + this.nodeName.toLowerCase(); } html += ">"; return html; }, enumerable: true, configurable: true }); Element.prototype.__getElementsByClassName = function (name, elements) { var els = elements || new HTMLCollectionOf_1.default(); if (this.__hasClassName(name)) { els.push(this); } var node = this.childNodes.item(0); while (node !== null) { if (node.nodeType !== Node_1.default.ELEMENT_NODE) { node = node.nextSibling; continue; } node.__getElementsByClassName(name, els); node = node.nextSibling; } return els; }; Element.prototype.__hasClassName = function (name) { var classVal = this.getAttribute("class"); if (classVal == null) { return false; } var classes = classVal.split(" "); return classes.indexOf(name) !== -1; }; Element.prototype.__getElementsByName = function (name, elements) { var els = elements || new NodeListOf_1.default(this); if (this.getAttribute("name") === name) { els.append(this); } var node = this.childNodes.item(0); while (node !== null) { if (node.nodeType !== Node_1.default.ELEMENT_NODE) { node = node.nextSibling; continue; } node.__getElementsByName(name, els); node = node.nextSibling; } return els; }; Element.prototype.__getElementsByTagName = function (name, elements) { var els = elements || new NodeListOf_1.default(this); if (this.nodeName === name) { els.append(this); } var node = this.childNodes.item(0); while (node !== null) { if (node.nodeType !== Node_1.default.ELEMENT_NODE) { node = node.nextSibling; continue; } node.__getElementsByTagName(name, els); node = node.nextSibling; } return els; }; return Element; }(Node_1.default)); exports.default = Element;