8
0

wrap.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 { wrap } from '/ckeditor5/engine/view/writer.js';
  7. import Element from '/ckeditor5/engine/view/element.js';
  8. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  9. import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  10. import Position from '/ckeditor5/engine/view/position.js';
  11. import Range from '/ckeditor5/engine/view/range.js';
  12. import Text from '/ckeditor5/engine/view/text.js';
  13. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  14. import { stringify, parse } from '/tests/engine/_utils/view.js';
  15. describe( 'writer', () => {
  16. /**
  17. * Executes test using `parse` and `stringify` utils functions.
  18. *
  19. * @param {String} input
  20. * @param {String} wrapAttribute
  21. * @param {String} expected
  22. */
  23. function test( input, wrapAttribute, expected ) {
  24. let { view, selection } = parse( input );
  25. const newRange = wrap( selection.getFirstRange(), parse( wrapAttribute ) );
  26. expect( stringify( view.root, newRange, { showType: true, showPriority: true } ) ).to.equal( expected );
  27. }
  28. describe( 'wrap', () => {
  29. it( 'should do nothing on collapsed ranges', () => {
  30. test(
  31. '<container:p>f{}oo</container:p>',
  32. '<attribute:b></attribute:b>',
  33. '<container:p>f{}oo</container:p>'
  34. );
  35. } );
  36. it( 'wraps single text node', () => {
  37. test(
  38. '<container:p>[foobar]</container:p>',
  39. '<attribute:b:1></attribute:b:1>',
  40. '<container:p>[<attribute:b:1>foobar</attribute:b:1>]</container:p>'
  41. );
  42. } );
  43. it( 'wraps single text node in document fragment', () => {
  44. test(
  45. '{foobar}',
  46. '<attribute:b:1></attribute:b:1>',
  47. '[<attribute:b:1>foobar</attribute:b:1>]'
  48. );
  49. } );
  50. it( 'should throw error when element is not instance of AttributeElement', () => {
  51. const container = new ContainerElement( 'p', null, new Text( 'foo' ) );
  52. const range = new Range(
  53. new Position( container, 0 ),
  54. new Position( container, 1 )
  55. );
  56. const b = new Element( 'b' );
  57. expect( () => {
  58. wrap( range, b );
  59. } ).to.throw( CKEditorError, 'view-writer-wrap-invalid-attribute' );
  60. } );
  61. it( 'should throw error when range placed in two containers', () => {
  62. const container1 = new ContainerElement( 'p' );
  63. const container2 = new ContainerElement( 'p' );
  64. const range = new Range(
  65. new Position( container1, 0 ),
  66. new Position( container2, 1 )
  67. );
  68. const b = new AttributeElement( 'b' );
  69. expect( () => {
  70. wrap( range, b );
  71. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  72. } );
  73. it( 'should throw when range has no parent container', () => {
  74. const el = new AttributeElement( 'b' );
  75. const b = new AttributeElement( 'b' );
  76. expect( () => {
  77. wrap( Range.createFromParentsAndOffsets( el, 0, el, 0 ), b );
  78. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  79. } );
  80. it( 'wraps part of a single text node #1', () => {
  81. test(
  82. '<container:p>[foo}bar</container:p>',
  83. '<attribute:b:1></attribute:b:1>',
  84. '<container:p>[<attribute:b:1>foo</attribute:b:1>]bar</container:p>'
  85. );
  86. } );
  87. it( 'wraps part of a single text node #2', () => {
  88. test(
  89. '<container:p>{foo}bar</container:p>',
  90. '<attribute:b:1></attribute:b:1>',
  91. '<container:p>[<attribute:b:1>foo</attribute:b:1>]bar</container:p>'
  92. );
  93. } );
  94. it( 'should support unicode', () => {
  95. test(
  96. '<container:p>நி{லை}க்கு</container:p>',
  97. '<attribute:b:1></attribute:b:1>',
  98. '<container:p>நி[<attribute:b:1>லை</attribute:b:1>]க்கு</container:p>'
  99. );
  100. } );
  101. it( 'wraps part of a single text node #3', () => {
  102. test(
  103. '<container:p>foo{bar}</container:p>',
  104. '<attribute:b:1></attribute:b:1>',
  105. '<container:p>foo[<attribute:b:1>bar</attribute:b:1>]</container:p>'
  106. );
  107. } );
  108. it( 'should not wrap inside nested containers', () => {
  109. test(
  110. '<container:div>[foobar<container:p>baz</container:p>]</container:div>',
  111. '<attribute:b:1></attribute:b:1>',
  112. '<container:div>[<attribute:b:1>foobar</attribute:b:1><container:p>baz</container:p>]</container:div>'
  113. );
  114. } );
  115. it( 'wraps according to priorities', () => {
  116. test(
  117. '<container:p>[<attribute:u:1>foobar</attribute:u:1>]</container:p>',
  118. '<attribute:b:2></attribute:b:2>',
  119. '<container:p>[<attribute:u:1><attribute:b:2>foobar</attribute:b:2></attribute:u:1>]</container:p>'
  120. );
  121. } );
  122. it( 'merges wrapped nodes #1', () => {
  123. test(
  124. '<container:p>[<attribute:b:1>foo</attribute:b:1>bar<attribute:b:1>baz</attribute:b:1>]</container:p>',
  125. '<attribute:b:1></attribute:b:1>',
  126. '<container:p>[<attribute:b:1>foobarbaz</attribute:b:1>]</container:p>'
  127. );
  128. } );
  129. it( 'merges wrapped nodes #2', () => {
  130. test(
  131. '<container:p><attribute:b:1>foo</attribute:b:1>[bar}baz</container:p>',
  132. '<attribute:b:1></attribute:b:1>',
  133. '<container:p><attribute:b:1>foo{bar</attribute:b:1>]baz</container:p>'
  134. );
  135. } );
  136. it( 'merges wrapped nodes #3', () => {
  137. test(
  138. '<container:p><attribute:b:1>foobar</attribute:b:1>[baz]</container:p>',
  139. '<attribute:b:1></attribute:b:1>',
  140. '<container:p><attribute:b:1>foobar{baz</attribute:b:1>]</container:p>'
  141. );
  142. } );
  143. it( 'merges wrapped nodes #4', () => {
  144. test(
  145. '<container:p>[foo<attribute:i:1>bar</attribute:i:1>]baz</container:p>',
  146. '<attribute:b:1></attribute:b:1>',
  147. '<container:p>[<attribute:b:1>foo<attribute:i:1>bar</attribute:i:1></attribute:b:1>]baz</container:p>'
  148. );
  149. } );
  150. it( 'merges wrapped nodes #5', () => {
  151. test(
  152. '<container:p>[foo<attribute:i:1>bar</attribute:i:1>baz]</container:p>',
  153. '<attribute:b:2></attribute:b:2>',
  154. '<container:p>' +
  155. '[' +
  156. '<attribute:b:2>foo</attribute:b:2>' +
  157. '<attribute:i:1>' +
  158. '<attribute:b:2>bar</attribute:b:2>' +
  159. '</attribute:i:1>' +
  160. '<attribute:b:2>baz</attribute:b:2>' +
  161. ']' +
  162. '</container:p>'
  163. );
  164. } );
  165. it( 'should wrap single element by merging attributes', () => {
  166. test(
  167. '<container:p>[<attribute:b:1 foo="bar" one="two"></attribute:b:1>]</container:p>',
  168. '<attribute:b:1 baz="qux" one="two"></attribute:b:1>',
  169. '<container:p>[<attribute:b:1 baz="qux" foo="bar" one="two"></attribute:b:1>]</container:p>'
  170. );
  171. } );
  172. it( 'should not merge attributes when they differ', () => {
  173. test(
  174. '<container:p>[<attribute:b:1 foo="bar"></attribute:b:1>]</container:p>',
  175. '<attribute:b:1 foo="baz"></attribute:b:1>',
  176. '<container:p>[<attribute:b:1 foo="baz"><attribute:b:1 foo="bar"></attribute:b:1></attribute:b:1>]</container:p>'
  177. );
  178. } );
  179. it( 'should wrap single element by merging classes', () => {
  180. test(
  181. '<container:p>[<attribute:b:1 class="foo bar baz"></attribute:b:1>]</container:p>',
  182. '<attribute:b:1 class="foo bar qux jax"></attribute:b:1>',
  183. '<container:p>[<attribute:b:1 class="foo bar baz qux jax"></attribute:b:1>]</container:p>'
  184. );
  185. } );
  186. it( 'should wrap single element by merging styles', () => {
  187. test(
  188. '<container:p>[<attribute:b:1 style="color:red; position: absolute;"></attribute:b:1>]</container:p>',
  189. '<attribute:b:1 style="color:red; top: 20px;"></attribute:b:1>',
  190. '<container:p>[<attribute:b:1 style="color:red;position:absolute;top:20px;"></attribute:b:1>]</container:p>'
  191. );
  192. } );
  193. it( 'should not merge styles when they differ', () => {
  194. test(
  195. '<container:p>[<attribute:b:1 style="color:red;"></attribute:b:1>]</container:p>',
  196. '<attribute:b:1 style="color:black;"></attribute:b:1>',
  197. '<container:p>' +
  198. '[' +
  199. '<attribute:b:1 style="color:black;">' +
  200. '<attribute:b:1 style="color:red;"></attribute:b:1>' +
  201. '</attribute:b:1>' +
  202. ']' +
  203. '</container:p>'
  204. );
  205. } );
  206. it( 'should not merge single elements when they have different priority', () => {
  207. test(
  208. '<container:p>[<attribute:b:2 style="color:red;"></attribute:b:2>]</container:p>',
  209. '<attribute:b:1 style="color:red;"></attribute:b:1>',
  210. '<container:p>' +
  211. '[' +
  212. '<attribute:b:1 style="color:red;">' +
  213. '<attribute:b:2 style="color:red;"></attribute:b:2>' +
  214. '</attribute:b:1>' +
  215. ']</container:p>'
  216. );
  217. } );
  218. it( 'should be merged with outside element when wrapping all children', () => {
  219. test(
  220. '<container:p><attribute:b:1 foo="bar">[foobar<attribute:i:1>baz</attribute:i:1>]</attribute:b:1></container:p>',
  221. '<attribute:b:1 baz="qux"></attribute:b:1>',
  222. '<container:p>' +
  223. '[' +
  224. '<attribute:b:1 baz="qux" foo="bar">' +
  225. 'foobar' +
  226. '<attribute:i:1>baz</attribute:i:1>' +
  227. '</attribute:b:1>' +
  228. ']' +
  229. '</container:p>'
  230. );
  231. } );
  232. } );
  233. } );