textfragment.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. const modules = bender.amd.require(
  8. 'core/treemodel/element',
  9. 'core/treemodel/text',
  10. 'core/treemodel/attribute',
  11. 'core/treemodel/textfragment',
  12. 'core/treemodel/position',
  13. 'core/treemodel/document'
  14. );
  15. describe( 'TextFragment', () => {
  16. let Element, Text, Attribute, TextFragment, Position, Document;
  17. let doc, text, element, textFragment, root;
  18. before( () => {
  19. Element = modules[ 'core/treemodel/element' ];
  20. Text = modules[ 'core/treemodel/text' ];
  21. Attribute = modules[ 'core/treemodel/attribute' ];
  22. TextFragment = modules[ 'core/treemodel/textfragment' ];
  23. Position = modules[ 'core/treemodel/position' ];
  24. Document = modules[ 'core/treemodel/document' ];
  25. text = new Text( 'foobar', [ new Attribute( 'abc', 'xyz' ) ] );
  26. element = new Element( 'div', [], [ text ] );
  27. doc = new Document();
  28. root = doc.createRoot( 'root' );
  29. root.insertChildren( 0, element );
  30. } );
  31. beforeEach( () => {
  32. textFragment = new TextFragment( new Position( root, [ 0, 2 ] ), 'oba' );
  33. } );
  34. it( 'should have first property pointing to the first character node contained in TextFragment', () => {
  35. let char = textFragment.first;
  36. expect( char.getPath() ).to.deep.equal( [ 0, 2 ] );
  37. expect( char.character ).to.equal( 'o' );
  38. } );
  39. it( 'should have last property pointing to the last character node contained in TextFragment', () => {
  40. let char = textFragment.last;
  41. expect( char.getPath() ).to.deep.equal( [ 0, 4 ] );
  42. expect( char.character ).to.equal( 'a' );
  43. } );
  44. it( 'should have correct attributes property', () => {
  45. expect( textFragment.attrs.size ).to.equal( 1 );
  46. expect( textFragment.attrs.getValue( 'abc' ) ).to.equal( 'xyz' );
  47. } );
  48. it( 'should have text property', () => {
  49. expect( textFragment ).to.have.property( 'text' ).that.equals( 'oba' );
  50. } );
  51. it( 'getRange should return range containing all characters from TextFragment', () => {
  52. let range = textFragment.getRange();
  53. expect( range.root ).to.equal( root );
  54. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  55. expect( range.end.path ).to.deep.equal( [ 0, 5 ] );
  56. } );
  57. } );