isattributeallowedinselection.js 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '/ckeditor5/engine/model/document.js';
  6. import Range from '/ckeditor5/engine/model/range.js';
  7. import Position from '/ckeditor5/engine/model/position.js';
  8. import Text from '/ckeditor5/engine/model/text.js';
  9. import Element from '/ckeditor5/engine/model/element.js';
  10. import isAttributeAllowedInSelection from '/ckeditor5/core/command/helpers/isattributeallowedinselection.js';
  11. describe( 'isAttributeAllowedInSelection', () => {
  12. const attribute = 'bold';
  13. let document, root;
  14. beforeEach( () => {
  15. document = new Document();
  16. root = document.createRoot();
  17. document.schema.registerItem( 'p', '$block' );
  18. document.schema.registerItem( 'h1', '$block' );
  19. document.schema.registerItem( 'img', '$inline' );
  20. // Bold text is allowed only in P.
  21. document.schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
  22. document.schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );
  23. // Disallow bold on image.
  24. document.schema.disallow( { name: 'img', attributes: 'bold', inside: '$root' } );
  25. root.insertChildren( 0, [
  26. new Element( 'p', [], [
  27. new Text( 'foo' ),
  28. new Element( 'img' ),
  29. new Element( 'img' ),
  30. new Text( 'bar' )
  31. ] ),
  32. new Element( 'h1' ),
  33. new Element( 'p' )
  34. ] );
  35. } );
  36. describe( 'when selection is collapsed', () => {
  37. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  38. document.selection.setRanges( [ new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) ] );
  39. expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.true;
  40. } );
  41. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  42. document.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] );
  43. expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.false;
  44. document.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  45. expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.false;
  46. } );
  47. } );
  48. describe( 'when selection is not collapsed', () => {
  49. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  50. // Simple selection on a few characters.
  51. document.selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 3 ] ) ) ] );
  52. expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.true;
  53. // Selection spans over characters but also include nodes that can't have attribute.
  54. document.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 6 ] ) ) ] );
  55. expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.true;
  56. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  57. document.selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ) ] );
  58. expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.true;
  59. // Selection on empty P. P can have the attribute.
  60. document.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 3 ] ) ) ] );
  61. expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.true;
  62. } );
  63. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  64. // Selection on DIV which can't have bold text.
  65. document.selection.setRanges( [ new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ) ] );
  66. expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.false;
  67. // Selection on two images which can't be bold.
  68. document.selection.setRanges( [ new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 5 ] ) ) ] );
  69. expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.false;
  70. } );
  71. } );
  72. } );