8
0

breakattributes.js 9.7 KB

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