+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)) {
+ if(from.text) {
+ from = document.createTextNode(from.text);
+ } else {
+ var node = $('<' + from.tagName + '>');
+
+ _.keys(from.attrs || {}).forEach(function(key) {
+ node.attr(key, from.attrs[key]);
+ });
+
+ from = node[0];
+ }
+ }
+ return new this.ElementNodeFactory(from, this);
+ },
+
+ createTextNode: function(nativeNode) {
+ return new this.TextNodeFactory(nativeNode, this);
+ },
+
+ loadXML: function(xml, options) {
+ options = options || {};
+ defineDocumentProperties(this, $(parseXML(xml)));
+ if(!options.silent) {
+ this.trigger('contentSet');
+ }
+ },
+
+ toXML: function() {
+ return this.root.toXML();
+ },
+
+ containsNode: function(node) {
+ return node._$.parents().index(this.root._$) !== -1;
+ },
+
+ wrapNodes: function(params) {
+ if(!(params.element1.parent().sameNode(params.element2.parent()))) {
+ throw new Error('Wrapping non-sibling nodes not supported.');
+ }
+
+ var parent = params.element1.parent(),
+ parentContents = parent.contents(),
+ wrapper = this.createElementNode({
+ tagName: params._with.tagName,
+ attrs: params._with.attrs}),
+ idx1 = parent.indexOf(params.element1),
+ idx2 = parent.indexOf(params.element2);
+
+ if(idx1 > idx2) {
+ var tmp = idx1;
+ idx1 = idx2;
+ idx2 = tmp;
+ }
+
+ var insertingMethod, insertingTarget;
+ if(idx1 === 0) {
+ insertingMethod = 'prepend';
+ insertingTarget = parent;
+ } else {
+ insertingMethod = 'after';
+ insertingTarget = parentContents[idx1-1];
+ }
+
+ for(var i = idx1; i <= idx2; i++) {
+ wrapper.append(parentContents[i].detach());
+ }
+
+ insertingTarget[insertingMethod](wrapper);
+ return wrapper;
+ },
+
+ _wrapText: function(params) {
+ params = _.extend({textNodeIdx: 0}, params);
+ if(typeof params.textNodeIdx === 'number') {
+ params.textNodeIdx = [params.textNodeIdx];
+ }
+
+ var contentsInside = params.inside.contents(),
+ idx1 = Math.min.apply(Math, params.textNodeIdx),
+ idx2 = Math.max.apply(Math, params.textNodeIdx),
+ textNode1 = contentsInside[idx1],
+ textNode2 = contentsInside[idx2],
+ sameNode = textNode1.sameNode(textNode2),
+ prefixOutside = textNode1.getText().substr(0, params.offsetStart),
+ prefixInside = textNode1.getText().substr(params.offsetStart),
+ suffixInside = textNode2.getText().substr(0, params.offsetEnd),
+ suffixOutside = textNode2.getText().substr(params.offsetEnd)
+ ;
+
+ if(!(textNode1.parent().sameNode(textNode2.parent()))) {
+ throw new Error('Wrapping text in non-sibling text nodes not supported.');
+ }
+
+ var wrapperElement = this.createElementNode({tagName: params._with.tagName, attrs: params._with.attrs});
+ textNode1.after(wrapperElement);
+ textNode1.detach();
+
+ if(prefixOutside.length > 0) {
+ wrapperElement.before({text:prefixOutside});
+ }
+ if(sameNode) {
+ var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
+ wrapperElement.append({text: core});
+ } else {
+ textNode2.detach();
+ if(prefixInside.length > 0) {
+ wrapperElement.append({text: prefixInside});
+ }
+ for(var i = idx1 + 1; i < idx2; i++) {
+ wrapperElement.append(contentsInside[i]);
+ }
+ if(suffixInside.length > 0) {
+ wrapperElement.append({text: suffixInside});
+ }
+ }
+ if(suffixOutside.length > 0) {
+ wrapperElement.after({text: suffixOutside});
+ }
+ return wrapperElement;
+ }
+});
+
+var defineDocumentProperties = function(doc, $document) {
+ Object.defineProperty(doc, 'root', {get: function() {
+ return doc.createElementNode($document[0]);
+ }, configurable: true});
+ Object.defineProperty(doc, 'dom', {get: function() {
+ return $document[0];
+ }, configurable: true});
+};
+