8
0

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. 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( 'offsetInText' ).to.equal( 2 );
  26. } );
  27. it( 'should have isPartial property', () => {
  28. let startTextProxy = new TextProxy( text, 0, 4 );
  29. let fullTextProxy = new TextProxy( text, 0, 8 );
  30. expect( textProxy.isPartial ).to.be.true;
  31. expect( startTextProxy.isPartial ).to.be.true;
  32. expect( fullTextProxy.isPartial ).to.be.false;
  33. } );
  34. } );
  35. describe( 'getDocument', () => {
  36. it( 'should return null if any parent has not set Document', () => {
  37. expect( textProxy.getDocument() ).to.be.null;
  38. } );
  39. it( 'should return Document attached to the parent element', () => {
  40. const docMock = createDocumentMock();
  41. const root = new RootEditableElement( docMock, 'div' );
  42. wrapper.parent = root;
  43. expect( textProxy.getDocument() ).to.equal( docMock );
  44. } );
  45. it( 'should return null if element is inside DocumentFragment', () => {
  46. new DocumentFragment( [ wrapper ] );
  47. expect( textProxy.getDocument() ).to.be.null;
  48. } );
  49. } );
  50. describe( 'getRoot', () => {
  51. it( 'should return root element', () => {
  52. const root = new RootEditableElement( createDocumentMock(), 'div' );
  53. wrapper.parent = root;
  54. expect( textProxy.getRoot() ).to.equal( root );
  55. } );
  56. } );
  57. describe( 'getAncestors', () => {
  58. it( 'should return array of ancestors', () => {
  59. const result = textProxy.getAncestors();
  60. expect( result ).to.be.an( 'array' );
  61. expect( result ).to.length( 2 );
  62. expect( result[ 0 ] ).to.equal( wrapper );
  63. expect( result[ 1 ] ).to.equal( parent );
  64. } );
  65. it( 'should return array of ancestors starting from parent `parentFirst`', () => {
  66. const result = textProxy.getAncestors( { parentFirst: true } );
  67. expect( result.length ).to.equal( 2 );
  68. expect( result[ 0 ] ).to.equal( parent );
  69. expect( result[ 1 ] ).to.equal( wrapper );
  70. } );
  71. it( 'should return array including node itself `includeNode`', () => {
  72. const result = textProxy.getAncestors( { includeNode: true } );
  73. expect( result ).to.be.an( 'array' );
  74. expect( result ).to.length( 3 );
  75. expect( result[ 0 ] ).to.equal( wrapper );
  76. expect( result[ 1 ] ).to.equal( parent );
  77. expect( result[ 2 ] ).to.equal( text );
  78. } );
  79. it( 'should return array of ancestors including node itself `includeNode` starting from parent `parentFirst`', () => {
  80. const result = textProxy.getAncestors( { includeNode: true, parentFirst: true } );
  81. expect( result.length ).to.equal( 3 );
  82. expect( result[ 0 ] ).to.equal( text );
  83. expect( result[ 1 ] ).to.equal( parent );
  84. expect( result[ 2 ] ).to.equal( wrapper );
  85. } );
  86. } );
  87. } );