8
0

getcommonancestor.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: dom, browser-only */
  7. import getCommonAncestor from 'ckeditor5/utils/dom/getcommonancestor.js';
  8. import createElement from 'ckeditor5/utils/dom/createelement.js';
  9. describe( 'getParents', () => {
  10. let b, span1, span2, p1, p2, i, div;
  11. beforeEach( () => {
  12. // DIV
  13. // |- P (1)
  14. // | |- SPAN (1)
  15. // | | |- B
  16. // | |
  17. // | |- SPAN (2)
  18. // |
  19. // |- P (2)
  20. // |- I
  21. b = createElement( document, 'b' );
  22. span1 = createElement( document, 'span', {}, [ b ] );
  23. span2 = createElement( document, 'span' );
  24. p1 = createElement( document, 'p', {}, [ span1, span2 ] );
  25. i = createElement( document, 'i' );
  26. p2 = createElement( document, 'p', {}, [ i ] );
  27. div = createElement( document, 'div', {}, [ p1, p2 ] );
  28. } );
  29. function test( a, b, lca ) {
  30. expect( getCommonAncestor( a, b ) ).to.equal( lca );
  31. expect( getCommonAncestor( b, a ) ).to.equal( lca );
  32. }
  33. it( 'should return lowest common ancestor of nodes in different tree branches', () => {
  34. test( p1, p2, div );
  35. test( span1, span2, p1 );
  36. test( b, span2, p1 );
  37. test( i, b, div );
  38. } );
  39. it( 'should return one of nodes if it is a parent of another node', () => {
  40. test( div, p1, div );
  41. test( p1, b, p1 );
  42. } );
  43. it( 'should return the node if both parameters are same', () => {
  44. test( div, div, div );
  45. test( b, b, b );
  46. } );
  47. it( 'should return null for nodes that do not have common ancestor (different trees)', () => {
  48. const diffB = createElement( document, 'b' );
  49. const diffDiv = createElement( document, 'div', {}, diffB );
  50. test( diffB, span1, null );
  51. test( diffDiv, p1, null );
  52. } );
  53. } );