text.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /* globals console */
  6. import Text from '../../src/model/text';
  7. import Node from '../../src/model/node';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. describe( 'Text', () => {
  10. describe( 'constructor()', () => {
  11. it( 'should create text node without attributes', () => {
  12. const text = new Text( 'bar', { bold: true } );
  13. expect( text ).to.be.instanceof( Node );
  14. expect( text ).to.have.property( 'data' ).that.equals( 'bar' );
  15. expect( Array.from( text.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  16. } );
  17. it( 'should create empty text object', () => {
  18. const empty1 = new Text();
  19. const empty2 = new Text( '' );
  20. expect( empty1.data ).to.equal( '' );
  21. expect( empty2.data ).to.equal( '' );
  22. } );
  23. } );
  24. describe( 'offsetSize', () => {
  25. it( 'should be equal to the number of characters in text node', () => {
  26. expect( new Text( '' ).offsetSize ).to.equal( 0 );
  27. expect( new Text( 'abc' ).offsetSize ).to.equal( 3 );
  28. } );
  29. } );
  30. describe( 'is()', () => {
  31. let text;
  32. testUtils.createSinonSandbox();
  33. before( () => {
  34. text = new Text( 'bar' );
  35. } );
  36. it( 'should return true for node, text', () => {
  37. expect( text.is( 'node' ) ).to.be.true;
  38. expect( text.is( 'model:node' ) ).to.be.true;
  39. expect( text.is( '$text' ) ).to.be.true;
  40. expect( text.is( 'model:$text' ) ).to.be.true;
  41. } );
  42. it( 'should return false for other accept values', () => {
  43. expect( text.is( '$textProxy' ) ).to.be.false;
  44. expect( text.is( 'element' ) ).to.be.false;
  45. expect( text.is( 'model:element' ) ).to.be.false;
  46. expect( text.is( 'rootElement' ) ).to.be.false;
  47. expect( text.is( 'documentFragment' ) ).to.be.false;
  48. } );
  49. it( 'should print warning for legacy "text" argument', () => {
  50. testUtils.sinon.stub( console, 'warn' );
  51. expect( text.is( 'text' ) ).to.be.false;
  52. sinon.assert.calledOnce( console.warn );
  53. sinon.assert.calledWithExactly( console.warn,
  54. sinon.match( /^model-text-node-deprecated-is-text-argument/ )
  55. );
  56. } );
  57. } );
  58. describe( '_clone()', () => {
  59. it( 'should return a new Text instance, with data and attributes equal to cloned text node', () => {
  60. const text = new Text( 'foo', { bold: true } );
  61. const copy = text._clone();
  62. expect( copy.data ).to.equal( 'foo' );
  63. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  64. } );
  65. } );
  66. describe( 'toJSON', () => {
  67. it( 'should serialize text node', () => {
  68. const text = new Text( 'foo', { bold: true } );
  69. expect( text.toJSON() ).to.deep.equal( {
  70. attributes: { bold: true },
  71. data: 'foo'
  72. } );
  73. } );
  74. } );
  75. describe( 'fromJSON', () => {
  76. it( 'should create text node', () => {
  77. const text = new Text( 'foo', { bold: true } );
  78. const serialized = text.toJSON();
  79. const deserialized = Text.fromJSON( serialized );
  80. expect( deserialized.data ).to.equal( 'foo' );
  81. expect( Array.from( deserialized.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  82. } );
  83. it( 'should support unicode', () => {
  84. const textQ = new Text( 'நி' );
  85. const json = textQ.toJSON();
  86. expect( json ).to.deep.equal( {
  87. data: 'நி'
  88. } );
  89. const deserialized = Text.fromJSON( json );
  90. expect( deserialized.data ).to.equal( 'நி' );
  91. } );
  92. } );
  93. } );