6 ], function($, _, Backbone, events) {
11 var TEXT_NODE = Node.TEXT_NODE;
14 var DocumentNode = function(nativeNode, document) {
16 throw new Error('undefined document for a node');
18 this.document = document;
19 this._setNativeNode(nativeNode);
23 $.extend(DocumentNode.prototype, {
24 _setNativeNode: function(nativeNode) {
25 this.nativeNode = nativeNode;
26 this._$ = $(nativeNode);
29 detach: function() { this._$.detach(); },
31 sameNode: function(otherNode) {
32 return this.nativeNode === otherNode.nativeNode;
36 return this.nativeNode.parentNode ? this.document.createElementNode(this.nativeNode.parentNode) : null;
39 before: function(node) {
40 this._$.before(node.nativeNode);
43 wrapWith: function(node) {
50 triggerChangeEvent: function(type, metaData) {
51 var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {}));
52 this.document.trigger('change', event);
56 var ElementNode = function(nativeNode, document) {
57 DocumentNode.call(this, nativeNode, document);
60 $.extend(ElementNode.prototype, DocumentNode.prototype, {
61 nodeType: Node.ELEMENT_NODE,
63 setData: function(key, value) {
64 if(value !== undefined) {
65 this._$.data(key, value);
67 this._$.removeData(_.keys(this._$.data()));
72 getData: function(key) {
74 return this._$.data(key);
76 return this._$.data();
79 getTagName: function() {
80 return this.nativeNode.tagName.toLowerCase();
83 contents: function() {
85 document = this.document;
86 this._$.contents().each(function() {
87 if(this.nodeType === Node.ELEMENT_NODE) {
88 toret.push(document.createElementNode(this));
90 else if(this.nodeType === Node.TEXT_NODE) {
91 toret.push(document.createTextNode(this));
97 indexOf: function(node) {
98 return this._$.contents().index(node._$);
101 setTag: function(tagName) {
102 var node = this.document.createElementNode({tagName: tagName}),
103 oldTagName = this.getTagName();
105 this.getAttrs().forEach(function(attribute) {
106 node.setAttr(attribute.name, attribute.value, true);
108 node.setData(this.getData());
110 this._$.replaceWith(node._$);
111 this._setNativeNode(node._$[0]);
112 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
115 getAttr: function(name) {
116 return this._$.attr(name);
119 setAttr: function(name, value, silent) {
120 var oldVal = this.getAttr(name);
121 this._$.attr(name, value);
123 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
127 getAttrs: function() {
129 for(var i = 0; i < this.nativeNode.attributes.length; i++) {
130 toret.push(this.nativeNode.attributes[i]);
135 append: function(documentNode) {
136 this._$.append(documentNode.nativeNode);
139 unwrapContent: function() {
140 var parent = this.parent();
145 var parentContents = parent.contents(),
146 myContents = this.contents(),
147 myIdx = parent.indexOf(this);
149 if(myContents.length === 0) {
150 return this.detach();
153 var moveLeftRange, moveRightRange, leftMerged;
155 if(myIdx > 0 && (parentContents[myIdx-1].nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) {
156 parentContents[myIdx-1].appendText(myContents[0].getText());
157 myContents[0].detach();
158 moveLeftRange = true;
164 if(!(leftMerged && myContents.length === 1)) {
165 if(myIdx < parentContents.length - 1 && (parentContents[myIdx+1].nodeType === TEXT_NODE) && (myContents[myContents.length-1].nodeType === TEXT_NODE)) {
166 parentContents[myIdx+1].prependText(myContents[myContents.length-1].getText());
167 myContents[myContents.length-1].detach();
168 moveRightRange = true;
172 var childrenLength = this.contents().length;
173 this.contents().forEach(function(child) {
180 element1: parent.contents()[myIdx + (moveLeftRange ? -1 : 0)],
181 element2: parent.contents()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
186 var wrapper = $('<div>');
187 wrapper.append(this._$);
188 return wrapper.html();
192 var TextNode = function(nativeNode, document) {
193 DocumentNode.call(this, nativeNode, document);
196 $.extend(TextNode.prototype, DocumentNode.prototype, {
197 nodeType: Node.TEXT_NODE,
199 getText: function() {
200 return this.nativeNode.data;
203 appendText: function(text) {
204 this.nativeNode.data = this.nativeNode.data + text;
207 prependText: function(text) {
208 this.nativeNode.data = text + this.nativeNode.data;
213 var parseXML = function(xml) {
217 var Document = function(xml) {
221 $.extend(Document.prototype, Backbone.Events, {
222 ElementNodeFactory: ElementNode,
223 TextNodeFactory: TextNode,
225 createElementNode: function(from) {
226 if(!(from instanceof HTMLElement)) {
227 from = $('<' + from.tagName + '>');
229 return new this.ElementNodeFactory(from, this);
232 createTextNode: function(nativeNode) {
233 return new this.TextNodeFactory(nativeNode, this);
236 loadXML: function(xml) {
237 var $document = $(parseXML(xml));
240 Object.defineProperty(this, 'root', {get: function() {
241 return doc.createElementNode($document[0]);
242 }, configurable: true});
243 Object.defineProperty(this, 'dom', {get: function() {
245 }, configurable: true});
247 this.trigger('contentSet');
251 return this.root.toXML();
257 documentFromXML: function(xml) {
258 return new Document(parseXML(xml));
261 elementNodeFromXML: function(xml) {
262 return this.documentFromXML(xml).root;
266 DocumentNode: DocumentNode,
267 ElementNode: ElementNode