writer.js 14 KB

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