writer.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Writer from '/ckeditor5/engine/treeview/writer.js';
  8. import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
  9. import AttributeElement from '/ckeditor5/engine/treeview/attributeelement.js';
  10. import Text from '/ckeditor5/engine/treeview/text.js';
  11. import Position from '/ckeditor5/engine/treeview/position.js';
  12. import utils from '/tests/engine/treeview/writer/_utils/utils.js';
  13. describe( 'Writer', () => {
  14. const create = utils.create;
  15. let writer;
  16. beforeEach( () => {
  17. writer = new Writer();
  18. } );
  19. describe( 'getParentContainer', () => {
  20. it( 'should return parent container of the node', () => {
  21. const text = new Text( 'foobar' );
  22. const b = new AttributeElement( 'b', null, [ text ] );
  23. const parent = new ContainerElement( 'p', null, [ b ] );
  24. b.priority = 1;
  25. const container = writer.getParentContainer( new Position( text, 0 ) );
  26. expect( container ).to.equal( parent );
  27. } );
  28. it( 'should return undefined if no parent container', () => {
  29. const text = new Text( 'foobar' );
  30. const b = new AttributeElement( 'b', null, [ text ] );
  31. b.priority = 1;
  32. const container = writer.getParentContainer( new Position( text, 0 ) );
  33. expect( container ).to.be.undefined;
  34. } );
  35. } );
  36. describe( 'move', () => {
  37. it( 'should move nodes using remove and insert methods', () => {
  38. // <p>[{foobar}]</p>
  39. // Move to <div>|</div>
  40. // <div>[{foobar}]</div>
  41. const source = create( writer, {
  42. instanceOf: ContainerElement,
  43. name: 'p',
  44. rangeStart: 0,
  45. rangeEnd: 1,
  46. children: [
  47. { instanceOf: Text, data: 'foobar' }
  48. ]
  49. } );
  50. const target = create( writer, {
  51. instanceOf: ContainerElement,
  52. name: 'div',
  53. position: 0
  54. } );
  55. const removeSpy = sinon.spy( writer, 'remove' );
  56. const insertSpy = sinon.spy( writer, 'insert' );
  57. const newRange = writer.move( source.range, target.position );
  58. sinon.assert.calledOnce( removeSpy );
  59. sinon.assert.calledWithExactly( removeSpy, source.range );
  60. sinon.assert.calledOnce( insertSpy );
  61. sinon.assert.calledWithExactly( insertSpy, target.position, removeSpy.firstCall.returnValue );
  62. expect( newRange ).to.equal( insertSpy.firstCall.returnValue );
  63. } );
  64. } );
  65. } );