8
0

unwrap.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 { unwrap } 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 = unwrap( selection.getFirstRange(), parse( unwrapAttribute ) );
  31. expect( stringify( view, newRange, { showType: true, showPriority: true } ) ).to.equal( expected );
  32. }
  33. describe( 'unwrap', () => {
  34. it( 'should do nothing on collapsed ranges', () => {
  35. test(
  36. '<container:p>f{}oo</container:p>',
  37. '<attribute:b:10></attribute:b:10>',
  38. '<container:p>f{}oo</container:p>'
  39. );
  40. } );
  41. it( 'should do nothing on single text node', () => {
  42. test(
  43. '<container:p>[foobar]</container:p>',
  44. '<attribute:b:1></attribute:b:1>',
  45. '<container:p>[foobar]</container:p>'
  46. );
  47. } );
  48. it( 'should throw error when element is not instance of AttributeElement', () => {
  49. const container = new ContainerElement( 'p', null, new AttributeElement( 'b', null, new Text( 'foo' ) ) );
  50. const range = new Range(
  51. new Position( container, 0 ),
  52. new Position( container, 1 )
  53. );
  54. const b = new Element( 'b' );
  55. expect( () => {
  56. unwrap( range, b );
  57. } ).to.throw( CKEditorError, 'view-writer-unwrap-invalid-attribute' );
  58. } );
  59. it( 'should throw error when range placed in two containers', () => {
  60. const container1 = new ContainerElement( 'p' );
  61. const container2 = new ContainerElement( 'p' );
  62. const range = new Range(
  63. new Position( container1, 0 ),
  64. new Position( container2, 1 )
  65. );
  66. const b = new AttributeElement( 'b' );
  67. expect( () => {
  68. unwrap( range, b );
  69. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  70. } );
  71. it( 'should throw when range has no parent container', () => {
  72. const el = new AttributeElement( 'b' );
  73. const b = new AttributeElement( 'b' );
  74. expect( () => {
  75. unwrap( Range.createFromParentsAndOffsets( el, 0, el, 0 ), b );
  76. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  77. } );
  78. it( 'should unwrap single node', () => {
  79. test(
  80. '<container:p>[<attribute:b:1>foobar</attribute:b:1>]</container:p>',
  81. '<attribute:b:1></attribute:b:1>',
  82. '<container:p>[foobar]</container:p>'
  83. );
  84. } );
  85. it( 'should not unwrap attributes with different priorities #1', () => {
  86. test(
  87. '<container:p>[<attribute:b:1>foobar</attribute:b:1>]</container:p>',
  88. '<attribute:b:2></attribute:b:2>',
  89. '<container:p>[<attribute:b:1>foobar</attribute:b:1>]</container:p>'
  90. );
  91. } );
  92. it( 'should not unwrap attributes with different priorities #2', () => {
  93. test(
  94. '<container:p>' +
  95. '[' +
  96. '<attribute:b:2>foo</attribute:b:2>' +
  97. '<attribute:b:1>bar</attribute:b:1>' +
  98. '<attribute:b:2>baz</attribute:b:2>' +
  99. ']' +
  100. '</container:p>',
  101. '<attribute:b:2></attribute:b:2>',
  102. '<container:p>[foo<attribute:b:1>bar</attribute:b:1>baz]</container:p>'
  103. );
  104. } );
  105. it( 'should unwrap part of the node', () => {
  106. test(
  107. '<container:p>[baz<attribute:b:1>foo}bar</attribute:b:1>',
  108. '<attribute:b:1></attribute:b:1>',
  109. '<container:p>[bazfoo]<attribute:b:1>bar</attribute:b:1></container:p>'
  110. );
  111. } );
  112. it( 'should unwrap nested attributes', () => {
  113. test(
  114. '<container:p>[<attribute:u:1><attribute:b:1>foobar</attribute:b:1></attribute:u:1>]</container:p>',
  115. '<attribute:b:1></attribute:b:1>',
  116. '<container:p>[<attribute:u:1>foobar</attribute:u:1>]</container:p>'
  117. );
  118. } );
  119. it( 'should merge unwrapped nodes #1', () => {
  120. test(
  121. '<container:p>foo[<attribute:b:1>bar</attribute:b:1>]baz</container:p>',
  122. '<attribute:b:1></attribute:b:1>',
  123. '<container:p>foo{bar}baz</container:p>'
  124. );
  125. } );
  126. it( 'should merge unwrapped nodes #2', () => {
  127. const input = '<container:p>' +
  128. 'foo' +
  129. '<attribute:u:1>bar</attribute:u:1>' +
  130. '[' +
  131. '<attribute:b:1>' +
  132. '<attribute:u:1>bazqux</attribute:u:1>' +
  133. '</attribute:b:1>' +
  134. ']' +
  135. '</container:p>';
  136. const attribute = '<attribute:b:1></attribute:b:1>';
  137. const result = '<container:p>foo<attribute:u:1>bar{bazqux</attribute:u:1>]</container:p>';
  138. test( input, attribute, result );
  139. } );
  140. it( 'should merge unwrapped nodes #3', () => {
  141. const input = '<container:p>' +
  142. 'foo' +
  143. '<attribute:u:1>bar</attribute:u:1>' +
  144. '[' +
  145. '<attribute:b:1>' +
  146. '<attribute:u:1>baz}qux</attribute:u:1>' +
  147. '</attribute:b:1>' +
  148. '</container:p>';
  149. const attribute = '<attribute:b:1></attribute:b:1>';
  150. const result = '<container:p>' +
  151. 'foo' +
  152. '<attribute:u:1>bar{baz</attribute:u:1>]' +
  153. '<attribute:b:1>' +
  154. '<attribute:u:1>qux</attribute:u:1>' +
  155. '</attribute:b:1>' +
  156. '</container:p>';
  157. test( input, attribute, result );
  158. } );
  159. it( 'should merge unwrapped nodes #4', () => {
  160. const input = '<container:p>' +
  161. 'foo' +
  162. '<attribute:u:1>bar</attribute:u:1>' +
  163. '[' +
  164. '<attribute:b:1>' +
  165. '<attribute:u:1>baz</attribute:u:1>' +
  166. '</attribute:b:1>' +
  167. ']' +
  168. '<attribute:u:1>qux</attribute:u:1>' +
  169. '</container:p>';
  170. const attribute = '<attribute:b:1></attribute:b:1>';
  171. const result = '<container:p>' +
  172. 'foo' +
  173. '<attribute:u:1>bar{baz}qux</attribute:u:1>' +
  174. '</container:p>';
  175. test( input, attribute, result );
  176. } );
  177. it( 'should merge unwrapped nodes #5', () => {
  178. const input = '<container:p>' +
  179. '[' +
  180. '<attribute:b:1><attribute:u:1>foo</attribute:u:1></attribute:b:1>' +
  181. '<attribute:b:1><attribute:u:1>bar</attribute:u:1></attribute:b:1>' +
  182. '<attribute:b:1><attribute:u:1>baz</attribute:u:1></attribute:b:1>' +
  183. ']' +
  184. '</container:p>';
  185. const attribute = '<attribute:b:1></attribute:b:1>';
  186. const result = '<container:p>[<attribute:u:1>foobarbaz</attribute:u:1>]</container:p>';
  187. test( input, attribute, result );
  188. } );
  189. it( 'should unwrap mixed ranges #1', () => {
  190. const input = '<container:p>' +
  191. '[' +
  192. '<attribute:u:1>' +
  193. '<attribute:b:1>foo]</attribute:b:1>' +
  194. '</attribute:u:1>' +
  195. '</container:p>';
  196. const attribute = '<attribute:b:1></attribute:b:1>';
  197. const result = '<container:p>[<attribute:u:1>foo</attribute:u:1>]</container:p>';
  198. test( input, attribute, result );
  199. } );
  200. it( 'should unwrap mixed ranges #2', () => {
  201. test(
  202. '<container:p>[<attribute:u:1><attribute:b:1>foo}</attribute:b:1></attribute:u></container:p>',
  203. '<attribute:b:1></attribute:b:1>',
  204. '<container:p>[<attribute:u:1>foo</attribute:u:1>]</container:p>'
  205. );
  206. } );
  207. it( 'should unwrap single element by removing matching attributes', () => {
  208. test(
  209. '<container:p>[<attribute:b:1 foo="bar" baz="qux">test</attribute:b:1>]</container:p>',
  210. '<attribute:b:1 baz="qux"></attribute:b:1>',
  211. '<container:p>[<attribute:b:1 foo="bar">test</attribute:b:1>]</container:p>'
  212. );
  213. } );
  214. it( 'should not unwrap single element when attributes are different', () => {
  215. test(
  216. '<container:p>[<attribute:b:1 baz="qux" foo="bar">test</attribute:b:1>]</container:p>',
  217. '<attribute:b:1 baz="qux" test="true"></attribute:b:1>',
  218. '<container:p>[<attribute:b:1 baz="qux" foo="bar">test</attribute:b:1>]</container:p>'
  219. );
  220. } );
  221. it( 'should unwrap single element by removing matching classes', () => {
  222. test(
  223. '<container:p>[<attribute:b:1 class="foo bar baz">test</attribute:b:1>]</container:p>',
  224. '<attribute:b:1 class="baz foo"></attribute:b:1>',
  225. '<container:p>[<attribute:b:1 class="bar">test</attribute:b:1>]</container:p>'
  226. );
  227. } );
  228. it( 'should not unwrap single element when classes are different', () => {
  229. test(
  230. '<container:p>[<attribute:b:1 class="foo bar baz">test</attribute:b:1>]</container:p>',
  231. '<attribute:b:1 class="baz foo qux"></attribute:b:1>',
  232. '<container:p>[<attribute:b:1 class="foo bar baz">test</attribute:b:1>]</container:p>'
  233. );
  234. } );
  235. it( 'should unwrap single element by removing matching styles', () => {
  236. test(
  237. '<container:p>[<attribute:b:1 style="color:red;position:absolute;top:10px;">test</attribute:b:1>]</container:p>',
  238. '<attribute:b:1 style="position: absolute;"></attribute:b:1>',
  239. '<container:p>[<attribute:b:1 style="color:red;top:10px;">test</attribute:b:1>]</container:p>'
  240. );
  241. } );
  242. it( 'should not unwrap single element when styles are different', () => {
  243. test(
  244. '<container:p>[<attribute:b:1 style="color:red;position:absolute;top:10px;">test</attribute:b:1>]</container:p>',
  245. '<attribute:b:1 style="position: relative;"></attribute:b:1>',
  246. '<container:p>[<attribute:b:1 style="color:red;position:absolute;top:10px;">test</attribute:b:1>]</container:p>'
  247. );
  248. } );
  249. it( 'should unwrap single node in document fragment', () => {
  250. test(
  251. '<container:p>[<attribute:b:1>foobar</attribute:b:1>]</container:p>',
  252. '<attribute:b:1></attribute:b:1>',
  253. '<container:p>[foobar]</container:p>'
  254. );
  255. } );
  256. } );
  257. } );