8
0

breakattributes.js 9.4 KB

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