8
0

text.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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( 'text' ) ).to.be.true;
  36. } );
  37. it( 'should return false for other accept values', () => {
  38. expect( text.is( 'textProxy' ) ).to.be.false;
  39. expect( text.is( 'element' ) ).to.be.false;
  40. expect( text.is( 'rootElement' ) ).to.be.false;
  41. expect( text.is( 'documentFragment' ) ).to.be.false;
  42. } );
  43. } );
  44. describe( '_clone()', () => {
  45. it( 'should return a new Text instance, with data and attributes equal to cloned text node', () => {
  46. const text = new Text( 'foo', { bold: true } );
  47. const copy = text._clone();
  48. expect( copy.data ).to.equal( 'foo' );
  49. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  50. } );
  51. } );
  52. describe( 'toJSON', () => {
  53. it( 'should serialize text node', () => {
  54. const text = new Text( 'foo', { bold: true } );
  55. expect( text.toJSON() ).to.deep.equal( {
  56. attributes: { bold: true },
  57. data: 'foo'
  58. } );
  59. } );
  60. } );
  61. describe( 'fromJSON', () => {
  62. it( 'should create text node', () => {
  63. const text = new Text( 'foo', { bold: true } );
  64. const serialized = text.toJSON();
  65. const deserialized = Text.fromJSON( serialized );
  66. expect( deserialized.data ).to.equal( 'foo' );
  67. expect( Array.from( deserialized.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  68. } );
  69. it( 'should support unicode', () => {
  70. const textQ = new Text( 'நி' );
  71. const json = textQ.toJSON();
  72. expect( json ).to.deep.equal( {
  73. data: 'நி'
  74. } );
  75. const deserialized = Text.fromJSON( json );
  76. expect( deserialized.data ).to.equal( 'நி' );
  77. } );
  78. } );
  79. } );