8
0

writer.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. } );
  118. it( 'should allow to pass custom rendering method', () => {
  119. const renderFn = function() {};
  120. const element = writer.createRawElement( 'foo', attributes, renderFn );
  121. expect( element.is( 'rawElement' ) ).to.be.true;
  122. expect( element.name ).to.equal( 'foo' );
  123. expect( element.render ).to.equal( renderFn );
  124. assertElementAttributes( element, attributes );
  125. } );
  126. } );
  127. describe( 'setAttribute()', () => {
  128. it( 'should set attribute on given element', () => {
  129. const element = writer.createAttributeElement( 'span' );
  130. writer.setAttribute( 'foo', 'bar', element );
  131. expect( element.getAttribute( 'foo' ) ).to.equal( 'bar' );
  132. } );
  133. } );
  134. describe( 'removeAttribute()', () => {
  135. it( 'should remove attribute on given element', () => {
  136. const element = writer.createAttributeElement( 'span', { foo: 'bar' } );
  137. writer.removeAttribute( 'foo', element );
  138. expect( element.getAttribute( 'foo' ) ).to.be.undefined;
  139. } );
  140. } );
  141. describe( 'addClass()', () => {
  142. it( 'should add class to given element', () => {
  143. const element = writer.createAttributeElement( 'span' );
  144. writer.addClass( 'foo', element );
  145. expect( element.hasClass( 'foo' ) ).to.be.true;
  146. } );
  147. it( 'should add multiple classes to given element', () => {
  148. const element = writer.createAttributeElement( 'span' );
  149. writer.addClass( [ 'foo', 'bar' ], element );
  150. expect( element.hasClass( 'foo' ) ).to.be.true;
  151. expect( element.hasClass( 'bar' ) ).to.be.true;
  152. } );
  153. } );
  154. describe( 'removeClass()', () => {
  155. it( 'should remove class from given element', () => {
  156. const element = writer.createAttributeElement( 'span', { class: 'foo bar' } );
  157. writer.removeClass( 'foo', element );
  158. expect( element.hasClass( 'foo' ) ).to.be.false;
  159. expect( element.hasClass( 'bar' ) ).to.be.true;
  160. } );
  161. it( 'should remove multiple classes from given element', () => {
  162. const element = writer.createAttributeElement( 'span', { class: 'foo bar' } );
  163. writer.removeClass( [ 'foo', 'bar' ], element );
  164. expect( element.hasClass( 'foo' ) ).to.be.false;
  165. expect( element.hasClass( 'bar' ) ).to.be.false;
  166. } );
  167. } );
  168. describe( 'addStyle()', () => {
  169. it( 'should add style to given element', () => {
  170. const element = writer.createAttributeElement( 'span' );
  171. writer.setStyle( 'foo', 'bar', element );
  172. expect( element.getStyle( 'foo' ) ).to.equal( 'bar' );
  173. } );
  174. it( 'should allow to add multiple styles to given element', () => {
  175. const element = writer.createAttributeElement( 'span' );
  176. writer.setStyle( {
  177. foo: 'bar',
  178. baz: 'quiz'
  179. }, element );
  180. expect( element.getStyle( 'foo' ) ).to.equal( 'bar' );
  181. expect( element.getStyle( 'baz' ) ).to.equal( 'quiz' );
  182. } );
  183. } );
  184. describe( 'removeStyle()', () => {
  185. it( 'should remove style from given element', () => {
  186. const element = writer.createAttributeElement( 'span', { style: 'foo:bar;baz:quiz;' } );
  187. writer.removeStyle( 'foo', element );
  188. expect( element.hasStyle( 'foo' ) ).to.be.false;
  189. expect( element.hasStyle( 'baz' ) ).to.be.true;
  190. } );
  191. it( 'should remove multiple styles from given element', () => {
  192. const element = writer.createAttributeElement( 'span', { style: 'foo:bar;baz:quiz;' } );
  193. writer.removeStyle( [ 'foo', 'bar' ], element );
  194. expect( element.hasStyle( 'foo' ) ).to.be.false;
  195. expect( element.hasStyle( 'baz' ) ).to.be.true;
  196. } );
  197. } );
  198. describe( 'setCustomProperty()', () => {
  199. it( 'should set custom property to given element', () => {
  200. const element = writer.createAttributeElement( 'span' );
  201. writer.setCustomProperty( 'foo', 'bar', element );
  202. expect( element.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
  203. } );
  204. } );
  205. describe( 'removeCustomProperty()', () => {
  206. it( 'should remove custom property from given element', () => {
  207. const element = writer.createAttributeElement( 'span' );
  208. writer.setCustomProperty( 'foo', 'bar', element );
  209. expect( element.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
  210. writer.removeCustomProperty( 'foo', element );
  211. expect( element.getCustomProperty( 'foo' ) ).to.be.undefined;
  212. } );
  213. } );
  214. describe( 'createPositionAt()', () => {
  215. it( 'should return instance of Position', () => {
  216. doc.getRoot()._appendChild( new ViewElement( 'p' ) );
  217. expect( writer.createPositionAt( doc.getRoot(), 0 ) ).to.be.instanceof( ViewPosition );
  218. } );
  219. } );
  220. describe( 'createPositionAfter()', () => {
  221. it( 'should return instance of Position', () => {
  222. doc.getRoot()._appendChild( new ViewElement( 'p' ) );
  223. expect( writer.createPositionAfter( doc.getRoot().getChild( 0 ) ) ).to.be.instanceof( ViewPosition );
  224. } );
  225. } );
  226. describe( 'createPositionBefore()', () => {
  227. it( 'should return instance of Position', () => {
  228. doc.getRoot()._appendChild( new ViewElement( 'p' ) );
  229. expect( writer.createPositionBefore( doc.getRoot().getChild( 0 ) ) ).to.be.instanceof( ViewPosition );
  230. } );
  231. } );
  232. describe( 'createRange()', () => {
  233. it( 'should return instance of Range', () => {
  234. doc.getRoot()._appendChild( new ViewElement( 'p' ) );
  235. expect( writer.createRange( writer.createPositionAt( doc.getRoot(), 0 ) ) ).to.be.instanceof( ViewRange );
  236. } );
  237. } );
  238. describe( 'createRangeIn()', () => {
  239. it( 'should return instance of Range', () => {
  240. doc.getRoot()._appendChild( new ViewElement( 'p' ) );
  241. expect( writer.createRangeIn( doc.getRoot().getChild( 0 ) ) ).to.be.instanceof( ViewRange );
  242. } );
  243. } );
  244. describe( 'createRangeOn()', () => {
  245. it( 'should return instance of Range', () => {
  246. doc.getRoot()._appendChild( new ViewElement( 'p' ) );
  247. expect( writer.createRangeOn( doc.getRoot().getChild( 0 ) ) ).to.be.instanceof( ViewRange );
  248. } );
  249. } );
  250. describe( 'createSelection()', () => {
  251. it( 'should return instance of Selection', () => {
  252. doc.getRoot()._appendChild( new ViewElement( 'p' ) );
  253. expect( writer.createSelection() ).to.be.instanceof( ViewSelection );
  254. } );
  255. } );
  256. describe( 'manages AttributeElement#_clonesGroup', () => {
  257. it( 'should return all clones of a broken attribute element with id', () => {
  258. const text = writer.createText( 'abccccde' );
  259. writer.insert( ViewPosition._createAt( root, 0 ), text );
  260. const span = writer.createAttributeElement( 'span', null, { id: 'foo' } );
  261. span._priority = 20;
  262. // <div>ab<span>cccc</span>de</div>
  263. writer.wrap( ViewRange._createFromParentsAndOffsets( text, 2, text, 6 ), span );
  264. const i = writer.createAttributeElement( 'i' );
  265. // <div>a<i>b<span>c</span></i><span>cc</span>de</div>
  266. writer.wrap(
  267. ViewRange._createFromParentsAndOffsets(
  268. root.getChild( 0 ), 1,
  269. root.getChild( 1 ).getChild( 0 ), 1
  270. ),
  271. i
  272. );
  273. // <div>a<i>b<span>c</span></i><span>c</span><i><span>cc</span>d</i>e</div>
  274. writer.wrap(
  275. ViewRange._createFromParentsAndOffsets(
  276. root.getChild( 2 ).getChild( 0 ), 1,
  277. root.getChild( 3 ), 1
  278. ),
  279. i
  280. );
  281. // Find all spans.
  282. const allSpans = Array.from( ViewRange._createIn( root ).getItems() ).filter( element => element.is( 'span' ) );
  283. // For each of the spans created above...
  284. for ( const oneOfAllSpans of allSpans ) {
  285. const brokenSet = oneOfAllSpans.getElementsWithSameId();
  286. const brokenArray = Array.from( brokenSet );
  287. // Check if all spans are included.
  288. for ( const s of allSpans ) {
  289. expect( brokenSet.has( s ) ).to.be.true;
  290. }
  291. expect( brokenArray.length ).to.equal( allSpans.length );
  292. }
  293. } );
  294. it( 'should not create groups for attribute elements that are not created in document root', () => {
  295. const p = writer.createContainerElement( 'p' );
  296. const foo = writer.createText( 'foo' );
  297. writer.insert( ViewPosition._createAt( p, 0 ), foo );
  298. // <p>foo</p>
  299. const span = writer.createAttributeElement( 'span', null, { id: 'span' } );
  300. // <p><span>foo</span></p>
  301. writer.wrap( ViewRange._createFromParentsAndOffsets( foo, 0, foo, 3 ), span );
  302. // Find the span.
  303. const createdSpan = p.getChild( 0 );
  304. expect( createdSpan.getElementsWithSameId().size ).to.equal( 0 );
  305. } );
  306. it( 'should add attribute elements to clone groups deeply', () => {
  307. const p = writer.createContainerElement( 'p' );
  308. const foo = writer.createText( 'foo' );
  309. writer.insert( ViewPosition._createAt( p, 0 ), foo );
  310. // <p>foo</p>
  311. const span = writer.createAttributeElement( 'span', null, { id: 'span' } );
  312. // <p><span>foo</span></p>
  313. writer.wrap( ViewRange._createFromParentsAndOffsets( foo, 0, foo, 3 ), span );
  314. // <div><p><span>foo</span></p>
  315. writer.insert( ViewPosition._createAt( root, 0 ), p );
  316. // Find the span.
  317. const createdSpan = p.getChild( 0 );
  318. expect( Array.from( createdSpan.getElementsWithSameId() ) ).to.deep.equal( [ createdSpan ] );
  319. } );
  320. it( 'should remove attribute elements from clone groups deeply', () => {
  321. const p1 = writer.createContainerElement( 'p' );
  322. const p2 = writer.createContainerElement( 'p' );
  323. const foo = writer.createText( 'foo' );
  324. const bar = writer.createText( 'bar' );
  325. writer.insert( ViewPosition._createAt( root, 0 ), p1 );
  326. writer.insert( ViewPosition._createAt( root, 1 ), p2 );
  327. writer.insert( ViewPosition._createAt( p1, 0 ), foo );
  328. writer.insert( ViewPosition._createAt( p2, 0 ), bar );
  329. // <div><p>foo</p><p>bar</p></div>
  330. const span = writer.createAttributeElement( 'span', null, { id: 'span' } );
  331. // <div><p>fo<span>o</span></p><p>bar</p></div>
  332. writer.wrap( ViewRange._createFromParentsAndOffsets( foo, 2, foo, 3 ), span );
  333. // <div><p>fo<span>o</span></p><p><span>b</span>ar</p></div>
  334. writer.wrap( ViewRange._createFromParentsAndOffsets( bar, 0, bar, 1 ), span );
  335. // <div><p><span>b</span>ar</p></div>
  336. writer.remove( ViewRange._createOn( p1 ) );
  337. // Find the span.
  338. const spanInTree = p2.getChild( 0 );
  339. expect( Array.from( spanInTree.getElementsWithSameId() ) ).to.deep.equal( [ spanInTree ] );
  340. } );
  341. } );
  342. function assertElementAttributes( element, attributes ) {
  343. for ( const key of Object.keys( attributes ) ) {
  344. if ( element.getAttribute( key ) !== attributes[ key ] ) {
  345. throw new Error( 'Attributes in element are different that those passed to the constructor method.' );
  346. }
  347. }
  348. }
  349. } );