textproxy.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import TextProxy from '../../src/view/textproxy';
  6. import Text from '../../src/view/text';
  7. import ContainerElement from '../../src/view/containerelement';
  8. import DocumentFragment from '../../src/view/documentfragment';
  9. import RootEditableElement from '../../src/view/rooteditableelement';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import createDocumentMock from '../../tests/view/_utils/createdocumentmock';
  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. const startTextProxy = new TextProxy( text, 0, 4 );
  29. const 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. it( 'should throw if wrong offsetInText is passed', () => {
  35. expect( () => {
  36. new TextProxy( text, -1, 2 ); // eslint-disable-line no-new
  37. } ).to.throw( CKEditorError, /view-textproxy-wrong-offsetintext/ );
  38. expect( () => {
  39. new TextProxy( text, 9, 1 ); // eslint-disable-line no-new
  40. } ).to.throw( CKEditorError, /view-textproxy-wrong-offsetintext/ );
  41. } );
  42. it( 'should throw if wrong length is passed', () => {
  43. expect( () => {
  44. new TextProxy( text, 2, -1 ); // eslint-disable-line no-new
  45. } ).to.throw( CKEditorError, /view-textproxy-wrong-length/ );
  46. expect( () => {
  47. new TextProxy( text, 2, 9 ); // eslint-disable-line no-new
  48. } ).to.throw( CKEditorError, /view-textproxy-wrong-length/ );
  49. } );
  50. } );
  51. describe( 'is', () => {
  52. it( 'should return true for textProxy', () => {
  53. expect( textProxy.is( 'textProxy' ) ).to.be.true;
  54. } );
  55. it( 'should return false for other accept values', () => {
  56. expect( textProxy.is( 'text' ) ).to.be.false;
  57. expect( textProxy.is( 'element' ) ).to.be.false;
  58. expect( textProxy.is( 'containerElement' ) ).to.be.false;
  59. expect( textProxy.is( 'attributeElement' ) ).to.be.false;
  60. expect( textProxy.is( 'uiElement' ) ).to.be.false;
  61. expect( textProxy.is( 'emptyElement' ) ).to.be.false;
  62. expect( textProxy.is( 'rootElement' ) ).to.be.false;
  63. expect( textProxy.is( 'documentFragment' ) ).to.be.false;
  64. } );
  65. } );
  66. describe( 'offsetSize', () => {
  67. it( 'should be equal to the number of characters in text proxy', () => {
  68. expect( textProxy.offsetSize ).to.equal( 3 );
  69. } );
  70. } );
  71. describe( 'getDocument', () => {
  72. it( 'should return null if any parent has not set Document', () => {
  73. expect( textProxy.document ).to.be.null;
  74. } );
  75. it( 'should return Document attached to the parent element', () => {
  76. const docMock = createDocumentMock();
  77. const root = new RootEditableElement( 'div' );
  78. root._document = docMock;
  79. wrapper.parent = root;
  80. expect( textProxy.document ).to.equal( docMock );
  81. } );
  82. it( 'should return null if element is inside DocumentFragment', () => {
  83. new DocumentFragment( [ wrapper ] ); // eslint-disable-line no-new
  84. expect( textProxy.document ).to.be.null;
  85. } );
  86. } );
  87. describe( 'getRoot', () => {
  88. it( 'should return root element', () => {
  89. const root = new RootEditableElement( 'div' );
  90. root._document = createDocumentMock();
  91. wrapper.parent = root;
  92. expect( textProxy.root ).to.equal( root );
  93. } );
  94. } );
  95. describe( 'getAncestors', () => {
  96. it( 'should return array of ancestors', () => {
  97. const result = textProxy.getAncestors();
  98. expect( result ).to.be.an( 'array' );
  99. expect( result ).to.length( 2 );
  100. expect( result[ 0 ] ).to.equal( wrapper );
  101. expect( result[ 1 ] ).to.equal( parent );
  102. } );
  103. it( 'should return array of ancestors starting from parent `parentFirst`', () => {
  104. const result = textProxy.getAncestors( { parentFirst: true } );
  105. expect( result.length ).to.equal( 2 );
  106. expect( result[ 0 ] ).to.equal( parent );
  107. expect( result[ 1 ] ).to.equal( wrapper );
  108. } );
  109. it( 'should return array including node itself `includeSelf`', () => {
  110. const result = textProxy.getAncestors( { includeSelf: true } );
  111. expect( result ).to.be.an( 'array' );
  112. expect( result ).to.length( 3 );
  113. expect( result[ 0 ] ).to.equal( wrapper );
  114. expect( result[ 1 ] ).to.equal( parent );
  115. expect( result[ 2 ] ).to.equal( text );
  116. } );
  117. it( 'should return array of ancestors including node itself `includeSelf` starting from parent `parentFirst`', () => {
  118. const result = textProxy.getAncestors( { includeSelf: true, parentFirst: true } );
  119. expect( result.length ).to.equal( 3 );
  120. expect( result[ 0 ] ).to.equal( text );
  121. expect( result[ 1 ] ).to.equal( parent );
  122. expect( result[ 2 ] ).to.equal( wrapper );
  123. } );
  124. } );
  125. } );