8
0

wrapposition.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Writer from '../../../src/view/writer';
  6. import Text from '../../../src/view/text';
  7. import Element from '../../../src/view/element';
  8. import ContainerElement from '../../../src/view/containerelement';
  9. import AttributeElement from '../../../src/view/attributeelement';
  10. import EmptyElement from '../../../src/view/emptyelement';
  11. import UIElement from '../../../src/view/uielement';
  12. import Position from '../../../src/view/position';
  13. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  14. import { stringify, parse } from '../../../src/dev-utils/view';
  15. describe( 'Writer', () => {
  16. describe( 'wrapPosition()', () => {
  17. let writer;
  18. // Executes test using `parse` and `stringify` utils functions.
  19. //
  20. // @param {String} input
  21. // @param {String} unwrapAttribute
  22. // @param {String} expected
  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. before( () => {
  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, 'view-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 view-priority="1"></attribute:b>',
  43. '<container:p><attribute:b view-priority="1">[]</attribute:b>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 view-priority="1"></attribute:b>',
  50. '<container:p>foo<attribute:b view-priority="1">[]</attribute:b>bar</container:p>'
  51. );
  52. } );
  53. it( 'should support unicode', () => {
  54. test(
  55. '<container:p>நிலை{}க்கு</container:p>',
  56. '<attribute:b view-priority="1"></attribute:b>',
  57. '<container:p>நிலை<attribute:b view-priority="1">[]</attribute:b>க்கு</container:p>'
  58. );
  59. } );
  60. it( 'should wrap position inside document fragment', () => {
  61. test(
  62. '<attribute:b view-priority="1">foo</attribute:b>[]<attribute:b view-priority="3">bar</attribute:b>',
  63. '<attribute:b view-priority="2"></attribute:b>',
  64. '<attribute:b view-priority="1">foo</attribute:b><attribute:b view-priority="2">[]</attribute:b>' +
  65. '<attribute:b view-priority="3">bar</attribute:b>'
  66. );
  67. } );
  68. it( 'should wrap position at the end of text node', () => {
  69. test(
  70. '<container:p>foobar{}</container:p>',
  71. '<attribute:b view-priority="1"></attribute:b>',
  72. '<container:p>foobar<attribute:b view-priority="1">[]</attribute:b></container:p>'
  73. );
  74. } );
  75. it( 'should merge with existing attributes #1', () => {
  76. test(
  77. '<container:p><attribute:b view-priority="1">foo</attribute:b>[]</container:p>',
  78. '<attribute:b view-priority="1"></attribute:b>',
  79. '<container:p><attribute:b view-priority="1">foo{}</attribute:b></container:p>'
  80. );
  81. } );
  82. it( 'should merge with existing attributes #2', () => {
  83. test(
  84. '<container:p>[]<attribute:b view-priority="1">foo</attribute:b></container:p>',
  85. '<attribute:b view-priority="1"></attribute:b>',
  86. '<container:p><attribute:b view-priority="1">{}foo</attribute:b></container:p>'
  87. );
  88. } );
  89. it( 'should wrap when inside nested attributes', () => {
  90. test(
  91. '<container:p><attribute:b view-priority="1">foo{}bar</attribute:b></container:p>',
  92. '<attribute:u view-priority="1"></attribute:u>',
  93. '<container:p>' +
  94. '<attribute:b view-priority="1">' +
  95. 'foo' +
  96. '<attribute:u view-priority="1">[]</attribute:u>' +
  97. 'bar' +
  98. '</attribute:b>' +
  99. '</container:p>'
  100. );
  101. } );
  102. it( 'should merge when wrapping between same attribute', () => {
  103. test(
  104. '<container:p>' +
  105. '<attribute:b view-priority="1">foo</attribute:b>[]<attribute:b view-priority="1">bar</attribute:b>' +
  106. '</container:p>',
  107. '<attribute:b view-priority="1"></attribute:b>',
  108. '<container:p><attribute:b view-priority="1">foo{}bar</attribute:b></container:p>'
  109. );
  110. } );
  111. it( 'should move position to text node if in same attribute', () => {
  112. test(
  113. '<container:p><attribute:b view-priority="1">foobar[]</attribute:b></container:p>',
  114. '<attribute:b view-priority="1"></attribute:b>',
  115. '<container:p><attribute:b view-priority="1">foobar{}</attribute:b></container:p>'
  116. );
  117. } );
  118. it( 'should throw if position is set inside EmptyElement', () => {
  119. const emptyElement = new EmptyElement( 'img' );
  120. new ContainerElement( 'p', null, emptyElement ); // eslint-disable-line no-new
  121. const attributeElement = new AttributeElement( 'b' );
  122. const position = new Position( emptyElement, 0 );
  123. expect( () => {
  124. writer.wrapPosition( position, attributeElement );
  125. } ).to.throw( CKEditorError, 'view-emptyelement-cannot-add' );
  126. } );
  127. it( 'should throw if position is set inside UIElement', () => {
  128. const uiElement = new UIElement( 'span' );
  129. new ContainerElement( 'p', null, uiElement ); // eslint-disable-line no-new
  130. const attributeElement = new AttributeElement( 'b' );
  131. const position = new Position( uiElement, 0 );
  132. expect( () => {
  133. writer.wrapPosition( position, attributeElement );
  134. } ).to.throw( CKEditorError, 'view-uielement-cannot-add' );
  135. } );
  136. } );
  137. } );