text.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. } );
  39. it( 'should return false for other accept values', () => {
  40. expect( text.is( 'textProxy' ) ).to.be.false;
  41. expect( text.is( 'element' ) ).to.be.false;
  42. expect( text.is( 'model:element' ) ).to.be.false;
  43. expect( text.is( 'rootElement' ) ).to.be.false;
  44. expect( text.is( 'documentFragment' ) ).to.be.false;
  45. } );
  46. } );
  47. describe( '_clone()', () => {
  48. it( 'should return a new Text instance, with data and attributes equal to cloned text node', () => {
  49. const text = new Text( 'foo', { bold: true } );
  50. const copy = text._clone();
  51. expect( copy.data ).to.equal( 'foo' );
  52. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  53. } );
  54. } );
  55. describe( 'toJSON', () => {
  56. it( 'should serialize text node', () => {
  57. const text = new Text( 'foo', { bold: true } );
  58. expect( text.toJSON() ).to.deep.equal( {
  59. attributes: { bold: true },
  60. data: 'foo'
  61. } );
  62. } );
  63. } );
  64. describe( 'fromJSON', () => {
  65. it( 'should create text node', () => {
  66. const text = new Text( 'foo', { bold: true } );
  67. const serialized = text.toJSON();
  68. const deserialized = Text.fromJSON( serialized );
  69. expect( deserialized.data ).to.equal( 'foo' );
  70. expect( Array.from( deserialized.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  71. } );
  72. it( 'should support unicode', () => {
  73. const textQ = new Text( 'நி' );
  74. const json = textQ.toJSON();
  75. expect( json ).to.deep.equal( {
  76. data: 'நி'
  77. } );
  78. const deserialized = Text.fromJSON( json );
  79. expect( deserialized.data ).to.equal( 'நி' );
  80. } );
  81. } );
  82. } );