writer.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view, browser-only */
  6. 'use strict';
  7. import Writer from '/ckeditor5/engine/view/writer.js';
  8. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  9. import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  10. import Text from '/ckeditor5/engine/view/text.js';
  11. import Position from '/ckeditor5/engine/view/position.js';
  12. import { parse } from '/tests/engine/_utils/view.js';
  13. describe( 'Writer', () => {
  14. let writer;
  15. beforeEach( () => {
  16. writer = new Writer();
  17. } );
  18. describe( 'getParentContainer', () => {
  19. it( 'should return parent container of the node', () => {
  20. const text = new Text( 'foobar' );
  21. const b = new AttributeElement( 'b', null, [ text ] );
  22. const parent = new ContainerElement( 'p', null, [ b ] );
  23. b.priority = 1;
  24. const container = writer.getParentContainer( new Position( text, 0 ) );
  25. expect( container ).to.equal( parent );
  26. } );
  27. it( 'should return undefined if no parent container', () => {
  28. const text = new Text( 'foobar' );
  29. const b = new AttributeElement( 'b', null, [ text ] );
  30. b.priority = 1;
  31. const container = writer.getParentContainer( new Position( text, 0 ) );
  32. expect( container ).to.be.undefined;
  33. } );
  34. } );
  35. describe( 'move', () => {
  36. it( 'should move nodes using remove and insert methods', () => {
  37. const { selection: sourceSelection } = parse( '<container:p>[foobar]</container:p>' );
  38. const { selection: targetSelection } = parse( '<container:div>[]</container:div>' );
  39. const removeSpy = sinon.spy( writer, 'remove' );
  40. const insertSpy = sinon.spy( writer, 'insert' );
  41. const sourceRange = sourceSelection.getFirstRange();
  42. const targetPosition = targetSelection.getFirstPosition();
  43. const newRange = writer.move( sourceRange, targetPosition );
  44. sinon.assert.calledOnce( removeSpy );
  45. sinon.assert.calledWithExactly( removeSpy, sourceRange );
  46. sinon.assert.calledOnce( insertSpy );
  47. sinon.assert.calledWithExactly( insertSpy, targetPosition, removeSpy.firstCall.returnValue );
  48. expect( newRange ).to.equal( insertSpy.firstCall.returnValue );
  49. } );
  50. } );
  51. } );