textproxy.js 6.9 KB

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