textproxy.js 3.7 KB

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