utils.js 15 KB

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