//https://developer.mozilla.org/en-US/docs/Web/API/Element function Node(cfg) { this.namespace = cfg.namespace || null; this.text = cfg.text; this._selfCloseTag = cfg.selfCloseTag; Object.defineProperties(this, { nodeType: { value: cfg.nodeType }, nodeName: { value: cfg.nodeType == 1 ? cfg.nodeName : '#text' }, childNodes: { value: cfg.childNodes }, firstChild: { get: function(){ return this.childNodes[0] || null; } }, lastChild: { get: function(){ return this.childNodes[this.childNodes.length-1] || null; } }, parentNode: { value: cfg.parentNode || null }, attributes: { value: cfg.attributes || [] }, innerHTML: { get: function(){ var result = '', cNode; for (var i = 0, l = this.childNodes.length; i < l; i++) { cNode = this.childNodes[i]; result += cNode.nodeType === 3 ? cNode.text : cNode.outerHTML; } return result; } }, outerHTML: { get: function(){ if (this.nodeType != 3){ var str, attrs = (this.attributes.map(function(elem){ return elem.name + (elem.value ? '=' + '"'+ elem.value +'"' : ''); }) || []).join(' '), childs = ''; str = '<' + this.nodeName + (attrs ? ' ' + attrs : '') + (this._selfCloseTag ? '/' : '') + '>'; if (!this._selfCloseTag){ childs = (this._selfCloseTag ? '' : this.childNodes.map(function(child){ return child.outerHTML; }) || []).join(''); str += childs; str += ''; } } else{ str = this.textContent; } return str; } }, textContent: { get: function(){ if (this.nodeType == Node.TEXT_NODE){ return this.text; } else{ return this.childNodes.map(function(node){ return node.textContent; }).join('').replace(/\x20+/g, ' '); } } } }); } Node.prototype.getAttribute = function (attributeName) { for (var i = 0, l = this.attributes.length; i < l; i++) { if (this.attributes[i].name == attributeName) { return this.attributes[i].value; } } return null; }; function searchElements(root, conditionFn, onlyFirst){ var result = []; onlyFirst = !!onlyFirst; if (root.nodeType !== 3) { for (var i = 0, l = root.childNodes.length; i < l; i++) { if (root.childNodes[i].nodeType !== 3 && conditionFn(root.childNodes[i])) { result.push(root.childNodes[i]); if (onlyFirst){ break; } } result = result.concat(searchElements(root.childNodes[i], conditionFn)); } } return onlyFirst ? result[0] : result; } Node.prototype.getElementsByTagName = function (tagName) { return searchElements(this, function(elem){ return elem.nodeName == tagName; }) }; Node.prototype.getElementsByClassName = function (className) { var expr = new RegExp('^(.*?\\s)?' + className + '(\\s.*?)?$'); return searchElements(this, function(elem){ return elem.attributes.length && expr.test(elem.getAttribute('class')); }) }; Node.prototype.getElementById = function (id) { return searchElements(this, function(elem){ return elem.attributes.length && elem.getAttribute('id') == id; }, true) }; Node.prototype.getElementsByName = function (name) { return searchElements(this, function(elem){ return elem.attributes.length && elem.getAttribute('name') == name; }) }; Node.ELEMENT_NODE = 1; Node.TEXT_NODE = 3; module.exports = Node;