textproxy.js 4.3 KB

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