breakAttributes.js 8.3 KB

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