1267.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  9. import Position from '../../src/model/position';
  10. import Range from '../../src/model/range';
  11. import { setData as setModelData, getData as getModelData } from '../../src/dev-utils/model';
  12. describe( 'Bug ckeditor5-engine#1267', () => {
  13. let element, editor, model;
  14. beforeEach( () => {
  15. element = document.createElement( 'div' );
  16. document.body.appendChild( element );
  17. return ClassicTestEditor
  18. .create( element, { plugins: [ Paragraph, Bold ] } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. } );
  23. } );
  24. afterEach( () => {
  25. element.remove();
  26. return editor.destroy();
  27. } );
  28. it( 'selection should not retain attributes after external change removal', () => {
  29. setModelData( model,
  30. '<paragraph>foo bar baz</paragraph>' +
  31. '<paragraph>foo <$text bold="true">bar{}</$text> baz</paragraph>'
  32. );
  33. // Remove second paragraph where selection is placed.
  34. model.enqueueChange( 'transparent', writer => {
  35. writer.remove( Range.createFromPositionAndShift( new Position( model.document.getRoot(), [ 1 ] ), 1 ) );
  36. } );
  37. expect( getModelData( model ) ).to.equal( '<paragraph>foo bar baz[]</paragraph>' );
  38. } );
  39. it( 'selection should retain attributes set manually', () => {
  40. setModelData( model,
  41. '<paragraph>foo bar baz</paragraph>' +
  42. '<paragraph>foo bar baz</paragraph>' +
  43. '<paragraph>[]</paragraph>'
  44. );
  45. // Execute bold command when selection is inside empty paragraph.
  46. editor.execute( 'bold' );
  47. expect( getModelData( model ) ).to.equal(
  48. '<paragraph>foo bar baz</paragraph>' +
  49. '<paragraph>foo bar baz</paragraph>' +
  50. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>'
  51. );
  52. // Remove second paragraph.
  53. model.enqueueChange( 'transparent', writer => {
  54. writer.remove( Range.createFromPositionAndShift( new Position( model.document.getRoot(), [ 1 ] ), 1 ) );
  55. } );
  56. // Selection attributes set by command should stay as they were.
  57. expect( getModelData( model ) ).to.equal(
  58. '<paragraph>foo bar baz</paragraph>' +
  59. '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' );
  60. } );
  61. } );