smartxml: additional text nodes merge strategies for ElementNode.detach
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 30 Jun 2014 12:47:57 +0000 (14:47 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 30 Jun 2014 13:02:11 +0000 (15:02 +0200)
src/smartxml/core.js

index 7118584..b40e9c3 100644 (file)
@@ -120,12 +120,24 @@ var documentNodeTransformations = {
 
 var elementNodeTransformations = {
 
-    detach: function() {
+    detach: function(params) {
         var next;
+        params = _.extend({
+            normalizeStrategy: 'merge'
+        }, params);
+
         if(this.parent() && this.isSurroundedByTextNodes()) {
-            next = this.next();
-            this.prev().appendText(next.getText());
-            next.detach();
+            if(params.normalizeStrategy === 'detach-left') {
+                this.prev().detach();
+            } else if(params.normalizeStrategy === 'detach-right') {
+                this.next().detach();
+            } else if(params.normalizeStrategy === 'merge') {
+                next = this.next();
+                this.prev().appendText(next.getText());
+                next.detach();
+            } else {
+                throw new Error('unknown normalize strategy for detach');
+            }
         }
         return this.__super__.detach();
     },