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