textproxy.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Element from '/ckeditor5/engine/model/element.js';
  7. import Text from '/ckeditor5/engine/model/text.js';
  8. import TextProxy from '/ckeditor5/engine/model/textproxy.js';
  9. import Document from '/ckeditor5/engine/model/document.js';
  10. import CharacterProxy from '/ckeditor5/engine/model/characterproxy.js';
  11. describe( 'TextProxy', () => {
  12. let doc, text, element, textFragment, root;
  13. beforeEach( () => {
  14. doc = new Document();
  15. root = doc.createRoot();
  16. element = new Element( 'div' );
  17. root.insertChildren( 0, element );
  18. text = new Text( 'foobar', { foo: 'bar' } );
  19. element.insertChildren( 0, text );
  20. textFragment = new TextProxy( element.getChild( 2 ), 3 );
  21. } );
  22. afterEach( () => {
  23. element.removeChildren( 0, 1 );
  24. } );
  25. it( 'should have first property pointing to the first character node contained in TextProxy', () => {
  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 TextProxy', () => {
  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 text property', () => {
  36. expect( textFragment ).to.have.property( 'text' ).that.equals( 'oba' );
  37. } );
  38. describe( 'getCharAt', () => {
  39. it( 'should return CharacterProxy element representing proper tree model character node', () => {
  40. let char = textFragment.getCharAt( 1 );
  41. expect( char ).to.be.instanceof( CharacterProxy );
  42. expect( char.character ).to.equal( 'b' );
  43. expect( char.getAttribute( 'foo' ) ).to.equal( 'bar' );
  44. expect( char.parent ).to.equal( element );
  45. } );
  46. it( 'should return null for wrong index', () => {
  47. expect( textFragment.getCharAt( -1 ) ).to.be.null;
  48. expect( textFragment.getCharAt( 4 ) ).to.be.null;
  49. } );
  50. } );
  51. describe( 'attributes interface', () => {
  52. describe( 'hasAttribute', () => {
  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( 'bar' );
  63. } );
  64. it( 'should return undefined if text fragment does not have given attribute', () => {
  65. expect( textFragment.getAttribute( 'bar' ) ).to.be.undefined;
  66. } );
  67. } );
  68. describe( 'getAttributes', () => {
  69. it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
  70. let attrs = Array.from( textFragment.getAttributes() );
  71. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  72. } );
  73. } );
  74. describe( 'setAttribute', () => {
  75. it( 'should set attribute on characters contained in text fragment', () => {
  76. textFragment.setAttribute( 'abc', 'xyz' );
  77. expect( element.getChild( 0 ).getAttribute( 'abc' ) ).to.be.undefined;
  78. expect( element.getChild( 1 ).getAttribute( 'abc' ) ).to.be.undefined;
  79. expect( element.getChild( 2 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
  80. expect( element.getChild( 3 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
  81. expect( element.getChild( 4 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
  82. expect( element.getChild( 5 ).getAttribute( 'abc' ) ).to.be.undefined;
  83. } );
  84. it( 'should remove attribute when passed attribute value is null', () => {
  85. textFragment.setAttribute( 'foo', null );
  86. expect( element.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  87. expect( element.getChild( 1 ).hasAttribute( 'foo' ) ).to.be.true;
  88. expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
  89. expect( element.getChild( 3 ).hasAttribute( 'foo' ) ).to.be.false;
  90. expect( element.getChild( 4 ).hasAttribute( 'foo' ) ).to.be.false;
  91. expect( element.getChild( 5 ).hasAttribute( 'foo' ) ).to.be.true;
  92. } );
  93. it( 'should correctly split and merge text fragments and refresh this text fragment properties', () => {
  94. let otherTextProxy = new TextProxy( element.getChild( 5 ), 1 );
  95. otherTextProxy.setAttribute( 'foo', null );
  96. textFragment.setAttribute( 'foo', null );
  97. expect( element._children._nodes.length ).to.equal( 2 );
  98. expect( textFragment.first._nodeListText ).to.equal( element._children._nodes[ 1 ] );
  99. } );
  100. } );
  101. describe( 'setAttributesTo', () => {
  102. it( 'should remove all attributes from text fragment and set given ones', () => {
  103. textFragment.setAttributesTo( { abc: 'xyz' } );
  104. expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
  105. expect( element.getChild( 3 ).hasAttribute( 'foo' ) ).to.be.false;
  106. expect( element.getChild( 4 ).hasAttribute( 'foo' ) ).to.be.false;
  107. expect( element.getChild( 2 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
  108. expect( element.getChild( 3 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
  109. expect( element.getChild( 4 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
  110. } );
  111. } );
  112. describe( 'removeAttribute', () => {
  113. it( 'should remove given attribute from text fragment', () => {
  114. textFragment.removeAttribute( 'foo' );
  115. expect( element.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.true;
  116. expect( element.getChild( 1 ).hasAttribute( 'foo' ) ).to.be.true;
  117. expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
  118. expect( element.getChild( 3 ).hasAttribute( 'foo' ) ).to.be.false;
  119. expect( element.getChild( 4 ).hasAttribute( 'foo' ) ).to.be.false;
  120. expect( element.getChild( 5 ).hasAttribute( 'foo' ) ).to.be.true;
  121. } );
  122. } );
  123. describe( 'clearAttributes', () => {
  124. it( 'should remove all attributes from text fragment', () => {
  125. textFragment.setAttribute( 'abc', 'xyz' );
  126. textFragment.clearAttributes();
  127. expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
  128. expect( element.getChild( 3 ).hasAttribute( 'foo' ) ).to.be.false;
  129. expect( element.getChild( 4 ).hasAttribute( 'foo' ) ).to.be.false;
  130. expect( element.getChild( 2 ).hasAttribute( 'abc' ) ).to.be.false;
  131. expect( element.getChild( 3 ).hasAttribute( 'abc' ) ).to.be.false;
  132. expect( element.getChild( 4 ).hasAttribute( 'abc' ) ).to.be.false;
  133. } );
  134. } );
  135. } );
  136. } );