text.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Text from '../../src/model/text';
  6. import Node from '../../src/model/node';
  7. describe( 'Text', () => {
  8. describe( 'constructor()', () => {
  9. it( 'should create text node without attributes', () => {
  10. const text = new Text( 'bar', { bold: true } );
  11. expect( text ).to.be.instanceof( Node );
  12. expect( text ).to.have.property( 'data' ).that.equals( 'bar' );
  13. expect( Array.from( text.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  14. } );
  15. it( 'should create empty text object', () => {
  16. const empty1 = new Text();
  17. const empty2 = new Text( '' );
  18. expect( empty1.data ).to.equal( '' );
  19. expect( empty2.data ).to.equal( '' );
  20. } );
  21. } );
  22. describe( 'offsetSize', () => {
  23. it( 'should be equal to the number of characters in text node', () => {
  24. expect( new Text( '' ).offsetSize ).to.equal( 0 );
  25. expect( new Text( 'abc' ).offsetSize ).to.equal( 3 );
  26. } );
  27. } );
  28. describe( 'is()', () => {
  29. let text;
  30. before( () => {
  31. text = new Text( 'bar' );
  32. } );
  33. it( 'should return true for node, text', () => {
  34. expect( text.is( 'node' ) ).to.be.true;
  35. expect( text.is( 'model:node' ) ).to.be.true;
  36. expect( text.is( '$text' ) ).to.be.true;
  37. expect( text.is( 'model:$text' ) ).to.be.true;
  38. expect( text.is( 'text' ) ).to.be.true;
  39. expect( text.is( 'model:text' ) ).to.be.true;
  40. } );
  41. it( 'should return false for other accept values', () => {
  42. expect( text.is( '$textProxy' ) ).to.be.false;
  43. expect( text.is( 'element' ) ).to.be.false;
  44. expect( text.is( 'model:element' ) ).to.be.false;
  45. expect( text.is( 'rootElement' ) ).to.be.false;
  46. expect( text.is( 'documentFragment' ) ).to.be.false;
  47. } );
  48. } );
  49. describe( '_clone()', () => {
  50. it( 'should return a new Text instance, with data and attributes equal to cloned text node', () => {
  51. const text = new Text( 'foo', { bold: true } );
  52. const copy = text._clone();
  53. expect( copy.data ).to.equal( 'foo' );
  54. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  55. } );
  56. } );
  57. describe( 'toJSON', () => {
  58. it( 'should serialize text node', () => {
  59. const text = new Text( 'foo', { bold: true } );
  60. expect( text.toJSON() ).to.deep.equal( {
  61. attributes: { bold: true },
  62. data: 'foo'
  63. } );
  64. } );
  65. } );
  66. describe( 'fromJSON', () => {
  67. it( 'should create text node', () => {
  68. const text = new Text( 'foo', { bold: true } );
  69. const serialized = text.toJSON();
  70. const deserialized = Text.fromJSON( serialized );
  71. expect( deserialized.data ).to.equal( 'foo' );
  72. expect( Array.from( deserialized.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  73. } );
  74. it( 'should support unicode', () => {
  75. const textQ = new Text( 'நி' );
  76. const json = textQ.toJSON();
  77. expect( json ).to.deep.equal( {
  78. data: 'நி'
  79. } );
  80. const deserialized = Text.fromJSON( json );
  81. expect( deserialized.data ).to.equal( 'நி' );
  82. } );
  83. } );
  84. } );