writer.js 8.0 KB

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