text.js 3.2 KB

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