smartxml: fix - handle creation of an empty text node
[fnpeditor.git] / src / smartxml / smartxml.test.js
index 09fdfeb..a0c1af4 100644 (file)
@@ -51,6 +51,14 @@ describe('smartxml', function() {
             expect(doc.containsNode(a)).to.equal(true, 'contains Element Node');
             expect(doc.containsNode(text)).to.equal(true, 'contains Text Node');
         });
+
+        it('creates text nodes', function() {
+            var doc = getDocumentFromXML('<div></div>'),
+                emptyTextNode = doc.createDocumentNode({text:''}),
+                nonEmptyTextNode = doc.createDocumentNode({text: 'alice'});
+            expect(emptyTextNode.getText()).to.equal('', 'empty ok');
+            expect(nonEmptyTextNode.getText()).to.equal('alice', 'non empty ok');
+        });
     });
 
     describe('Basic ElementNode properties', function() {
@@ -262,6 +270,17 @@ describe('smartxml', function() {
 
     describe('Manipulations', function() {
 
+        it('merges adjacent text nodes resulting from detaching an element node in between', function() {
+            var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
+                span = doc.root.contents()[1];
+
+            span.detach();
+
+            var rootContents = doc.root.contents();
+            expect(rootContents).to.have.length(1, 'one child left');
+            expect(rootContents[0].getText()).to.equal('Alice a cat');
+        });
+
         it('appends element node to another element node', function() {
             var node1 = elementNodeFromParams({tag: 'div'}),
                 node2 = elementNodeFromParams({tag: 'a'}),
@@ -302,7 +321,7 @@ describe('smartxml', function() {
             expect(node.contents()[2].getText()).to.equal(' a cat!');
         });
 
-        it('unwrap single node from its parent', function() {
+        it('unwrap single element node from its parent', function() {
             var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
                 div = doc.root,
                 a = div.contents()[0],
@@ -315,6 +334,19 @@ describe('smartxml', function() {
             expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
         });
 
+        it('unwrap single text node from its parent', function() {
+            var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
+                div = doc.root,
+                span = div.contents()[1],
+                text = span.contents()[0];
+
+            var parent = text.unwrap();
+
+            expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
+            expect(div.contents()).to.have.length(1, 'root contains only one node');
+            expect(div.contents()[0].getText()).to.equal('Some text!');
+        });
+
         describe('Wrapping text', function() {
             it('wraps text spanning multiple sibling TextNodes', function() {
                 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),