text.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import Text from '/ckeditor5/core/treemodel/text.js';
  8. import TextNode from '/ckeditor5/core/treemodel/textnode.js';
  9. import Attribute from '/ckeditor5/core/treemodel/attribute.js';
  10. import AttributeList from '/ckeditor5/core/treemodel/attributelist.js';
  11. describe( 'Text', () => {
  12. describe( 'constructor', () => {
  13. it( 'should create character without attributes', () => {
  14. let attrs = [ new Attribute( 'bold', true ) ];
  15. let text = new Text( 'bar', attrs );
  16. expect( text ).to.have.property( 'text' ).that.equals( 'bar' );
  17. expect( text ).to.have.property( 'attrs' ).that.is.instanceof( AttributeList );
  18. expect( Array.from( text.attrs ) ).to.deep.equal( attrs );
  19. } );
  20. it( 'should create empty text object', () => {
  21. let empty1 = new Text();
  22. let empty2 = new Text( '' );
  23. expect( empty1.text ).to.equal( '' );
  24. expect( empty2.text ).to.equal( '' );
  25. } );
  26. } );
  27. describe( 'getTextNode', () => {
  28. let attrs, text;
  29. beforeEach( () => {
  30. attrs = [ new Attribute( 'bold', true ) ];
  31. text = new Text( 'bar', attrs );
  32. } );
  33. it( 'should return text node containing whole text object if no parameters are passed', () => {
  34. let textNode = text.getTextNode();
  35. expect( textNode ).to.be.instanceof( TextNode );
  36. expect( textNode.text ).to.equal( 'bar' );
  37. expect( textNode._start ).to.equal( 0 );
  38. expect( textNode._textItem ).to.equal( text );
  39. } );
  40. it( 'should return text node containing characters from start index to the end of text object if one parameter is passed', () => {
  41. let textNode = text.getTextNode( 1 );
  42. expect( textNode ).to.be.instanceof( TextNode );
  43. expect( textNode.text ).to.equal( 'ar' );
  44. expect( textNode._start ).to.equal( 1 );
  45. expect( textNode._textItem ).to.equal( text );
  46. } );
  47. it( 'should return text node containing given number of characters, starting from given index if two parameters are passed', () => {
  48. let textNode = text.getTextNode( 1, 1 );
  49. expect( textNode ).to.be.instanceof( TextNode );
  50. expect( textNode.text ).to.equal( 'a' );
  51. expect( textNode._start ).to.equal( 1 );
  52. expect( textNode._textItem ).to.equal( text );
  53. } );
  54. } );
  55. it( 'should create proper JSON string using toJSON method', () => {
  56. let text = new Text( 'bar' );
  57. let parsed = JSON.parse( JSON.stringify( text ) );
  58. expect( parsed.text ).to.equal( 'bar' );
  59. expect( parsed.parent ).to.equal( null );
  60. } );
  61. } );