writer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/core/treeview/writer.js';
  8. import Element from '/ckeditor5/core/treeview/element.js';
  9. import Text from '/ckeditor5/core/treeview/text.js';
  10. import Position from '/ckeditor5/core/treeview/position.js';
  11. import utils from '/tests/core/treeview/writer/_utils/utils.js';
  12. describe( 'Writer', () => {
  13. const create = utils.create;
  14. let writer;
  15. beforeEach( () => {
  16. writer = new Writer();
  17. } );
  18. describe( 'isContainer', () => {
  19. it( 'should return true for container elements', () => {
  20. const containerElement = new Element( 'p' );
  21. const attributeElement = new Element( 'b' );
  22. writer._priorities.set( attributeElement, 1 );
  23. expect( writer.isContainer( containerElement ) ).to.be.true;
  24. expect( writer.isContainer( attributeElement ) ).to.be.false;
  25. } );
  26. } );
  27. describe( 'isAttribute', () => {
  28. it( 'should return true for attribute elements', () => {
  29. const containerElement = new Element( 'p' );
  30. const attributeElement = new Element( 'b' );
  31. writer._priorities.set( attributeElement, 1 );
  32. expect( writer.isAttribute( containerElement ) ).to.be.false;
  33. expect( writer.isAttribute( attributeElement ) ).to.be.true;
  34. } );
  35. } );
  36. describe( 'setPriority', () => {
  37. it( 'sets node priority', () => {
  38. const nodeMock = {};
  39. writer.setPriority( nodeMock, 10 );
  40. expect( writer._priorities.get( nodeMock ) ).to.equal( 10 );
  41. } );
  42. } );
  43. describe( 'getPriority', () => {
  44. it( 'gets node priority', () => {
  45. const nodeMock = {};
  46. writer._priorities.set( nodeMock, 12 );
  47. expect( writer.getPriority( nodeMock ) ).to.equal( 12 );
  48. } );
  49. } );
  50. describe( 'getParentContainer', () => {
  51. it( 'should return parent container of the node', () => {
  52. const text = new Text( 'foobar' );
  53. const b = new Element( 'b', null, [ text ] );
  54. const parent = new Element( 'p', null, [ b ] );
  55. writer.setPriority( b, 1 );
  56. const container = writer.getParentContainer( new Position( text, 0 ) );
  57. expect( container ).to.equal( parent );
  58. } );
  59. it( 'should return undefined if no parent container', () => {
  60. const text = new Text( 'foobar' );
  61. const b = new Element( 'b', null, [ text ] );
  62. writer.setPriority( b, 1 );
  63. const container = writer.getParentContainer( new Position( text, 0 ) );
  64. expect( container ).to.be.undefined;
  65. } );
  66. } );
  67. describe( 'move', () => {
  68. it( 'should move nodes using remove and insert methods', () => {
  69. // <p>[{foobar}]</p>
  70. // Move to <div>|</div>
  71. // <div>[{foobar}]</div>
  72. const source = create( writer, {
  73. instanceOf: Element,
  74. name: 'p',
  75. rangeStart: 0,
  76. rangeEnd: 1,
  77. children: [
  78. { instanceOf: Text, data: 'foobar' }
  79. ]
  80. } );
  81. const target = create( writer, {
  82. instanceOf: Element,
  83. name: 'div',
  84. position: 0
  85. } );
  86. const removeSpy = sinon.spy( writer, 'remove' );
  87. const insertSpy = sinon.spy( writer, 'insert' );
  88. const newRange = writer.move( source.range, target.position );
  89. sinon.assert.calledOnce( removeSpy );
  90. sinon.assert.calledWithExactly( removeSpy, source.range );
  91. sinon.assert.calledOnce( insertSpy );
  92. sinon.assert.calledWithExactly( insertSpy, target.position, removeSpy.firstCall.returnValue );
  93. expect( newRange ).to.equal( insertSpy.firstCall.returnValue );
  94. } );
  95. } );
  96. } );