+var parseXML = function(xml) {
+ return $(xml)[0];
+};
+
+var Document = function(xml) {
+ this.loadXML(xml);
+};
+
+$.extend(Document.prototype, Backbone.Events, {
+ ElementNodeFactory: ElementNode,
+ TextNodeFactory: TextNode,
+
+ createElementNode: function(from) {
+ if(!(from instanceof HTMLElement)) {
+ from = $('<' + from.tagName + '>');
+ }
+ return new this.ElementNodeFactory(from, this);
+ },
+
+ createTextNode: function(nativeNode) {
+ return new this.TextNodeFactory(nativeNode, this);
+ },
+
+ loadXML: function(xml) {
+ var $document = $(parseXML(xml));
+
+ var doc = this;
+ Object.defineProperty(this, 'root', {get: function() {
+ return doc.createElementNode($document[0]);
+ }, configurable: true});
+ Object.defineProperty(this, 'dom', {get: function() {
+ return $document[0];
+ }, configurable: true});
+
+ this.trigger('contentSet');
+ },
+
+ toXML: function() {
+ return this.root.toXML();
+ }
+});
+
+