textfragment.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import Element from '/ckeditor5/core/treemodel/element.js';
  8. import Text from '/ckeditor5/core/treemodel/text.js';
  9. import Attribute from '/ckeditor5/core/treemodel/attribute.js';
  10. import TextFragment from '/ckeditor5/core/treemodel/textfragment.js';
  11. import Position from '/ckeditor5/core/treemodel/position.js';
  12. import Document from '/ckeditor5/core/treemodel/document.js';
  13. describe( 'TextFragment', () => {
  14. let doc, text, element, textFragment, root;
  15. before( () => {
  16. text = new Text( 'foobar', [ new Attribute( 'abc', 'xyz' ) ] );
  17. element = new Element( 'div', [], [ text ] );
  18. doc = new Document();
  19. root = doc.createRoot( 'root' );
  20. root.insertChildren( 0, element );
  21. } );
  22. beforeEach( () => {
  23. textFragment = new TextFragment( new Position( root, [ 0, 2 ] ), 'oba' );
  24. } );
  25. it( 'should have first property pointing to the first character node contained in TextFragment', () => {
  26. let char = textFragment.first;
  27. expect( char.getPath() ).to.deep.equal( [ 0, 2 ] );
  28. expect( char.character ).to.equal( 'o' );
  29. } );
  30. it( 'should have last property pointing to the last character node contained in TextFragment', () => {
  31. let char = textFragment.last;
  32. expect( char.getPath() ).to.deep.equal( [ 0, 4 ] );
  33. expect( char.character ).to.equal( 'a' );
  34. } );
  35. it( 'should have correct attributes property', () => {
  36. expect( textFragment.attrs.size ).to.equal( 1 );
  37. expect( textFragment.attrs.getValue( 'abc' ) ).to.equal( 'xyz' );
  38. } );
  39. it( 'should have text property', () => {
  40. expect( textFragment ).to.have.property( 'text' ).that.equals( 'oba' );
  41. } );
  42. it( 'getRange should return range containing all characters from TextFragment', () => {
  43. let range = textFragment.getRange();
  44. expect( range.root ).to.equal( root );
  45. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  46. expect( range.end.path ).to.deep.equal( [ 0, 5 ] );
  47. } );
  48. } );