text.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. } );
  56. // All characters, code points, combined symbols, etc. can be looked up in browsers console to better understand what is going on.
  57. describe( 'unicode support', () => {
  58. it( 'should normalize strings kept in data', () => {
  59. // This is a letter "n" with so-called combining mark, similar to ~, which code point is \u0303.
  60. // Those two characters together combines to "ñ", but that character already has it's code point: \u00F1.
  61. let dataCombined = '\u006E\u0303';
  62. let textN = new Text( dataCombined );
  63. expect( textN.data ).to.equal( '\u00F1' ); // "ñ" got normalized to \u00F1.
  64. expect( textN.data.length ).to.equal( 1 ); // It is now just one character.
  65. expect( textN.offsetSize ).to.equal( 1 ); // And has correct offset size.
  66. } );
  67. it( 'should be properly serialized and de-serialized', () => {
  68. let textQ = new Text( 'நி' );
  69. let json = jsonParseStringify( textQ );
  70. expect( json ).to.deep.equal( {
  71. data: 'நி'
  72. } );
  73. let deserialized = Text.fromJSON( json );
  74. expect( deserialized.data ).to.equal( 'நி' );
  75. } );
  76. } );
  77. } );