textproxy.js 6.5 KB

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