textproxy.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 createDocumentMock from '../../tests/view/_utils/createdocumentmock';
  11. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. import Document from '../../src/view/document';
  13. import { StylesProcessor } from '../../src/view/stylesmap';
  14. describe( 'TextProxy', () => {
  15. let text, parent, wrapper, textProxy, document;
  16. beforeEach( () => {
  17. document = new Document( new StylesProcessor() );
  18. text = new Text( document, 'abcdefgh' );
  19. parent = new ContainerElement( document, 'p', [], [ text ] );
  20. wrapper = new ContainerElement( document, 'div', [], parent );
  21. textProxy = new TextProxy( text, 2, 3 );
  22. } );
  23. describe( 'constructor()', () => {
  24. it( 'should create TextProxy instance with specified properties', () => {
  25. expect( textProxy ).to.have.property( 'parent' ).to.equal( parent );
  26. expect( textProxy ).to.have.property( 'data' ).to.equal( 'cde' );
  27. expect( textProxy ).to.have.property( 'textNode' ).to.equal( text );
  28. expect( textProxy ).to.have.property( 'offsetInText' ).to.equal( 2 );
  29. } );
  30. it( 'should have isPartial property', () => {
  31. const startTextProxy = new TextProxy( text, 0, 4 );
  32. const fullTextProxy = new TextProxy( text, 0, 8 );
  33. expect( textProxy.isPartial ).to.be.true;
  34. expect( startTextProxy.isPartial ).to.be.true;
  35. expect( fullTextProxy.isPartial ).to.be.false;
  36. } );
  37. it( 'should throw if wrong offsetInText is passed', () => {
  38. expectToThrowCKEditorError( () => {
  39. new TextProxy( text, -1, 2 ); // eslint-disable-line no-new
  40. }, /view-textproxy-wrong-offsetintext/ );
  41. expectToThrowCKEditorError( () => {
  42. new TextProxy( text, 9, 1 ); // eslint-disable-line no-new
  43. }, /view-textproxy-wrong-offsetintext/ );
  44. } );
  45. it( 'should throw if wrong length is passed', () => {
  46. expectToThrowCKEditorError( () => {
  47. new TextProxy( text, 2, -1 ); // eslint-disable-line no-new
  48. }, /view-textproxy-wrong-length/ );
  49. expectToThrowCKEditorError( () => {
  50. new TextProxy( text, 2, 9 ); // eslint-disable-line no-new
  51. }, /view-textproxy-wrong-length/ );
  52. } );
  53. } );
  54. describe( 'is()', () => {
  55. it( 'should return true for $textProxy', () => {
  56. expect( textProxy.is( '$textProxy' ) ).to.be.true;
  57. expect( textProxy.is( 'view:$textProxy' ) ).to.be.true;
  58. expect( textProxy.is( 'textProxy' ) ).to.be.true;
  59. expect( textProxy.is( 'view:textProxy' ) ).to.be.true;
  60. } );
  61. it( 'should return false for other accept values', () => {
  62. expect( textProxy.is( 'node' ) ).to.be.false;
  63. expect( textProxy.is( 'view:node' ) ).to.be.false;
  64. expect( textProxy.is( '$text' ) ).to.be.false;
  65. expect( textProxy.is( 'view:$text' ) ).to.be.false;
  66. expect( textProxy.is( 'element' ) ).to.be.false;
  67. expect( textProxy.is( 'containerElement' ) ).to.be.false;
  68. expect( textProxy.is( 'attributeElement' ) ).to.be.false;
  69. expect( textProxy.is( 'uiElement' ) ).to.be.false;
  70. expect( textProxy.is( 'emptyElement' ) ).to.be.false;
  71. expect( textProxy.is( 'rootElement' ) ).to.be.false;
  72. expect( textProxy.is( 'documentFragment' ) ).to.be.false;
  73. expect( textProxy.is( 'model:$textProxy' ) ).to.be.false;
  74. } );
  75. } );
  76. describe( 'offsetSize', () => {
  77. it( 'should be equal to the number of characters in text proxy', () => {
  78. expect( textProxy.offsetSize ).to.equal( 3 );
  79. } );
  80. } );
  81. describe( 'getDocument', () => {
  82. it( 'should return Document attached to the parent element', () => {
  83. const root = new RootEditableElement( document, 'div' );
  84. wrapper.parent = root;
  85. expect( textProxy.document ).to.equal( document );
  86. } );
  87. it( 'should return Document if element is inside DocumentFragment', () => {
  88. new DocumentFragment( document, [ wrapper ] ); // eslint-disable-line no-new
  89. expect( textProxy.document ).to.equal( document );
  90. } );
  91. } );
  92. describe( 'getRoot', () => {
  93. it( 'should return root element', () => {
  94. const docMock = createDocumentMock();
  95. const root = new RootEditableElement( docMock, 'div' );
  96. wrapper.parent = root;
  97. expect( textProxy.root ).to.equal( root );
  98. } );
  99. } );
  100. describe( 'getAncestors', () => {
  101. it( 'should return array of ancestors', () => {
  102. const result = textProxy.getAncestors();
  103. expect( result ).to.be.an( 'array' );
  104. expect( result ).to.length( 2 );
  105. expect( result[ 0 ] ).to.equal( wrapper );
  106. expect( result[ 1 ] ).to.equal( parent );
  107. } );
  108. it( 'should return array of ancestors starting from parent `parentFirst`', () => {
  109. const result = textProxy.getAncestors( { parentFirst: true } );
  110. expect( result.length ).to.equal( 2 );
  111. expect( result[ 0 ] ).to.equal( parent );
  112. expect( result[ 1 ] ).to.equal( wrapper );
  113. } );
  114. it( 'should return array including node itself `includeSelf`', () => {
  115. const result = textProxy.getAncestors( { includeSelf: true } );
  116. expect( result ).to.be.an( 'array' );
  117. expect( result ).to.length( 3 );
  118. expect( result[ 0 ] ).to.equal( wrapper );
  119. expect( result[ 1 ] ).to.equal( parent );
  120. expect( result[ 2 ] ).to.equal( text );
  121. } );
  122. it( 'should return array of ancestors including node itself `includeSelf` starting from parent `parentFirst`', () => {
  123. const result = textProxy.getAncestors( { includeSelf: true, parentFirst: true } );
  124. expect( result.length ).to.equal( 3 );
  125. expect( result[ 0 ] ).to.equal( text );
  126. expect( result[ 1 ] ).to.equal( parent );
  127. expect( result[ 2 ] ).to.equal( wrapper );
  128. } );
  129. } );
  130. } );