X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/e5d6cddf0560271b56c4c4683d6194b067e8348c..24e73bdbeb5e83ff18d2f9b359e7b41fe8230c1a:/src/smartxml/smartxml.js?ds=sidebyside diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 8eeca1a..43e3041 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -9,36 +9,54 @@ var TEXT_NODE = Node.TEXT_NODE, ELEMENT_NODE = Node.ELEMENT_NODE; var parseXML = function(xml) { return $(xml)[0]; -} +}; var Document = function(nativeNode) { var $document = $(nativeNode); - Object.defineProperty(this, 'root', {get: function() { return new ElementNode($document[0])}}); -} + Object.defineProperty(this, 'root', {get: function() { return new ElementNode($document[0]);}}); +}; -var ElementNode = function(nativeNode) { +var DocumentNode = function(nativeNode) { this.nativeNode = nativeNode; this._$ = $(nativeNode); }; -$.extend(ElementNode.prototype, { - nodeType: Node.ELEMENT_NODE, +$.extend(DocumentNode.prototype, { + detach: function() { this._$.detach(); }, - getTagName: function() { - return this.nativeNode.tagName.toLowerCase(); + sameNode: function(otherNode) { + return this.nativeNode === otherNode.nativeNode; }, - append: function(documentNode) { - this._$.append(documentNode.nativeNode); + parent: function() { + return this.nativeNode.parentNode ? new ElementNode(this.nativeNode.parentNode) : null; }, before: function(node) { this._$.before(node.nativeNode); }, + wrapWith: function(node) { + if(this.parent()) + this.before(node); + node.append(this); + }, +}); + +var ElementNode = function(nativeNode) { + DocumentNode.apply(this, arguments); +}; + +$.extend(ElementNode.prototype, DocumentNode.prototype, { + nodeType: Node.ELEMENT_NODE, + + getTagName: function() { + return this.nativeNode.tagName.toLowerCase(); + }, + contents: function() { var toret = []; this._$.contents().each(function() { @@ -50,21 +68,20 @@ $.extend(ElementNode.prototype, { return toret; }, - - sameNode: function(otherNode) { - return this.nativeNode === otherNode.nativeNode; - }, - indexOf: function(node) { return this._$.contents().index(node._$); }, - detach: function() { - this._$.detach(); + getAttr: function(name) { + return this._$.attr(name); }, - parent: function() { - return new ElementNode(this._$.parent()); + setAttr: function(name, value) { + this._$.attr(name, value); + }, + + append: function(documentNode) { + this._$.append(documentNode.nativeNode); }, unwrapContent: function() { @@ -114,17 +131,12 @@ $.extend(ElementNode.prototype, { }); var TextNode = function(nativeNode) { - this.nativeNode = nativeNode; - this._$ = $(nativeNode); -} + DocumentNode.apply(this, arguments); +}; -$.extend(TextNode.prototype, { +$.extend(TextNode.prototype, DocumentNode.prototype, { nodeType: Node.TEXT_NODE, - detach: function() { - this._$.detach(); - }, - getText: function() { return this.nativeNode.data; }, @@ -136,7 +148,7 @@ $.extend(TextNode.prototype, { prependText: function(text) { this.nativeNode.data = text + this.nativeNode.data; } -}) +}); return {