From: Aleksander Ɓukasz Date: Tue, 6 Aug 2013 10:07:46 +0000 (+0200) Subject: canvas api: getting sibling parents of two elements X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/808306985627fd87f75a1b61fbd2d2b0fa61b186 canvas api: getting sibling parents of two elements --- diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index 9ce8a72..27f752d 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -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); diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index ded28c2..b327bce 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -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('
\ +
\ +
A
\ +
B
\ +
\ +
'), + 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('
Alice has a cat
'), + 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('
Alice has a cat
'),