isattributeallowedinselection.js 3.2 KB

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