8
0

writer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import DowncastWriter from '../../../src/view/downcastwriter';
  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( 'DowncastWriter', () => {
  12. let writer, attributes, root, doc;
  13. beforeEach( () => {
  14. attributes = { foo: 'bar', baz: 'quz' };
  15. doc = new Document();
  16. root = createViewRoot( doc );
  17. writer = new DowncastWriter( 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 position = ViewPosition.createAt( root );
  38. writer.setSelection( position );
  39. const spy = sinon.spy( writer.document.selection, '_setFocus' );
  40. writer.setSelectionFocus( root, 0 );
  41. sinon.assert.calledWithExactly( spy, root, 0 );
  42. spy.restore();
  43. } );
  44. } );
  45. describe( 'createText()', () => {
  46. it( 'should create Text instance', () => {
  47. const text = writer.createText( 'foo bar' );
  48. expect( text.is( 'text' ) ).to.be.true;
  49. expect( text.data ).to.equal( 'foo bar' );
  50. } );
  51. } );
  52. describe( 'createAttributeElement()', () => {
  53. it( 'should create AttributeElement', () => {
  54. const element = writer.createAttributeElement( 'foo', attributes );
  55. expect( element.is( 'attributeElement' ) ).to.be.true;
  56. expect( element.name ).to.equal( 'foo' );
  57. assertElementAttributes( element, attributes );
  58. } );
  59. it( 'should allow to pass additional options', () => {
  60. const element = writer.createAttributeElement( 'foo', attributes, { priority: 99, id: 'bar' } );
  61. expect( element.is( 'attributeElement' ) ).to.be.true;
  62. expect( element.name ).to.equal( 'foo' );
  63. expect( element.priority ).to.equal( 99 );
  64. expect( element.id ).to.equal( 'bar' );
  65. assertElementAttributes( element, attributes );
  66. } );
  67. } );
  68. describe( 'createContainerElement()', () => {
  69. it( 'should create ContainerElement', () => {
  70. const element = writer.createContainerElement( 'foo', attributes );
  71. expect( element.is( 'containerElement' ) ).to.be.true;
  72. expect( element.name ).to.equal( 'foo' );
  73. assertElementAttributes( element, attributes );
  74. } );
  75. } );
  76. describe( 'createEditableElement()', () => {
  77. it( 'should create EditableElement', () => {
  78. const element = writer.createEditableElement( 'foo', attributes );
  79. expect( element ).to.be.instanceOf( EditableElement );
  80. expect( element.name ).to.equal( 'foo' );
  81. assertElementAttributes( element, attributes );
  82. } );
  83. } );
  84. describe( 'createEmptyElement()', () => {
  85. it( 'should create EmptyElement', () => {
  86. const element = writer.createEmptyElement( 'foo', attributes );
  87. expect( element.is( 'emptyElement' ) ).to.be.true;
  88. expect( element.name ).to.equal( 'foo' );
  89. assertElementAttributes( element, attributes );
  90. } );
  91. } );
  92. describe( 'createUIElement()', () => {
  93. it( 'should create UIElement', () => {
  94. const element = writer.createUIElement( 'foo', attributes );
  95. expect( element.is( 'uiElement' ) ).to.be.true;
  96. expect( element.name ).to.equal( 'foo' );
  97. assertElementAttributes( element, attributes );
  98. } );
  99. it( 'should allow to pass custom rendering method', () => {
  100. const renderFn = function() {};
  101. const element = writer.createUIElement( 'foo', attributes, renderFn );
  102. expect( element.is( 'uiElement' ) ).to.be.true;
  103. expect( element.name ).to.equal( 'foo' );
  104. expect( element.render ).to.equal( renderFn );
  105. assertElementAttributes( element, attributes );
  106. } );
  107. } );
  108. describe( 'setAttribute()', () => {
  109. it( 'should set attribute on given element', () => {
  110. const element = writer.createAttributeElement( 'span' );
  111. writer.setAttribute( 'foo', 'bar', element );
  112. expect( element.getAttribute( 'foo' ) ).to.equal( 'bar' );
  113. } );
  114. } );
  115. describe( 'removeAttribute()', () => {
  116. it( 'should remove attribute on given element', () => {
  117. const element = writer.createAttributeElement( 'span', { foo: 'bar' } );
  118. writer.removeAttribute( 'foo', element );
  119. expect( element.getAttribute( 'foo' ) ).to.be.undefined;
  120. } );
  121. } );
  122. describe( 'addClass()', () => {
  123. it( 'should add class to given element', () => {
  124. const element = writer.createAttributeElement( 'span' );
  125. writer.addClass( 'foo', element );
  126. expect( element.hasClass( 'foo' ) ).to.be.true;
  127. } );
  128. it( 'should add multiple classes to given element', () => {
  129. const element = writer.createAttributeElement( 'span' );
  130. writer.addClass( [ 'foo', 'bar' ], element );
  131. expect( element.hasClass( 'foo' ) ).to.be.true;
  132. expect( element.hasClass( 'bar' ) ).to.be.true;
  133. } );
  134. } );
  135. describe( 'removeClass()', () => {
  136. it( 'should remove class from given element', () => {
  137. const element = writer.createAttributeElement( 'span', { class: 'foo bar' } );
  138. writer.removeClass( 'foo', element );
  139. expect( element.hasClass( 'foo' ) ).to.be.false;
  140. expect( element.hasClass( 'bar' ) ).to.be.true;
  141. } );
  142. it( 'should remove multiple classes from given element', () => {
  143. const element = writer.createAttributeElement( 'span', { class: 'foo bar' } );
  144. writer.removeClass( [ 'foo', 'bar' ], element );
  145. expect( element.hasClass( 'foo' ) ).to.be.false;
  146. expect( element.hasClass( 'bar' ) ).to.be.false;
  147. } );
  148. } );
  149. describe( 'addStyle()', () => {
  150. it( 'should add style to given element', () => {
  151. const element = writer.createAttributeElement( 'span' );
  152. writer.setStyle( 'foo', 'bar', element );
  153. expect( element.getStyle( 'foo' ) ).to.equal( 'bar' );
  154. } );
  155. it( 'should allow to add multiple styles to given element', () => {
  156. const element = writer.createAttributeElement( 'span' );
  157. writer.setStyle( {
  158. foo: 'bar',
  159. baz: 'quiz'
  160. }, element );
  161. expect( element.getStyle( 'foo' ) ).to.equal( 'bar' );
  162. expect( element.getStyle( 'baz' ) ).to.equal( 'quiz' );
  163. } );
  164. } );
  165. describe( 'removeStyle()', () => {
  166. it( 'should remove style from given element', () => {
  167. const element = writer.createAttributeElement( 'span', { style: 'foo:bar;baz:quiz;' } );
  168. writer.removeStyle( 'foo', element );
  169. expect( element.hasStyle( 'foo' ) ).to.be.false;
  170. expect( element.hasStyle( 'baz' ) ).to.be.true;
  171. } );
  172. it( 'should remove multiple styles from given element', () => {
  173. const element = writer.createAttributeElement( 'span', { style: 'foo:bar;baz:quiz;' } );
  174. writer.removeStyle( [ 'foo', 'bar' ], element );
  175. expect( element.hasStyle( 'foo' ) ).to.be.false;
  176. expect( element.hasStyle( 'baz' ) ).to.be.true;
  177. } );
  178. } );
  179. describe( 'setCustomProperty()', () => {
  180. it( 'should set custom property to given element', () => {
  181. const element = writer.createAttributeElement( 'span' );
  182. writer.setCustomProperty( 'foo', 'bar', element );
  183. expect( element.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
  184. } );
  185. } );
  186. describe( 'removeCustomProperty()', () => {
  187. it( 'should remove custom property from given element', () => {
  188. const element = writer.createAttributeElement( 'span' );
  189. writer.setCustomProperty( 'foo', 'bar', element );
  190. expect( element.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
  191. writer.removeCustomProperty( 'foo', element );
  192. expect( element.getCustomProperty( 'foo' ) ).to.be.undefined;
  193. } );
  194. } );
  195. describe( 'manages AttributeElement#_clonesGroup', () => {
  196. it( 'should return all clones of a broken attribute element with id', () => {
  197. const text = writer.createText( 'abccccde' );
  198. writer.insert( ViewPosition.createAt( root, 0 ), text );
  199. const span = writer.createAttributeElement( 'span', null, { id: 'foo' } );
  200. span._priority = 20;
  201. // <div>ab<span>cccc</span>de</div>
  202. writer.wrap( ViewRange.createFromParentsAndOffsets( text, 2, text, 6 ), span );
  203. const i = writer.createAttributeElement( 'i' );
  204. // <div>a<i>b<span>c</span></i><span>cc</span>de</div>
  205. writer.wrap(
  206. ViewRange.createFromParentsAndOffsets(
  207. root.getChild( 0 ), 1,
  208. root.getChild( 1 ).getChild( 0 ), 1
  209. ),
  210. i
  211. );
  212. // <div>a<i>b<span>c</span></i><span>c</span><i><span>cc</span>d</i>e</div>
  213. writer.wrap(
  214. ViewRange.createFromParentsAndOffsets(
  215. root.getChild( 2 ).getChild( 0 ), 1,
  216. root.getChild( 3 ), 1
  217. ),
  218. i
  219. );
  220. // Find all spans.
  221. const allSpans = Array.from( ViewRange.createIn( root ).getItems() ).filter( element => element.is( 'span' ) );
  222. // For each of the spans created above...
  223. for ( const oneOfAllSpans of allSpans ) {
  224. const brokenSet = oneOfAllSpans.getElementsWithSameId();
  225. const brokenArray = Array.from( brokenSet );
  226. // Check if all spans are included.
  227. for ( const s of allSpans ) {
  228. expect( brokenSet.has( s ) ).to.be.true;
  229. }
  230. expect( brokenArray.length ).to.equal( allSpans.length );
  231. }
  232. } );
  233. it( 'should not create groups for attribute elements that are not created in document root', () => {
  234. const p = writer.createContainerElement( 'p' );
  235. const foo = writer.createText( 'foo' );
  236. writer.insert( ViewPosition.createAt( p, 0 ), foo );
  237. // <p>foo</p>
  238. const span = writer.createAttributeElement( 'span', null, { id: 'span' } );
  239. // <p><span>foo</span></p>
  240. writer.wrap( ViewRange.createFromParentsAndOffsets( foo, 0, foo, 3 ), span );
  241. // Find the span.
  242. const createdSpan = p.getChild( 0 );
  243. expect( createdSpan.getElementsWithSameId().size ).to.equal( 0 );
  244. } );
  245. it( 'should add attribute elements to clone groups deeply', () => {
  246. const p = writer.createContainerElement( 'p' );
  247. const foo = writer.createText( 'foo' );
  248. writer.insert( ViewPosition.createAt( p, 0 ), foo );
  249. // <p>foo</p>
  250. const span = writer.createAttributeElement( 'span', null, { id: 'span' } );
  251. // <p><span>foo</span></p>
  252. writer.wrap( ViewRange.createFromParentsAndOffsets( foo, 0, foo, 3 ), span );
  253. // <div><p><span>foo</span></p>
  254. writer.insert( ViewPosition.createAt( root, 0 ), p );
  255. // Find the span.
  256. const createdSpan = p.getChild( 0 );
  257. expect( Array.from( createdSpan.getElementsWithSameId() ) ).to.deep.equal( [ createdSpan ] );
  258. } );
  259. it( 'should remove attribute elements from clone groups deeply', () => {
  260. const p1 = writer.createContainerElement( 'p' );
  261. const p2 = writer.createContainerElement( 'p' );
  262. const foo = writer.createText( 'foo' );
  263. const bar = writer.createText( 'bar' );
  264. writer.insert( ViewPosition.createAt( root, 0 ), p1 );
  265. writer.insert( ViewPosition.createAt( root, 1 ), p2 );
  266. writer.insert( ViewPosition.createAt( p1, 0 ), foo );
  267. writer.insert( ViewPosition.createAt( p2, 0 ), bar );
  268. // <div><p>foo</p><p>bar</p></div>
  269. const span = writer.createAttributeElement( 'span', null, { id: 'span' } );
  270. // <div><p>fo<span>o</span></p><p>bar</p></div>
  271. writer.wrap( ViewRange.createFromParentsAndOffsets( foo, 2, foo, 3 ), span );
  272. // <div><p>fo<span>o</span></p><p><span>b</span>ar</p></div>
  273. writer.wrap( ViewRange.createFromParentsAndOffsets( bar, 0, bar, 1 ), span );
  274. // <div><p><span>b</span>ar</p></div>
  275. writer.remove( ViewRange.createOn( p1 ) );
  276. // Find the span.
  277. const spanInTree = p2.getChild( 0 );
  278. expect( Array.from( spanInTree.getElementsWithSameId() ) ).to.deep.equal( [ spanInTree ] );
  279. } );
  280. } );
  281. function assertElementAttributes( element, attributes ) {
  282. for ( const key of Object.keys( attributes ) ) {
  283. if ( element.getAttribute( key ) !== attributes[ key ] ) {
  284. throw new Error( 'Attributes in element are different that those passed to the constructor method.' );
  285. }
  286. }
  287. }
  288. } );