restricteddocumentcommand.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. import RestrictedDocumentCommand from '../src/restricteddocumentcommand';
  6. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  7. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. describe( 'RestrictedDocumentCommand', () => {
  9. let editor, command, model;
  10. beforeEach( () => {
  11. return ModelTestEditor
  12. .create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. command = new RestrictedDocumentCommand( editor, 'nonRestricted' );
  17. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  18. model.schema.register( 'h1', { inheritAllFrom: '$block' } );
  19. model.schema.register( 'img', {
  20. allowWhere: [ '$block', '$text' ],
  21. isObject: true
  22. } );
  23. editor.model.schema.extend( '$text', { allowAttributes: [ 'nonRestricted' ] } );
  24. } );
  25. } );
  26. afterEach( () => {
  27. command.destroy();
  28. return editor.destroy();
  29. } );
  30. describe( 'value', () => {
  31. it( 'is true when collapsed selection has the attribute', () => {
  32. model.change( writer => {
  33. writer.setSelectionAttribute( 'nonRestricted', true );
  34. } );
  35. expect( command.value ).to.be.true;
  36. } );
  37. it( 'is false when collapsed selection does not have the attribute', () => {
  38. model.change( writer => {
  39. writer.setSelectionAttribute( 'nonRestricted', true );
  40. } );
  41. model.change( writer => {
  42. writer.removeSelectionAttribute( 'nonRestricted' );
  43. } );
  44. expect( command.value ).to.be.false;
  45. } );
  46. it( 'is true when selection is inside text with attribute', () => {
  47. setData( model, '<p><$text nonRestricted="true">fo[]o</$text></p><h1>bar</h1>' );
  48. expect( command.value ).to.be.true;
  49. } );
  50. it( 'is true when selection is on text with attribute', () => {
  51. setData( model, '<p>foo[<$text nonRestricted="true">bar</$text>]baz</p>' );
  52. expect( command.value ).to.be.true;
  53. } );
  54. } );
  55. describe( 'isEnabled', () => {
  56. beforeEach( () => {
  57. model.schema.register( 'x', { inheritAllFrom: '$block' } );
  58. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  59. if ( ctx.endsWith( 'x $text' ) && attributeName == 'nonRestricted' ) {
  60. return false;
  61. }
  62. } );
  63. } );
  64. describe( 'when selection is collapsed', () => {
  65. it( 'should return true if attribute is allowed at caret position', () => {
  66. setData( model, '<p>f[]oo</p>' );
  67. expect( command.isEnabled ).to.be.true;
  68. } );
  69. it( 'should return true if attribute is not allowed at caret position', () => {
  70. setData( model, '<x>fo[]o</x>' );
  71. expect( command.isEnabled ).to.be.false;
  72. } );
  73. } );
  74. describe( 'when selection is not collapsed', () => {
  75. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  76. setData( model, '<p>[foo]</p>' );
  77. expect( command.isEnabled ).to.be.true;
  78. } );
  79. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  80. setData( model, '<x>[foo]</x>' );
  81. expect( command.isEnabled ).to.be.false;
  82. } );
  83. } );
  84. } );
  85. describe( 'execute()', () => {
  86. it( 'should do nothing if the command is disabled', () => {
  87. setData( model, '<p>fo[ob]ar</p>' );
  88. command.isEnabled = false;
  89. command.execute();
  90. expect( getData( model ) ).to.equal( '<p>fo[ob]ar</p>' );
  91. } );
  92. it( 'should add attribute on selected nodes if the command value was false', () => {
  93. setData( model, '<p>foo[bar]baz</p>' );
  94. expect( command.value ).to.be.false;
  95. command.execute();
  96. expect( command.value ).to.be.true;
  97. expect( getData( model ) ).to.equal( '<p>foo[<$text nonRestricted="true">bar</$text>]baz</p>' );
  98. } );
  99. it( 'should remove attribute from selected nodes if the command value was true', () => {
  100. setData( model, '<p>abc[<$text nonRestricted="true">foo]bar</$text>xyz</p>' );
  101. expect( command.value ).to.be.true;
  102. command.execute();
  103. expect( getData( model ) ).to.equal( '<p>abc[foo]<$text nonRestricted="true">bar</$text>xyz</p>' );
  104. expect( command.value ).to.be.false;
  105. } );
  106. } );
  107. } );