text.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. 'use strict';
  7. import Text from '/ckeditor5/engine/model/text.js';
  8. import Node from '/ckeditor5/engine/model/node.js';
  9. import { jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  10. describe( 'Text', () => {
  11. describe( 'constructor', () => {
  12. it( 'should create text node without attributes', () => {
  13. let text = new Text( 'bar', { bold: true } );
  14. expect( text ).to.be.instanceof( Node );
  15. expect( text ).to.have.property( 'data' ).that.equals( 'bar' );
  16. expect( Array.from( text.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  17. } );
  18. it( 'should create empty text object', () => {
  19. let empty1 = new Text();
  20. let empty2 = new Text( '' );
  21. expect( empty1.data ).to.equal( '' );
  22. expect( empty2.data ).to.equal( '' );
  23. } );
  24. } );
  25. describe( 'offsetSize', () => {
  26. it( 'should be equal to the number of characters in text node', () => {
  27. expect( new Text( '' ).offsetSize ).to.equal( 0 );
  28. expect( new Text( 'abc' ).offsetSize ).to.equal( 3 );
  29. } );
  30. } );
  31. describe( 'clone', () => {
  32. it( 'should return a new Text instance, with data and attributes equal to cloned text node', () => {
  33. let text = new Text( 'foo', { bold: true } );
  34. let copy = text.clone();
  35. expect( copy.data ).to.equal( 'foo' );
  36. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  37. } );
  38. } );
  39. describe( 'toJSON', () => {
  40. it( 'should serialize text node', () => {
  41. let text = new Text( 'foo', { bold: true } );
  42. expect( jsonParseStringify( text ) ).to.deep.equal( {
  43. attributes: [ [ 'bold', true ] ],
  44. data: 'foo'
  45. } );
  46. } );
  47. } );
  48. describe( 'fromJSON', () => {
  49. it( 'should create text node', () => {
  50. let text = new Text( 'foo', { bold: true } );
  51. let serialized = jsonParseStringify( text );
  52. let deserialized = Text.fromJSON( serialized );
  53. expect( deserialized.data ).to.equal( 'foo' );
  54. expect( Array.from( deserialized.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ] ] );
  55. } );
  56. } );
  57. } );