treewalker-utils.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewAttributeElement from '../../src/view/attributeelement';
  6. import ViewContainerElement from '../../src/view/containerelement';
  7. import ViewText from '../../src/view/text';
  8. import { getTouchingTextNode } from '../../src/view/treewalker-utils';
  9. describe( 'getTouchingTextNode()', () => {
  10. let a, b, x, y, z;
  11. beforeEach( () => {
  12. a = new ViewText( 'a' );
  13. b = new ViewText( 'b' );
  14. x = new ViewText( 'x' );
  15. y = new ViewText( 'y' );
  16. z = new ViewText( 'z' );
  17. // <div><p>ab</p><p><em><strong>x</strong></em>y</p></div>
  18. /* eslint-disable no-new */
  19. new ViewContainerElement( 'div', null, [
  20. new ViewContainerElement( 'p', null, [ a, b ] ),
  21. new ViewContainerElement( 'p', null, [
  22. new ViewAttributeElement( 'em', null, new ViewAttributeElement( 'strong', null, x ) ),
  23. y
  24. ] ),
  25. z,
  26. new ViewContainerElement( 'p', null, [ new ViewText( '_' ) ] )
  27. ] );
  28. } );
  29. it( 'should return next touching view text node when direction is not specified', () => {
  30. expect( getTouchingTextNode( a ) ).to.equal( b );
  31. } );
  32. it( 'should return next touching view text node', () => {
  33. expect( getTouchingTextNode( a, 'forward' ) ).to.equal( b );
  34. } );
  35. it( 'should return previous touching view text node', () => {
  36. expect( getTouchingTextNode( b, 'backward' ) ).to.equal( a );
  37. } );
  38. it( 'should go outside of attribute element looking for text nodes', () => {
  39. expect( getTouchingTextNode( x, 'forward' ) ).to.equal( y );
  40. } );
  41. it( 'should go inside attribute element looking for text nodes', () => {
  42. expect( getTouchingTextNode( y, 'backward' ) ).to.equal( x );
  43. } );
  44. it( 'should return null if there is no next text node in given container element', () => {
  45. expect( getTouchingTextNode( b, 'forward' ) ).to.be.null;
  46. expect( getTouchingTextNode( y, 'forward' ) ).to.be.null;
  47. } );
  48. it( 'should return null if there is no previous text node in given container element', () => {
  49. expect( getTouchingTextNode( a, 'backward' ) ).to.be.null;
  50. expect( getTouchingTextNode( x, 'backward' ) ).to.be.null;
  51. } );
  52. it( 'should not enter container element when looking for touching text node', () => {
  53. expect( getTouchingTextNode( z ) ).to.be.null;
  54. expect( getTouchingTextNode( z, 'backward' ) ).to.be.null;
  55. } );
  56. } );