+var parseXML = function(xml) {
+ var toret = $($.trim(xml));
+ if(!toret.length) {
+ throw new Error('Unable to parse XML: ' + xml);
+ }
+ return toret[0];
+
+};
+
+var registerTransformation = function(desc, name, target) {
+ var Transformation = transformations.createContextTransformation(desc, name);
+ target[name] = function() {
+ var instance = this,
+ args = Array.prototype.slice.call(arguments, 0);
+ return instance.transform(Transformation, args);
+ };
+};
+
+var registerMethod = function(methodName, method, target) {
+ if(target[methodName]) {
+ throw new Error('Cannot extend {target} with method name {methodName}. Name already exists.'
+ .replace('{target}', target)
+ .replace('{methodName}', methodName)
+ );
+ }
+ target[methodName] = method;
+};
+
+
+var Document = function(xml) {
+ this.loadXML(xml);
+ this.undoStack = [];
+ this.redoStack = [];
+ this._transactionStack = [];
+ this._transformationLevel = 0;
+
+ this._nodeMethods = {};
+ this._textNodeMethods = {};
+ this._elementNodeMethods = {};
+ this._nodeTransformations = {};
+ this._textNodeTransformations = {};
+ this._elementNodeTransformations = {};
+
+ this.registerExtension(coreTransformations);
+};
+
+$.extend(Document.prototype, Backbone.Events, {
+ ElementNodeFactory: ElementNode,
+ TextNodeFactory: TextNode,
+
+ createDocumentNode: function(from) {
+ if(!(from instanceof Node)) {
+ if(typeof from === 'string') {
+ from = parseXML(from);
+ this.normalizeXML(from);
+ } else {
+ if(from.text !== undefined) {
+ /* globals document */
+ from = document.createTextNode(from.text);
+ } else {
+ if(!from.tagName) {
+ throw new Error('tagName missing');
+ }
+ var node = $('<' + from.tagName + '>');
+
+ _.keys(from.attrs || {}).forEach(function(key) {
+ node.attr(key, from.attrs[key]);
+ });
+
+ from = node[0];
+ }
+ }
+ }
+ var Factory, typeMethods, typeTransformations;
+ if(from.nodeType === Node.TEXT_NODE) {
+ Factory = this.TextNodeFactory;
+ typeMethods = this._textNodeMethods;
+ typeTransformations = this._textNodeTransformations;
+ } else if(from.nodeType === Node.ELEMENT_NODE) {
+ Factory = this.ElementNodeFactory;
+ typeMethods = this._elementNodeMethods;
+ typeTransformations = this._elementNodeTransformations;