unwrap.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Writer from '/ckeditor5/engine/treeview/writer.js';
  8. import Element from '/ckeditor5/engine/treeview/element.js';
  9. import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
  10. import AttributeElement from '/ckeditor5/engine/treeview/attributeelement.js';
  11. import Position from '/ckeditor5/engine/treeview/position.js';
  12. import Range from '/ckeditor5/engine/treeview/range.js';
  13. import Text from '/ckeditor5/engine/treeview/text.js';
  14. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  15. import { stringify, parse } from '/tests/engine/_utils/view.js';
  16. describe( 'Writer', () => {
  17. let 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. const { view, selection } = parse( input );
  27. const newRange = writer.unwrap( selection.getFirstRange(), parse( unwrapAttribute ) );
  28. expect( stringify( view, newRange, { showType: true, showPriority: true } ) ).to.equal( expected );
  29. }
  30. beforeEach( () => {
  31. writer = new Writer();
  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. writer.unwrap( range, b );
  57. } ).to.throw( CKEditorError, 'treeview-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. writer.unwrap( range, b, 1 );
  69. } ).to.throw( CKEditorError, 'treeview-writer-invalid-range-container' );
  70. } );
  71. it( 'should unwrap single node', () => {
  72. test(
  73. '<container:p>[<attribute:b:1>foobar</attribute:b:1>]</container:p>',
  74. '<attribute:b:1></attribute:b:1>',
  75. '<container:p>[foobar]</container:p>'
  76. );
  77. } );
  78. it( 'should not unwrap attributes with different priorities #1', () => {
  79. test(
  80. '<container:p>[<attribute:b:1>foobar</attribute:b:1>]</container:p>',
  81. '<attribute:b:2></attribute:b:2>',
  82. '<container:p>[<attribute:b:1>foobar</attribute:b:1>]</container:p>'
  83. );
  84. } );
  85. it( 'should not unwrap attributes with different priorities #2', () => {
  86. test(
  87. '<container:p>' +
  88. '[' +
  89. '<attribute:b:2>foo</attribute:b:2>' +
  90. '<attribute:b:1>bar</attribute:b:1>' +
  91. '<attribute:b:2>baz</attribute:b:2>' +
  92. ']' +
  93. '</container:p>',
  94. '<attribute:b:2></attribute:b:2>',
  95. '<container:p>[foo<attribute:b:1>bar</attribute:b:1>baz]</container:p>'
  96. );
  97. } );
  98. it( 'should unwrap part of the node', () => {
  99. test(
  100. '<container:p>[baz<attribute:b:1>foo}bar</attribute:b:1>',
  101. '<attribute:b:1></attribute:b:1>',
  102. '<container:p>[bazfoo]<attribute:b:1>bar</attribute:b:1></container:p>'
  103. );
  104. } );
  105. it( 'should unwrap nested attributes', () => {
  106. test(
  107. '<container:p>[<attribute:u:1><attribute:b:1>foobar</attribute:b:1></attribute:u:1>]</container:p>',
  108. '<attribute:b:1></attribute:b:1>',
  109. '<container:p>[<attribute:u:1>foobar</attribute:u:1>]</container:p>'
  110. );
  111. } );
  112. it( 'should merge unwrapped nodes #1', () => {
  113. test(
  114. '<container:p>foo[<attribute:b:1>bar</attribute:b:1>]baz</container:p>',
  115. '<attribute:b:1></attribute:b:1>',
  116. '<container:p>foo{bar}baz</container:p>'
  117. );
  118. } );
  119. it( 'should merge unwrapped nodes #2', () => {
  120. const input = '<container:p>' +
  121. 'foo' +
  122. '<attribute:u:1>bar</attribute:u:1>' +
  123. '[' +
  124. '<attribute:b:1>' +
  125. '<attribute:u:1>bazqux</attribute:u:1>' +
  126. '</attribute:b:1>' +
  127. ']' +
  128. '</container:p>';
  129. const attribute = '<attribute:b:1></attribute:b:1>';
  130. const result = '<container:p>foo<attribute:u:1>bar{bazqux</attribute:u:1>]</container:p>';
  131. test( input, attribute, result );
  132. } );
  133. it( 'should merge unwrapped nodes #3', () => {
  134. const input = '<container:p>' +
  135. 'foo' +
  136. '<attribute:u:1>bar</attribute:u:1>' +
  137. '[' +
  138. '<attribute:b:1>' +
  139. '<attribute:u:1>baz}qux</attribute:u:1>' +
  140. '</attribute:b:1>' +
  141. '</container:p>';
  142. const attribute = '<attribute:b:1></attribute:b:1>';
  143. const result = '<container:p>' +
  144. 'foo' +
  145. '<attribute:u:1>bar{baz</attribute:u:1>]' +
  146. '<attribute:b:1>' +
  147. '<attribute:u:1>qux</attribute:u:1>' +
  148. '</attribute:b:1>' +
  149. '</container:p>';
  150. test( input, attribute, result );
  151. } );
  152. it( 'should merge unwrapped nodes #4', () => {
  153. const input = '<container:p>' +
  154. 'foo' +
  155. '<attribute:u:1>bar</attribute:u:1>' +
  156. '[' +
  157. '<attribute:b:1>' +
  158. '<attribute:u:1>baz</attribute:u:1>' +
  159. '</attribute:b:1>' +
  160. ']' +
  161. '<attribute:u:1>qux</attribute:u:1>' +
  162. '</container:p>';
  163. const attribute = '<attribute:b:1></attribute:b:1>';
  164. const result = '<container:p>' +
  165. 'foo' +
  166. '<attribute:u:1>bar{baz}qux</attribute:u:1>' +
  167. '</container:p>';
  168. test( input, attribute, result );
  169. } );
  170. it( 'should merge unwrapped nodes #5', () => {
  171. const input = '<container:p>' +
  172. '[' +
  173. '<attribute:b:1><attribute:u:1>foo</attribute:u:1></attribute:b:1>' +
  174. '<attribute:b:1><attribute:u:1>bar</attribute:u:1></attribute:b:1>' +
  175. '<attribute:b:1><attribute:u:1>baz</attribute:u:1></attribute:b:1>' +
  176. ']' +
  177. '</container:p>';
  178. const attribute = '<attribute:b:1></attribute:b:1>';
  179. const result = '<container:p>[<attribute:u:1>foobarbaz</attribute:u:1>]</container:p>';
  180. test( input, attribute, result );
  181. } );
  182. it( 'should unwrap mixed ranges #1', () => {
  183. const input = '<container:p>' +
  184. '[' +
  185. '<attribute:u:1>' +
  186. '<attribute:b:1>foo]</attribute:b:1>' +
  187. '</attribute:u:1>' +
  188. '</container:p>';
  189. const attribute = '<attribute:b:1></attribute:b:1>';
  190. const result = '<container:p>[<attribute:u:1>foo</attribute:u:1>]</container:p>';
  191. test( input, attribute, result );
  192. } );
  193. it( 'should unwrap mixed ranges #2', () => {
  194. test(
  195. '<container:p>[<attribute:u:1><attribute:b:1>foo}</attribute:b:1></attribute:u></container:p>',
  196. '<attribute:b:1></attribute:b:1>',
  197. '<container:p>[<attribute:u:1>foo</attribute:u:1>]</container:p>'
  198. );
  199. } );
  200. it( 'should unwrap single element by removing matching attributes', () => {
  201. test(
  202. '<container:p>[<attribute:b:1 foo="bar" baz="qux">test</attribute:b:1>]</container:p>',
  203. '<attribute:b:1 baz="qux"></attribute:b:1>',
  204. '<container:p>[<attribute:b:1 foo="bar">test</attribute:b:1>]</container:p>'
  205. );
  206. } );
  207. it( 'should not unwrap single element when attributes are different', () => {
  208. test(
  209. '<container:p>[<attribute:b:1 foo="bar" baz="qux">test</attribute:b:1>]</container:p>',
  210. '<attribute:b:1 baz="qux" test="true"></attribute:b:1>',
  211. '<container:p>[<attribute:b:1 foo="bar" baz="qux">test</attribute:b:1>]</container:p>'
  212. );
  213. } );
  214. it( 'should unwrap single element by removing matching classes', () => {
  215. test(
  216. '<container:p>[<attribute:b:1 class="foo bar baz">test</attribute:b:1>]</container:p>',
  217. '<attribute:b:1 class="baz foo"></attribute:b:1>',
  218. '<container:p>[<attribute:b:1 class="bar">test</attribute:b:1>]</container:p>'
  219. );
  220. } );
  221. it( 'should not unwrap single element when classes are different', () => {
  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 qux"></attribute:b:1>',
  225. '<container:p>[<attribute:b:1 class="foo bar baz">test</attribute:b:1>]</container:p>'
  226. );
  227. } );
  228. it( 'should unwrap single element by removing matching styles', () => {
  229. test(
  230. '<container:p>[<attribute:b:1 style="color:red;position:absolute;top:10px;">test</attribute:b:1>]</container:p>',
  231. '<attribute:b:1 style="position: absolute;"></attribute:b:1>',
  232. '<container:p>[<attribute:b:1 style="color:red;top:10px;">test</attribute:b:1>]</container:p>'
  233. );
  234. } );
  235. it( 'should not unwrap single element when styles are different', () => {
  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: relative;"></attribute:b:1>',
  239. '<container:p>[<attribute:b:1 style="color:red;position:absolute;top:10px;">test</attribute:b:1>]</container:p>'
  240. );
  241. } );
  242. } );
  243. } );