textproxy.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. describe( 'getPath', () => {
  60. it( 'should return path to the text proxy', () => {
  61. expect( textProxy.getPath() ).to.deep.equal( [ 0, 5 ] );
  62. expect( textProxyNoParent.getPath() ).to.deep.equal( [] );
  63. } );
  64. } );
  65. describe( 'attributes interface', () => {
  66. describe( 'hasAttribute', () => {
  67. it( 'should return true if text proxy has attribute with given key', () => {
  68. expect( textProxy.hasAttribute( 'foo' ) ).to.be.true;
  69. } );
  70. it( 'should return false if text proxy does not have attribute with given key', () => {
  71. expect( textProxy.hasAttribute( 'abc' ) ).to.be.false;
  72. } );
  73. } );
  74. describe( 'getAttribute', () => {
  75. it( 'should return attribute with given key if text proxy has given attribute', () => {
  76. expect( textProxy.getAttribute( 'foo' ) ).to.equal( 'bar' );
  77. } );
  78. it( 'should return undefined if text proxy does not have given attribute', () => {
  79. expect( textProxy.getAttribute( 'bar' ) ).to.be.undefined;
  80. } );
  81. } );
  82. describe( 'getAttributes', () => {
  83. it( 'should return an iterator that iterates over all attributes set on the text proxy', () => {
  84. expect( Array.from( textProxy.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  85. expect( Array.from( textProxyNoParent.getAttributes() ) ).to.deep.equal( [] );
  86. } );
  87. } );
  88. describe( 'getAttributeKeys', () => {
  89. it( 'should return an iterator that iterates over all attribute keys set on the text proxy', () => {
  90. expect( Array.from( textProxy.getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  91. expect( Array.from( textProxyNoParent.getAttributeKeys() ) ).to.deep.equal( [] );
  92. } );
  93. } );
  94. } );
  95. } );