8
0

textproxy.js 5.7 KB

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