breakattributes.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Text from '/ckeditor5/engine/treeview/text.js';
  10. import Position from '/ckeditor5/engine/treeview/position.js';
  11. import { stringify, parse } from '/tests/engine/_utils/view.js';
  12. describe( 'Writer', () => {
  13. let writer;
  14. /**
  15. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  16. * test break position.
  17. *
  18. * @param {String} input
  19. * @param {String} expected
  20. */
  21. function test( input, expected ) {
  22. const { view, selection } = parse( input );
  23. const newPosition = writer.breakAttributes( selection.getFirstPosition() );
  24. expect( stringify( view, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected );
  25. }
  26. beforeEach( () => {
  27. writer = new Writer();
  28. } );
  29. describe( 'breakAttributes', () => {
  30. it( 'should move position from begin of text node to the element', () => {
  31. test( '<container:p>{}foobar</container:p>', '<container:p>[]foobar</container:p>' );
  32. } );
  33. it( 'should split text node', () => {
  34. const text = new Text( 'foobar' );
  35. const container = new ContainerElement( 'p', null, text );
  36. const position = new Position( text, 3 );
  37. const newPosition = writer.breakAttributes( position );
  38. expect( container.getChildCount() ).to.equal( 2 );
  39. expect( container.getChild( 0 ) ).to.be.instanceOf( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  40. expect( container.getChild( 1 ) ).to.be.instanceOf( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  41. expect( newPosition.isEqual( new Position( container, 1 ) ) ).to.be.true;
  42. } );
  43. it( 'should move position from end of text node to the element', () => {
  44. test( '<container:p>foobar{}</container:p>', '<container:p>foobar[]</container:p>' );
  45. } );
  46. it( 'should split attribute element', () => {
  47. test(
  48. '<container:p><attribute:b:1>foo{}bar</attribute:b:1></container:p>',
  49. '<container:p><attribute:b:1>foo</attribute:b:1>[]<attribute:b:1>bar</attribute:b:1></container:p>'
  50. );
  51. } );
  52. it( 'should move from beginning of the nested text node to the container', () => {
  53. test(
  54. '<container:p><attribute:b:1><attribute:u:1>{}foobar</attribute:u:1></attribute:b:1></container:p>',
  55. '<container:p>[]<attribute:b:1><attribute:u:1>foobar</attribute:u:1></attribute:b:1></container:p>'
  56. );
  57. } );
  58. it( 'should split nested attributes', () => {
  59. test(
  60. '<container:p><attribute:b:1><attribute:u:1>foo{}bar</attribute:u:1></attribute:b:1></container:p>',
  61. '<container:p>' +
  62. '<attribute:b:1>' +
  63. '<attribute:u:1>' +
  64. 'foo' +
  65. '</attribute:u:1>' +
  66. '</attribute:b:1>' +
  67. '[]' +
  68. '<attribute:b:1>' +
  69. '<attribute:u:1>' +
  70. 'bar' +
  71. '</attribute:u:1>' +
  72. '</attribute:b:1>' +
  73. '</container:p>'
  74. );
  75. } );
  76. it( 'should move from end of the nested text node to the container', () => {
  77. test(
  78. '<container:p><attribute:b:1><attribute:u:1>foobar{}</attribute:u:1></attribute:b:1></container:p>',
  79. '<container:p><attribute:b:1><attribute:u:1>foobar</attribute:u:1></attribute:b:1>[]</container:p>'
  80. );
  81. } );
  82. } );
  83. } );