DocumentElement.childIndex
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 8 Jul 2013 10:30:34 +0000 (12:30 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 8 Jul 2013 10:30:34 +0000 (12:30 +0200)
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 778c4bf..a92ddb3 100644 (file)
@@ -20,6 +20,15 @@ describe('Canvas', function() {
             var c = canvas.fromXML('<section></section>');
             expect(c.doc().wlxmlTag).to.equal('section');
         });
+
+        describe('DocumentElement', function() {
+            it('knows index of its child', function() {
+                var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
+                    root = c.doc(),
+                    child = root.children()[1];
+                expect(root.childIndex(child)).to.equal(1);
+            });
+        });
     });
 
     describe('document representation api', function() {
index 5e3983e..d1973b8 100644 (file)
@@ -43,6 +43,18 @@ $.extend(DocumentElement.prototype, {
     wrapWithNodeElement: function(wlxmlNode) {
         this.$element.wrap($('<' + wlxmlNode.tag + ' class="' + wlxmlNode.klass + '"">')[0]);
         return documentElementFromHTMLElement(this.$element.parent().get(0));
+    },
+
+    childIndex: function(child) {
+        var children = this.children(),
+            toret = null;
+        children.forEach(function(c, idx) {
+            if(c.sameNode(child)) {
+                toret = idx;
+                return false;
+            }
+        });
+        return toret;
     }
 });