textproxy.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view */
  6. import TextProxy from '/ckeditor5/engine/view/textproxy.js';
  7. import Text from '/ckeditor5/engine/view/text.js';
  8. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  9. import DocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  10. import RootEditableElement from '/ckeditor5/engine/view/rooteditableelement.js';
  11. import createDocumentMock from '/tests/engine/view/_utils/createdocumentmock.js';
  12. describe( 'TextProxy', () => {
  13. let text, parent, wrapper, textProxy;
  14. beforeEach( () => {
  15. text = new Text( 'abcdefgh' );
  16. parent = new ContainerElement( 'p', [], [ text ] );
  17. wrapper = new ContainerElement( 'div', [], parent );
  18. textProxy = new TextProxy( text, 2, 3 );
  19. } );
  20. describe( 'constructor', () => {
  21. it( 'should create TextProxy instance with specified properties', () => {
  22. expect( textProxy ).to.have.property( 'parent' ).to.equal( parent );
  23. expect( textProxy ).to.have.property( 'data' ).to.equal( 'cde' );
  24. expect( textProxy ).to.have.property( 'textNode' ).to.equal( text );
  25. expect( textProxy ).to.have.property( 'index' ).to.equal( 2 );
  26. } );
  27. it( 'should get text from specified offset to the end of textNode if length is not defined', () => {
  28. textProxy = new TextProxy( text, 2 );
  29. expect( textProxy ).to.have.property( 'data' ).to.equal( 'cdefgh' );
  30. expect( textProxy ).to.have.property( 'index' ).to.equal( 2 );
  31. } );
  32. } );
  33. describe( 'getDocument', () => {
  34. it( 'should return null if any parent has not set Document', () => {
  35. expect( textProxy.getDocument() ).to.be.null;
  36. } );
  37. it( 'should return Document attached to the parent element', () => {
  38. const docMock = createDocumentMock();
  39. const root = new RootEditableElement( docMock, 'div' );
  40. wrapper.parent = root;
  41. expect( textProxy.getDocument() ).to.equal( docMock );
  42. } );
  43. it( 'should return null if element is inside DocumentFragment', () => {
  44. new DocumentFragment( [ wrapper ] );
  45. expect( textProxy.getDocument() ).to.be.null;
  46. } );
  47. } );
  48. describe( 'getRoot', () => {
  49. it( 'should return root element', () => {
  50. const root = new RootEditableElement( createDocumentMock(), 'div' );
  51. wrapper.parent = root;
  52. expect( textProxy.getRoot() ).to.equal( root );
  53. } );
  54. } );
  55. describe( 'getAncestors', () => {
  56. it( 'should return array of ancestors', () => {
  57. const result = textProxy.getAncestors();
  58. expect( result ).to.be.an( 'array' );
  59. expect( result ).to.length( 2 );
  60. expect( result[ 0 ] ).to.equal( wrapper );
  61. expect( result[ 1 ] ).to.equal( parent );
  62. } );
  63. it( 'should return array of ancestors starting from parent `parentFirst`', () => {
  64. const result = textProxy.getAncestors( { parentFirst: true } );
  65. expect( result.length ).to.equal( 2 );
  66. expect( result[ 0 ] ).to.equal( parent );
  67. expect( result[ 1 ] ).to.equal( wrapper );
  68. } );
  69. it( 'should return array including node itself `includeNode`', () => {
  70. const result = textProxy.getAncestors( { includeNode: true } );
  71. expect( result ).to.be.an( 'array' );
  72. expect( result ).to.length( 3 );
  73. expect( result[ 0 ] ).to.equal( wrapper );
  74. expect( result[ 1 ] ).to.equal( parent );
  75. expect( result[ 2 ] ).to.equal( text );
  76. } );
  77. it( 'should return array of ancestors including node itself `includeNode` starting from parent `parentFirst`', () => {
  78. const result = textProxy.getAncestors( { includeNode: true, parentFirst: true } );
  79. expect( result.length ).to.equal( 3 );
  80. expect( result[ 0 ] ).to.equal( text );
  81. expect( result[ 1 ] ).to.equal( parent );
  82. expect( result[ 2 ] ).to.equal( wrapper );
  83. } );
  84. } );
  85. } );