writer.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Writer from '../../../src/view/writer';
  6. import Document from '../../../src/view/document';
  7. import EditableElement from '../../../src/view/editableelement';
  8. import ViewPosition from '../../../src/view/position';
  9. import createViewRoot from '../_utils/createroot';
  10. describe( 'Writer', () => {
  11. let writer, attributes, root, doc;
  12. before( () => {
  13. attributes = { foo: 'bar', baz: 'quz' };
  14. doc = new Document();
  15. root = createViewRoot( doc );
  16. writer = new Writer( doc );
  17. } );
  18. describe( 'setSelection()', () => {
  19. it( 'should set document view selection', () => {
  20. const position = ViewPosition.createAt( root );
  21. writer.setSelection( position );
  22. const ranges = Array.from( doc.selection.getRanges() );
  23. expect( ranges.length ).to.equal( 1 );
  24. expect( ranges[ 0 ].start.compareWith( position ) ).to.equal( 'same' );
  25. expect( ranges[ 0 ].end.compareWith( position ) ).to.equal( 'same' );
  26. } );
  27. it( 'should be able to set fake selection', () => {
  28. const position = ViewPosition.createAt( root );
  29. writer.setSelection( position, { fake: true, label: 'foo' } );
  30. expect( doc.selection.isFake ).to.be.true;
  31. expect( doc.selection.fakeSelectionLabel ).to.equal( 'foo' );
  32. } );
  33. } );
  34. describe( 'setSelectionFocus()', () => {
  35. it( 'should use selection._setFocus method internally', () => {
  36. const spy = sinon.spy( writer.document.selection, '_setFocus' );
  37. writer.setSelectionFocus( root, 0 );
  38. sinon.assert.calledWithExactly( spy, root, 0 );
  39. spy.restore();
  40. } );
  41. } );
  42. describe( 'createText()', () => {
  43. it( 'should create Text instance', () => {
  44. const text = writer.createText( 'foo bar' );
  45. expect( text.is( 'text' ) ).to.be.true;
  46. expect( text.data ).to.equal( 'foo bar' );
  47. } );
  48. } );
  49. describe( 'createAttributeElement()', () => {
  50. it( 'should create AttributeElement', () => {
  51. const element = writer.createAttributeElement( 'foo', attributes );
  52. expect( element.is( 'attributeElement' ) ).to.be.true;
  53. expect( element.name ).to.equal( 'foo' );
  54. assertElementAttributes( element, attributes );
  55. } );
  56. it( 'should allow to pass priority', () => {
  57. const element = writer.createAttributeElement( 'foo', attributes, 99 );
  58. expect( element.is( 'attributeElement' ) ).to.be.true;
  59. expect( element.name ).to.equal( 'foo' );
  60. expect( element.priority ).to.equal( 99 );
  61. assertElementAttributes( element, attributes );
  62. } );
  63. } );
  64. describe( 'createContainerElement()', () => {
  65. it( 'should create ContainerElement', () => {
  66. const element = writer.createContainerElement( 'foo', attributes );
  67. expect( element.is( 'containerElement' ) ).to.be.true;
  68. expect( element.name ).to.equal( 'foo' );
  69. assertElementAttributes( element, attributes );
  70. } );
  71. } );
  72. describe( 'createEditableElement()', () => {
  73. it( 'should create EditableElement', () => {
  74. const element = writer.createEditableElement( 'foo', attributes );
  75. expect( element ).to.be.instanceOf( EditableElement );
  76. expect( element.name ).to.equal( 'foo' );
  77. assertElementAttributes( element, attributes );
  78. } );
  79. } );
  80. describe( 'createEmptyElement()', () => {
  81. it( 'should create EmptyElement', () => {
  82. const element = writer.createEmptyElement( 'foo', attributes );
  83. expect( element.is( 'emptyElement' ) ).to.be.true;
  84. expect( element.name ).to.equal( 'foo' );
  85. assertElementAttributes( element, attributes );
  86. } );
  87. } );
  88. describe( 'createUIElement()', () => {
  89. it( 'should create UIElement', () => {
  90. const element = writer.createUIElement( 'foo', attributes );
  91. expect( element.is( 'uiElement' ) ).to.be.true;
  92. expect( element.name ).to.equal( 'foo' );
  93. assertElementAttributes( element, attributes );
  94. } );
  95. it( 'should allow to pass custom rendering method', () => {
  96. const renderFn = function() {};
  97. const element = writer.createUIElement( 'foo', attributes, renderFn );
  98. expect( element.is( 'uiElement' ) ).to.be.true;
  99. expect( element.name ).to.equal( 'foo' );
  100. expect( element.render ).to.equal( renderFn );
  101. assertElementAttributes( element, attributes );
  102. } );
  103. } );
  104. describe( 'setTextData()', () => {
  105. it( 'should update the content for text node', () => {
  106. const textNode = writer.createText( 'foo' );
  107. writer.setTextData( 'bar', textNode );
  108. expect( textNode.data ).to.equal( 'bar' );
  109. } );
  110. } );
  111. describe( 'setAttribute()', () => {
  112. it( 'should set attribute on given element', () => {
  113. const element = writer.createAttributeElement( 'span' );
  114. writer.setAttribute( 'foo', 'bar', element );
  115. expect( element.getAttribute( 'foo' ) ).to.equal( 'bar' );
  116. } );
  117. } );
  118. describe( 'removeAttribute()', () => {
  119. it( 'should remove attribute on given element', () => {
  120. const element = writer.createAttributeElement( 'span', { foo: 'bar' } );
  121. writer.removeAttribute( 'foo', element );
  122. expect( element.getAttribute( 'foo' ) ).to.be.undefined;
  123. } );
  124. } );
  125. describe( 'addClass()', () => {
  126. it( 'should add class to given element', () => {
  127. const element = writer.createAttributeElement( 'span' );
  128. writer.addClass( 'foo', element );
  129. expect( element.hasClass( 'foo' ) ).to.be.true;
  130. } );
  131. it( 'should add multiple classes to given element', () => {
  132. const element = writer.createAttributeElement( 'span' );
  133. writer.addClass( [ 'foo', 'bar' ], element );
  134. expect( element.hasClass( 'foo' ) ).to.be.true;
  135. expect( element.hasClass( 'bar' ) ).to.be.true;
  136. } );
  137. } );
  138. describe( 'removeClass()', () => {
  139. it( 'should remove class from given element', () => {
  140. const element = writer.createAttributeElement( 'span', { class: 'foo bar' } );
  141. writer.removeClass( 'foo', element );
  142. expect( element.hasClass( 'foo' ) ).to.be.false;
  143. expect( element.hasClass( 'bar' ) ).to.be.true;
  144. } );
  145. it( 'should remove multiple classes from given element', () => {
  146. const element = writer.createAttributeElement( 'span', { class: 'foo bar' } );
  147. writer.removeClass( [ 'foo', 'bar' ], element );
  148. expect( element.hasClass( 'foo' ) ).to.be.false;
  149. expect( element.hasClass( 'bar' ) ).to.be.false;
  150. } );
  151. } );
  152. describe( 'addStyle()', () => {
  153. it( 'should add style to given element', () => {
  154. const element = writer.createAttributeElement( 'span' );
  155. writer.setStyle( 'foo', 'bar', element );
  156. expect( element.getStyle( 'foo' ) ).to.equal( 'bar' );
  157. } );
  158. it( 'should allow to add multiple styles to given element', () => {
  159. const element = writer.createAttributeElement( 'span' );
  160. writer.setStyle( {
  161. foo: 'bar',
  162. baz: 'quiz'
  163. }, element );
  164. expect( element.getStyle( 'foo' ) ).to.equal( 'bar' );
  165. expect( element.getStyle( 'baz' ) ).to.equal( 'quiz' );
  166. } );
  167. } );
  168. describe( 'removeStyle()', () => {
  169. it( 'should remove style from given element', () => {
  170. const element = writer.createAttributeElement( 'span', { style: 'foo:bar;baz:quiz;' } );
  171. writer.removeStyle( 'foo', element );
  172. expect( element.hasStyle( 'foo' ) ).to.be.false;
  173. expect( element.hasStyle( 'baz' ) ).to.be.true;
  174. } );
  175. it( 'should remove multiple styles from given element', () => {
  176. const element = writer.createAttributeElement( 'span', { style: 'foo:bar;baz:quiz;' } );
  177. writer.removeStyle( [ 'foo', 'bar' ], element );
  178. expect( element.hasStyle( 'foo' ) ).to.be.false;
  179. expect( element.hasStyle( 'baz' ) ).to.be.true;
  180. } );
  181. } );
  182. describe( 'setCustomProperty()', () => {
  183. it( 'should set custom property to given element', () => {
  184. const element = writer.createAttributeElement( 'span' );
  185. writer.setCustomProperty( 'foo', 'bar', element );
  186. expect( element.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
  187. } );
  188. } );
  189. describe( 'removeCustomProperty()', () => {
  190. it( 'should remove custom property from given element', () => {
  191. const element = writer.createAttributeElement( 'span' );
  192. writer.setCustomProperty( 'foo', 'bar', element );
  193. expect( element.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
  194. writer.removeCustomProperty( 'foo', element );
  195. expect( element.getCustomProperty( 'foo' ) ).to.be.undefined;
  196. } );
  197. } );
  198. function assertElementAttributes( element, attributes ) {
  199. for ( const key of Object.keys( attributes ) ) {
  200. if ( element.getAttribute( key ) !== attributes[ key ] ) {
  201. throw new Error( 'Attributes in element are different that those passed to the constructor method.' );
  202. }
  203. }
  204. }
  205. } );