smartxml: DocumentNode.getPath
[fnpeditor.git] / src / smartxml / smartxml.test.js
index 822243d..180fa87 100644 (file)
@@ -61,6 +61,40 @@ describe('smartxml', function() {
         });
     });
 
+    describe('DocumentNode', function() {
+        it('can be cloned', function() {
+            var doc = getDocumentFromXML('<div>Alice</div>'),
+                text = doc.root.contents()[0],
+                clone, suffix;
+
+            [doc.root, text].forEach(function(node) {
+                suffix = ' (' + (node.nodeType === Node.TEXT_NODE ? 'text' : 'element')  + ')';
+                clone = node.clone();
+                expect(doc.containsNode(clone)).to.equal(false, 'clone is not contained in a document' + suffix);
+                expect(node.sameNode(clone)).to.equal(false, 'clone is not same node as its originator' + suffix);
+                expect(node.nativeNode.isEqualNode(clone.nativeNode)).to.equal(true, 'clone is identical as its originator' + suffix);
+            });
+        });
+
+        it('knows its path in the document tree', function() {
+            var doc = getDocumentFromXML('<root><a><b><c></c>text</b></a></root>'),
+                root = doc.root,
+                a = root.contents()[0],
+                b = a.contents()[0],
+                text = b.contents()[1];
+
+            expect(root.getPath()).to.eql([], 'path of the root element is empty');
+            expect(a.getPath()).to.eql([0]);
+            expect(b.getPath()).to.eql([0, 0]);
+            expect(text.getPath()).to.eql([0,0,1]);
+
+            /* Paths relative to a given ancestor */
+            expect(text.getPath(root)).to.eql([0,0,1]);
+            expect(text.getPath(a)).to.eql([0,1]);
+            expect(text.getPath(b)).to.eql([1]);
+        });
+    });
+
     describe('Basic ElementNode properties', function() {
         it('exposes node contents', function() {
             var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),