smartxml: refactor
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 19 Nov 2013 16:10:31 +0000 (17:10 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 19 Nov 2013 16:10:31 +0000 (17:10 +0100)
src/smartxml/smartxml.js

index 1d9fdd0..637fda6 100644 (file)
@@ -82,6 +82,12 @@ $.extend(DocumentNode.prototype, {
         return myIdx < parentContents.length - 1 ? parentContents[myIdx+1] : null;
     },
 
         return myIdx < parentContents.length - 1 ? parentContents[myIdx+1] : null;
     },
 
+    isSurroundedByTextElements: function() {
+        var prev = this.prev(),
+            next = this.next();
+        return prev && (prev.nodeType === Node.TEXT_NODE) && next && (next.nodeType === Node.TEXT_NODE);
+    },
+
     after: INSERTION(function(nativeNode) {
         return this._$.after(nativeNode);
     }),
     after: INSERTION(function(nativeNode) {
         return this._$.after(nativeNode);
     }),
@@ -151,13 +157,11 @@ $.extend(ElementNode.prototype, {
     nodeType: Node.ELEMENT_NODE,
 
     detach: function() {
     nodeType: Node.ELEMENT_NODE,
 
     detach: function() {
-        var prev = this.prev(),
+        var next;
+        if(parent && this.isSurroundedByTextElements()) {
             next = this.next();
             next = this.next();
-        if(parent) {
-            if(prev && prev.nodeType === Node.TEXT_NODE && next && next.nodeType === Node.TEXT_NODE) {
-                prev.appendText(next.getText());
-                next.detach();
-            }
+            this.prev().appendText(next.getText());
+            next.detach();
         }
         return DocumentNode.prototype.detach.call(this);
     },
         }
         return DocumentNode.prototype.detach.call(this);
     },