textproxy.js 6.1 KB

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