textproxy.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @license Copyright (c) 2003-2017, 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( 'getDocument', () => {
  67. it( 'should return null if any parent has not set Document', () => {
  68. expect( textProxy.document ).to.be.null;
  69. } );
  70. it( 'should return Document attached to the parent element', () => {
  71. const docMock = createDocumentMock();
  72. const root = new RootEditableElement( 'div' );
  73. root.document = docMock;
  74. wrapper.parent = root;
  75. expect( textProxy.document ).to.equal( docMock );
  76. } );
  77. it( 'should return null if element is inside DocumentFragment', () => {
  78. new DocumentFragment( [ wrapper ] ); // eslint-disable-line no-new
  79. expect( textProxy.document ).to.be.null;
  80. } );
  81. } );
  82. describe( 'getRoot', () => {
  83. it( 'should return root element', () => {
  84. const root = new RootEditableElement( 'div' );
  85. root.document = createDocumentMock();
  86. wrapper.parent = root;
  87. expect( textProxy.root ).to.equal( root );
  88. } );
  89. } );
  90. describe( 'getAncestors', () => {
  91. it( 'should return array of ancestors', () => {
  92. const result = textProxy.getAncestors();
  93. expect( result ).to.be.an( 'array' );
  94. expect( result ).to.length( 2 );
  95. expect( result[ 0 ] ).to.equal( wrapper );
  96. expect( result[ 1 ] ).to.equal( parent );
  97. } );
  98. it( 'should return array of ancestors starting from parent `parentFirst`', () => {
  99. const result = textProxy.getAncestors( { parentFirst: true } );
  100. expect( result.length ).to.equal( 2 );
  101. expect( result[ 0 ] ).to.equal( parent );
  102. expect( result[ 1 ] ).to.equal( wrapper );
  103. } );
  104. it( 'should return array including node itself `includeNode`', () => {
  105. const result = textProxy.getAncestors( { includeNode: true } );
  106. expect( result ).to.be.an( 'array' );
  107. expect( result ).to.length( 3 );
  108. expect( result[ 0 ] ).to.equal( wrapper );
  109. expect( result[ 1 ] ).to.equal( parent );
  110. expect( result[ 2 ] ).to.equal( text );
  111. } );
  112. it( 'should return array of ancestors including node itself `includeNode` starting from parent `parentFirst`', () => {
  113. const result = textProxy.getAncestors( { includeNode: true, parentFirst: true } );
  114. expect( result.length ).to.equal( 3 );
  115. expect( result[ 0 ] ).to.equal( text );
  116. expect( result[ 1 ] ).to.equal( parent );
  117. expect( result[ 2 ] ).to.equal( wrapper );
  118. } );
  119. } );
  120. } );