breakattributes.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { breakAttributes } from '../../../src/view/writer';
  6. import { stringify, parse } from '../../../src/dev-utils/view';
  7. import ContainerElement from '../../../src/view/containerelement';
  8. import AttributeElement from '../../../src/view/attributeelement';
  9. import EmptyElement from '../../../src/view/emptyelement';
  10. import UIElement from '../../../src/view/uielement';
  11. import Range from '../../../src/view/range';
  12. import Position from '../../../src/view/position';
  13. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  14. describe( 'writer', () => {
  15. describe( 'breakAttributes', () => {
  16. describe( 'break position', () => {
  17. /**
  18. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  19. * test break position.
  20. *
  21. * @param {String} input
  22. * @param {String} expected
  23. */
  24. function test( input, expected ) {
  25. const { view, selection } = parse( input );
  26. const newPosition = breakAttributes( selection.getFirstPosition() );
  27. expect( stringify( view.root, newPosition, {
  28. showType: true,
  29. showPriority: true
  30. } ) ).to.equal( expected );
  31. }
  32. it( 'should not break text nodes if they are not in attribute elements - middle', () => {
  33. test(
  34. '<container:p>foo{}bar</container:p>',
  35. '<container:p>foo{}bar</container:p>'
  36. );
  37. } );
  38. it( 'should not break text nodes if they are not in attribute elements - beginning', () => {
  39. test(
  40. '<container:p>{}foobar</container:p>',
  41. '<container:p>{}foobar</container:p>'
  42. );
  43. } );
  44. it( 'should not break text nodes if they are not in attribute elements #2 - end', () => {
  45. test(
  46. '<container:p>foobar{}</container:p>',
  47. '<container:p>foobar{}</container:p>'
  48. );
  49. } );
  50. it( 'should split attribute element', () => {
  51. test(
  52. '<container:p><attribute:b view-priority="1">foo{}bar</attribute:b></container:p>',
  53. '<container:p>' +
  54. '<attribute:b view-priority="1">foo</attribute:b>[]<attribute:b view-priority="1">bar</attribute:b>' +
  55. '</container:p>'
  56. );
  57. } );
  58. it( 'should move from beginning of the nested text node to the container', () => {
  59. test(
  60. '<container:p>' +
  61. '<attribute:b view-priority="1"><attribute:u view-priority="1">{}foobar</attribute:u></attribute:b>' +
  62. '</container:p>',
  63. '<container:p>' +
  64. '[]<attribute:b view-priority="1"><attribute:u view-priority="1">foobar</attribute:u></attribute:b>' +
  65. '</container:p>'
  66. );
  67. } );
  68. it( 'should stick selection in text node if it is in container', () => {
  69. test(
  70. '<container:p>foo{}<attribute:b view-priority="1">bar</attribute:b></container:p>',
  71. '<container:p>foo{}<attribute:b view-priority="1">bar</attribute:b></container:p>'
  72. );
  73. } );
  74. it( 'should split nested attributes', () => {
  75. test(
  76. '<container:p>' +
  77. '<attribute:b view-priority="1"><attribute:u view-priority="1">foo{}bar</attribute:u></attribute:b>' +
  78. '</container:p>',
  79. '<container:p>' +
  80. '<attribute:b view-priority="1">' +
  81. '<attribute:u view-priority="1">' +
  82. 'foo' +
  83. '</attribute:u>' +
  84. '</attribute:b>' +
  85. '[]' +
  86. '<attribute:b view-priority="1">' +
  87. '<attribute:u view-priority="1">' +
  88. 'bar' +
  89. '</attribute:u>' +
  90. '</attribute:b>' +
  91. '</container:p>'
  92. );
  93. } );
  94. it( 'should move from end of the nested text node to the container', () => {
  95. test(
  96. '<container:p>' +
  97. '<attribute:b view-priority="1"><attribute:u view-priority="1">foobar{}</attribute:u></attribute:b>' +
  98. '</container:p>',
  99. '<container:p>' +
  100. '<attribute:b view-priority="1"><attribute:u view-priority="1">foobar</attribute:u></attribute:b>[]' +
  101. '</container:p>'
  102. );
  103. } );
  104. it( 'should split attribute element directly in document fragment', () => {
  105. test(
  106. '<attribute:b view-priority="1">foo{}bar</attribute:b>',
  107. '<attribute:b view-priority="1">foo</attribute:b>[]<attribute:b view-priority="1">bar</attribute:b>'
  108. );
  109. } );
  110. it( 'should not split text directly in document fragment', () => {
  111. test(
  112. 'foo{}bar',
  113. 'foo{}bar'
  114. );
  115. } );
  116. } );
  117. describe( 'break range', () => {
  118. /**
  119. * Executes test using `parse` and `stringify` utils functions.
  120. *
  121. * @param {String} input
  122. * @param {String} expected
  123. */
  124. function test( input, expected ) {
  125. const { view, selection } = parse( input );
  126. const newRange = breakAttributes( selection.getFirstRange() );
  127. expect( stringify( view.root, newRange, { showType: true } ) ).to.equal( expected );
  128. }
  129. it( 'should throw when range placed in two containers', () => {
  130. const p1 = new ContainerElement( 'p' );
  131. const p2 = new ContainerElement( 'p' );
  132. expect( () => {
  133. breakAttributes( Range.createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  134. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  135. } );
  136. it( 'should throw when range has no parent container', () => {
  137. const el = new AttributeElement( 'b' );
  138. expect( () => {
  139. breakAttributes( Range.createFromParentsAndOffsets( el, 0, el, 0 ) );
  140. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  141. } );
  142. it( 'should not break text nodes if they are not in attribute elements', () => {
  143. test(
  144. '<container:p>foo{}bar</container:p>',
  145. '<container:p>foo{}bar</container:p>'
  146. );
  147. } );
  148. it( 'should break at collapsed range and return collapsed one', () => {
  149. test(
  150. '<container:p><attribute:b>foo{}bar</attribute:b></container:p>',
  151. '<container:p><attribute:b>foo</attribute:b>[]<attribute:b>bar</attribute:b></container:p>'
  152. );
  153. } );
  154. it( 'should break inside text node #1', () => {
  155. test(
  156. '<container:p><attribute:b>foo{bar}baz</attribute:b></container:p>',
  157. '<container:p>' +
  158. '<attribute:b>foo</attribute:b>[<attribute:b>bar</attribute:b>]<attribute:b>baz</attribute:b>' +
  159. '</container:p>'
  160. );
  161. } );
  162. it( 'should break inside text node #2', () => {
  163. test(
  164. '<container:p><attribute:b>foo{barbaz}</attribute:b></container:p>',
  165. '<container:p><attribute:b>foo</attribute:b>[<attribute:b>barbaz</attribute:b>]</container:p>'
  166. );
  167. } );
  168. it( 'should break inside text node #3', () => {
  169. test(
  170. '<container:p><attribute:b>foo{barbaz]</attribute:b></container:p>',
  171. '<container:p><attribute:b>foo</attribute:b>[<attribute:b>barbaz</attribute:b>]</container:p>'
  172. );
  173. } );
  174. it( 'should break inside text node #4', () => {
  175. test(
  176. '<container:p><attribute:b>{foo}barbaz</attribute:b></container:p>',
  177. '<container:p>[<attribute:b>foo</attribute:b>]<attribute:b>barbaz</attribute:b></container:p>'
  178. );
  179. } );
  180. it( 'should break inside text node #5', () => {
  181. test(
  182. '<container:p><attribute:b>[foo}barbaz</attribute:b></container:p>',
  183. '<container:p>[<attribute:b>foo</attribute:b>]<attribute:b>barbaz</attribute:b></container:p>'
  184. );
  185. } );
  186. it( 'should break placed inside different nodes', () => {
  187. test(
  188. '<container:p>foo{bar<attribute:b>baz}qux</attribute:b></container:p>',
  189. '<container:p>foo{bar<attribute:b>baz</attribute:b>]<attribute:b>qux</attribute:b></container:p>'
  190. );
  191. } );
  192. it( 'should split attribute element directly in document fragment', () => {
  193. test(
  194. '<attribute:b>fo{ob}ar</attribute:b>',
  195. '<attribute:b>fo</attribute:b>[<attribute:b>ob</attribute:b>]<attribute:b>ar</attribute:b>'
  196. );
  197. } );
  198. it( 'should not split text directly in document fragment', () => {
  199. test(
  200. 'foo{}bar',
  201. 'foo{}bar'
  202. );
  203. } );
  204. it( 'should throw if breaking inside EmptyElement #1', () => {
  205. const img = new EmptyElement( 'img' );
  206. new ContainerElement( 'p', null, img ); // eslint-disable-line no-new
  207. const position = new Position( img, 0 );
  208. expect( () => {
  209. breakAttributes( position );
  210. } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' );
  211. } );
  212. it( 'should throw if breaking inside EmptyElement #2', () => {
  213. const img = new EmptyElement( 'img' );
  214. const b = new AttributeElement( 'b' );
  215. new ContainerElement( 'p', null, [ img, b ] ); // eslint-disable-line no-new
  216. const range = Range.createFromParentsAndOffsets( img, 0, b, 0 );
  217. expect( () => {
  218. breakAttributes( range );
  219. } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' );
  220. } );
  221. it( 'should throw if breaking inside UIElement #1', () => {
  222. const span = new UIElement( 'span' );
  223. new ContainerElement( 'p', null, span ); // eslint-disable-line no-new
  224. const position = new Position( span, 0 );
  225. expect( () => {
  226. breakAttributes( position );
  227. } ).to.throw( CKEditorError, 'view-writer-cannot-break-ui-element' );
  228. } );
  229. it( 'should throw if breaking inside UIElement #2', () => {
  230. const span = new UIElement( 'span' );
  231. const b = new AttributeElement( 'b' );
  232. new ContainerElement( 'p', null, [ span, b ] ); // eslint-disable-line no-new
  233. const range = Range.createFromParentsAndOffsets( span, 0, b, 0 );
  234. expect( () => {
  235. breakAttributes( range );
  236. } ).to.throw( CKEditorError, 'view-writer-cannot-break-ui-element' );
  237. } );
  238. } );
  239. } );
  240. } );