8
0

mergeattributes.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/core/treeview/writer.js';
  8. import Element from '/ckeditor5/core/treeview/element.js';
  9. import Text from '/ckeditor5/core/treeview/text.js';
  10. import utils from '/tests/core/treeview/writer/_utils/utils.js';
  11. describe( 'Writer', () => {
  12. const create = utils.create;
  13. const test = utils.test;
  14. let writer;
  15. beforeEach( () => {
  16. writer = new Writer();
  17. } );
  18. describe( 'mergeAttributes', () => {
  19. it( 'should not merge if inside text node', () => {
  20. // <p>{fo|obar}</p>
  21. const description = {
  22. instanceOf: Element,
  23. name: 'p',
  24. children: [
  25. { instanceOf: Text, data: 'foobar', position: 2 }
  26. ]
  27. };
  28. const created = create( writer, description );
  29. const newPosition = writer.mergeAttributes( created.position );
  30. test( writer, newPosition, created.node, description );
  31. } );
  32. it( 'should not merge if between containers', () => {
  33. // <div><p>{foo}</p>|<p>{bar}</p></div>
  34. const description = {
  35. instanceOf: Element,
  36. name: 'div',
  37. position: 1,
  38. children: [
  39. {
  40. instanceOf: Element,
  41. name: 'p',
  42. children: [
  43. { instanceOf: Text, data: 'foo' }
  44. ]
  45. },
  46. {
  47. instanceOf: Element,
  48. name: 'p',
  49. children: [
  50. { instanceOf: Text, data: 'bar' }
  51. ]
  52. }
  53. ]
  54. };
  55. const created = create( writer, description );
  56. const newPosition = writer.mergeAttributes( created.position );
  57. test( writer, newPosition, created.node, description );
  58. } );
  59. it( 'should return same position when inside empty container', () => {
  60. // <p>|</p>
  61. const description = { instanceOf: Element, name: 'p', position: 0 };
  62. const created = create( writer, description );
  63. const newPosition = writer.mergeAttributes( created.position );
  64. test( writer, newPosition, created.node, description );
  65. } );
  66. it( 'should not merge when position is placed at the beginning of the container', () => {
  67. // <p>|<b></b></p>
  68. const description = {
  69. instanceOf: Element,
  70. name: 'p',
  71. position: 0,
  72. children: [
  73. {
  74. instanceOf: Element,
  75. name: 'b',
  76. priority: 1
  77. }
  78. ]
  79. };
  80. const created = create( writer, description );
  81. const newPosition = writer.mergeAttributes( created.position );
  82. test( writer, newPosition, created.node, description );
  83. } );
  84. it( 'should not merge when position is placed at the end of the container', () => {
  85. // <p><b></b>|</p>
  86. const description = {
  87. instanceOf: Element,
  88. name: 'p',
  89. position: 1,
  90. children: [
  91. {
  92. instanceOf: Element,
  93. name: 'b',
  94. priority: 1
  95. }
  96. ]
  97. };
  98. const created = create( writer, description );
  99. const newPosition = writer.mergeAttributes( created.position );
  100. test( writer, newPosition, created.node, description );
  101. } );
  102. it( 'should merge when placed between two text nodes', () => {
  103. // <p>{foo}|{bar}</p> -> <p>{foo|bar}</p>
  104. const created = create( writer, {
  105. instanceOf: Element,
  106. name: 'p',
  107. position: 1,
  108. children: [
  109. { instanceOf: Text, data: 'foo' },
  110. { instanceOf: Text, data: 'bar' }
  111. ]
  112. } );
  113. const newPosition = writer.mergeAttributes( created.position );
  114. test( writer, newPosition, created.node, {
  115. instanceOf: Element,
  116. name: 'p',
  117. children: [
  118. { instanceOf: Text, data: 'foobar', position: 3 }
  119. ]
  120. } );
  121. } );
  122. it( 'should merge when placed between similar attribute nodes', () => {
  123. // <p><b foo="bar"></b>|<b foo="bar"></b></p> -> <p><b foo="bar">|</b></p>
  124. const created = create( writer, {
  125. instanceOf: Element,
  126. name: 'p',
  127. position: 1,
  128. children: [
  129. {
  130. instanceOf: Element,
  131. name: 'b',
  132. priority: 1,
  133. attributes: { foo: 'bar' }
  134. },
  135. {
  136. instanceOf: Element,
  137. name: 'b',
  138. priority: 1,
  139. attributes: { foo: 'bar' }
  140. }
  141. ]
  142. } );
  143. const newPosition = writer.mergeAttributes( created.position );
  144. test( writer, newPosition, created.node, {
  145. instanceOf: Element,
  146. name: 'p',
  147. children: [
  148. {
  149. instanceOf: Element,
  150. name: 'b',
  151. priority: 1,
  152. position: 0,
  153. attributes: { foo: 'bar' }
  154. }
  155. ]
  156. } );
  157. } );
  158. it( 'should not merge when placed between non-similar attribute nodes', () => {
  159. // <p><b foo="bar"></b>|<b foo="baz"></b></p> ->
  160. // <p><b foo="bar"></b>|<b foo="baz"></b></p>
  161. const description = {
  162. instanceOf: Element,
  163. name: 'p',
  164. position: 1,
  165. children: [
  166. {
  167. instanceOf: Element,
  168. name: 'b',
  169. priority: 1,
  170. attributes: { foo: 'bar' }
  171. },
  172. {
  173. instanceOf: Element,
  174. name: 'b',
  175. priority: 1,
  176. attributes: { foo: 'baz' }
  177. }
  178. ]
  179. };
  180. const created = create( writer, description );
  181. const newPosition = writer.mergeAttributes( created.position );
  182. test( writer, newPosition, created.node, description );
  183. } );
  184. it( 'should not merge when placed between similar attribute nodes with different priority', () => {
  185. // <p><b foo="bar"></b>|<b foo="bar"></b></p> -> <p><b foo="bar"></b>|<b foo="bar"></b></p>
  186. const description = {
  187. instanceOf: Element,
  188. name: 'p',
  189. position: 1,
  190. children: [
  191. {
  192. instanceOf: Element,
  193. name: 'b',
  194. priority: 1,
  195. attributes: { foo: 'bar' }
  196. },
  197. {
  198. instanceOf: Element,
  199. name: 'b',
  200. priority: 2,
  201. attributes: { foo: 'bar' }
  202. }
  203. ]
  204. };
  205. const created = create( writer,description );
  206. const newPosition = writer.mergeAttributes( created.position );
  207. test( writer, newPosition, created.node, description );
  208. } );
  209. it( 'should merge attribute nodes and their contents if possible', () => {
  210. // <p><b foo="bar">{foo}</b>|<b foo="bar">{bar}</b></p>
  211. // <p><b foo="bar">{foo}|{bar}</b></p>
  212. // <p><b foo="bar">{foo|bar}</b></p>
  213. const created = create( writer, {
  214. instanceOf: Element,
  215. name: 'p',
  216. position: 1,
  217. children: [
  218. {
  219. instanceOf: Element,
  220. name: 'b',
  221. priority: 1,
  222. attributes: { foo: 'bar' },
  223. children: [
  224. { instanceOf: Text, data: 'foo' }
  225. ]
  226. },
  227. {
  228. instanceOf: Element,
  229. name: 'b',
  230. priority: 1,
  231. attributes: { foo: 'bar' },
  232. children: [
  233. { instanceOf: Text, data: 'bar' }
  234. ]
  235. }
  236. ]
  237. } );
  238. const newPosition = writer.mergeAttributes( created.position );
  239. test( writer, newPosition, created.node, {
  240. instanceOf: Element,
  241. name: 'p',
  242. children: [
  243. {
  244. instanceOf: Element,
  245. name: 'b',
  246. priority: 1,
  247. attributes: { foo: 'bar' },
  248. children: [
  249. { instanceOf: Text, data: 'foobar', position: 3 }
  250. ]
  251. }
  252. ]
  253. } );
  254. } );
  255. } );
  256. } );