8 var INSERTION = function(implementation) {
9 var toret = function(node) {
10 var insertion = this.getNodeInsertion(node),
11 nodeWasContained = this.document.containsNode(insertion.ofNode),
13 if(!(this.document.containsNode(this))) {
14 nodeParent = insertion.ofNode.parent();
16 implementation.call(this, insertion.ofNode.nativeNode);
17 this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent, nodeWasContained);
18 return insertion.ofNode;
23 var documentNodeTransformations = {
25 var parent = this.parent();
27 this.triggerChangeEvent('nodeDetached', {parent: parent});
31 replaceWith: function(node) {
34 return this.document.replaceRoot(node);
36 toret = this.after(node);
41 after: INSERTION(function(nativeNode) {
42 return this._$.after(nativeNode);
45 before: INSERTION(function(nativeNode) {
46 return this._$.before(nativeNode);
49 wrapWith: function(node) {
50 var insertion = this.getNodeInsertion(node);
52 this.before(insertion.ofNode);
54 insertion.ofNode.append(this);
55 return insertion.ofNode;
59 * Removes parent of a node if node has no siblings.
65 var parent = this.parent(),
67 if(parent.contents().length === 1) {
68 grandParent = parent.parent();
69 parent.unwrapContent();
75 var elementNodeTransformations = {
79 if(this.parent() && this.isSurroundedByTextElements()) {
81 this.prev().appendText(next.getText());
84 return DocumentNode.prototype.detach.call(this);
87 setTag: function(tagName) {
88 var node = this.document.createDocumentNode({tagName: tagName}),
89 oldTagName = this.getTagName(),
90 myContents = this._$.contents();
92 this.getAttrs().forEach(function(attribute) {
93 node.setAttr(attribute.name, attribute.value, true);
95 node.setData(this.getData());
97 if(this.sameNode(this.document.root)) {
98 this.document._defineDocumentProperties(node._$);
100 this._$.replaceWith(node._$);
101 this._setNativeNode(node._$[0]);
102 this._$.append(myContents);
103 this.triggerChangeEvent('nodeTagChange', {oldTagName: oldTagName, newTagName: this.getTagName()});
107 setAttr: function(name, value, silent) {
108 var oldVal = this.getAttr(name);
109 this._$.attr(name, value);
111 this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value});
115 append: INSERTION(function(nativeNode) {
116 this._$.append(nativeNode);
119 prepend: INSERTION(function(nativeNode) {
120 this._$.prepend(nativeNode);
123 insertAtIndex: function(nativeNode, index) {
124 var contents = this.contents();
125 if(index < contents.length) {
126 return contents[index].before(nativeNode);
127 } else if(index === contents.length) {
128 return this.append(nativeNode);
132 unwrapContent: function() {
133 var parent = this.parent();
138 var myContents = this.contents(),
139 myIdx = parent.indexOf(this);
142 if(myContents.length === 0) {
143 return this.detach();
146 var prev = this.prev(),
148 moveLeftRange, moveRightRange, leftMerged;
150 if(prev && (prev.nodeType === TEXT_NODE) && (myContents[0].nodeType === TEXT_NODE)) {
151 prev.appendText(myContents[0].getText());
152 myContents[0].detach();
153 moveLeftRange = true;
159 if(!(leftMerged && myContents.length === 1)) {
160 var lastContents = _.last(myContents);
161 if(next && (next.nodeType === TEXT_NODE) && (lastContents.nodeType === TEXT_NODE)) {
162 next.prependText(lastContents.getText());
163 lastContents.detach();
164 moveRightRange = true;
168 var childrenLength = this.contents().length;
169 this.contents().forEach(function(child) {
176 element1: parent.contents()[myIdx + (moveLeftRange ? -1 : 0)],
177 element2: parent.contents()[myIdx + childrenLength-1 + (moveRightRange ? 1 : 0)]
181 wrapText: function(params) {
182 return this.document._wrapText(_.extend({inside: this}, params));
186 var textNodeTransformations = {
187 setText: function(text) {
188 //console.log('smartxml: ' + text);
189 this.nativeNode.data = text;
190 this.triggerTextChangeEvent();
193 appendText: function(text) {
194 this.nativeNode.data = this.nativeNode.data + text;
195 this.triggerTextChangeEvent();
198 prependText: function(text) {
199 this.nativeNode.data = text + this.nativeNode.data;
200 this.triggerTextChangeEvent();
203 wrapWith: function(desc) {
204 if(typeof desc.start === 'number' && typeof desc.end === 'number') {
205 return this.document._wrapText({
206 inside: this.parent(),
207 textNodeIdx: this.parent().indexOf(this),
208 offsetStart: Math.min(desc.start, desc.end),
209 offsetEnd: Math.max(desc.start, desc.end),
210 _with: {tagName: desc.tagName, attrs: desc.attrs}
213 return DocumentNode.prototype.wrapWith.call(this, desc);
217 split: function(params) {
218 var parentElement = this.parent(),
220 succeedingChildren = [],
221 prefix = this.getText().substr(0, params.offset),
222 suffix = this.getText().substr(params.offset);
224 parentElement.contents().forEach(function(child) {
226 succeedingChildren.push(child);
228 if(child.sameNode(this)) {
233 if(prefix.length > 0) {
234 this.setText(prefix);
241 parentElement.getAttrs().forEach(function(attr) {attrs[attr.name] = attr.value; });
242 var newElement = this.document.createDocumentNode({tagName: parentElement.getTagName(), attrs: attrs});
243 parentElement.after(newElement);
245 if(suffix.length > 0) {
246 newElement.append({text: suffix});
248 succeedingChildren.forEach(function(child) {
249 newElement.append(child);
252 return {first: parentElement, second: newElement};
256 var documentTransformations = {
257 wrapNodes: function(params) {
258 if(!(params.node1.parent().sameNode(params.node2.parent()))) {
259 throw new Error('Wrapping non-sibling nodes not supported.');
262 var parent = params.node1.parent(),
263 parentContents = parent.contents(),
264 wrapper = this.createDocumentNode({
265 tagName: params._with.tagName,
266 attrs: params._with.attrs}),
267 idx1 = parent.indexOf(params.node1),
268 idx2 = parent.indexOf(params.node2);
276 var insertingMethod, insertingTarget;
278 insertingMethod = 'prepend';
279 insertingTarget = parent;
281 insertingMethod = 'after';
282 insertingTarget = parentContents[idx1-1];
285 for(var i = idx1; i <= idx2; i++) {
286 wrapper.append(parentContents[i].detach());
289 insertingTarget[insertingMethod](wrapper);
293 _wrapText: function(params) {
294 params = _.extend({textNodeIdx: 0}, params);
295 if(typeof params.textNodeIdx === 'number') {
296 params.textNodeIdx = [params.textNodeIdx];
299 var contentsInside = params.inside.contents(),
300 idx1 = Math.min.apply(Math, params.textNodeIdx),
301 idx2 = Math.max.apply(Math, params.textNodeIdx),
302 textNode1 = contentsInside[idx1],
303 textNode2 = contentsInside[idx2],
304 sameNode = textNode1.sameNode(textNode2),
305 prefixOutside = textNode1.getText().substr(0, params.offsetStart),
306 prefixInside = textNode1.getText().substr(params.offsetStart),
307 suffixInside = textNode2.getText().substr(0, params.offsetEnd),
308 suffixOutside = textNode2.getText().substr(params.offsetEnd)
311 if(!(textNode1.parent().sameNode(textNode2.parent()))) {
312 throw new Error('Wrapping text in non-sibling text nodes not supported.');
315 var wrapperElement = this.createDocumentNode({tagName: params._with.tagName, attrs: params._with.attrs});
316 textNode1.after(wrapperElement);
319 if(prefixOutside.length > 0) {
320 wrapperElement.before({text:prefixOutside});
323 var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
324 wrapperElement.append({text: core});
327 if(prefixInside.length > 0) {
328 wrapperElement.append({text: prefixInside});
330 for(var i = idx1 + 1; i < idx2; i++) {
331 wrapperElement.append(contentsInside[i]);
333 if(suffixInside.length > 0) {
334 wrapperElement.append({text: suffixInside});
337 if(suffixOutside.length > 0) {
338 wrapperElement.after({text: suffixOutside});
340 return wrapperElement;
342 replaceRoot: function(node) {
343 var insertion = this.getNodeInsertion(node);
345 defineDocumentProperties(this, insertion.ofNode._$);
346 insertion.ofNode.triggerChangeEvent('nodeAdded');
347 return insertion.ofNode;
353 transformations: documentTransformations
356 transformations: documentNodeTransformations
359 transformations: elementNodeTransformations
362 transformations: textNodeTransformations