smartxml: fix in DocumentNode.getParentSiblings
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 11 Feb 2014 16:09:36 +0000 (17:09 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 11 Feb 2014 16:11:58 +0000 (17:11 +0100)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index 102a506..7eab11b 100644 (file)
@@ -373,13 +373,17 @@ $.extend(Document.prototype, Backbone.Events, {
             return noSiblingParents;
         }
 
-        var i;
-        for(i = 0; i < Math.min(parents1.length, parents2.length); i++) {
+        var stop = Math.min(parents1.length, parents2.length),
+            i;
+        for(i = 0; i < stop; i++) {
             if(parents1[i].sameNode(parents2[i])) {
                 continue;
             }
             break;
         }
+        if(i === stop) {
+            i--;
+        }
         return {node1: parents1[i], node2: parents2[i]};
     },
 
index 1e778ce..3410407 100644 (file)
@@ -1112,6 +1112,16 @@ describe('smartxml', function() {
                 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
                 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
             });
+
+            it('returns node itself for two same nodes', function() {
+                var doc = getDocumentFromXML('<section><div></div></section>'),
+                    div = doc.root.contents()[0];
+
+                var siblingParents = doc.getSiblingParents({node1: div, node2: div});
+                expect(!!siblingParents.node1 && !!siblingParents.node2).to.equal(true, 'nodes defined');
+                expect(siblingParents.node1.sameNode(div)).to.be.equal(true, 'node1');
+                expect(siblingParents.node2.sameNode(div)).to.be.equal(true, 'node2');
+            });
         });
     });