wrap.js 8.5 KB

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