breakattributes.js 9.8 KB

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