utils.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import DowncastWriter from '@ckeditor/ckeditor5-engine/src/view/downcastwriter';
  7. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  8. import ViewEditableElement from '@ckeditor/ckeditor5-engine/src/view/editableelement';
  9. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  10. import {
  11. toWidget,
  12. isWidget,
  13. setLabel,
  14. getLabel,
  15. toWidgetEditable,
  16. setHighlightHandling,
  17. findOptimalInsertionPosition,
  18. WIDGET_CLASS_NAME
  19. } from '../src/utils';
  20. import UIElement from '@ckeditor/ckeditor5-engine/src/view/uielement';
  21. import env from '@ckeditor/ckeditor5-utils/src/env';
  22. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  23. import Model from '@ckeditor/ckeditor5-engine/src/model/model';
  24. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  25. describe( 'widget utils', () => {
  26. let element, writer, viewDocument;
  27. testUtils.createSinonSandbox();
  28. beforeEach( () => {
  29. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  30. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  31. viewDocument = new ViewDocument();
  32. writer = new DowncastWriter( viewDocument );
  33. element = writer.createContainerElement( 'div' );
  34. toWidget( element, writer );
  35. } );
  36. describe( 'toWidget()', () => {
  37. it( 'should set contenteditable to "false"', () => {
  38. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  39. } );
  40. it( 'should define getFillerOffset method', () => {
  41. expect( element.getFillerOffset ).to.be.a( 'function' );
  42. expect( element.getFillerOffset() ).to.be.null;
  43. } );
  44. it( 'should add proper CSS class', () => {
  45. expect( element.hasClass( WIDGET_CLASS_NAME ) ).to.be.true;
  46. } );
  47. it( 'should add element\'s label if one is provided', () => {
  48. toWidget( element, writer, { label: 'foo bar baz label' } );
  49. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  50. } );
  51. it( 'should add element\'s label if one is provided as function', () => {
  52. toWidget( element, writer, { label: () => 'foo bar baz label' } );
  53. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  54. } );
  55. it( 'should set default highlight handling methods', () => {
  56. toWidget( element, writer );
  57. const set = element.getCustomProperty( 'addHighlight' );
  58. const remove = element.getCustomProperty( 'removeHighlight' );
  59. expect( typeof set ).to.equal( 'function' );
  60. expect( typeof remove ).to.equal( 'function' );
  61. set( element, { priority: 1, classes: 'highlight', id: 'highlight' }, writer );
  62. expect( element.hasClass( 'highlight' ) ).to.be.true;
  63. remove( element, 'highlight', writer );
  64. expect( element.hasClass( 'highlight' ) ).to.be.false;
  65. } );
  66. it( 'should set default highlight handling methods - CSS classes array', () => {
  67. toWidget( element, writer );
  68. const set = element.getCustomProperty( 'addHighlight' );
  69. const remove = element.getCustomProperty( 'removeHighlight' );
  70. expect( typeof set ).to.equal( 'function' );
  71. expect( typeof remove ).to.equal( 'function' );
  72. set( element, { priority: 1, classes: [ 'highlight', 'foo' ], id: 'highlight' }, writer );
  73. expect( element.hasClass( 'highlight' ) ).to.be.true;
  74. expect( element.hasClass( 'foo' ) ).to.be.true;
  75. remove( element, 'highlight', writer );
  76. expect( element.hasClass( 'highlight' ) ).to.be.false;
  77. expect( element.hasClass( 'foo' ) ).to.be.false;
  78. } );
  79. it( 'should add element a selection handler to widget if hasSelectionHandler=true is passed', () => {
  80. toWidget( element, writer, { hasSelectionHandler: true } );
  81. expect( element.hasClass( 'ck-widget_selectable' ) ).to.be.true;
  82. const selectionHandler = element.getChild( 0 );
  83. expect( selectionHandler ).to.be.instanceof( UIElement );
  84. const domSelectionHandler = selectionHandler.render( document );
  85. expect( domSelectionHandler.classList.contains( 'ck' ) ).to.be.true;
  86. expect( domSelectionHandler.classList.contains( 'ck-widget__selection-handler' ) ).to.be.true;
  87. const icon = domSelectionHandler.firstChild;
  88. expect( icon.nodeName ).to.equal( 'svg' );
  89. expect( icon.classList.contains( 'ck' ) ).to.be.true;
  90. expect( icon.classList.contains( 'ck-icon' ) ).to.be.true;
  91. } );
  92. describe( 'on Edge', () => {
  93. beforeEach( () => {
  94. testUtils.sinon.stub( env, 'isEdge' ).get( () => true );
  95. element = writer.createContainerElement( 'div' );
  96. toWidget( element, writer );
  97. } );
  98. it( 'should not set contenteditable onEdge', () => {
  99. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  100. } );
  101. } );
  102. } );
  103. describe( 'isWidget()', () => {
  104. it( 'should return true for widgetized elements', () => {
  105. expect( isWidget( element ) ).to.be.true;
  106. } );
  107. it( 'should return false for non-widgetized elements', () => {
  108. expect( isWidget( new ViewElement( 'p' ) ) ).to.be.false;
  109. } );
  110. } );
  111. describe( 'label utils', () => {
  112. it( 'should allow to set label for element', () => {
  113. const element = new ViewElement( 'p' );
  114. setLabel( element, 'foo bar baz', writer );
  115. expect( getLabel( element ) ).to.equal( 'foo bar baz' );
  116. } );
  117. it( 'should return empty string for elements without label', () => {
  118. const element = new ViewElement( 'div' );
  119. expect( getLabel( element ) ).to.equal( '' );
  120. } );
  121. it( 'should allow to use a function as label creator', () => {
  122. const element = new ViewElement( 'p' );
  123. let caption = 'foo';
  124. setLabel( element, () => caption, writer );
  125. expect( getLabel( element ) ).to.equal( 'foo' );
  126. caption = 'bar';
  127. expect( getLabel( element ) ).to.equal( 'bar' );
  128. } );
  129. } );
  130. describe( 'toWidgetEditable', () => {
  131. let viewDocument, element;
  132. beforeEach( () => {
  133. viewDocument = new ViewDocument();
  134. element = new ViewEditableElement( 'div' );
  135. element._document = viewDocument;
  136. toWidgetEditable( element, writer );
  137. } );
  138. it( 'should be created in context of proper document', () => {
  139. expect( element.document ).to.equal( viewDocument );
  140. } );
  141. it( 'should add proper class', () => {
  142. expect( element.hasClass( 'ck-editor__editable', 'ck-editor__nested-editable' ) ).to.be.true;
  143. } );
  144. it( 'should add proper contenteditable value when element is read-only - initialization', () => {
  145. const element = new ViewEditableElement( 'div' );
  146. element._document = viewDocument;
  147. element.isReadOnly = true;
  148. toWidgetEditable( element, writer );
  149. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  150. } );
  151. it( 'should add proper contenteditable value when element is read-only - when changing', () => {
  152. element.isReadOnly = true;
  153. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  154. element.isReadOnly = false;
  155. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'true' );
  156. } );
  157. it( 'should add proper class when element is focused', () => {
  158. element.isFocused = true;
  159. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.true;
  160. element.isFocused = false;
  161. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.false;
  162. } );
  163. describe( 'on Edge', () => {
  164. beforeEach( () => {
  165. testUtils.sinon.stub( env, 'isEdge' ).get( () => true );
  166. viewDocument = new ViewDocument();
  167. element = new ViewEditableElement( 'div' );
  168. element._document = viewDocument;
  169. toWidgetEditable( element, writer );
  170. } );
  171. it( 'should add contenteditable attribute when element is read-only - initialization', () => {
  172. const element = new ViewEditableElement( 'div' );
  173. element._document = viewDocument;
  174. element.isReadOnly = true;
  175. toWidgetEditable( element, writer );
  176. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  177. } );
  178. it( 'should add contenteditable attribute when element is read-only - when changing', () => {
  179. element.isReadOnly = true;
  180. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  181. element.isReadOnly = false;
  182. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  183. } );
  184. } );
  185. } );
  186. describe( 'addHighlightHandling()', () => {
  187. let element, addSpy, removeSpy, set, remove;
  188. beforeEach( () => {
  189. element = new ViewElement( 'p' );
  190. addSpy = sinon.spy();
  191. removeSpy = sinon.spy();
  192. setHighlightHandling( element, writer, addSpy, removeSpy );
  193. set = element.getCustomProperty( 'addHighlight' );
  194. remove = element.getCustomProperty( 'removeHighlight' );
  195. } );
  196. it( 'should set highlight handling methods', () => {
  197. expect( typeof set ).to.equal( 'function' );
  198. expect( typeof remove ).to.equal( 'function' );
  199. } );
  200. it( 'should call highlight methods when descriptor is added and removed', () => {
  201. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight' };
  202. set( element, descriptor, writer );
  203. remove( element, descriptor.id, writer );
  204. sinon.assert.calledOnce( addSpy );
  205. sinon.assert.calledWithExactly( addSpy, element, descriptor, writer );
  206. sinon.assert.calledOnce( removeSpy );
  207. sinon.assert.calledWithExactly( removeSpy, element, descriptor, writer );
  208. } );
  209. it( 'should call highlight methods when next descriptor is added', () => {
  210. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  211. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  212. set( element, descriptor );
  213. set( element, secondDescriptor );
  214. sinon.assert.calledTwice( addSpy );
  215. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  216. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  217. } );
  218. it( 'should not call highlight methods when descriptor with lower priority is added', () => {
  219. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  220. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  221. set( element, descriptor );
  222. set( element, secondDescriptor );
  223. sinon.assert.calledOnce( addSpy );
  224. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  225. } );
  226. it( 'should call highlight methods when descriptor is removed changing active descriptor', () => {
  227. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  228. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  229. set( element, descriptor );
  230. set( element, secondDescriptor );
  231. remove( element, secondDescriptor.id );
  232. sinon.assert.calledThrice( addSpy );
  233. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  234. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  235. expect( addSpy.thirdCall.args[ 1 ] ).to.equal( descriptor );
  236. sinon.assert.calledTwice( removeSpy );
  237. expect( removeSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  238. expect( removeSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  239. } );
  240. it( 'should call highlight methods when descriptor is removed not changing active descriptor', () => {
  241. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  242. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  243. set( element, descriptor );
  244. set( element, secondDescriptor );
  245. remove( element, secondDescriptor );
  246. sinon.assert.calledOnce( addSpy );
  247. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  248. sinon.assert.notCalled( removeSpy );
  249. } );
  250. it( 'should call highlight methods - CSS class array', () => {
  251. const descriptor = { priority: 10, classes: [ 'highlight', 'a' ], id: 'highlight-1' };
  252. const secondDescriptor = { priority: 10, classes: [ 'highlight', 'b' ], id: 'highlight-2' };
  253. set( element, descriptor );
  254. set( element, secondDescriptor );
  255. sinon.assert.calledTwice( addSpy );
  256. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  257. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  258. } );
  259. } );
  260. describe( 'findOptimalInsertionPosition()', () => {
  261. let model, doc;
  262. beforeEach( () => {
  263. model = new Model();
  264. doc = model.document;
  265. doc.createRoot();
  266. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  267. model.schema.register( 'image' );
  268. model.schema.register( 'span' );
  269. model.schema.extend( 'image', {
  270. allowIn: '$root',
  271. isObject: true
  272. } );
  273. model.schema.extend( 'span', { allowIn: 'paragraph' } );
  274. model.schema.extend( '$text', { allowIn: 'span' } );
  275. } );
  276. it( 'returns position after selected element', () => {
  277. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  278. const pos = findOptimalInsertionPosition( doc.selection );
  279. expect( pos.path ).to.deep.equal( [ 2 ] );
  280. } );
  281. it( 'returns position inside empty block', () => {
  282. setData( model, '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  283. const pos = findOptimalInsertionPosition( doc.selection );
  284. expect( pos.path ).to.deep.equal( [ 1, 0 ] );
  285. } );
  286. it( 'returns position before block if at the beginning of that block', () => {
  287. setData( model, '<paragraph>x</paragraph><paragraph>[]foo</paragraph><paragraph>y</paragraph>' );
  288. const pos = findOptimalInsertionPosition( doc.selection );
  289. expect( pos.path ).to.deep.equal( [ 1 ] );
  290. } );
  291. it( 'returns position before block if in the middle of that block', () => {
  292. setData( model, '<paragraph>x</paragraph><paragraph>f[]oo</paragraph><paragraph>y</paragraph>' );
  293. const pos = findOptimalInsertionPosition( doc.selection );
  294. expect( pos.path ).to.deep.equal( [ 1 ] );
  295. } );
  296. it( 'returns position after block if at the end of that block', () => {
  297. setData( model, '<paragraph>x</paragraph><paragraph>foo[]</paragraph><paragraph>y</paragraph>' );
  298. const pos = findOptimalInsertionPosition( doc.selection );
  299. expect( pos.path ).to.deep.equal( [ 2 ] );
  300. } );
  301. // Checking if isTouching() was used.
  302. it( 'returns position after block if at the end of that block (deeply nested)', () => {
  303. setData( model, '<paragraph>x</paragraph><paragraph>foo<span>bar[]</span></paragraph><paragraph>y</paragraph>' );
  304. const pos = findOptimalInsertionPosition( doc.selection );
  305. expect( pos.path ).to.deep.equal( [ 2 ] );
  306. } );
  307. it( 'returns selection focus if not in a block', () => {
  308. model.schema.extend( '$text', { allowIn: '$root' } );
  309. setData( model, 'foo[]bar' );
  310. const pos = findOptimalInsertionPosition( doc.selection );
  311. expect( pos.path ).to.deep.equal( [ 3 ] );
  312. } );
  313. } );
  314. } );