textproxy.js 4.3 KB

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