text.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. import Text from 'ckeditor5/engine/model/text.js';
  7. import Node from 'ckeditor5/engine/model/node.js';
  8. import { jsonParseStringify } from 'tests/engine/model/_utils/utils.js';
  9. describe( 'Text', () => {
  10. describe( 'constructor()', () => {
  11. it( 'should create text node without attributes', () => {
  12. let 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. let empty1 = new Text();
  19. let 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( 'clone', () => {
  31. it( 'should return a new Text instance, with data and attributes equal to cloned text node', () => {
  32. let text = new Text( 'foo', { bold: true } );
  33. let copy = text.clone();
  34. expect( copy.data ).to.equal( 'foo' );
  35. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  36. } );
  37. } );
  38. describe( 'toJSON', () => {
  39. it( 'should serialize text node', () => {
  40. let text = new Text( 'foo', { bold: true } );
  41. expect( jsonParseStringify( text ) ).to.deep.equal( {
  42. attributes: [ [ 'bold', true ] ],
  43. data: 'foo'
  44. } );
  45. } );
  46. } );
  47. describe( 'fromJSON', () => {
  48. it( 'should create text node', () => {
  49. let text = new Text( 'foo', { bold: true } );
  50. let serialized = jsonParseStringify( text );
  51. let deserialized = Text.fromJSON( serialized );
  52. expect( deserialized.data ).to.equal( 'foo' );
  53. expect( Array.from( deserialized.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  54. } );
  55. it( 'should support unicode', () => {
  56. let textQ = new Text( 'நி' );
  57. let json = jsonParseStringify( textQ );
  58. expect( json ).to.deep.equal( {
  59. data: 'நி'
  60. } );
  61. let deserialized = Text.fromJSON( json );
  62. expect( deserialized.data ).to.equal( 'நி' );
  63. } );
  64. } );
  65. } );