8
0

getancestors.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import getAncestors from '../../src/dom/getancestors';
  7. import createElement from '../../src/dom/createelement';
  8. describe( 'getAncestors', () => {
  9. it( 'should return all parents of given node and the node itself, starting from top-most parent', () => {
  10. // DIV
  11. // |- P (1)
  12. // | |- SPAN (1)
  13. // | |- B
  14. // |
  15. // |- P (2)
  16. // |- I
  17. const b = createElement( document, 'b' );
  18. const span = createElement( document, 'span', {}, [ b ] );
  19. const p1 = createElement( document, 'p', {}, [ span ] );
  20. const p2 = createElement( document, 'p', {}, [ createElement( document, 'i' ) ] );
  21. const div = createElement( document, 'div', {}, [ p1, p2 ] );
  22. expect( getAncestors( b ) ).to.deep.equal( [ div, p1, span, b ] );
  23. } );
  24. it( 'should not return document object', () => {
  25. const span = createElement( document, 'span' );
  26. document.documentElement.appendChild( span );
  27. const ancestors = getAncestors( span );
  28. expect( ancestors.includes( document ) ).to.be.false;
  29. } );
  30. it( 'should not return any non-Node, non-DocumentFragment object if given node is in iframe', () => {
  31. const iframe = document.createElement( 'iframe' );
  32. document.body.appendChild( iframe );
  33. const iframeDoc = iframe.contentWindow.document;
  34. const span = createElement( iframeDoc, 'span' );
  35. iframeDoc.documentElement.appendChild( span );
  36. const ancestors = getAncestors( span );
  37. expect( ancestors.includes( iframeDoc ) ).to.be.false;
  38. document.body.removeChild( iframe );
  39. } );
  40. } );