8
0

utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { getClosestListItem, getSelectedBlocks, getPositionBeforeBlock } from '../src/utils';
  6. import Element from '@ckeditor/ckeditor5-engine/src/model/element';
  7. import Text from '@ckeditor/ckeditor5-engine/src/model/text';
  8. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  9. import Schema from '@ckeditor/ckeditor5-engine/src/model/schema';
  10. import Selection from '@ckeditor/ckeditor5-engine/src/model/selection';
  11. describe( 'getClosestListItem', () => {
  12. const item = new Element( 'listItem', null, 'foobar' );
  13. const root = new Element( '$root', null, [ item ] );
  14. it( 'should return model listItem element if given position is in such element', () => {
  15. expect( getClosestListItem( Position.createAt( item ) ) ).to.equal( item );
  16. } );
  17. it( 'should return null if position is not in listItem', () => {
  18. expect( getClosestListItem( Position.createAt( root ) ) ).to.be.null;
  19. } );
  20. } );
  21. describe( 'getSelectedBlocks', () => {
  22. const paragraph1 = new Element( 'paragraph', null, '---' );
  23. const item1 = new Element( 'listItem', null, '---' );
  24. const item2 = new Element( 'listItem', null, '---' );
  25. const item3 = new Element( 'listItem', null, '---' );
  26. const paragraph2 = new Element( 'paragraph', null, '---' );
  27. const root = new Element( '$root', null, [
  28. paragraph1, item1, item2, item3, paragraph2
  29. ] );
  30. const schema = new Schema();
  31. schema.registerItem( 'paragraph', '$block' );
  32. schema.registerItem( 'listItem', '$block' );
  33. const selection = new Selection();
  34. it( 'should return just one block if selection is over one block', () => {
  35. selection.collapse( root, 2 );
  36. selection.setFocus( root, 3 );
  37. expect( getSelectedBlocks( selection, schema ) ).to.deep.equal( [ item2 ] );
  38. } );
  39. it( 'should return ancestor block if selection is collapsed and not before a block', () => {
  40. selection.collapse( paragraph1, 2 );
  41. expect( getSelectedBlocks( selection, schema ) ).to.deep.equal( [ paragraph1 ] );
  42. } );
  43. it( 'should return empty array for collapsed selection before a block, in a root', () => {
  44. selection.collapse( root, 1 );
  45. expect( getSelectedBlocks( selection, schema ) ).to.deep.equal( [] );
  46. } );
  47. it( 'should return all blocks "touched" by the selection if it spans over multiple blocks', () => {
  48. selection.collapse( item1, 3 );
  49. selection.setFocus( root, 4 );
  50. expect( getSelectedBlocks( selection, schema ) ).to.deep.equal( [ item1, item2, item3 ] );
  51. } );
  52. } );
  53. describe( 'getPositionBeforeBlock', () => {
  54. const paragraph = new Element( 'paragraph', null, 'foo' );
  55. const item = new Element( 'listItem', null, 'bar' );
  56. const text = new Text( 'xyz' );
  57. const root = new Element( '$root' );
  58. root.appendChildren( [ paragraph, item, text ] );
  59. const schema = new Schema();
  60. schema.registerItem( 'paragraph', '$block' );
  61. schema.registerItem( 'listItem', '$block' );
  62. it( 'should return same position if position is already before a block', () => {
  63. const position = Position.createBefore( paragraph );
  64. expect( getPositionBeforeBlock( position, schema ).isEqual( position ) ).to.be.true;
  65. } );
  66. it( 'should return position before position parent if position is inside a block', () => {
  67. const position = Position.createAt( item );
  68. expect( getPositionBeforeBlock( position, schema ).isEqual( Position.createBefore( item ) ) ).to.be.true;
  69. } );
  70. it( 'should return null if position is not next to block and is not in a block other than root', () => {
  71. const position = Position.createBefore( text );
  72. expect( getPositionBeforeBlock( position, schema ) ).to.be.null;
  73. } );
  74. } );