wrapposition.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 Text from '/ckeditor5/engine/treeview/text.js';
  9. import Element from '/ckeditor5/engine/treeview/element.js';
  10. import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
  11. import Position from '/ckeditor5/engine/treeview/position.js';
  12. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  13. import { stringify, parse } from '/tests/engine/_utils/view.js';
  14. describe( 'wrapPosition', () => {
  15. let writer;
  16. /**
  17. * Executes test using `parse` and `stringify` utils functions.
  18. *
  19. * @param {String} input
  20. * @param {String} unwrapAttribute
  21. * @param {String} expected
  22. */
  23. function test( input, unwrapAttribute, expected ) {
  24. const { view, selection } = parse( input );
  25. const newPosition = writer.wrapPosition( selection.getFirstPosition(), parse( unwrapAttribute ) );
  26. expect( stringify( view, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected );
  27. }
  28. beforeEach( () => {
  29. writer = new Writer();
  30. } );
  31. it( 'should throw error when element is not instance of AttributeElement', () => {
  32. const container = new ContainerElement( 'p', null, new Text( 'foo' ) );
  33. const position = new Position( container, 0 );
  34. const b = new Element( 'b' );
  35. expect( () => {
  36. writer.wrapPosition( position, b );
  37. } ).to.throw( CKEditorError, 'treeview-writer-wrap-invalid-attribute' );
  38. } );
  39. it( 'should wrap position at the beginning of text node', () => {
  40. test(
  41. '<container:p>{}foobar</container:p>',
  42. '<attribute:b:1></attribute:b:1>',
  43. '<container:p><attribute:b:1>[]</attribute:b:1>foobar</container:p>'
  44. );
  45. } );
  46. it( 'should wrap position inside text node', () => {
  47. test(
  48. '<container:p>foo{}bar</container:p>',
  49. '<attribute:b:1></attribute:b:1>',
  50. '<container:p>foo<attribute:b:1>[]</attribute:b:1>bar</container:p>'
  51. );
  52. } );
  53. it( 'should wrap position at the end of text node', () => {
  54. test(
  55. '<container:p>foobar{}</container:p>',
  56. '<attribute:b:1></attribute:b:1>',
  57. '<container:p>foobar<attribute:b:1>[]</attribute:b:1></container:p>'
  58. );
  59. } );
  60. it( 'should merge with existing attributes #1', () => {
  61. test(
  62. '<container:p><attribute:b:1>foo</attribute:b:1>[]</container:p>',
  63. '<attribute:b:1></attribute:b:1>',
  64. '<container:p><attribute:b:1>foo{}</attribute:b:1></container:p>'
  65. );
  66. } );
  67. it( 'should merge with existing attributes #2', () => {
  68. test(
  69. '<container:p>[]<attribute:b:1>foo</attribute:b:1></container:p>',
  70. '<attribute:b:1></attribute:b:1>',
  71. '<container:p><attribute:b:1>{}foo</attribute:b:1></container:p>'
  72. );
  73. } );
  74. it( 'should wrap when inside nested attributes', () => {
  75. test(
  76. '<container:p><attribute:b:1>foo{}bar</attribute:b:1></container:p>',
  77. '<attribute:u:1></attribute:u:1>',
  78. '<container:p>' +
  79. '<attribute:b:1>foo</attribute:b:1>' +
  80. '<attribute:u:1><attribute:b:1>[]</attribute:b:1></attribute:u:1>' +
  81. '<attribute:b:1>bar</attribute:b:1>' +
  82. '</container:p>'
  83. );
  84. } );
  85. it( 'should merge when wrapping between same attribute', () => {
  86. test(
  87. '<container:p><attribute:b:1>foo</attribute:b:1>[]<attribute:b:1>bar</attribute:b:1></container:p>',
  88. '<attribute:b:1></attribute:b:1>',
  89. '<container:p><attribute:b:1>foo{}bar</attribute:b:1></container:p>'
  90. );
  91. } );
  92. it( 'should move position to text node if in same attribute', () => {
  93. test(
  94. '<container:p><attribute:b:1>foobar[]</attribute:b:1></container:p>',
  95. '<attribute:b:1></attribute:b:1>',
  96. '<container:p><attribute:b:1>foobar{}</attribute:b:1></container:p>'
  97. );
  98. } );
  99. } );