+ getAttrs: function() {
+ var toret = [];
+ for(var i = 0; i < this.nativeNode.attributes.length; i++) {
+ toret.push(this.nativeNode.attributes[i]);
+ }
+ return toret;
+ },
+
+ toXML: function() {
+ var wrapper = $('<div>');
+ wrapper.append(this._getXMLDOMToDump());
+ return wrapper.html();
+ },
+
+ _getXMLDOMToDump: function() {
+ return this._$;
+ }
+});
+
+
+var TextNode = function(nativeNode, document) {
+ DocumentNode.call(this, nativeNode, document);
+};
+TextNode.prototype = Object.create(DocumentNode.prototype);
+
+$.extend(TextNode.prototype, {
+ nodeType: Node.TEXT_NODE,
+
+ getText: function() {
+ return this.nativeNode.data;
+ },
+
+ triggerTextChangeEvent: function() {
+ var event = new events.ChangeEvent('nodeTextChange', {node: this});
+ this.document.trigger('change', event);
+ }
+});
+
+
+var parseXML = function(xml) {
+ return $($.trim(xml))[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 {
+ 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;
+ }
+ var toret = new Factory(from, this);
+ _.extend(toret, this._nodeMethods);
+ _.extend(toret, typeMethods);
+
+ _.extend(toret, this._nodeTransformations);
+ _.extend(toret, typeTransformations);
+
+ toret.__super__ = _.extend({}, this._nodeMethods, this._nodeTransformations);
+ _.keys(toret.__super__).forEach(function(key) {
+ toret.__super__[key] = _.bind(toret.__super__[key], toret);
+ });
+
+ return toret;
+ },
+
+ loadXML: function(xml, options) {
+ options = options || {};
+ this._defineDocumentProperties($(parseXML(xml)));
+ this.normalizeXML(this.dom);
+ if(!options.silent) {
+ this.trigger('contentSet');
+ }