8
0

writer.js 15 KB

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