textproxy.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. 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. let startTextProxy = new TextProxy( text, 0, 4 );
  61. let 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. describe( 'getPath', () => {
  67. it( 'should return path to the text proxy', () => {
  68. expect( textProxy.getPath() ).to.deep.equal( [ 0, 5 ] );
  69. expect( textProxyNoParent.getPath() ).to.deep.equal( [] );
  70. } );
  71. } );
  72. describe( 'getAncestors', () => {
  73. it( 'should return proper array of ancestor nodes', () => {
  74. expect( textProxy.getAncestors() ).to.deep.equal( [ root, element ] );
  75. } );
  76. it( 'should include itself if includeNode option is set to true', () => {
  77. expect( textProxy.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root, element, textProxy ] );
  78. } );
  79. it( 'should reverse order if parentFirst option is set to true', () => {
  80. expect( textProxy.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ textProxy, element, root ] );
  81. } );
  82. } );
  83. describe( 'attributes interface', () => {
  84. describe( 'hasAttribute', () => {
  85. it( 'should return true if text proxy has attribute with given key', () => {
  86. expect( textProxy.hasAttribute( 'foo' ) ).to.be.true;
  87. } );
  88. it( 'should return false if text proxy does not have attribute with given key', () => {
  89. expect( textProxy.hasAttribute( 'abc' ) ).to.be.false;
  90. } );
  91. } );
  92. describe( 'getAttribute', () => {
  93. it( 'should return attribute with given key if text proxy has given attribute', () => {
  94. expect( textProxy.getAttribute( 'foo' ) ).to.equal( 'bar' );
  95. } );
  96. it( 'should return undefined if text proxy does not have given attribute', () => {
  97. expect( textProxy.getAttribute( 'bar' ) ).to.be.undefined;
  98. } );
  99. } );
  100. describe( 'getAttributes', () => {
  101. it( 'should return an iterator that iterates over all attributes set on the text proxy', () => {
  102. expect( Array.from( textProxy.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  103. expect( Array.from( textProxyNoParent.getAttributes() ) ).to.deep.equal( [] );
  104. } );
  105. } );
  106. describe( 'getAttributeKeys', () => {
  107. it( 'should return an iterator that iterates over all attribute keys set on the text proxy', () => {
  108. expect( Array.from( textProxy.getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  109. expect( Array.from( textProxyNoParent.getAttributeKeys() ) ).to.deep.equal( [] );
  110. } );
  111. } );
  112. } );
  113. } );