canvas api: getting sibling parents of two elements
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 6 Aug 2013 10:07:46 +0000 (12:07 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 6 Aug 2013 10:07:46 +0000 (12:07 +0200)
modules/documentCanvas/canvas/canvas.js
modules/documentCanvas/canvas/canvas.test3.js

index 9ce8a72..27f752d 100644 (file)
@@ -319,6 +319,23 @@ $.extend(Canvas.prototype, {
             wrapperElement.after({text: suffixOutside});
         return wrapperElement;
     },
+    getSiblingParents: function(params) {
+        var parents1 = [params.element1].concat(params.element1.parents()).reverse(),
+            parents2 = [params.element2].concat(params.element2.parents()).reverse(),
+            noSiblingParents = null;
+
+        if(parents1.length === 0 || parents2.length === 0 || !(parents1[0].sameNode(parents2[0])))
+            return noSiblingParents;
+
+        var i;
+        for(i = 0; i < Math.min(parents1.length, parents2.length); i++) {
+            if(parents1[i].sameNode(parents2[i]))
+                continue;
+            break;
+        }
+        return {element1: parents1[i], element2: parents2[i]};
+    },
+
     getDocumentElement: function(from) {
         if(from instanceof HTMLElement || from instanceof Text) {
            return documentElement.DocumentElement.fromHTMLElement(from, this);
index ded28c2..b327bce 100644 (file)
@@ -207,6 +207,39 @@ describe('Canvas', function() {
                 });
             });
 
+            describe('accessing sibling parents of two elements', function() {
+                it('returns elements themself if they have direct common parent', function() {
+                    var c = canvas.fromXML('<section>\
+                        <div>\
+                            <div>A</div>\
+                            <div>B</div>\
+                        </div>\
+                    </section>'),
+                        section = c.doc(),
+                        wrappingDiv = c.doc().children()[0],
+                        divA = wrappingDiv.children()[0],
+                        divB = wrappingDiv.children()[1];
+
+                    var siblingParents = c.getSiblingParents({element1: divA, element2: divB});
+
+                    expect(siblingParents.element1.sameNode(divA)).to.equal(true, 'divA');
+                    expect(siblingParents.element2.sameNode(divB)).to.equal(true, 'divB');
+                });
+
+                it('returns sibling parents - example 1', function() {
+                    var c = canvas.fromXML('<section>Alice <span>has a cat</span></section>'),
+                        section = c.doc(),
+                        aliceText = section.children()[0],
+                        span = section.children()[1],
+                        spanText = span.children()[0];
+
+                    var siblingParents = c.getSiblingParents({element1: aliceText, element2: spanText});
+
+                    expect(siblingParents.element1.sameNode(aliceText)).to.equal(true, 'aliceText');
+                    expect(siblingParents.element2.sameNode(span)).to.equal(true, 'span');
+                });
+            })
+
             describe('free text handling', function() {
                     it('sees free text', function() {
                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),