8
0

getancestors.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 getAncestors from 'ckeditor5/utils/dom/getancestors.js';
  8. import createElement from 'ckeditor5/utils/dom/createelement.js';
  9. describe( 'getAncestors', () => {
  10. it( 'should return all parents of given node and the node itself, starting from top-most parent', () => {
  11. // DIV
  12. // |- P (1)
  13. // | |- SPAN (1)
  14. // | |- B
  15. // |
  16. // |- P (2)
  17. // |- I
  18. const b = createElement( document, 'b' );
  19. const span = createElement( document, 'span', {}, [ b ] );
  20. const p1 = createElement( document, 'p', {}, [ span ] );
  21. const p2 = createElement( document, 'p', {}, [ createElement( document, 'i' ) ] );
  22. const div = createElement( document, 'div', {}, [ p1, p2 ] );
  23. expect( getAncestors( b ) ).to.deep.equal( [ div, p1, span, b ] );
  24. } );
  25. it( 'should not return document object', () => {
  26. const span = createElement( document, 'span' );
  27. document.documentElement.appendChild( span );
  28. const ancestors = getAncestors( span );
  29. expect( ancestors.includes( document ) ).to.be.false;
  30. } );
  31. it( 'should not return any non-Node, non-DocumentFragment object if given node is in iframe', () => {
  32. const iframe = document.createElement( 'iframe' );
  33. document.body.appendChild( iframe );
  34. const iframeDoc = iframe.contentWindow.document;
  35. const span = createElement( iframeDoc, 'span' );
  36. iframeDoc.documentElement.appendChild( span );
  37. const ancestors = getAncestors( span );
  38. expect( ancestors.includes( iframeDoc ) ).to.be.false;
  39. document.body.removeChild( iframe );
  40. } );
  41. } );