textfragment.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. describe( 'attributes interface', () => {
  45. let attr2 = new Attribute( 'abc', 'xyz' );
  46. describe( 'hasAttribute', () => {
  47. it( 'should return true if text fragment has given attribute', () => {
  48. expect( textFragment.hasAttribute( attr ) ).to.be.true;
  49. } );
  50. it( 'should return false if text fragment does not have attribute', () => {
  51. expect( textFragment.hasAttribute( attr2 ) ).to.be.false;
  52. } );
  53. it( 'should return true if text fragment has attribute with given key', () => {
  54. expect( textFragment.hasAttribute( 'foo' ) ).to.be.true;
  55. } );
  56. it( 'should return false if text fragment does not have attribute with given key', () => {
  57. expect( textFragment.hasAttribute( 'abc' ) ).to.be.false;
  58. } );
  59. } );
  60. describe( 'getAttribute', () => {
  61. it( 'should return attribute with given key if text fragment has given attribute', () => {
  62. expect( textFragment.getAttribute( 'foo' ) ).to.equal( attr );
  63. } );
  64. it( 'should return null if text fragment does not have given attribute', () => {
  65. expect( textFragment.getAttribute( 'bar' ) ).to.be.undefined;
  66. } );
  67. } );
  68. describe( 'getAttributeValue', () => {
  69. it( 'should return attribute value for given key if text fragment has given attribute', () => {
  70. expect( textFragment.getAttributeValue( 'foo' ) ).to.equal( 'bar' );
  71. } );
  72. it( 'should return null if text fragment does not have given attribute', () => {
  73. expect( textFragment.getAttributeValue( 'bar' ) ).to.be.null;
  74. } );
  75. } );
  76. describe( 'getAttributes', () => {
  77. it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
  78. let it = textFragment.getAttributes();
  79. let attrs = [];
  80. let step = it.next();
  81. while ( !step.done ) {
  82. attrs.push( step.value );
  83. step = it.next();
  84. }
  85. expect( attrs ).to.deep.equal( [ attr ] );
  86. } );
  87. } );
  88. describe( 'setAttribute', () => {
  89. it( 'should set given attribute on the text fragment', () => {
  90. textFragment.setAttribute( new Attribute( 'abc', 'xyz' ) );
  91. expect( textFragment.getAttributeValue( 'abc' ) ).to.equal( 'xyz' );
  92. } );
  93. } );
  94. describe( 'removeAttribute', () => {
  95. it( 'should remove attribute set on the text fragment and return true', () => {
  96. let result = textFragment.removeAttribute( 'foo' );
  97. expect( textFragment.getAttributeValue( 'foo' ) ).to.be.null;
  98. expect( result ).to.be.true;
  99. } );
  100. it( 'should return false if text fragment does not have given attribute', () => {
  101. let result = textFragment.removeAttribute( 'abc' );
  102. expect( result ).to.be.false;
  103. } );
  104. } );
  105. } );
  106. } );