utils.js 19 KB

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