6 ], function($, _, Backbone, events) {
11 var TEXT_NODE = Node.TEXT_NODE;
14 var INSERTION = function(implementation) {
15 var toret = function(node) {
16 var insertion = this.getNodeInsertion(node);
17 implementation.call(this, insertion.ofNode.nativeNode);
18 this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode});
19 return insertion.ofNode;
24 var DocumentNode = function(nativeNode, document) {
26 throw new Error('undefined document for a node');
28 this.document = document;
29 this._setNativeNode(nativeNode);
33 $.extend(DocumentNode.prototype, {
34 _setNativeNode: function(nativeNode) {
35 this.nativeNode = nativeNode;
36 this._$ = $(nativeNode);
40 return this.document.root.sameNode(this);
44 var parent = this.parent();
46 this.triggerChangeEvent('nodeDetached', {parent: parent});
50 sameNode: function(otherNode) {
51 return otherNode && this.nativeNode === otherNode.nativeNode;
55 var parentNode = this.nativeNode.parentNode;
56 if(parentNode && parentNode.nodeType === Node.ELEMENT_NODE) {
57 return this.document.createDocumentNode(parentNode);
63 var parent = this.parent(),
64 parents = parent ? parent.parents() : [];
66 parents.unshift(parent);
72 var myIdx = this.getIndex();
73 return myIdx > 0 ? this.parent().contents()[myIdx-1] : null;
80 var myIdx = this.getIndex(),
81 parentContents = this.parent().contents();
82 return myIdx < parentContents.length - 1 ? parentContents[myIdx+1] : null;
85 isSurroundedByTextElements: function() {
86 var prev = this.prev(),
88 return prev && (prev.nodeType === Node.TEXT_NODE) && next && (next.nodeType === Node.TEXT_NODE);
91 after: INSERTION(function(nativeNode) {
92 return this._$.after(nativeNode);
95 before: INSERTION(function(nativeNode) {
96 return this._$.before(nativeNode);
99 wrapWith: function(node) {
100 var insertion = this.getNodeInsertion(node);
102 this.before(insertion.ofNode);
104 insertion.ofNode.append(this);
105 return insertion.ofNode;
109 * Removes parent of a node if node has no siblings.
115 var parent = this.parent(),
117 if(parent.contents().length === 1) {
118 grandParent = parent.parent();
119 parent.unwrapContent();
124 triggerChangeEvent: function(type, metaData) {
125 var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {}));
126 if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) {
127 this.document.trigger('change', event);
131 getNodeInsertion: function(node) {
133 if(node instanceof DocumentNode) {
134 insertion.ofNode = node;
135 insertion.insertsNew = !this.document.containsNode(node);
137 insertion.ofNode = this.document.createDocumentNode(node);
138 insertion.insertsNew = true;
143 getIndex: function() {
147 return this.parent().indexOf(this);
151 var ElementNode = function(nativeNode, document) {
152 DocumentNode.call(this, nativeNode, document);
154 ElementNode.prototype = Object.create(DocumentNode.prototype);
156 $.extend(ElementNode.prototype, {
157 nodeType: Node.ELEMENT_NODE,
161 if(parent && this.isSurroundedByTextElements()) {
163 this.prev().appendText(next.getText());
166 return DocumentNode.prototype.detach.call(this);
169 setData: function(key, value) {
170 if(value !== undefined) {
171 this._$.data(key, value);
173 this._$.removeData(_.keys(this._$.data()));
178 getData: function(key) {
180 return this._$.data(key);
182 return this._$.data();
185 getTagName: function() {
186 return this.nativeNode.tagName.toLowerCase();
189 contents: function() {
191 document = this.document;
192 this._$.contents().each(function() {
193 toret.push(document.createDocumentNode(this));
198 indexOf: function(node) {
199 return this._$.contents().index(node._$);
202 setTag: function(tagName) {
203 var node = this.document.createDocumentNode({tagName: tagName}),
204 oldTagName = this.getTagName(),
205 myContents = this._$.contents();
207 this.getAttrs().forEach(function(attribute) {
208 node.setAttr(attribute.name, attribute.value, true);
210 node.setData(this.getData());
212 if(this.sameNode(this.document.root)) {
213 defineDocumentProperties(this.document, node._$);
215 this._$.replaceWith(node._$);
216 this._setNativeNode(node._$[0]);
217 this._$.append(myContents);
218 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
221 getAttr: function(name) {
222 return this._$.attr(name);
225 setAttr: function(name, value, silent) {
226 var oldVal = this.getAttr(name);
227 this._$.attr(name, value);
229 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
233 getAttrs: function() {
235 for(var i = 0; i < this.nativeNode.attributes.length; i++) {
236 toret.push(this.nativeNode.attributes[i]);
241 append: INSERTION(function(nativeNode) {
242 this._$.append(nativeNode);
245 prepend: INSERTION(function(nativeNode) {
246 this._$.prepend(nativeNode);
249 unwrapContent: function() {
250 var parent = this.parent();
255 var myContents = this.contents(),
256 myIdx = parent.indexOf(this);
259 if(myContents.length === 0) {
260 return this.detach();
263 var prev = this.prev(),
265 moveLeftRange, moveRightRange, leftMerged;
267 if(prev && (prev.nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) {
268 prev.appendText(myContents[0].getText());
269 myContents[0].detach();
270 moveLeftRange = true;
276 if(!(leftMerged && myContents.length === 1)) {
277 var lastContents = _.last(myContents);
278 if(next && (next.nodeType === TEXT_NODE) && (lastContents.nodeType === TEXT_NODE)) {
279 next.prependText(lastContents.getText());
280 lastContents.detach();
281 moveRightRange = true;
285 var childrenLength = this.contents().length;
286 this.contents().forEach(function(child) {
293 element1: parent.contents()[myIdx + (moveLeftRange ? -1 : 0)],
294 element2: parent.contents()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
298 wrapText: function(params) {
299 return this.document._wrapText(_.extend({inside: this}, params));
303 var wrapper = $('<div>');
304 wrapper.append(this._getXMLDOMToDump());
305 return wrapper.html();
308 _getXMLDOMToDump: function() {
313 var TextNode = function(nativeNode, document) {
314 DocumentNode.call(this, nativeNode, document);
316 TextNode.prototype = Object.create(DocumentNode.prototype);
318 $.extend(TextNode.prototype, {
319 nodeType: Node.TEXT_NODE,
321 getText: function() {
322 return this.nativeNode.data;
325 setText: function(text) {
326 this.nativeNode.data = text;
327 this.triggerTextChangeEvent();
330 appendText: function(text) {
331 this.nativeNode.data = this.nativeNode.data + text;
332 this.triggerTextChangeEvent();
335 prependText: function(text) {
336 this.nativeNode.data = text + this.nativeNode.data;
337 this.triggerTextChangeEvent();
340 wrapWith: function(desc) {
341 if(typeof desc.start === 'number' && typeof desc.end === 'number') {
342 return this.document._wrapText({
343 inside: this.parent(),
344 textNodeIdx: this.parent().indexOf(this),
345 offsetStart: Math.min(desc.start, desc.end),
346 offsetEnd: Math.max(desc.start, desc.end),
347 _with: {tagName: desc.tagName, attrs: desc.attrs}
350 return DocumentNode.prototype.wrapWith.call(this, desc);
354 triggerTextChangeEvent: function() {
355 var event = new events.ChangeEvent('nodeTextChange', {node: this});
356 this.document.trigger('change', event);
361 var parseXML = function(xml) {
365 var Document = function(xml) {
369 $.extend(Document.prototype, Backbone.Events, {
370 ElementNodeFactory: ElementNode,
371 TextNodeFactory: TextNode,
373 createDocumentNode: function(from) {
374 if(!(from instanceof Node)) {
375 if(from.text !== undefined) {
376 from = document.createTextNode(from.text);
378 var node = $('<' + from.tagName + '>');
380 _.keys(from.attrs || {}).forEach(function(key) {
381 node.attr(key, from.attrs[key]);
388 if(from.nodeType === Node.TEXT_NODE) {
389 Factory = this.TextNodeFactory;
390 } else if(from.nodeType === Node.ELEMENT_NODE) {
391 Factory = this.ElementNodeFactory;
393 return new Factory(from, this);
396 loadXML: function(xml, options) {
397 options = options || {};
398 defineDocumentProperties(this, $(parseXML(xml)));
399 if(!options.silent) {
400 this.trigger('contentSet');
405 return this.root.toXML();
408 containsNode: function(node) {
409 return this.root && (node.nativeNode === this.root.nativeNode || node._$.parents().index(this.root._$) !== -1);
412 wrapNodes: function(params) {
413 if(!(params.element1.parent().sameNode(params.element2.parent()))) {
414 throw new Error('Wrapping non-sibling nodes not supported.');
417 var parent = params.element1.parent(),
418 parentContents = parent.contents(),
419 wrapper = this.createDocumentNode({
420 tagName: params._with.tagName,
421 attrs: params._with.attrs}),
422 idx1 = parent.indexOf(params.element1),
423 idx2 = parent.indexOf(params.element2);
431 var insertingMethod, insertingTarget;
433 insertingMethod = 'prepend';
434 insertingTarget = parent;
436 insertingMethod = 'after';
437 insertingTarget = parentContents[idx1-1];
440 for(var i = idx1; i <= idx2; i++) {
441 wrapper.append(parentContents[i].detach());
444 insertingTarget[insertingMethod](wrapper);
448 getSiblingParents: function(params) {
449 var parents1 = [params.node1].concat(params.node1.parents()).reverse(),
450 parents2 = [params.node2].concat(params.node2.parents()).reverse(),
451 noSiblingParents = null;
453 if(parents1.length === 0 || parents2.length === 0 || !(parents1[0].sameNode(parents2[0]))) {
454 return noSiblingParents;
458 for(i = 0; i < Math.min(parents1.length, parents2.length); i++) {
459 if(parents1[i].sameNode(parents2[i])) {
464 return {node1: parents1[i], node2: parents2[i]};
467 _wrapText: function(params) {
468 params = _.extend({textNodeIdx: 0}, params);
469 if(typeof params.textNodeIdx === 'number') {
470 params.textNodeIdx = [params.textNodeIdx];
473 var contentsInside = params.inside.contents(),
474 idx1 = Math.min.apply(Math, params.textNodeIdx),
475 idx2 = Math.max.apply(Math, params.textNodeIdx),
476 textNode1 = contentsInside[idx1],
477 textNode2 = contentsInside[idx2],
478 sameNode = textNode1.sameNode(textNode2),
479 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
480 prefixInside = textNode1.getText().substr(params.offsetStart),
481 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
482 suffixOutside = textNode2.getText().substr(params.offsetEnd)
485 if(!(textNode1.parent().sameNode(textNode2.parent()))) {
486 throw new Error('Wrapping text in non-sibling text nodes not supported.');
489 var wrapperElement = this.createDocumentNode({tagName: params._with.tagName, attrs: params._with.attrs});
490 textNode1.after(wrapperElement);
493 if(prefixOutside.length > 0) {
494 wrapperElement.before({text:prefixOutside});
497 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
498 wrapperElement.append({text: core});
501 if(prefixInside.length > 0) {
502 wrapperElement.append({text: prefixInside});
504 for(var i = idx1 + 1; i < idx2; i++) {
505 wrapperElement.append(contentsInside[i]);
507 if(suffixInside.length > 0) {
508 wrapperElement.append({text: suffixInside});
511 if(suffixOutside.length > 0) {
512 wrapperElement.after({text: suffixOutside});
514 return wrapperElement;
517 trigger: function() {
518 //console.log('trigger: ' + arguments[0] + (arguments[1] ? ', ' + arguments[1].type : ''));
519 Backbone.Events.trigger.apply(this, arguments);
523 var defineDocumentProperties = function(doc, $document) {
524 Object.defineProperty(doc, 'root', {get: function() {
525 return doc.createDocumentNode($document[0]);
526 }, configurable: true});
527 Object.defineProperty(doc, 'dom', {get: function() {
529 }, configurable: true});
533 documentFromXML: function(xml) {
534 return new Document(parseXML(xml));
537 elementNodeFromXML: function(xml) {
538 return this.documentFromXML(xml).root;
542 DocumentNode: DocumentNode,
543 ElementNode: ElementNode