breakAttributes.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view, browser-only */
  6. import { breakAttributes } from '/ckeditor5/engine/view/writer.js';
  7. import { stringify, parse } from '/ckeditor5/engine/dev-utils/view.js';
  8. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  9. import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  10. import Range from '/ckeditor5/engine/view/range.js';
  11. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. describe( 'writer', () => {
  13. describe( 'breakAttributes', () => {
  14. describe( 'break position', () => {
  15. /**
  16. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  17. * test break position.
  18. *
  19. * @param {String} input
  20. * @param {String} expected
  21. */
  22. function test( input, expected ) {
  23. let { view, selection } = parse( input );
  24. const newPosition = breakAttributes( selection.getFirstPosition() );
  25. expect( stringify( view.root, newPosition, {
  26. showType: true,
  27. showPriority: true
  28. } ) ).to.equal( expected );
  29. }
  30. it( 'should not break text nodes if they are not in attribute elements - middle', () => {
  31. test(
  32. '<container:p>foo{}bar</container:p>',
  33. '<container:p>foo{}bar</container:p>'
  34. );
  35. } );
  36. it( 'should not break text nodes if they are not in attribute elements - beginning', () => {
  37. test(
  38. '<container:p>{}foobar</container:p>',
  39. '<container:p>{}foobar</container:p>'
  40. );
  41. } );
  42. it( 'should not break text nodes if they are not in attribute elements #2 - end', () => {
  43. test(
  44. '<container:p>foobar{}</container:p>',
  45. '<container:p>foobar{}</container:p>'
  46. );
  47. } );
  48. it( 'should split attribute element', () => {
  49. test(
  50. '<container:p><attribute:b view-priority="1">foo{}bar</attribute:b></container:p>',
  51. '<container:p>' +
  52. '<attribute:b view-priority="1">foo</attribute:b>[]<attribute:b view-priority="1">bar</attribute:b>' +
  53. '</container:p>'
  54. );
  55. } );
  56. it( 'should move from beginning of the nested text node to the container', () => {
  57. test(
  58. '<container:p>' +
  59. '<attribute:b view-priority="1"><attribute:u view-priority="1">{}foobar</attribute:u></attribute:b>' +
  60. '</container:p>',
  61. '<container:p>' +
  62. '[]<attribute:b view-priority="1"><attribute:u view-priority="1">foobar</attribute:u></attribute:b>' +
  63. '</container:p>'
  64. );
  65. } );
  66. it( 'should stick selection in text node if it is in container', () => {
  67. test(
  68. '<container:p>foo{}<attribute:b view-priority="1">bar</attribute:b></container:p>',
  69. '<container:p>foo{}<attribute:b view-priority="1">bar</attribute:b></container:p>'
  70. );
  71. } );
  72. it( 'should split nested attributes', () => {
  73. test(
  74. '<container:p>' +
  75. '<attribute:b view-priority="1"><attribute:u view-priority="1">foo{}bar</attribute:u></attribute:b>' +
  76. '</container:p>',
  77. '<container:p>' +
  78. '<attribute:b view-priority="1">' +
  79. '<attribute:u view-priority="1">' +
  80. 'foo' +
  81. '</attribute:u>' +
  82. '</attribute:b>' +
  83. '[]' +
  84. '<attribute:b view-priority="1">' +
  85. '<attribute:u view-priority="1">' +
  86. 'bar' +
  87. '</attribute:u>' +
  88. '</attribute:b>' +
  89. '</container:p>'
  90. );
  91. } );
  92. it( 'should move from end of the nested text node to the container', () => {
  93. test(
  94. '<container:p>' +
  95. '<attribute:b view-priority="1"><attribute:u view-priority="1">foobar{}</attribute:u></attribute:b>' +
  96. '</container:p>',
  97. '<container:p>' +
  98. '<attribute:b view-priority="1"><attribute:u view-priority="1">foobar</attribute:u></attribute:b>[]' +
  99. '</container:p>'
  100. );
  101. } );
  102. it( 'should split attribute element directly in document fragment', () => {
  103. test(
  104. '<attribute:b view-priority="1">foo{}bar</attribute:b>',
  105. '<attribute:b view-priority="1">foo</attribute:b>[]<attribute:b view-priority="1">bar</attribute:b>'
  106. );
  107. } );
  108. it( 'should not split text directly in document fragment', () => {
  109. test(
  110. 'foo{}bar',
  111. 'foo{}bar'
  112. );
  113. } );
  114. } );
  115. describe( 'break range', () => {
  116. /**
  117. * Executes test using `parse` and `stringify` utils functions.
  118. *
  119. * @param {String} input
  120. * @param {String} expected
  121. */
  122. function test( input, expected ) {
  123. let { view, selection } = parse( input );
  124. const newRange = breakAttributes( selection.getFirstRange() );
  125. expect( stringify( view.root, newRange, { showType: true } ) ).to.equal( expected );
  126. }
  127. it( 'should throw when range placed in two containers', () => {
  128. const p1 = new ContainerElement( 'p' );
  129. const p2 = new ContainerElement( 'p' );
  130. expect( () => {
  131. breakAttributes( Range.createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  132. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  133. } );
  134. it( 'should throw when range has no parent container', () => {
  135. const el = new AttributeElement( 'b' );
  136. expect( () => {
  137. breakAttributes( Range.createFromParentsAndOffsets( el, 0, el, 0 ) );
  138. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  139. } );
  140. it( 'should not break text nodes if they are not in attribute elements', () => {
  141. test(
  142. '<container:p>foo{}bar</container:p>',
  143. '<container:p>foo{}bar</container:p>'
  144. );
  145. } );
  146. it( 'should break at collapsed range and return collapsed one', () => {
  147. test(
  148. '<container:p><attribute:b>foo{}bar</attribute:b></container:p>',
  149. '<container:p><attribute:b>foo</attribute:b>[]<attribute:b>bar</attribute:b></container:p>'
  150. );
  151. } );
  152. it( 'should break inside text node #1', () => {
  153. test(
  154. '<container:p><attribute:b>foo{bar}baz</attribute:b></container:p>',
  155. '<container:p><attribute:b>foo</attribute:b>[<attribute:b>bar</attribute:b>]<attribute:b>baz</attribute:b></container:p>'
  156. );
  157. } );
  158. it( 'should break inside text node #2', () => {
  159. test(
  160. '<container:p><attribute:b>foo{barbaz}</attribute:b></container:p>',
  161. '<container:p><attribute:b>foo</attribute:b>[<attribute:b>barbaz</attribute:b>]</container:p>'
  162. );
  163. } );
  164. it( 'should break inside text node #3', () => {
  165. test(
  166. '<container:p><attribute:b>foo{barbaz]</attribute:b></container:p>',
  167. '<container:p><attribute:b>foo</attribute:b>[<attribute:b>barbaz</attribute:b>]</container:p>'
  168. );
  169. } );
  170. it( 'should break inside text node #4', () => {
  171. test(
  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 #5', () => {
  177. test(
  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 placed inside different nodes', () => {
  183. test(
  184. '<container:p>foo{bar<attribute:b>baz}qux</attribute:b></container:p>',
  185. '<container:p>foo{bar<attribute:b>baz</attribute:b>]<attribute:b>qux</attribute:b></container:p>'
  186. );
  187. } );
  188. it( 'should split attribute element directly in document fragment', () => {
  189. test(
  190. '<attribute:b>fo{ob}ar</attribute:b>',
  191. '<attribute:b>fo</attribute:b>[<attribute:b>ob</attribute:b>]<attribute:b>ar</attribute:b>'
  192. );
  193. } );
  194. it( 'should not split text directly in document fragment', () => {
  195. test(
  196. 'foo{}bar',
  197. 'foo{}bar'
  198. );
  199. } );
  200. } );
  201. } );
  202. } );