1 define(function(require) {
6 var _ = require('libs/underscore'),
7 TEXT_NODE = Node.TEXT_NODE;
10 var INSERTION = function(implementation) {
11 var toret = function(node, options) {
12 var insertion = this.getNodeInsertion(node),
13 nodeWasContained = this.document.containsNode(insertion.ofNode),
16 options = options || {};
17 if(!(this.document.containsNode(this))) {
18 nodeParent = insertion.ofNode.parent();
20 returned = implementation.call(this, insertion.ofNode);
21 if(!options.silent && returned.sameNode(insertion.ofNode)) {
22 this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent, nodeWasContained);
29 var documentNodeTransformations = {
31 var parent = this.parent();
33 this.triggerChangeEvent('nodeDetached', {parent: parent});
37 replaceWith: function(node) {
40 return this.document.replaceRoot(node);
42 toret = this.after(node);
47 after: INSERTION(function(node) {
48 var next = this.next();
49 if(next && next.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
50 next.setText(node.getText() + next.getText());
53 this._$.after(node.nativeNode);
57 before: INSERTION(function(node) {
58 var prev = this.prev();
59 if(prev && prev.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
60 prev.setText(prev.getText() + node.getText());
63 this._$.before(node.nativeNode);
67 wrapWith: function(node) {
68 var insertion = this.getNodeInsertion(node);
70 this.before(insertion.ofNode);
72 insertion.ofNode.append(this);
73 return insertion.ofNode;
77 * Removes parent of a node if node has no siblings.
83 var parent = this.parent(),
85 if(parent.contents().length === 1) {
86 grandParent = parent.parent();
87 parent.unwrapContent();
93 var elementNodeTransformations = {
97 if(this.parent() && this.isSurroundedByTextElements()) {
99 this.prev().appendText(next.getText());
102 return this.__super__.detach();
105 setTag: function(tagName) {
106 var node = this.document.createDocumentNode({tagName: tagName}),
107 oldTagName = this.getTagName(),
108 myContents = this._$.contents();
110 this.getAttrs().forEach(function(attribute) {
111 node.setAttr(attribute.name, attribute.value, true);
113 node.setData(this.getData());
115 if(this.sameNode(this.document.root)) {
116 this.document._defineDocumentProperties(node._$);
119 /* TODO: This invalidates old references to this node. Caching instances on nodes would fix this. */
120 this._$.replaceWith(node._$);
121 this._setNativeNode(node._$[0]);
122 this._$.append(myContents);
123 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
127 setAttr: function(name, value, silent) {
128 var oldVal = this.getAttr(name);
129 this._$.attr(name, value);
131 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
135 append: INSERTION(function(node) {
136 var last = _.last(this.contents());
137 if(last && last.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
138 last.setText(last.getText() + node.getText());
141 this._$.append(node.nativeNode);
146 prepend: INSERTION(function(node) {
147 var first = this.contents()[0];
148 if(first && first.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
149 first.setText(node.getText() + first.getText());
152 this._$.prepend(node.nativeNode);
157 insertAtIndex: function(nativeNode, index) {
158 var contents = this.contents();
159 if(index < contents.length) {
160 return contents[index].before(nativeNode);
161 } else if(index === contents.length) {
162 return this.append(nativeNode);
166 unwrapContent: function() {
167 var parent = this.parent();
172 var myContents = this.contents(),
173 myIdx = parent.indexOf(this);
176 if(myContents.length === 0) {
177 return this.detach();
180 var prev = this.prev(),
182 shiftRange, leftMerged;
184 if(prev && (prev.nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) {
185 prev.appendText(myContents[0].getText());
186 myContents[0].detach();
193 if(!(leftMerged && myContents.length === 1)) {
194 var lastContents = _.last(myContents);
195 if(next && (next.nodeType === TEXT_NODE) && (lastContents.nodeType === TEXT_NODE)) {
196 next.prependText(lastContents.getText());
197 lastContents.detach();
201 var childrenLength = this.contents().length;
202 this.contents().forEach(function(child) {
209 element1: parent.contents()[myIdx + (shiftRange ? -1 : 0)],
210 element2: parent.contents()[myIdx + childrenLength-1 + (shiftRange ? -1 : 0)]
214 wrapText: function(params) {
215 return this.document._wrapText(_.extend({inside: this}, params));
219 var textNodeTransformations = {
221 impl: function(t, text) {
222 t.oldText = this.getText();
223 this.nativeNode.data = text;
224 this.triggerTextChangeEvent();
227 this.setText(t.oldText);
231 before: INSERTION(function(node) {
232 if(node.nodeType === Node.TEXT_NODE) {
233 this.prependText(node.getText());
236 return this.__super__.before(node, {silent:true});
240 after: INSERTION(function(node) {
241 if(node.nodeType === Node.TEXT_NODE) {
242 this.appendText(node.getText());
245 return this.__super__.after(node, {silent:true});
249 appendText: function(text) {
250 this.nativeNode.data = this.nativeNode.data + text;
251 this.triggerTextChangeEvent();
254 prependText: function(text) {
255 this.nativeNode.data = text + this.nativeNode.data;
256 this.triggerTextChangeEvent();
259 wrapWith: function(desc) {
260 if(typeof desc.start === 'number' && typeof desc.end === 'number') {
261 return this.document._wrapText({
262 inside: this.parent(),
263 textNodeIdx: this.parent().indexOf(this),
264 offsetStart: Math.min(desc.start, desc.end),
265 offsetEnd: Math.max(desc.start, desc.end),
266 _with: {tagName: desc.tagName, attrs: desc.attrs}
269 return this.__super__.wrapWith.call(this, desc);
273 split: function(params) {
274 var parentElement = this.parent(),
276 succeedingChildren = [],
277 prefix = this.getText().substr(0, params.offset),
278 suffix = this.getText().substr(params.offset);
280 parentElement.contents().forEach(function(child) {
282 succeedingChildren.push(child);
284 if(child.sameNode(this)) {
289 if(prefix.length > 0) {
290 this.setText(prefix);
297 parentElement.getAttrs().forEach(function(attr) {attrs[attr.name] = attr.value; });
298 var newElement = this.document.createDocumentNode({tagName: parentElement.getTagName(), attrs: attrs});
299 parentElement.after(newElement);
301 if(suffix.length > 0) {
302 newElement.append({text: suffix});
304 succeedingChildren.forEach(function(child) {
305 newElement.append(child);
308 return {first: parentElement, second: newElement};
311 divideWithElementNode: function(node, params) {
312 var insertion = this.getNodeInsertion(node),
313 myText = this.getText();
315 if(params.offset === myText.length) {
316 return this.after(node);
318 if(params.offset === 0) {
319 return this.before(node);
322 var lhsText = myText.substr(0, params.offset),
323 rhsText = myText.substr(params.offset),
324 rhsTextNode = this.document.createDocumentNode({text: rhsText});
326 this.setText(lhsText);
327 this.after(insertion.ofNode);
328 insertion.ofNode.after(rhsTextNode);
329 return insertion.ofNode;
333 var documentTransformations = {
334 wrapNodes: function(params) {
335 if(!(params.node1.parent().sameNode(params.node2.parent()))) {
336 throw new Error('Wrapping non-sibling nodes not supported.');
339 var parent = params.node1.parent(),
340 parentContents = parent.contents(),
341 wrapper = this.createDocumentNode({
342 tagName: params._with.tagName,
343 attrs: params._with.attrs}),
344 idx1 = parent.indexOf(params.node1),
345 idx2 = parent.indexOf(params.node2);
353 var insertingMethod, insertingTarget;
355 insertingMethod = 'prepend';
356 insertingTarget = parent;
358 insertingMethod = 'after';
359 insertingTarget = parentContents[idx1-1];
362 for(var i = idx1; i <= idx2; i++) {
363 wrapper.append(parentContents[i].detach());
366 insertingTarget[insertingMethod](wrapper);
370 _wrapText: function(params) {
371 params = _.extend({textNodeIdx: 0}, params);
372 if(typeof params.textNodeIdx === 'number') {
373 params.textNodeIdx = [params.textNodeIdx];
376 var contentsInside = params.inside.contents(),
377 idx1 = Math.min.apply(Math, params.textNodeIdx),
378 idx2 = Math.max.apply(Math, params.textNodeIdx),
379 textNode1 = contentsInside[idx1],
380 textNode2 = contentsInside[idx2],
381 sameNode = textNode1.sameNode(textNode2),
382 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
383 prefixInside = textNode1.getText().substr(params.offsetStart),
384 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
385 suffixOutside = textNode2.getText().substr(params.offsetEnd)
388 if(!(textNode1.parent().sameNode(textNode2.parent()))) {
389 throw new Error('Wrapping text in non-sibling text nodes not supported.');
392 var wrapperElement = this.createDocumentNode({tagName: params._with.tagName, attrs: params._with.attrs});
393 textNode1.after(wrapperElement);
396 if(prefixOutside.length > 0) {
397 wrapperElement.before({text:prefixOutside});
400 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
401 wrapperElement.append({text: core});
404 if(prefixInside.length > 0) {
405 wrapperElement.append({text: prefixInside});
407 for(var i = idx1 + 1; i < idx2; i++) {
408 wrapperElement.append(contentsInside[i]);
410 if(suffixInside.length > 0) {
411 wrapperElement.append({text: suffixInside});
414 if(suffixOutside.length > 0) {
415 wrapperElement.after({text: suffixOutside});
417 return wrapperElement;
419 replaceRoot: function(node) {
420 var insertion = this.getNodeInsertion(node);
422 this._defineDocumentProperties(insertion.ofNode._$);
423 insertion.ofNode.triggerChangeEvent('nodeAdded');
424 return insertion.ofNode;
430 transformations: documentTransformations
433 transformations: documentNodeTransformations
436 transformations: elementNodeTransformations
439 transformations: textNodeTransformations