textproxy.js 6.3 KB

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