8
0

textproxy.js 6.5 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. import Element from '../../src/model/element';
  6. import Text from '../../src/model/text';
  7. import TextProxy from '../../src/model/textproxy';
  8. import Model from '../../src/model/model';
  9. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  10. describe( 'TextProxy', () => {
  11. let model, doc, element, textProxy, root, textProxyNoParent, text, textNoParent;
  12. beforeEach( () => {
  13. model = new Model();
  14. doc = model.document;
  15. root = doc.createRoot();
  16. element = new Element( 'div' );
  17. root._insertChild( 0, element );
  18. text = new Text( 'foobar', { foo: 'bar' } );
  19. element._insertChild( 0, [ new Text( 'abc' ), text ] );
  20. textProxy = new TextProxy( text, 2, 3 );
  21. textNoParent = new Text( 'abcxyz' );
  22. textProxyNoParent = new TextProxy( textNoParent, 1, 1 );
  23. } );
  24. it( 'should have data property', () => {
  25. expect( textProxy ).to.have.property( 'data' ).that.equals( 'oba' );
  26. expect( textProxyNoParent ).to.have.property( 'data' ).that.equals( 'b' );
  27. } );
  28. it( 'should have root property', () => {
  29. expect( textProxy ).to.have.property( 'root' ).that.equals( root );
  30. expect( textProxyNoParent ).to.have.property( 'root' ).that.equals( textNoParent );
  31. } );
  32. it( 'should have parent property', () => {
  33. expect( textProxy ).to.have.property( 'parent' ).that.equals( element );
  34. expect( textProxyNoParent ).to.have.property( 'parent' ).that.equals( null );
  35. } );
  36. it( 'should have textNode property', () => {
  37. expect( textProxy ).to.have.property( 'textNode' ).that.equals( text );
  38. expect( textProxyNoParent ).to.have.property( 'textNode' ).that.equals( textNoParent );
  39. } );
  40. it( 'should have startOffset property', () => {
  41. expect( textProxy ).to.have.property( 'startOffset' ).that.equals( 5 );
  42. expect( textProxyNoParent ).to.have.property( 'startOffset' ).that.is.null;
  43. } );
  44. it( 'should have offsetSize property', () => {
  45. expect( textProxy ).to.have.property( 'offsetSize' ).that.equals( 3 );
  46. expect( textProxyNoParent ).to.have.property( 'offsetSize' ).that.equals( 1 );
  47. } );
  48. it( 'should have endOffset property', () => {
  49. expect( textProxy ).to.have.property( 'endOffset' ).that.equals( 8 );
  50. expect( textProxyNoParent ).to.have.property( 'endOffset' ).that.equals( null );
  51. } );
  52. it( 'should have offsetInText property', () => {
  53. expect( textProxy ).to.have.property( 'offsetInText' ).that.equals( 2 );
  54. expect( textProxyNoParent ).to.have.property( 'offsetInText' ).that.equals( 1 );
  55. } );
  56. it( 'should have isPartial property', () => {
  57. const startTextProxy = new TextProxy( text, 0, 4 );
  58. const fullTextProxy = new TextProxy( text, 0, 6 );
  59. expect( textProxy.isPartial ).to.be.true;
  60. expect( startTextProxy.isPartial ).to.be.true;
  61. expect( fullTextProxy.isPartial ).to.be.false;
  62. } );
  63. it( 'should throw if wrong offsetInText is passed', () => {
  64. expectToThrowCKEditorError( () => {
  65. new TextProxy( text, -1, 2 ); // eslint-disable-line no-new
  66. }, /model-textproxy-wrong-offsetintext/, model );
  67. expectToThrowCKEditorError( () => {
  68. new TextProxy( text, 9, 1 ); // eslint-disable-line no-new
  69. }, /model-textproxy-wrong-offsetintext/, model );
  70. } );
  71. it( 'should throw if wrong length is passed', () => {
  72. expectToThrowCKEditorError( () => {
  73. new TextProxy( text, 2, -1 ); // eslint-disable-line no-new
  74. }, /model-textproxy-wrong-length/, model );
  75. expectToThrowCKEditorError( () => {
  76. new TextProxy( text, 2, 9 ); // eslint-disable-line no-new
  77. }, /model-textproxy-wrong-length/, model );
  78. } );
  79. describe( 'is()', () => {
  80. it( 'should return true for $textProxy', () => {
  81. expect( textProxy.is( '$textProxy' ) ).to.be.true;
  82. expect( textProxy.is( 'model:$textProxy' ) ).to.be.true;
  83. expect( textProxy.is( 'textProxy' ) ).to.be.true;
  84. expect( textProxy.is( 'model:textProxy' ) ).to.be.true;
  85. } );
  86. it( 'should return false for other accept values', () => {
  87. expect( textProxy.is( 'node' ) ).to.be.false;
  88. expect( textProxy.is( 'model:node' ) ).to.be.false;
  89. expect( textProxy.is( '$text' ) ).to.be.false;
  90. expect( textProxy.is( 'element' ) ).to.be.false;
  91. expect( textProxy.is( 'model:element', 'image' ) ).to.be.false;
  92. expect( textProxy.is( 'documentFragment' ) ).to.be.false;
  93. expect( textProxy.is( 'rootElement' ) ).to.be.false;
  94. } );
  95. } );
  96. describe( 'getPath', () => {
  97. it( 'should return path to the text proxy', () => {
  98. expect( textProxy.getPath() ).to.deep.equal( [ 0, 5 ] );
  99. expect( textProxyNoParent.getPath() ).to.deep.equal( [] );
  100. } );
  101. } );
  102. describe( 'getAncestors', () => {
  103. it( 'should return proper array of ancestor nodes', () => {
  104. expect( textProxy.getAncestors() ).to.deep.equal( [ root, element ] );
  105. } );
  106. it( 'should include itself if includeSelf option is set to true', () => {
  107. expect( textProxy.getAncestors( { includeSelf: true } ) ).to.deep.equal( [ root, element, textProxy ] );
  108. } );
  109. it( 'should reverse order if parentFirst option is set to true', () => {
  110. expect( textProxy.getAncestors( { includeSelf: true, parentFirst: true } ) ).to.deep.equal( [ textProxy, element, root ] );
  111. } );
  112. } );
  113. describe( 'attributes interface', () => {
  114. describe( 'hasAttribute', () => {
  115. it( 'should return true if text proxy has attribute with given key', () => {
  116. expect( textProxy.hasAttribute( 'foo' ) ).to.be.true;
  117. } );
  118. it( 'should return false if text proxy does not have attribute with given key', () => {
  119. expect( textProxy.hasAttribute( 'abc' ) ).to.be.false;
  120. } );
  121. } );
  122. describe( 'getAttribute', () => {
  123. it( 'should return attribute with given key if text proxy has given attribute', () => {
  124. expect( textProxy.getAttribute( 'foo' ) ).to.equal( 'bar' );
  125. } );
  126. it( 'should return undefined if text proxy does not have given attribute', () => {
  127. expect( textProxy.getAttribute( 'bar' ) ).to.be.undefined;
  128. } );
  129. } );
  130. describe( 'getAttributes', () => {
  131. it( 'should return an iterator that iterates over all attributes set on the text proxy', () => {
  132. expect( Array.from( textProxy.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  133. expect( Array.from( textProxyNoParent.getAttributes() ) ).to.deep.equal( [] );
  134. } );
  135. } );
  136. describe( 'getAttributeKeys', () => {
  137. it( 'should return an iterator that iterates over all attribute keys set on the text proxy', () => {
  138. expect( Array.from( textProxy.getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  139. expect( Array.from( textProxyNoParent.getAttributeKeys() ) ).to.deep.equal( [] );
  140. } );
  141. } );
  142. } );
  143. } );