8
0

wrap.js 8.3 KB

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