breakattributes.js 11 KB

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