8
0

utils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 Writer from '@ckeditor/ckeditor5-engine/src/view/writer';
  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. WIDGET_CLASS_NAME
  18. } from '../src/utils';
  19. import UIElement from '@ckeditor/ckeditor5-engine/src/view/uielement';
  20. import env from '@ckeditor/ckeditor5-utils/src/env';
  21. describe( 'widget utils', () => {
  22. const initialEnvEdge = env.isEdge;
  23. let element, writer, viewDocument;
  24. beforeEach( () => {
  25. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  26. env.isEdge = false;
  27. viewDocument = new ViewDocument();
  28. writer = new Writer( viewDocument );
  29. element = writer.createContainerElement( 'div' );
  30. toWidget( element, writer );
  31. } );
  32. afterEach( () => {
  33. env.isEdge = initialEnvEdge;
  34. } );
  35. describe( 'toWidget()', () => {
  36. it( 'should set contenteditable to "false"', () => {
  37. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  38. } );
  39. it( 'should define getFillerOffset method', () => {
  40. expect( element.getFillerOffset ).to.be.a( 'function' );
  41. expect( element.getFillerOffset() ).to.be.null;
  42. } );
  43. it( 'should add proper CSS class', () => {
  44. expect( element.hasClass( WIDGET_CLASS_NAME ) ).to.be.true;
  45. } );
  46. it( 'should add element\'s label if one is provided', () => {
  47. toWidget( element, writer, { label: 'foo bar baz label' } );
  48. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  49. } );
  50. it( 'should add element\'s label if one is provided as function', () => {
  51. toWidget( element, writer, { label: () => 'foo bar baz label' } );
  52. expect( getLabel( element ) ).to.equal( 'foo bar baz label' );
  53. } );
  54. it( 'should set default highlight handling methods', () => {
  55. toWidget( element, writer );
  56. const set = element.getCustomProperty( 'addHighlight' );
  57. const remove = element.getCustomProperty( 'removeHighlight' );
  58. expect( typeof set ).to.equal( 'function' );
  59. expect( typeof remove ).to.equal( 'function' );
  60. set( element, { priority: 1, classes: 'highlight', id: 'highlight' }, writer );
  61. expect( element.hasClass( 'highlight' ) ).to.be.true;
  62. remove( element, 'highlight', writer );
  63. expect( element.hasClass( 'highlight' ) ).to.be.false;
  64. } );
  65. it( 'should set default highlight handling methods - CSS classes array', () => {
  66. toWidget( element, writer );
  67. const set = element.getCustomProperty( 'addHighlight' );
  68. const remove = element.getCustomProperty( 'removeHighlight' );
  69. expect( typeof set ).to.equal( 'function' );
  70. expect( typeof remove ).to.equal( 'function' );
  71. set( element, { priority: 1, classes: [ 'highlight', 'foo' ], id: 'highlight' }, writer );
  72. expect( element.hasClass( 'highlight' ) ).to.be.true;
  73. expect( element.hasClass( 'foo' ) ).to.be.true;
  74. remove( element, 'highlight', writer );
  75. expect( element.hasClass( 'highlight' ) ).to.be.false;
  76. expect( element.hasClass( 'foo' ) ).to.be.false;
  77. } );
  78. it( 'should add element a selection handler to widget if hasSelectionHandler=true is passed', () => {
  79. toWidget( element, writer, { hasSelectionHandler: true } );
  80. expect( element.hasClass( 'ck-widget_selectable' ) ).to.be.true;
  81. const selectionHandler = element.getChild( 0 );
  82. expect( selectionHandler ).to.be.instanceof( UIElement );
  83. const domSelectionHandler = selectionHandler.render( document );
  84. expect( domSelectionHandler.classList.contains( 'ck' ) ).to.be.true;
  85. expect( domSelectionHandler.classList.contains( 'ck-widget__selection-handler' ) ).to.be.true;
  86. const icon = domSelectionHandler.firstChild;
  87. expect( icon.nodeName ).to.equal( 'svg' );
  88. expect( icon.classList.contains( 'ck' ) ).to.be.true;
  89. expect( icon.classList.contains( 'ck-icon' ) ).to.be.true;
  90. } );
  91. describe( 'on Edge', () => {
  92. beforeEach( () => {
  93. env.isEdge = true;
  94. element = writer.createContainerElement( 'div' );
  95. toWidget( element, writer );
  96. } );
  97. it( 'should not set contenteditable onEdge', () => {
  98. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  99. } );
  100. } );
  101. } );
  102. describe( 'isWidget()', () => {
  103. it( 'should return true for widgetized elements', () => {
  104. expect( isWidget( element ) ).to.be.true;
  105. } );
  106. it( 'should return false for non-widgetized elements', () => {
  107. expect( isWidget( new ViewElement( 'p' ) ) ).to.be.false;
  108. } );
  109. } );
  110. describe( 'label utils', () => {
  111. it( 'should allow to set label for element', () => {
  112. const element = new ViewElement( 'p' );
  113. setLabel( element, 'foo bar baz', writer );
  114. expect( getLabel( element ) ).to.equal( 'foo bar baz' );
  115. } );
  116. it( 'should return empty string for elements without label', () => {
  117. const element = new ViewElement( 'div' );
  118. expect( getLabel( element ) ).to.equal( '' );
  119. } );
  120. it( 'should allow to use a function as label creator', () => {
  121. const element = new ViewElement( 'p' );
  122. let caption = 'foo';
  123. setLabel( element, () => caption, writer );
  124. expect( getLabel( element ) ).to.equal( 'foo' );
  125. caption = 'bar';
  126. expect( getLabel( element ) ).to.equal( 'bar' );
  127. } );
  128. } );
  129. describe( 'toWidgetEditable', () => {
  130. let viewDocument, element;
  131. beforeEach( () => {
  132. viewDocument = new ViewDocument();
  133. element = new ViewEditableElement( 'div' );
  134. element._document = viewDocument;
  135. toWidgetEditable( element, writer );
  136. } );
  137. it( 'should be created in context of proper document', () => {
  138. expect( element.document ).to.equal( viewDocument );
  139. } );
  140. it( 'should add proper class', () => {
  141. expect( element.hasClass( 'ck-editor__editable', 'ck-editor__nested-editable' ) ).to.be.true;
  142. } );
  143. it( 'should add proper contenteditable value when element is read-only - initialization', () => {
  144. const element = new ViewEditableElement( 'div' );
  145. element._document = viewDocument;
  146. element.isReadOnly = true;
  147. toWidgetEditable( element, writer );
  148. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  149. } );
  150. it( 'should add proper contenteditable value when element is read-only - when changing', () => {
  151. element.isReadOnly = true;
  152. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  153. element.isReadOnly = false;
  154. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'true' );
  155. } );
  156. it( 'should add proper class when element is focused', () => {
  157. element.isFocused = true;
  158. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.true;
  159. element.isFocused = false;
  160. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.false;
  161. } );
  162. describe( 'on Edge', () => {
  163. beforeEach( () => {
  164. env.isEdge = true;
  165. viewDocument = new ViewDocument();
  166. element = new ViewEditableElement( 'div' );
  167. element._document = viewDocument;
  168. toWidgetEditable( element, writer );
  169. } );
  170. it( 'should add contenteditable attribute when element is read-only - initialization', () => {
  171. const element = new ViewEditableElement( 'div' );
  172. element._document = viewDocument;
  173. element.isReadOnly = true;
  174. toWidgetEditable( element, writer );
  175. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  176. } );
  177. it( 'should add contenteditable attribute when element is read-only - when changing', () => {
  178. element.isReadOnly = true;
  179. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  180. element.isReadOnly = false;
  181. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  182. } );
  183. } );
  184. } );
  185. describe( 'addHighlightHandling()', () => {
  186. let element, addSpy, removeSpy, set, remove;
  187. beforeEach( () => {
  188. element = new ViewElement( 'p' );
  189. addSpy = sinon.spy();
  190. removeSpy = sinon.spy();
  191. setHighlightHandling( element, writer, addSpy, removeSpy );
  192. set = element.getCustomProperty( 'addHighlight' );
  193. remove = element.getCustomProperty( 'removeHighlight' );
  194. } );
  195. it( 'should set highlight handling methods', () => {
  196. expect( typeof set ).to.equal( 'function' );
  197. expect( typeof remove ).to.equal( 'function' );
  198. } );
  199. it( 'should call highlight methods when descriptor is added and removed', () => {
  200. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight' };
  201. set( element, descriptor, writer );
  202. remove( element, descriptor.id, writer );
  203. sinon.assert.calledOnce( addSpy );
  204. sinon.assert.calledWithExactly( addSpy, element, descriptor, writer );
  205. sinon.assert.calledOnce( removeSpy );
  206. sinon.assert.calledWithExactly( removeSpy, element, descriptor, writer );
  207. } );
  208. it( 'should call highlight methods when next descriptor is added', () => {
  209. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  210. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  211. set( element, descriptor );
  212. set( element, secondDescriptor );
  213. sinon.assert.calledTwice( addSpy );
  214. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  215. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  216. } );
  217. it( 'should not call highlight methods when descriptor with lower priority is added', () => {
  218. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  219. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  220. set( element, descriptor );
  221. set( element, secondDescriptor );
  222. sinon.assert.calledOnce( addSpy );
  223. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  224. } );
  225. it( 'should call highlight methods when descriptor is removed changing active descriptor', () => {
  226. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  227. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  228. set( element, descriptor );
  229. set( element, secondDescriptor );
  230. remove( element, secondDescriptor.id );
  231. sinon.assert.calledThrice( addSpy );
  232. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  233. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  234. expect( addSpy.thirdCall.args[ 1 ] ).to.equal( descriptor );
  235. sinon.assert.calledTwice( removeSpy );
  236. expect( removeSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  237. expect( removeSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  238. } );
  239. it( 'should call highlight methods when descriptor is removed not changing active descriptor', () => {
  240. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  241. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  242. set( element, descriptor );
  243. set( element, secondDescriptor );
  244. remove( element, secondDescriptor );
  245. sinon.assert.calledOnce( addSpy );
  246. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  247. sinon.assert.notCalled( removeSpy );
  248. } );
  249. it( 'should call highlight methods - CSS class array', () => {
  250. const descriptor = { priority: 10, classes: [ 'highlight', 'a' ], id: 'highlight-1' };
  251. const secondDescriptor = { priority: 10, classes: [ 'highlight', 'b' ], id: 'highlight-2' };
  252. set( element, descriptor );
  253. set( element, secondDescriptor );
  254. sinon.assert.calledTwice( addSpy );
  255. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  256. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  257. } );
  258. } );
  259. } );