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 otherNode && this.nativeNode === otherNode.nativeNode;
36 return this.nativeNode.parentNode ? this.document.createElementNode(this.nativeNode.parentNode) : null;
39 after: function(node) {
40 node = node instanceof ElementNode ? node : this.document.createElementNode(node);
41 this._$.after(node.nativeNode);
45 before: function(node) {
46 node = node instanceof ElementNode ? node : this.document.createElementNode(node);
47 this._$.before(node.nativeNode);
51 wrapWith: function(node) {
52 node = node instanceof ElementNode ? node : this.document.createElementNode(node);
61 triggerChangeEvent: function(type, metaData) {
62 var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {}));
63 this.document.trigger('change', event);
67 var ElementNode = function(nativeNode, document) {
68 DocumentNode.call(this, nativeNode, document);
70 ElementNode.prototype = Object.create(DocumentNode.prototype);
72 $.extend(ElementNode.prototype, {
73 nodeType: Node.ELEMENT_NODE,
75 setData: function(key, value) {
76 if(value !== undefined) {
77 this._$.data(key, value);
79 this._$.removeData(_.keys(this._$.data()));
84 getData: function(key) {
86 return this._$.data(key);
88 return this._$.data();
91 getTagName: function() {
92 return this.nativeNode.tagName.toLowerCase();
95 contents: function() {
97 document = this.document;
98 this._$.contents().each(function() {
99 if(this.nodeType === Node.ELEMENT_NODE) {
100 toret.push(document.createElementNode(this));
102 else if(this.nodeType === Node.TEXT_NODE) {
103 toret.push(document.createTextNode(this));
109 indexOf: function(node) {
110 return this._$.contents().index(node._$);
113 setTag: function(tagName) {
114 var node = this.document.createElementNode({tagName: tagName}),
115 oldTagName = this.getTagName(),
116 myContents = this._$.contents();
118 this.getAttrs().forEach(function(attribute) {
119 node.setAttr(attribute.name, attribute.value, true);
121 node.setData(this.getData());
123 if(this.sameNode(this.document.root)) {
124 defineDocumentProperties(this.document, node._$);
126 this._$.replaceWith(node._$);
127 this._setNativeNode(node._$[0]);
128 this._$.append(myContents);
129 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
132 getAttr: function(name) {
133 return this._$.attr(name);
136 setAttr: function(name, value, silent) {
137 var oldVal = this.getAttr(name);
138 this._$.attr(name, value);
140 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
144 getAttrs: function() {
146 for(var i = 0; i < this.nativeNode.attributes.length; i++) {
147 toret.push(this.nativeNode.attributes[i]);
152 append: function(node) {
153 node = node instanceof DocumentNode ? node : this.document.createElementNode(node);
154 this._$.append(node.nativeNode);
157 unwrapContent: function() {
158 var parent = this.parent();
163 var parentContents = parent.contents(),
164 myContents = this.contents(),
165 myIdx = parent.indexOf(this);
167 if(myContents.length === 0) {
168 return this.detach();
171 var moveLeftRange, moveRightRange, leftMerged;
173 if(myIdx > 0 && (parentContents[myIdx-1].nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) {
174 parentContents[myIdx-1].appendText(myContents[0].getText());
175 myContents[0].detach();
176 moveLeftRange = true;
182 if(!(leftMerged && myContents.length === 1)) {
183 if(myIdx < parentContents.length - 1 && (parentContents[myIdx+1].nodeType === TEXT_NODE) && (myContents[myContents.length-1].nodeType === TEXT_NODE)) {
184 parentContents[myIdx+1].prependText(myContents[myContents.length-1].getText());
185 myContents[myContents.length-1].detach();
186 moveRightRange = true;
190 var childrenLength = this.contents().length;
191 this.contents().forEach(function(child) {
198 element1: parent.contents()[myIdx + (moveLeftRange ? -1 : 0)],
199 element2: parent.contents()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
204 var wrapper = $('<div>');
205 wrapper.append(this._getXMLDOMToDump());
206 return wrapper.html();
209 _getXMLDOMToDump: function() {
214 var TextNode = function(nativeNode, document) {
215 DocumentNode.call(this, nativeNode, document);
217 TextNode.prototype = Object.create(DocumentNode.prototype);
219 $.extend(TextNode.prototype, {
220 nodeType: Node.TEXT_NODE,
222 getText: function() {
223 return this.nativeNode.data;
226 setText: function(text) {
227 this.nativeNode.data = text;
228 this.triggerTextChangeEvent();
231 appendText: function(text) {
232 this.nativeNode.data = this.nativeNode.data + text;
233 this.triggerTextChangeEvent();
236 prependText: function(text) {
237 this.nativeNode.data = text + this.nativeNode.data;
238 this.triggerTextChangeEvent();
241 wrapWith: function(desc) {
242 if(typeof desc.start === 'number' && typeof desc.end === 'number') {
243 return this.document._wrapText({
244 inside: this.parent(),
245 textNodeIdx: this.parent().indexOf(this),
246 offsetStart: Math.min(desc.start, desc.end),
247 offsetEnd: Math.max(desc.start, desc.end),
248 _with: {tag: desc.tagName, attrs: desc.attrs}
251 return DocumentNode.prototype.wrapWith.call(this, desc);
255 triggerTextChangeEvent: function() {
256 var event = new events.ChangeEvent('nodeTextChange', {node: this});
257 this.document.trigger('change', event);
262 var parseXML = function(xml) {
266 var Document = function(xml) {
270 $.extend(Document.prototype, Backbone.Events, {
271 ElementNodeFactory: ElementNode,
272 TextNodeFactory: TextNode,
274 createElementNode: function(from) {
275 if(!(from instanceof HTMLElement)) {
277 from = document.createTextNode(from.text);
279 var node = $('<' + from.tagName + '>');
281 _.keys(from.attrs || {}).forEach(function(key) {
282 node.attr(key, from.attrs[key]);
288 return new this.ElementNodeFactory(from, this);
291 createTextNode: function(nativeNode) {
292 return new this.TextNodeFactory(nativeNode, this);
295 loadXML: function(xml, options) {
296 options = options || {};
297 defineDocumentProperties(this, $(parseXML(xml)));
298 if(!options.silent) {
299 this.trigger('contentSet');
304 return this.root.toXML();
307 _wrapText: function(params) {
308 params = _.extend({textNodeIdx: 0}, params);
309 if(typeof params.textNodeIdx === 'number') {
310 params.textNodeIdx = [params.textNodeIdx];
313 var contentsInside = params.inside.contents(),
314 idx1 = Math.min.apply(Math, params.textNodeIdx),
315 idx2 = Math.max.apply(Math, params.textNodeIdx),
316 textNode1 = contentsInside[idx1],
317 textNode2 = contentsInside[idx2],
318 sameNode = textNode1.sameNode(textNode2),
319 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
320 prefixInside = textNode1.getText().substr(params.offsetStart),
321 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
322 suffixOutside = textNode2.getText().substr(params.offsetEnd)
325 var wrapperElement = this.createElementNode({tagName: params._with.tag, attrs: params._with.attrs});
326 textNode1.after(wrapperElement);
329 if(prefixOutside.length > 0) {
330 wrapperElement.before({text:prefixOutside});
333 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
334 wrapperElement.append({text: core});
337 if(prefixInside.length > 0) {
338 wrapperElement.append({text: prefixInside});
340 for(var i = idx1 + 1; i < idx2; i++) {
341 wrapperElement.append(contentsInside[i]);
343 if(suffixInside.length > 0) {
344 wrapperElement.append({text: suffixInside});
347 if(suffixOutside.length > 0) {
348 wrapperElement.after({text: suffixOutside});
350 return wrapperElement;
354 var defineDocumentProperties = function(doc, $document) {
355 Object.defineProperty(doc, 'root', {get: function() {
356 return doc.createElementNode($document[0]);
357 }, configurable: true});
358 Object.defineProperty(doc, 'dom', {get: function() {
360 }, configurable: true});
364 documentFromXML: function(xml) {
365 return new Document(parseXML(xml));
368 elementNodeFromXML: function(xml) {
369 return this.documentFromXML(xml).root;
373 DocumentNode: DocumentNode,
374 ElementNode: ElementNode