textproxy.js 5.8 KB

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