text.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Text from '/ckeditor5/engine/model/text.js';
  7. describe( 'Text', () => {
  8. describe( 'constructor', () => {
  9. it( 'should create character without attributes', () => {
  10. let text = new Text( 'bar', { bold: true } );
  11. expect( text ).to.have.property( 'text' ).that.equals( 'bar' );
  12. expect( text ).to.have.property( '_attrs' ).that.is.instanceof( Map );
  13. expect( Array.from( text._attrs ) ).to.deep.equal( [ [ 'bold', true ] ] );
  14. } );
  15. it( 'should create empty text object', () => {
  16. let empty1 = new Text();
  17. let empty2 = new Text( '' );
  18. expect( empty1.text ).to.equal( '' );
  19. expect( empty2.text ).to.equal( '' );
  20. } );
  21. } );
  22. describe( 'attributes interface', () => {
  23. let text;
  24. beforeEach( () => {
  25. text = new Text( 'bar', { foo: 'bar' } );
  26. } );
  27. describe( 'hasAttribute', () => {
  28. it( 'should return true if element contains attribute with given key', () => {
  29. expect( text.hasAttribute( 'foo' ) ).to.be.true;
  30. } );
  31. it( 'should return false if element does not contain attribute with given key', () => {
  32. expect( text.hasAttribute( 'bar' ) ).to.be.false;
  33. } );
  34. } );
  35. describe( 'getAttribute', () => {
  36. it( 'should return attribute value for given key if element contains given attribute', () => {
  37. expect( text.getAttribute( 'foo' ) ).to.equal( 'bar' );
  38. } );
  39. it( 'should return undefined if element does not contain given attribute', () => {
  40. expect( text.getAttribute( 'bar' ) ).to.be.undefined;
  41. } );
  42. } );
  43. describe( 'getAttributes', () => {
  44. it( 'should return an iterator that iterates over all attributes set on the element', () => {
  45. expect( Array.from( text.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  46. } );
  47. } );
  48. describe( 'setAttribute', () => {
  49. it( 'should set given attribute on the element', () => {
  50. text.setAttribute( 'abc', 'xyz' );
  51. expect( text.getAttribute( 'abc' ) ).to.equal( 'xyz' );
  52. } );
  53. } );
  54. describe( 'setAttributesTo', () => {
  55. it( 'should remove all attributes set on element and set the given ones', () => {
  56. text.setAttribute( 'abc', 'xyz' );
  57. text.setAttributesTo( { bold: true } );
  58. expect( text.getAttribute( 'bold' ) ).to.equal( true );
  59. expect( text.getAttribute( 'foo' ) ).to.be.undefined;
  60. expect( text.getAttribute( 'abc' ) ).to.be.undefined;
  61. } );
  62. } );
  63. describe( 'removeAttribute', () => {
  64. it( 'should remove attribute set on the element and return true', () => {
  65. let result = text.removeAttribute( 'foo' );
  66. expect( text.getAttribute( 'foo' ) ).to.be.undefined;
  67. expect( result ).to.be.true;
  68. } );
  69. it( 'should return false if element does not contain given attribute', () => {
  70. let result = text.removeAttribute( 'abc' );
  71. expect( result ).to.be.false;
  72. } );
  73. } );
  74. describe( 'clearAttributes', () => {
  75. it( 'should remove all attributes from the element', () => {
  76. text.setAttribute( 'abc', 'xyz' );
  77. text.clearAttributes();
  78. expect( text.getAttribute( 'foo' ) ).to.be.undefined;
  79. expect( text.getAttribute( 'abc' ) ).to.be.undefined;
  80. } );
  81. } );
  82. } );
  83. } );