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))) {
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});
39 replaceWith: function(node) {
42 return this.document.replaceRoot(node);
44 toret = this.after(node);
49 after: INSERTION(function(node) {
50 var next = this.next();
51 if(next && next.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
52 next.setText(node.getText() + next.getText());
56 this._$.after(node.nativeNode);
60 before: INSERTION(function(node) {
61 var prev = this.prev();
62 if(prev && prev.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
63 prev.setText(prev.getText() + node.getText());
67 this._$.before(node.nativeNode);
71 wrapWith: function(node) {
72 var insertion = this.getNodeInsertion(node);
74 this.before(insertion.ofNode);
76 insertion.ofNode.append(this);
77 return insertion.ofNode;
81 * Removes parent of a node if node has no siblings.
87 var parent = this.parent(),
89 if(parent.contents().length === 1) {
90 grandParent = parent.parent();
91 parent.unwrapContent();
97 var elementNodeTransformations = {
101 if(this.parent() && this.isSurroundedByTextElements()) {
103 this.prev().appendText(next.getText());
106 return this.__super__.detach();
109 setTag: function(tagName) {
110 var node = this.document.createDocumentNode({tagName: tagName}),
111 oldTagName = this.getTagName(),
112 myContents = this._$.contents();
114 this.getAttrs().forEach(function(attribute) {
115 node.setAttr(attribute.name, attribute.value, true);
117 node.setData(this.getData());
119 if(this.sameNode(this.document.root)) {
120 this.document._defineDocumentProperties(node._$);
123 /* TODO: This invalidates old references to this node. Caching instances on nodes would fix this. */
124 this._$.replaceWith(node._$);
125 this._setNativeNode(node._$[0]);
126 this._$.append(myContents);
127 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
131 setAttr: function(name, value, silent) {
132 var oldVal = this.getAttr(name);
133 this._$.attr(name, value);
135 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
139 append: INSERTION(function(node) {
140 var last = _.last(this.contents());
141 if(last && last.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
142 last.setText(last.getText() + node.getText());
146 this._$.append(node.nativeNode);
151 prepend: INSERTION(function(node) {
152 var first = this.contents()[0];
153 if(first && first.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE) {
154 first.setText(node.getText() + first.getText());
158 this._$.prepend(node.nativeNode);
163 insertAtIndex: function(nativeNode, index) {
164 var contents = this.contents();
165 if(index < contents.length) {
166 return contents[index].before(nativeNode);
167 } else if(index === contents.length) {
168 return this.append(nativeNode);
172 unwrapContent: function() {
173 var parent = this.parent();
178 var myContents = this.contents(),
179 myIdx = parent.indexOf(this);
182 if(myContents.length === 0) {
183 return this.detach();
187 var childrenLength = this.contents().length,
190 this.contents().forEach(function(child) {
191 var returned = this.before(child);
192 if(first && !(returned.sameNode(child))) {
201 element1: parent.contents()[myIdx + (shiftRange ? -1 : 0)],
202 element2: parent.contents()[myIdx + childrenLength-1 + (shiftRange ? -1 : 0)]
206 wrapText: function(params) {
207 return this.document._wrapText(_.extend({inside: this}, params));
211 var textNodeTransformations = {
213 impl: function(t, text) {
214 t.oldText = this.getText();
215 this.nativeNode.data = text;
216 this.triggerTextChangeEvent();
219 this.setText(t.oldText);
223 before: INSERTION(function(node) {
224 if(node.nodeType === Node.TEXT_NODE) {
225 this.prependText(node.getText());
229 return this.__super__.before(node, {silent:true});
233 after: INSERTION(function(node) {
234 if(node.nodeType === Node.TEXT_NODE) {
235 this.appendText(node.getText());
239 return this.__super__.after(node, {silent:true});
243 appendText: function(text) {
244 this.nativeNode.data = this.nativeNode.data + text;
245 this.triggerTextChangeEvent();
248 prependText: function(text) {
249 this.nativeNode.data = text + this.nativeNode.data;
250 this.triggerTextChangeEvent();
253 wrapWith: function(desc) {
254 if(typeof desc.start === 'number' && typeof desc.end === 'number') {
255 return this.document._wrapText({
256 inside: this.parent(),
257 textNodeIdx: this.parent().indexOf(this),
258 offsetStart: Math.min(desc.start, desc.end),
259 offsetEnd: Math.max(desc.start, desc.end),
260 _with: {tagName: desc.tagName, attrs: desc.attrs}
263 return this.__super__.wrapWith.call(this, desc);
267 split: function(params) {
268 var parentElement = this.parent(),
270 succeedingChildren = [],
271 prefix = this.getText().substr(0, params.offset),
272 suffix = this.getText().substr(params.offset);
274 parentElement.contents().forEach(function(child) {
276 succeedingChildren.push(child);
278 if(child.sameNode(this)) {
283 if(prefix.length > 0) {
284 this.setText(prefix);
291 parentElement.getAttrs().forEach(function(attr) {attrs[attr.name] = attr.value; });
292 var newElement = this.document.createDocumentNode({tagName: parentElement.getTagName(), attrs: attrs});
293 parentElement.after(newElement);
295 if(suffix.length > 0) {
296 newElement.append({text: suffix});
298 succeedingChildren.forEach(function(child) {
299 newElement.append(child);
302 return {first: parentElement, second: newElement};
305 divideWithElementNode: function(node, params) {
306 var insertion = this.getNodeInsertion(node),
307 myText = this.getText();
309 if(params.offset === myText.length) {
310 return this.after(node);
312 if(params.offset === 0) {
313 return this.before(node);
316 var lhsText = myText.substr(0, params.offset),
317 rhsText = myText.substr(params.offset),
318 rhsTextNode = this.document.createDocumentNode({text: rhsText});
320 this.setText(lhsText);
321 this.after(insertion.ofNode);
322 insertion.ofNode.after(rhsTextNode);
323 return insertion.ofNode;
327 var documentTransformations = {
328 wrapNodes: function(params) {
329 if(!(params.node1.parent().sameNode(params.node2.parent()))) {
330 throw new Error('Wrapping non-sibling nodes not supported.');
333 var parent = params.node1.parent(),
334 parentContents = parent.contents(),
335 wrapper = this.createDocumentNode({
336 tagName: params._with.tagName,
337 attrs: params._with.attrs}),
338 idx1 = parent.indexOf(params.node1),
339 idx2 = parent.indexOf(params.node2);
347 var insertingMethod, insertingTarget;
349 insertingMethod = 'prepend';
350 insertingTarget = parent;
352 insertingMethod = 'after';
353 insertingTarget = parentContents[idx1-1];
356 for(var i = idx1; i <= idx2; i++) {
357 wrapper.append(parentContents[i].detach());
360 insertingTarget[insertingMethod](wrapper);
364 _wrapText: function(params) {
365 params = _.extend({textNodeIdx: 0}, params);
366 if(typeof params.textNodeIdx === 'number') {
367 params.textNodeIdx = [params.textNodeIdx];
370 var contentsInside = params.inside.contents(),
371 idx1 = Math.min.apply(Math, params.textNodeIdx),
372 idx2 = Math.max.apply(Math, params.textNodeIdx),
373 textNode1 = contentsInside[idx1],
374 textNode2 = contentsInside[idx2],
375 sameNode = textNode1.sameNode(textNode2),
376 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
377 prefixInside = textNode1.getText().substr(params.offsetStart),
378 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
379 suffixOutside = textNode2.getText().substr(params.offsetEnd)
382 if(!(textNode1.parent().sameNode(textNode2.parent()))) {
383 throw new Error('Wrapping text in non-sibling text nodes not supported.');
386 var wrapperElement = this.createDocumentNode({tagName: params._with.tagName, attrs: params._with.attrs});
387 textNode1.after(wrapperElement);
390 if(prefixOutside.length > 0) {
391 wrapperElement.before({text:prefixOutside});
394 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
395 wrapperElement.append({text: core});
398 if(prefixInside.length > 0) {
399 wrapperElement.append({text: prefixInside});
401 for(var i = idx1 + 1; i < idx2; i++) {
402 wrapperElement.append(contentsInside[i]);
404 if(suffixInside.length > 0) {
405 wrapperElement.append({text: suffixInside});
408 if(suffixOutside.length > 0) {
409 wrapperElement.after({text: suffixOutside});
411 return wrapperElement;
413 replaceRoot: function(node) {
414 var insertion = this.getNodeInsertion(node);
416 this._defineDocumentProperties(insertion.ofNode._$);
417 insertion.ofNode.triggerChangeEvent('nodeAdded');
418 return insertion.ofNode;
424 transformations: documentTransformations
427 transformations: documentNodeTransformations
430 transformations: elementNodeTransformations
433 transformations: textNodeTransformations