utils.js 17 KB

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