textfragment.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 TextFragment from '/ckeditor5/core/treemodel/textfragment.js';
  10. import Document from '/ckeditor5/core/treemodel/document.js';
  11. import CharacterProxy from '/ckeditor5/core/treemodel/characterproxy.js';
  12. describe( 'TextFragment', () => {
  13. let doc, text, element, textFragment, root;
  14. before( () => {
  15. doc = new Document();
  16. root = doc.createRoot( 'root' );
  17. element = new Element( 'div' );
  18. root.insertChildren( 0, element );
  19. } );
  20. beforeEach( () => {
  21. text = new Text( 'foobar', { foo: 'bar' } );
  22. element.insertChildren( 0, text );
  23. textFragment = new TextFragment( element.getChild( 2 ), 3 );
  24. } );
  25. afterEach( () => {
  26. element.removeChildren( 0, 1 );
  27. } );
  28. it( 'should have first property pointing to the first character node contained in TextFragment', () => {
  29. let char = textFragment.first;
  30. expect( char.getPath() ).to.deep.equal( [ 0, 2 ] );
  31. expect( char.character ).to.equal( 'o' );
  32. } );
  33. it( 'should have last property pointing to the last character node contained in TextFragment', () => {
  34. let char = textFragment.last;
  35. expect( char.getPath() ).to.deep.equal( [ 0, 4 ] );
  36. expect( char.character ).to.equal( 'a' );
  37. } );
  38. it( 'should have text property', () => {
  39. expect( textFragment ).to.have.property( 'text' ).that.equals( 'oba' );
  40. } );
  41. describe( 'getCharAt', () => {
  42. it( 'should return CharacterProxy element representing proper tree model character node', () => {
  43. let char = textFragment.getCharAt( 1 );
  44. expect( char ).to.be.instanceof( CharacterProxy );
  45. expect( char.character ).to.equal( 'b' );
  46. expect( char.getAttribute( 'foo' ) ).to.equal( 'bar' );
  47. expect( char.parent ).to.equal( element );
  48. } );
  49. it( 'should return null for wrong index', () => {
  50. expect( textFragment.getCharAt( -1 ) ).to.be.null;
  51. expect( textFragment.getCharAt( 4 ) ).to.be.null;
  52. } );
  53. } );
  54. describe( 'attributes interface', () => {
  55. describe( 'hasAttribute', () => {
  56. it( 'should return true if text fragment has attribute with given key', () => {
  57. expect( textFragment.hasAttribute( 'foo' ) ).to.be.true;
  58. } );
  59. it( 'should return false if text fragment does not have attribute with given key', () => {
  60. expect( textFragment.hasAttribute( 'abc' ) ).to.be.false;
  61. } );
  62. } );
  63. describe( 'getAttribute', () => {
  64. it( 'should return attribute with given key if text fragment has given attribute', () => {
  65. expect( textFragment.getAttribute( 'foo' ) ).to.equal( 'bar' );
  66. } );
  67. it( 'should return null if text fragment does not have given attribute', () => {
  68. expect( textFragment.getAttribute( 'bar' ) ).to.be.undefined;
  69. } );
  70. } );
  71. describe( 'getAttributes', () => {
  72. it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
  73. let attrs = Array.from( textFragment.getAttributes() );
  74. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  75. } );
  76. } );
  77. } );
  78. } );