writer.js 14 KB

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