@@ -29,7 +29,7 @@ describe( 'Node', () => {
2929 root = new Element ( null , null , [ one , two , three ] ) ;
3030 } ) ;
3131
32- describe ( 'getNextSibling/getPreviousSibling' , ( ) => {
32+ describe ( 'getNextSibling/getPreviousSibling() ' , ( ) => {
3333 it ( 'should return next sibling' , ( ) => {
3434 expect ( root . nextSibling ) . to . be . null ;
3535
@@ -57,7 +57,7 @@ describe( 'Node', () => {
5757 } ) ;
5858 } ) ;
5959
60- describe ( 'getAncestors' , ( ) => {
60+ describe ( 'getAncestors() ' , ( ) => {
6161 it ( 'should return empty array for node without ancestors' , ( ) => {
6262 const result = root . getAncestors ( ) ;
6363 expect ( result ) . to . be . an ( 'array' ) ;
@@ -109,7 +109,58 @@ describe( 'Node', () => {
109109 } ) ;
110110 } ) ;
111111
112- describe ( 'getIndex' , ( ) => {
112+ describe ( 'getCommonAncestor()' , ( ) => {
113+ it ( 'should return the parent element for the same node' , ( ) => {
114+ expect ( img . getCommonAncestor ( img ) ) . to . equal ( two ) ;
115+ } ) ;
116+
117+ it ( 'should return null for detached subtrees' , ( ) => {
118+ const detached = new Element ( 'foo' ) ;
119+
120+ expect ( img . getCommonAncestor ( detached ) ) . to . be . null ;
121+ expect ( detached . getCommonAncestor ( img ) ) . to . be . null ;
122+ } ) ;
123+
124+ it ( 'should return null when one of the nodes is a tree root itself' , ( ) => {
125+ expect ( root . getCommonAncestor ( img ) ) . to . be . null ;
126+ expect ( img . getCommonAncestor ( root ) ) . to . be . null ;
127+ expect ( root . getCommonAncestor ( root ) ) . to . be . null ;
128+ } ) ;
129+
130+ it ( 'should return parent of the nodes at the same level' , ( ) => {
131+ expect ( img . getCommonAncestor ( charA ) ) . to . equal ( two ) ;
132+ expect ( charB . getCommonAncestor ( charA ) ) . to . equal ( two ) ;
133+ } ) ;
134+
135+ it ( 'should return proper element for nodes in different branches and on different levels' , ( ) => {
136+ const foo = new Text ( 'foo' ) ;
137+ const bar = new Text ( 'bar' ) ;
138+ const bom = new Text ( 'bom' ) ;
139+ const d = new Element ( 'd' , null , [ bar ] ) ;
140+ const c = new Element ( 'c' , null , [ foo , d ] ) ;
141+ const b = new Element ( 'b' , null , [ c ] ) ;
142+ const e = new Element ( 'e' , null , [ bom ] ) ;
143+ const a = new Element ( 'a' , null , [ b , e ] ) ;
144+
145+ // <a><b><c>foo<d>bar</d></c></b><e>bom</e></a>
146+
147+ expect ( bar . getCommonAncestor ( foo ) , 1 ) . to . equal ( c ) ;
148+ expect ( foo . getCommonAncestor ( d ) , 2 ) . to . equal ( c ) ;
149+ expect ( c . getCommonAncestor ( b ) , 3 ) . to . equal ( a ) ;
150+ expect ( bom . getCommonAncestor ( d ) , 4 ) . to . equal ( a ) ;
151+ expect ( b . getCommonAncestor ( bom ) , 5 ) . to . equal ( a ) ;
152+ } ) ;
153+
154+ it ( 'should return document fragment' , ( ) => {
155+ const foo = new Text ( 'foo' ) ;
156+ const bar = new Text ( 'bar' ) ;
157+ const df = new DocumentFragment ( [ foo , bar ] ) ;
158+
159+ expect ( foo . getCommonAncestor ( bar ) ) . to . equal ( df ) ;
160+ } ) ;
161+ } ) ;
162+
163+ describe ( 'getIndex()' , ( ) => {
113164 it ( 'should return null if the parent is null' , ( ) => {
114165 expect ( root . index ) . to . be . null ;
115166 } ) ;
@@ -139,7 +190,7 @@ describe( 'Node', () => {
139190 } ) ;
140191 } ) ;
141192
142- describe ( 'getDocument' , ( ) => {
193+ describe ( 'getDocument() ' , ( ) => {
143194 it ( 'should return null if any parent has not set Document' , ( ) => {
144195 expect ( charA . document ) . to . be . null ;
145196 } ) ;
@@ -164,7 +215,7 @@ describe( 'Node', () => {
164215 } ) ;
165216 } ) ;
166217
167- describe ( 'getRoot' , ( ) => {
218+ describe ( 'getRoot() ' , ( ) => {
168219 it ( 'should return this element if it has no parent' , ( ) => {
169220 const child = new Element ( 'p' ) ;
170221
@@ -183,7 +234,7 @@ describe( 'Node', () => {
183234 } ) ;
184235 } ) ;
185236
186- describe ( 'remove' , ( ) => {
237+ describe ( 'remove() ' , ( ) => {
187238 it ( 'should remove node from its parent' , ( ) => {
188239 const char = new Text ( 'a' ) ;
189240 const parent = new Element ( 'p' , null , [ char ] ) ;
@@ -246,7 +297,7 @@ describe( 'Node', () => {
246297 sinon . assert . calledWith ( rootChangeSpy , 'attributes' , img ) ;
247298 } ) ;
248299
249- describe ( 'setAttribute' , ( ) => {
300+ describe ( 'setAttribute() ' , ( ) => {
250301 it ( 'should fire change event' , ( ) => {
251302 img . setAttribute ( 'width' , 100 ) ;
252303
@@ -255,7 +306,7 @@ describe( 'Node', () => {
255306 } ) ;
256307 } ) ;
257308
258- describe ( 'removeAttribute' , ( ) => {
309+ describe ( 'removeAttribute() ' , ( ) => {
259310 it ( 'should fire change event' , ( ) => {
260311 img . removeAttribute ( 'src' ) ;
261312
@@ -264,7 +315,7 @@ describe( 'Node', () => {
264315 } ) ;
265316 } ) ;
266317
267- describe ( 'insertChildren' , ( ) => {
318+ describe ( 'insertChildren() ' , ( ) => {
268319 it ( 'should fire change event' , ( ) => {
269320 root . insertChildren ( 1 , new Element ( 'img' ) ) ;
270321
@@ -273,7 +324,7 @@ describe( 'Node', () => {
273324 } ) ;
274325 } ) ;
275326
276- describe ( 'appendChildren' , ( ) => {
327+ describe ( 'appendChildren() ' , ( ) => {
277328 it ( 'should fire change event' , ( ) => {
278329 root . appendChildren ( new Element ( 'img' ) ) ;
279330
@@ -282,7 +333,7 @@ describe( 'Node', () => {
282333 } ) ;
283334 } ) ;
284335
285- describe ( 'removeChildren' , ( ) => {
336+ describe ( 'removeChildren() ' , ( ) => {
286337 it ( 'should fire change event' , ( ) => {
287338 root . removeChildren ( 1 , 1 ) ;
288339
@@ -291,7 +342,7 @@ describe( 'Node', () => {
291342 } ) ;
292343 } ) ;
293344
294- describe ( 'removeChildren' , ( ) => {
345+ describe ( 'removeChildren() ' , ( ) => {
295346 it ( 'should fire change event' , ( ) => {
296347 text . data = 'bar' ;
297348
0 commit comments