Refactor
[fnpeditor.git] / src / smartxml / smartxml.js
1 define([
2     'libs/jquery'
3 ], function($) {
4     
5 'use strict';
6
7
8 var parseXML = function(xml) {
9     return $(xml)[0];
10 }
11
12 var Document = function(nativeNode) {
13     var $document = $(nativeNode);
14
15
16     Object.defineProperty(this, 'root', {get: function() { return new ElementNode($document[0])}}); 
17 }
18
19
20 var ElementNode = function(nativeNode) {
21     this.nativeNode = nativeNode;
22     this._$ = $(nativeNode);
23 };
24
25 $.extend(ElementNode.prototype, {
26     getTagName: function() {
27         return this.nativeNode.tagName.toLowerCase();
28     },
29
30     append: function(documentNode) {
31         this._$.append(documentNode.nativeNode);
32     },
33
34     contents: function() {
35         var toret = [];
36         this._$.contents().each(function() {
37             if(this.nodeType === Node.ELEMENT_NODE)
38                 toret.push(new ElementNode(this));
39         });
40         return toret;
41     },
42
43
44     sameNode: function(otherNode) {
45         return this.nativeNode === otherNode.nativeNode;
46     }
47
48 });
49
50 return {
51     documentFromXML: function(xml) {
52         return new Document(parseXML(xml));
53     },
54
55     elementNodeFromXML: function(xml) {
56         return new ElementNode(parseXML(xml));
57     }
58 };
59
60 });