8
0

textfragment.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Document from '/ckeditor5/core/treemodel/document.js';
  12. import CharacterProxy from '/ckeditor5/core/treemodel/characterproxy.js';
  13. describe( 'TextFragment', () => {
  14. let doc, attr, text, element, textFragment, root;
  15. before( () => {
  16. doc = new Document();
  17. root = doc.createRoot( 'root' );
  18. element = new Element( 'div' );
  19. root.insertChildren( 0, element );
  20. attr = new Attribute( 'foo', 'bar' );
  21. } );
  22. beforeEach( () => {
  23. text = new Text( 'foobar', [ attr ] );
  24. element.insertChildren( 0, text );
  25. textFragment = new TextFragment( element.getChild( 2 ), 3 );
  26. } );
  27. afterEach( () => {
  28. element.removeChildren( 0, 1 );
  29. } );
  30. it( 'should have first property pointing to the first character node contained in TextFragment', () => {
  31. let char = textFragment.first;
  32. expect( char.getPath() ).to.deep.equal( [ 0, 2 ] );
  33. expect( char.character ).to.equal( 'o' );
  34. } );
  35. it( 'should have last property pointing to the last character node contained in TextFragment', () => {
  36. let char = textFragment.last;
  37. expect( char.getPath() ).to.deep.equal( [ 0, 4 ] );
  38. expect( char.character ).to.equal( 'a' );
  39. } );
  40. it( 'should have text property', () => {
  41. expect( textFragment ).to.have.property( 'text' ).that.equals( 'oba' );
  42. } );
  43. describe( 'getCharAt', () => {
  44. it( 'should return CharacterProxy element representing proper tree model character node', () => {
  45. let char = textFragment.getCharAt( 1 );
  46. expect( char ).to.be.instanceof( CharacterProxy );
  47. expect( char.character ).to.equal( 'b' );
  48. expect( char.getAttributeValue( 'foo' ) ).to.equal( 'bar' );
  49. expect( char.parent ).to.equal( element );
  50. } );
  51. it( 'should return null for wrong index', () => {
  52. expect( textFragment.getCharAt( -1 ) ).to.be.null;
  53. expect( textFragment.getCharAt( 4 ) ).to.be.null;
  54. } );
  55. } );
  56. describe( 'attributes interface', () => {
  57. let attr2 = new Attribute( 'abc', 'xyz' );
  58. describe( 'hasAttribute', () => {
  59. it( 'should return true if text fragment has given attribute', () => {
  60. expect( textFragment.hasAttribute( attr ) ).to.be.true;
  61. } );
  62. it( 'should return false if text fragment does not have attribute', () => {
  63. expect( textFragment.hasAttribute( attr2 ) ).to.be.false;
  64. } );
  65. it( 'should return true if text fragment has attribute with given key', () => {
  66. expect( textFragment.hasAttribute( 'foo' ) ).to.be.true;
  67. } );
  68. it( 'should return false if text fragment does not have attribute with given key', () => {
  69. expect( textFragment.hasAttribute( 'abc' ) ).to.be.false;
  70. } );
  71. } );
  72. describe( 'getAttribute', () => {
  73. it( 'should return attribute with given key if text fragment has given attribute', () => {
  74. expect( textFragment.getAttribute( 'foo' ) ).to.equal( attr );
  75. } );
  76. it( 'should return null if text fragment does not have given attribute', () => {
  77. expect( textFragment.getAttribute( 'bar' ) ).to.be.undefined;
  78. } );
  79. } );
  80. describe( 'getAttributeValue', () => {
  81. it( 'should return attribute value for given key if text fragment has given attribute', () => {
  82. expect( textFragment.getAttributeValue( 'foo' ) ).to.equal( 'bar' );
  83. } );
  84. it( 'should return null if text fragment does not have given attribute', () => {
  85. expect( textFragment.getAttributeValue( 'bar' ) ).to.be.null;
  86. } );
  87. } );
  88. describe( 'getAttributes', () => {
  89. it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
  90. let it = textFragment.getAttributes();
  91. let attrs = [];
  92. let step = it.next();
  93. while ( !step.done ) {
  94. attrs.push( step.value );
  95. step = it.next();
  96. }
  97. expect( attrs ).to.deep.equal( [ attr ] );
  98. } );
  99. } );
  100. describe( 'setAttribute', () => {
  101. it( 'should set given attribute on the text fragment', () => {
  102. textFragment.setAttribute( new Attribute( 'abc', 'xyz' ) );
  103. expect( textFragment.getAttributeValue( 'abc' ) ).to.equal( 'xyz' );
  104. } );
  105. } );
  106. describe( 'removeAttribute', () => {
  107. it( 'should remove attribute set on the text fragment and return true', () => {
  108. let result = textFragment.removeAttribute( 'foo' );
  109. expect( textFragment.getAttributeValue( 'foo' ) ).to.be.null;
  110. expect( result ).to.be.true;
  111. } );
  112. it( 'should return false if text fragment does not have given attribute', () => {
  113. let result = textFragment.removeAttribute( 'abc' );
  114. expect( result ).to.be.false;
  115. } );
  116. } );
  117. } );
  118. } );