utils.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /**
  2. * @license Copyright (c) 2003-2019, 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( 'p' ) ) ).to.be.false;
  115. } );
  116. it( 'should return false for text node', () => {
  117. expect( isWidget( new ViewText( 'p' ) ) ).to.be.false;
  118. } );
  119. } );
  120. describe( 'label utils', () => {
  121. it( 'should allow to set label for element', () => {
  122. const element = new ViewElement( '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( 'div' );
  128. expect( getLabel( element ) ).to.equal( '' );
  129. } );
  130. it( 'should allow to use a function as label creator', () => {
  131. const element = new ViewElement( '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( 'div' );
  144. element._document = viewDocument;
  145. toWidgetEditable( element, writer );
  146. } );
  147. it( 'should be created in context of proper document', () => {
  148. expect( element.document ).to.equal( viewDocument );
  149. } );
  150. it( 'should add proper class', () => {
  151. expect( element.hasClass( 'ck-editor__editable', 'ck-editor__nested-editable' ) ).to.be.true;
  152. } );
  153. it( 'should add proper contenteditable value when element is read-only - initialization', () => {
  154. const element = new ViewEditableElement( 'div' );
  155. element._document = viewDocument;
  156. element.isReadOnly = true;
  157. toWidgetEditable( element, writer );
  158. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  159. } );
  160. it( 'should add proper contenteditable value when element is read-only - when changing', () => {
  161. element.isReadOnly = true;
  162. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'false' );
  163. element.isReadOnly = false;
  164. expect( element.getAttribute( 'contenteditable' ) ).to.equal( 'true' );
  165. } );
  166. it( 'should add proper class when element is focused', () => {
  167. element.isFocused = true;
  168. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.true;
  169. element.isFocused = false;
  170. expect( element.hasClass( 'ck-editor__nested-editable_focused' ) ).to.be.false;
  171. } );
  172. describe( 'on Edge', () => {
  173. beforeEach( () => {
  174. testUtils.sinon.stub( env, 'isEdge' ).get( () => true );
  175. viewDocument = new ViewDocument();
  176. element = new ViewEditableElement( 'div' );
  177. element._document = viewDocument;
  178. toWidgetEditable( element, writer );
  179. } );
  180. it( 'should add contenteditable attribute when element is read-only - initialization', () => {
  181. const element = new ViewEditableElement( 'div' );
  182. element._document = viewDocument;
  183. element.isReadOnly = true;
  184. toWidgetEditable( element, writer );
  185. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  186. } );
  187. it( 'should add contenteditable attribute when element is read-only - when changing', () => {
  188. element.isReadOnly = true;
  189. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  190. element.isReadOnly = false;
  191. expect( element.getAttribute( 'contenteditable' ) ).to.be.undefined;
  192. } );
  193. } );
  194. } );
  195. describe( 'addHighlightHandling()', () => {
  196. let element, addSpy, removeSpy, set, remove;
  197. beforeEach( () => {
  198. element = new ViewElement( 'p' );
  199. addSpy = sinon.spy();
  200. removeSpy = sinon.spy();
  201. setHighlightHandling( element, writer, addSpy, removeSpy );
  202. set = element.getCustomProperty( 'addHighlight' );
  203. remove = element.getCustomProperty( 'removeHighlight' );
  204. } );
  205. it( 'should set highlight handling methods', () => {
  206. expect( typeof set ).to.equal( 'function' );
  207. expect( typeof remove ).to.equal( 'function' );
  208. } );
  209. it( 'should call highlight methods when descriptor is added and removed', () => {
  210. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight' };
  211. set( element, descriptor, writer );
  212. remove( element, descriptor.id, writer );
  213. sinon.assert.calledOnce( addSpy );
  214. sinon.assert.calledWithExactly( addSpy, element, descriptor, writer );
  215. sinon.assert.calledOnce( removeSpy );
  216. sinon.assert.calledWithExactly( removeSpy, element, descriptor, writer );
  217. } );
  218. it( 'should call highlight methods when next descriptor is added', () => {
  219. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  220. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  221. set( element, descriptor );
  222. set( element, secondDescriptor );
  223. sinon.assert.calledTwice( addSpy );
  224. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  225. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  226. } );
  227. it( 'should not call highlight methods when descriptor with lower priority is added', () => {
  228. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  229. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  230. set( element, descriptor );
  231. set( element, secondDescriptor );
  232. sinon.assert.calledOnce( addSpy );
  233. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  234. } );
  235. it( 'should call highlight methods when descriptor is removed changing active descriptor', () => {
  236. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  237. const secondDescriptor = { priority: 11, classes: 'highlight', id: 'highlight-2' };
  238. set( element, descriptor );
  239. set( element, secondDescriptor );
  240. remove( element, secondDescriptor.id );
  241. sinon.assert.calledThrice( addSpy );
  242. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  243. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  244. expect( addSpy.thirdCall.args[ 1 ] ).to.equal( descriptor );
  245. sinon.assert.calledTwice( removeSpy );
  246. expect( removeSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  247. expect( removeSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  248. } );
  249. it( 'should call highlight methods when descriptor is removed not changing active descriptor', () => {
  250. const descriptor = { priority: 10, classes: 'highlight', id: 'highlight-1' };
  251. const secondDescriptor = { priority: 9, classes: 'highlight', id: 'highlight-2' };
  252. set( element, descriptor );
  253. set( element, secondDescriptor );
  254. remove( element, secondDescriptor );
  255. sinon.assert.calledOnce( addSpy );
  256. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  257. sinon.assert.notCalled( removeSpy );
  258. } );
  259. it( 'should call highlight methods - CSS class array', () => {
  260. const descriptor = { priority: 10, classes: [ 'highlight', 'a' ], id: 'highlight-1' };
  261. const secondDescriptor = { priority: 10, classes: [ 'highlight', 'b' ], id: 'highlight-2' };
  262. set( element, descriptor );
  263. set( element, secondDescriptor );
  264. sinon.assert.calledTwice( addSpy );
  265. expect( addSpy.firstCall.args[ 1 ] ).to.equal( descriptor );
  266. expect( addSpy.secondCall.args[ 1 ] ).to.equal( secondDescriptor );
  267. } );
  268. } );
  269. describe( 'findOptimalInsertionPosition()', () => {
  270. let model, doc;
  271. beforeEach( () => {
  272. model = new Model();
  273. doc = model.document;
  274. doc.createRoot();
  275. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  276. model.schema.register( 'image' );
  277. model.schema.register( 'span' );
  278. model.schema.extend( 'image', {
  279. allowIn: '$root',
  280. isObject: true,
  281. isBlock: true
  282. } );
  283. model.schema.extend( 'span', { allowIn: 'paragraph' } );
  284. model.schema.extend( '$text', { allowIn: 'span' } );
  285. } );
  286. it( 'returns position after selected element', () => {
  287. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  288. const pos = findOptimalInsertionPosition( doc.selection, model );
  289. expect( pos.path ).to.deep.equal( [ 2 ] );
  290. } );
  291. it( 'returns position before parent block if an inline object is selected', () => {
  292. model.schema.register( 'placeholder', {
  293. allowWhere: '$text',
  294. isInline: true,
  295. isObject: true
  296. } );
  297. setData( model, '<paragraph>x</paragraph><paragraph>f[<placeholder></placeholder>]oo</paragraph><paragraph>y</paragraph>' );
  298. const pos = findOptimalInsertionPosition( doc.selection, model );
  299. expect( pos.path ).to.deep.equal( [ 1 ] );
  300. } );
  301. it( 'returns position inside empty block', () => {
  302. setData( model, '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  303. const pos = findOptimalInsertionPosition( doc.selection, model );
  304. expect( pos.path ).to.deep.equal( [ 1, 0 ] );
  305. } );
  306. it( 'returns position before block if at the beginning of that block', () => {
  307. setData( model, '<paragraph>x</paragraph><paragraph>[]foo</paragraph><paragraph>y</paragraph>' );
  308. const pos = findOptimalInsertionPosition( doc.selection, model );
  309. expect( pos.path ).to.deep.equal( [ 1 ] );
  310. } );
  311. it( 'returns position before block if in the middle of that block (collapsed selection)', () => {
  312. setData( model, '<paragraph>x</paragraph><paragraph>f[]oo</paragraph><paragraph>y</paragraph>' );
  313. const pos = findOptimalInsertionPosition( doc.selection, model );
  314. expect( pos.path ).to.deep.equal( [ 1 ] );
  315. } );
  316. it( 'returns position before block if in the middle of that block (non-collapsed selection)', () => {
  317. setData( model, '<paragraph>x</paragraph><paragraph>f[o]o</paragraph><paragraph>y</paragraph>' );
  318. const pos = findOptimalInsertionPosition( doc.selection, model );
  319. expect( pos.path ).to.deep.equal( [ 1 ] );
  320. } );
  321. it( 'returns position after block if at the end of that block', () => {
  322. setData( model, '<paragraph>x</paragraph><paragraph>foo[]</paragraph><paragraph>y</paragraph>' );
  323. const pos = findOptimalInsertionPosition( doc.selection, model );
  324. expect( pos.path ).to.deep.equal( [ 2 ] );
  325. } );
  326. // Checking if isTouching() was used.
  327. it( 'returns position after block if at the end of that block (deeply nested)', () => {
  328. setData( model, '<paragraph>x</paragraph><paragraph>foo<span>bar[]</span></paragraph><paragraph>y</paragraph>' );
  329. const pos = findOptimalInsertionPosition( doc.selection, model );
  330. expect( pos.path ).to.deep.equal( [ 2 ] );
  331. } );
  332. it( 'returns selection focus if not in a block', () => {
  333. model.schema.extend( '$text', { allowIn: '$root' } );
  334. setData( model, 'foo[]bar' );
  335. const pos = findOptimalInsertionPosition( doc.selection, model );
  336. expect( pos.path ).to.deep.equal( [ 3 ] );
  337. } );
  338. } );
  339. describe( 'viewToModelPositionOutsideModelElement()', () => {
  340. let mapper, model, modelP, viewP, viewXyz, modelSpan, viewSpan;
  341. beforeEach( () => {
  342. mapper = new Mapper();
  343. model = new Model();
  344. // MODEL: <p>foo<span></span>bar</p>
  345. const modelFoo = new ModelText( 'foo' );
  346. modelSpan = new ModelElement( 'span' );
  347. const modelBar = new ModelText( 'bar' );
  348. modelP = new ModelElement( 'p', null, [ modelFoo, modelSpan, modelBar ] );
  349. // VIEW: <p>foo<span>xyz</span>bar</p>
  350. const viewFoo = new ViewText( 'foo' );
  351. viewXyz = new ViewText( 'xyz' );
  352. viewSpan = new ViewElement( 'span', null, viewXyz );
  353. const viewBar = new ViewText( 'bar' );
  354. viewP = new ViewElement( 'p', null, [ viewFoo, viewSpan, viewBar ] );
  355. mapper.bindElements( modelP, viewP );
  356. mapper.bindElements( modelSpan, viewSpan );
  357. } );
  358. it( 'should map view position that is at the beginning of the view element to a position before the model element', () => {
  359. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  360. // View:
  361. // <p>foo<span>|xyz</span>bar</p>.
  362. const viewPosition = new ViewPosition( viewXyz, 0 );
  363. // Model:
  364. // <p>foo|<span></span>bar</p>.
  365. const modelPosition = mapper.toModelPosition( viewPosition );
  366. expect( modelPosition.path ).to.deep.equal( [ 3 ] );
  367. } );
  368. it( 'should map view position that is in the middle of the view element to a position after the model element', () => {
  369. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  370. // View:
  371. // <p>foo<span>x|yz</span>bar</p>.
  372. const viewPosition = new ViewPosition( viewXyz, 1 );
  373. // Model:
  374. // <p>foo|<span></span>bar</p>.
  375. const modelPosition = mapper.toModelPosition( viewPosition );
  376. expect( modelPosition.path ).to.deep.equal( [ 4 ] );
  377. } );
  378. it( 'should map view position that is at the end of the view element to a position after the model element', () => {
  379. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, viewElement => viewElement.name == 'span' ) );
  380. // View:
  381. // <p>foo<span>xyz|</span>bar</p>.
  382. const viewPosition = new ViewPosition( viewXyz, 3 );
  383. // Model:
  384. // <p>foo<span></span>|bar</p>.
  385. const modelPosition = mapper.toModelPosition( viewPosition );
  386. expect( modelPosition.path ).to.deep.equal( [ 4 ] );
  387. } );
  388. it( 'should not fire if view element is not matched', () => {
  389. mapper.on( 'viewToModelPosition', viewToModelPositionOutsideModelElement( model, () => false ) );
  390. // View:
  391. // <p>foo<span>x|yz</span>bar</p>.
  392. const viewPosition = new ViewPosition( viewXyz, 1 );
  393. // Model:
  394. // <p>foo<span>x|yz</span>bar</p>.
  395. modelSpan._appendChild( new ModelText( 'xyz' ) );
  396. const modelPosition = mapper.toModelPosition( viewPosition );
  397. expect( modelPosition.path ).to.deep.equal( [ 3, 1 ] );
  398. } );
  399. } );
  400. } );