breakattributes.js 9.9 KB

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