8
0

writer.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 ViewRange from '../../../src/view/range';
  10. import createViewRoot from '../_utils/createroot';
  11. describe( 'Writer', () => {
  12. let writer, attributes, root, doc;
  13. before( () => {
  14. attributes = { foo: 'bar', baz: 'quz' };
  15. doc = new Document();
  16. root = createViewRoot( doc );
  17. writer = new Writer( doc );
  18. } );
  19. describe( 'setSelection()', () => {
  20. it( 'should set document view selection', () => {
  21. const position = ViewPosition.createAt( root );
  22. writer.setSelection( position );
  23. const ranges = Array.from( doc.selection.getRanges() );
  24. expect( ranges.length ).to.equal( 1 );
  25. expect( ranges[ 0 ].start.compareWith( position ) ).to.equal( 'same' );
  26. expect( ranges[ 0 ].end.compareWith( position ) ).to.equal( 'same' );
  27. } );
  28. it( 'should be able to set fake selection', () => {
  29. const position = ViewPosition.createAt( root );
  30. writer.setSelection( position, { fake: true, label: 'foo' } );
  31. expect( doc.selection.isFake ).to.be.true;
  32. expect( doc.selection.fakeSelectionLabel ).to.equal( 'foo' );
  33. } );
  34. } );
  35. describe( 'setSelectionFocus()', () => {
  36. it( 'should use selection._setFocus method internally', () => {
  37. const spy = sinon.spy( writer.document.selection, '_setFocus' );
  38. writer.setSelectionFocus( root, 0 );
  39. sinon.assert.calledWithExactly( spy, root, 0 );
  40. spy.restore();
  41. } );
  42. } );
  43. describe( 'createText()', () => {
  44. it( 'should create Text instance', () => {
  45. const text = writer.createText( 'foo bar' );
  46. expect( text.is( 'text' ) ).to.be.true;
  47. expect( text.data ).to.equal( 'foo bar' );
  48. } );
  49. } );
  50. describe( 'createAttributeElement()', () => {
  51. it( 'should create AttributeElement', () => {
  52. const element = writer.createAttributeElement( 'foo', attributes );
  53. expect( element.is( 'attributeElement' ) ).to.be.true;
  54. expect( element.name ).to.equal( 'foo' );
  55. assertElementAttributes( element, attributes );
  56. } );
  57. it( 'should allow to pass additional options', () => {
  58. const element = writer.createAttributeElement( 'foo', attributes, { priority: 99, id: 'bar' } );
  59. expect( element.is( 'attributeElement' ) ).to.be.true;
  60. expect( element.name ).to.equal( 'foo' );
  61. expect( element.priority ).to.equal( 99 );
  62. expect( element.id ).to.equal( 'bar' );
  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. describe( 'manages AttributeElement#_clonesGroup', () => {
  194. it( 'should return all clones of a broken attribute element with id', () => {
  195. const container = writer.createContainerElement( 'div' );
  196. const text = writer.createText( 'abccccde' );
  197. writer.insert( ViewPosition.createAt( container, 0 ), text );
  198. const span = writer.createAttributeElement( 'span', null, { id: 'foo' } );
  199. span._priority = 20;
  200. // <div>ab<span>cccc</span>de</div>
  201. writer.wrap( ViewRange.createFromParentsAndOffsets( text, 2, text, 6 ), span );
  202. const i = writer.createAttributeElement( 'i' );
  203. // <div>a<i>b<span>c</span></i><span>cc</span>de</div>
  204. writer.wrap(
  205. ViewRange.createFromParentsAndOffsets(
  206. container.getChild( 0 ), 1,
  207. container.getChild( 1 ).getChild( 0 ), 1
  208. ),
  209. i
  210. );
  211. // <div>a<i>b<span>c</span></i><span>c</span><i><span>cc</span>d</i>e</div>
  212. writer.wrap(
  213. ViewRange.createFromParentsAndOffsets(
  214. container.getChild( 2 ).getChild( 0 ), 1,
  215. container.getChild( 3 ), 1
  216. ),
  217. i
  218. );
  219. // Find all spans.
  220. const allSpans = Array.from( ViewRange.createIn( container ).getItems() ).filter( element => element.is( 'span' ) );
  221. // For each of the spans created above...
  222. for ( const oneOfAllSpans of allSpans ) {
  223. const brokenSet = oneOfAllSpans.getElementsWithSameId();
  224. const brokenArray = Array.from( brokenSet );
  225. // Check if all spans are included.
  226. for ( const s of allSpans ) {
  227. expect( brokenSet.has( s ) ).to.be.true;
  228. }
  229. expect( brokenArray.length ).to.equal( allSpans.length );
  230. }
  231. } );
  232. } );
  233. function assertElementAttributes( element, attributes ) {
  234. for ( const key of Object.keys( attributes ) ) {
  235. if ( element.getAttribute( key ) !== attributes[ key ] ) {
  236. throw new Error( 'Attributes in element are different that those passed to the constructor method.' );
  237. }
  238. }
  239. }
  240. } );