From bb2c206bdd17b1f42cfdcdd62b105873a1b8f9a2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 26 Sep 2013 13:12:05 +0200 Subject: [PATCH] appending element node --- src/smartxml/smartxml.js | 35 +++++++++++++++++++++++++++++++---- src/smartxml/smartxml.test.js | 32 ++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index e996f76..4bb5afe 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -5,8 +5,12 @@ define([ 'use strict'; -var Document = function(xml) { - var $document = $(xml); +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])}}); @@ -17,15 +21,38 @@ var ElementNode = function(nativeNode) { var myNode = nativeNode, $myNode = $(nativeNode); + this._$myNode = $myNode; + this._myNode= myNode; this.getTagName = function() { return myNode.tagName.toLowerCase(); + }; + + this.append = function(documentNode) { + this._$myNode.append(documentNode._$myNode); + }; + + this.contents = function() { + var toret = []; + this._$myNode.contents().each(function() { + if(this.nodeType === Node.ELEMENT_NODE) + toret.push(new ElementNode(this)); + }); + return toret; + }; + + this.sameNode = function(otherNode) { + return this._myNode === otherNode._myNode; } }; return { - fromXML: function(xml) { - return new Document(xml); + documentFromXML: function(xml) { + return new Document(parseXML(xml)); + }, + + elementNodeFromXML: function(xml) { + return new ElementNode(parseXML(xml)); } }; diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index c4f99e8..35cca61 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -9,11 +9,35 @@ define([ var expect = chai.expect; -describe.only('Basic use', function() { - it('exposes root element', function() { - var doc = smartxml.fromXML('
'); - expect(doc.root.getTagName()).to.equal('div'); +var getDocumentFromXML = function(xml) { + return smartxml.documentFromXML(xml); +} + +var elementNodeFromParams = function(params) { + return smartxml.elementNodeFromXML('<' + params.tag + '>'); +} + + +describe.only('smartxml', function() { + + describe('Basic use', function() { + it('exposes root element', function() { + var doc = getDocumentFromXML('
'); + expect(doc.root.getTagName()).to.equal('div'); + }); }); + + describe('Manipulations', function() { + + it('appende element node to another element node', function() { + var node1 = elementNodeFromParams({tag: 'div'}), + node2 = elementNodeFromParams({tag: 'a'}); + node1.append(node2); + expect(node1.contents()[0].sameNode(node2)).to.be.true; + }); + + }); + }); }); \ No newline at end of file -- 2.20.1