1 define(function(require) {
6 var _ = require('libs/underscore');
9 var INSERTION = function(implementation) {
10 var toret = function(node, options) {
11 var insertion = this.getNodeInsertion(node),
12 nodeWasContained = this.document.containsNode(insertion.ofNode),
15 options = options || {};
16 if(!(this.document.containsNode(this)) || !insertion.isNew) {
17 nodeParent = insertion.ofNode.parent();
19 returned = implementation.call(this, insertion.ofNode);
20 if(!options.silent && returned.sameNode(insertion.ofNode)) {
21 this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent, nodeWasContained);
28 var documentNodeTransformations = {
30 var parent = this.parent(),
31 existed = this.document.containsNode(this);
34 this.triggerChangeEvent('nodeDetached', {parent: parent});
36 // This was the root of the document
37 this.document._defineDocumentProperties(null);
43 replaceWith: function(node) {
46 return this.document.replaceRoot(node);
48 toret = this.after(node);
53 after: INSERTION(function(node) {
54 var next = this.next();
55 if(next && next.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
56 next.setText(node.getText() + next.getText());
60 this._$.after(node.nativeNode);
64 before: INSERTION(function(node) {
65 var prev = this.prev();
66 if(prev && prev.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
67 prev.setText(prev.getText() + node.getText());
71 this._$.before(node.nativeNode);
75 wrapWith: function(node) {
76 var insertion = this.getNodeInsertion(node);
78 this.before(insertion.ofNode);
80 insertion.ofNode.append(this);
81 return insertion.ofNode;
85 * Removes parent of a node if node has no siblings.
91 var parent = this.parent(),
93 if(parent.contents().length === 1) {
94 grandParent = parent.parent();
95 parent.unwrapContent();
101 var elementNodeTransformations = {
105 if(this.parent() && this.isSurroundedByTextElements()) {
107 this.prev().appendText(next.getText());
110 return this.__super__.detach();
113 setTag: function(tagName) {
114 var node = this.document.createDocumentNode({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 this.document._defineDocumentProperties(node._$);
127 /* TODO: This invalidates old references to this node. Caching instances on nodes would fix this. */
128 this._$.replaceWith(node._$);
129 this._setNativeNode(node._$[0]);
130 this._$.append(myContents);
131 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
135 setAttr: function(name, value, silent) {
136 var oldVal = this.getAttr(name);
137 this._$.attr(name, value);
139 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
143 append: INSERTION(function(node) {
144 var last = _.last(this.contents());
145 if(last && last.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
146 last.setText(last.getText() + node.getText());
150 this._$.append(node.nativeNode);
155 prepend: INSERTION(function(node) {
156 var first = this.contents()[0];
157 if(first && first.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
158 first.setText(node.getText() + first.getText());
162 this._$.prepend(node.nativeNode);
167 insertAtIndex: function(nativeNode, index) {
168 var contents = this.contents();
169 if(index < contents.length) {
170 return contents[index].before(nativeNode);
171 } else if(index === contents.length) {
172 return this.append(nativeNode);
176 unwrapContent: function() {
177 var parent = this.parent();
182 var myContents = this.contents(),
183 myIdx = parent.indexOf(this);
186 if(myContents.length === 0) {
187 return this.detach();
191 var childrenLength = this.contents().length,
194 this.contents().forEach(function(child) {
195 var returned = this.before(child);
196 if(first && !(returned.sameNode(child))) {
205 element1: parent.contents()[myIdx + (shiftRange ? -1 : 0)],
206 element2: parent.contents()[myIdx + childrenLength-1 + (shiftRange ? -1 : 0)]
210 wrapText: function(params) {
211 return this.document._wrapText(_.extend({inside: this}, params));
215 var textNodeTransformations = {
217 impl: function(t, text) {
218 t.oldText = this.getText();
219 this.nativeNode.data = text;
220 this.triggerTextChangeEvent();
223 this.setText(t.oldText);
227 before: INSERTION(function(node) {
228 if(node.nodeType === Node.TEXT_NODE) {
229 this.prependText(node.getText());
233 return this.__super__.before(node, {silent:true});
237 after: INSERTION(function(node) {
238 if(node.nodeType === Node.TEXT_NODE) {
239 this.appendText(node.getText());
243 return this.__super__.after(node, {silent:true});
247 append: function(node) {
248 if(node.nodeType === Node.TEXT_NODE) {
249 this.appendText(node.getText());
254 prepend: function(node) {
255 if(node.nodeType === Node.TEXT_NODE) {
256 this.prependText(node.getText());
262 appendText: function(text) {
263 this.nativeNode.data = this.nativeNode.data + text;
264 this.triggerTextChangeEvent();
267 prependText: function(text) {
268 this.nativeNode.data = text + this.nativeNode.data;
269 this.triggerTextChangeEvent();
272 wrapWith: function(desc) {
273 if(typeof desc.start === 'number' && typeof desc.end === 'number') {
274 return this.document._wrapText({
275 inside: this.parent(),
276 textNodeIdx: this.parent().indexOf(this),
277 offsetStart: Math.min(desc.start, desc.end),
278 offsetEnd: Math.max(desc.start, desc.end),
279 _with: {tagName: desc.tagName, attrs: desc.attrs}
282 return this.__super__.wrapWith.call(this, desc);
286 split: function(params) {
287 var parentElement = this.parent(),
289 succeedingChildren = [],
290 prefix = this.getText().substr(0, params.offset),
291 suffix = this.getText().substr(params.offset);
293 parentElement.contents().forEach(function(child) {
295 succeedingChildren.push(child);
297 if(child.sameNode(this)) {
302 if(prefix.length > 0) {
303 this.setText(prefix);
310 parentElement.getAttrs().forEach(function(attr) {attrs[attr.name] = attr.value; });
311 var newElement = this.document.createDocumentNode({tagName: parentElement.getTagName(), attrs: attrs});
312 parentElement.after(newElement);
314 if(suffix.length > 0) {
315 newElement.append({text: suffix});
317 succeedingChildren.forEach(function(child) {
318 newElement.append(child);
321 return {first: parentElement, second: newElement};
324 divideWithElementNode: function(node, params) {
325 var insertion = this.getNodeInsertion(node),
326 myText = this.getText();
328 if(params.offset === myText.length) {
329 return this.after(node);
331 if(params.offset === 0) {
332 return this.before(node);
335 var lhsText = myText.substr(0, params.offset),
336 rhsText = myText.substr(params.offset),
337 rhsTextNode = this.document.createDocumentNode({text: rhsText});
339 this.setText(lhsText);
340 this.after(insertion.ofNode);
341 insertion.ofNode.after(rhsTextNode);
342 return insertion.ofNode;
346 var documentTransformations = {
347 wrapNodes: function(params) {
348 if(!(params.node1.parent().sameNode(params.node2.parent()))) {
349 throw new Error('Wrapping non-sibling nodes not supported.');
352 var parent = params.node1.parent(),
353 parentContents = parent.contents(),
354 wrapper = this.createDocumentNode({
355 tagName: params._with.tagName,
356 attrs: params._with.attrs}),
357 idx1 = parent.indexOf(params.node1),
358 idx2 = parent.indexOf(params.node2);
366 var insertingMethod, insertingTarget;
368 insertingMethod = 'prepend';
369 insertingTarget = parent;
371 insertingMethod = 'after';
372 insertingTarget = parentContents[idx1-1];
375 for(var i = idx1; i <= idx2; i++) {
376 wrapper.append(parentContents[i].detach());
379 insertingTarget[insertingMethod](wrapper);
383 _wrapText: function(params) {
384 params = _.extend({textNodeIdx: 0}, params);
385 if(typeof params.textNodeIdx === 'number') {
386 params.textNodeIdx = [params.textNodeIdx];
389 var contentsInside = params.inside.contents(),
390 idx1 = Math.min.apply(Math, params.textNodeIdx),
391 idx2 = Math.max.apply(Math, params.textNodeIdx),
392 textNode1 = contentsInside[idx1],
393 textNode2 = contentsInside[idx2],
394 sameNode = textNode1.sameNode(textNode2),
395 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
396 prefixInside = textNode1.getText().substr(params.offsetStart),
397 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
398 suffixOutside = textNode2.getText().substr(params.offsetEnd)
401 if(!(textNode1.parent().sameNode(textNode2.parent()))) {
402 throw new Error('Wrapping text in non-sibling text nodes not supported.');
405 var wrapperElement = this.createDocumentNode({tagName: params._with.tagName, attrs: params._with.attrs});
406 textNode1.after(wrapperElement);
409 if(prefixOutside.length > 0) {
410 wrapperElement.before({text:prefixOutside});
413 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
414 wrapperElement.append({text: core});
417 if(prefixInside.length > 0) {
418 wrapperElement.append({text: prefixInside});
420 for(var i = idx1 + 1; i < idx2; i++) {
421 wrapperElement.append(contentsInside[i]);
423 if(suffixInside.length > 0) {
424 wrapperElement.append({text: suffixInside});
427 if(suffixOutside.length > 0) {
428 wrapperElement.after({text: suffixOutside});
430 return wrapperElement;
432 replaceRoot: function(node) {
433 var insertion = this.getNodeInsertion(node);
435 this._defineDocumentProperties(insertion.ofNode._$);
436 insertion.ofNode.triggerChangeEvent('nodeAdded');
437 return insertion.ofNode;
439 deleteText: function(params) {
440 var ptr, next, toDetach, middle, text;
442 if(params.from.node.sameNode(params.to.node)) {
443 ptr = params.from.node;
444 text = ptr.getText();
445 ptr.setText(text.substr(0, params.from.offset) + text.substr(params.to.offset));
449 // Both edge text nodes need to be edited before anything else happen in case that
450 // they get merged when detaching content between them.
451 params.from.node.setText(params.from.node.getText().substr(0, params.from.offset));
452 params.to.node.setText(params.to.node.getText().substr(params.to.offset));
454 ptr = params.from.node;
457 while(next || ptr.parent()) {
459 if(next.sameNode(params.to.node)) {
462 else if(next.nodeType === Node.ELEMENT_NODE && next.containsNode(params.to.node)) {
476 if(!this.containsNode(params.to.node)) {
477 // The end node was merged during detaching nodes above - there is nothing more left to do.
481 ptr = middle.contents()[0];
482 while(ptr && !ptr.sameNode(params.to.node)) {
483 if(ptr.nodeType === Node.ELEMENT_NODE && ptr.containsNode(params.to.node)) {
484 ptr = ptr.contents()[0];
496 transformations: documentTransformations
499 transformations: documentNodeTransformations
502 transformations: elementNodeTransformations
505 transformations: textNodeTransformations