insert.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 Position from '/ckeditor5/engine/treeview/position.js';
  11. import Text from '/ckeditor5/engine/treeview/text.js';
  12. import utils from '/tests/engine/treeview/writer/_utils/utils.js';
  13. describe( 'Writer', () => {
  14. const create = utils.create;
  15. const test = utils.test;
  16. let writer;
  17. beforeEach( () => {
  18. writer = new Writer();
  19. } );
  20. describe( 'insert', () => {
  21. it( 'should return collapsed range in insertion position when using empty array', () => {
  22. // <p>{foo|bar}</p> -> <p>{foo[]bar}</p>
  23. const created = create( writer, {
  24. instanceOf: ContainerElement,
  25. name: 'p',
  26. children: [
  27. { instanceOf: Text, data: 'foobar', position: 3 }
  28. ]
  29. } );
  30. const newRange = writer.insert( created.position, [] );
  31. test( writer, newRange, created.node, {
  32. instanceOf: ContainerElement,
  33. name: 'p',
  34. children: [
  35. { instanceOf: Text, data: 'foobar', rangeStart: 3, rangeEnd: 3 }
  36. ]
  37. } );
  38. } );
  39. it( 'should insert text into another text node #1', () => {
  40. // <p>{foo|bar}</p> insert {baz}
  41. // <p>{foo[baz]bar}</p>
  42. const created = create( writer, {
  43. instanceOf: ContainerElement,
  44. name: 'p',
  45. children: [
  46. { instanceOf: Text, data: 'foobar', position: 3 }
  47. ]
  48. } );
  49. const newRange = writer.insert( created.position, new Text( 'baz' ) );
  50. test( writer, newRange, created.node, {
  51. instanceOf: ContainerElement,
  52. name: 'p',
  53. children: [
  54. { instanceOf: Text, data: 'foobazbar', rangeStart: 3, rangeEnd: 6 }
  55. ]
  56. } );
  57. } );
  58. it( 'should insert text into another text node #2', () => {
  59. // <p>{foobar|}</p> insert {baz}
  60. // <p>{foobar[baz}]</p>
  61. const created = create( writer, {
  62. instanceOf: ContainerElement,
  63. name: 'p',
  64. children: [
  65. { instanceOf: Text, data: 'foobar', position: 6 }
  66. ]
  67. } );
  68. const newRange = writer.insert( created.position, new Text( 'baz' ) );
  69. test( writer, newRange, created.node, {
  70. instanceOf: ContainerElement,
  71. name: 'p',
  72. rangeEnd: 1,
  73. children: [
  74. { instanceOf: Text, data: 'foobarbaz', rangeStart: 6 }
  75. ]
  76. } );
  77. } );
  78. it( 'should insert text into another text node #3', () => {
  79. // <p>{|foobar}</p> insert {baz}
  80. // <p>[{baz]foobar}</p>
  81. const created = create( writer, {
  82. instanceOf: ContainerElement,
  83. name: 'p',
  84. children: [
  85. { instanceOf: Text, data: 'foobar', position: 0 }
  86. ]
  87. } );
  88. const newRange = writer.insert( created.position, new Text( 'baz' ) );
  89. test( writer, newRange, created.node, {
  90. instanceOf: ContainerElement,
  91. name: 'p',
  92. rangeStart: 0,
  93. children: [
  94. { instanceOf: Text, data: 'bazfoobar', rangeEnd: 3 }
  95. ]
  96. } );
  97. } );
  98. it( 'should break attributes when inserting into text node', () => {
  99. // <p>{foo|bar}</p> insert <b>{baz}</b>
  100. // <p>{foo}[<b>baz</b>]{bar}</p>
  101. const created = create( writer, {
  102. instanceOf: ContainerElement,
  103. name: 'p',
  104. children: [
  105. { instanceOf: Text, data: 'foobar', position: 3 }
  106. ]
  107. } );
  108. const toInsert = create( writer, {
  109. instanceOf: ContainerElement,
  110. name: 'b',
  111. priority: 1,
  112. children: [
  113. { instanceOf: Text, data: 'baz' }
  114. ]
  115. } );
  116. const newRange = writer.insert( created.position, toInsert.node );
  117. test( writer, newRange, created.node, {
  118. instanceOf: ContainerElement,
  119. name: 'p',
  120. rangeStart: 1,
  121. rangeEnd: 2,
  122. children: [
  123. { instanceOf: Text, data: 'foo' },
  124. {
  125. instanceOf: ContainerElement,
  126. name: 'b',
  127. priority: 1,
  128. children: [
  129. { instanceOf: Text, data: 'baz' }
  130. ]
  131. },
  132. { instanceOf: Text, data: 'bar' }
  133. ]
  134. } );
  135. } );
  136. it( 'should merge text ndoes', () => {
  137. // <p>|{foobar}</p> insert {baz}
  138. // <p>[{baz]foobar}</p>
  139. const created = create( writer, {
  140. instanceOf: ContainerElement,
  141. name: 'p',
  142. position: 0,
  143. children: [
  144. { instanceOf: Text, data: 'foobar' }
  145. ]
  146. } );
  147. const newRange = writer.insert( created.position, new Text( 'baz' ) );
  148. test( writer, newRange, created.node, {
  149. instanceOf: ContainerElement,
  150. name: 'p',
  151. rangeStart: 0,
  152. children: [
  153. { instanceOf: Text, data: 'bazfoobar', rangeEnd: 3 }
  154. ]
  155. } );
  156. } );
  157. it( 'should merge same attribute nodes', () => {
  158. // <p><b>{foo|bar}</b></p> insert <b>{baz}</b>
  159. // <p><b>{foo[baz]bar}</b></p>
  160. const created = create( writer, {
  161. instanceOf: ContainerElement,
  162. name: 'p',
  163. children: [
  164. {
  165. instanceOf: AttributeElement,
  166. name: 'b',
  167. priority: 1,
  168. children: [
  169. { instanceOf: Text, data: 'foobar', position: 3 }
  170. ]
  171. }
  172. ]
  173. } );
  174. const toInsert = create( writer, {
  175. instanceOf: AttributeElement,
  176. name: 'b',
  177. priority: 1,
  178. children: [
  179. { instanceOf: Text, data: 'baz' }
  180. ]
  181. } );
  182. const newRange = writer.insert( created.position, toInsert.node );
  183. test( writer, newRange, created.node, {
  184. instanceOf: ContainerElement,
  185. name: 'p',
  186. children: [
  187. {
  188. instanceOf: AttributeElement,
  189. name: 'b',
  190. priority: 1,
  191. children: [
  192. { instanceOf: Text, data: 'foobazbar', rangeStart: 3, rangeEnd: 6 }
  193. ]
  194. }
  195. ]
  196. } );
  197. } );
  198. it( 'should not merge different attributes', () => {
  199. // <p><b>{foo|bar}</b></p> insert <b>{baz}</b> ( different priority )
  200. // <p><b>{foo}</b>[<b>{baz}</b>]<b>{bar}</b></p>
  201. const created = create( writer, {
  202. instanceOf: ContainerElement,
  203. name: 'p',
  204. children: [
  205. {
  206. instanceOf: AttributeElement,
  207. name: 'b',
  208. priority: 1,
  209. children: [
  210. { instanceOf: Text, data: 'foobar', position: 3 }
  211. ]
  212. }
  213. ]
  214. } );
  215. const toInsert = create( writer, {
  216. instanceOf: AttributeElement,
  217. name: 'b',
  218. priority: 2,
  219. children: [
  220. { instanceOf: Text, data: 'baz' }
  221. ]
  222. } );
  223. const newRange = writer.insert( created.position, toInsert.node );
  224. test( writer, newRange, created.node, {
  225. instanceOf: ContainerElement,
  226. name: 'p',
  227. rangeStart: 1,
  228. rangeEnd: 2,
  229. children: [
  230. {
  231. instanceOf: AttributeElement,
  232. name: 'b',
  233. priority: 1,
  234. children: [
  235. { instanceOf: Text, data: 'foo' }
  236. ]
  237. },
  238. {
  239. instanceOf: AttributeElement,
  240. name: 'b',
  241. priority: 2,
  242. children: [
  243. { instanceOf: Text, data: 'baz' }
  244. ]
  245. },
  246. {
  247. instanceOf: AttributeElement,
  248. name: 'b',
  249. priority: 1,
  250. children: [
  251. { instanceOf: Text, data: 'bar' }
  252. ]
  253. }
  254. ]
  255. } );
  256. } );
  257. it( 'should allow to insert multiple nodes', () => {
  258. // <p>|</p> insert <b>{foo}</b>{bar}
  259. // <p>[<b>{foo}</b>{bar}]</p>
  260. const root = new ContainerElement( 'p' );
  261. const toInsert = create( writer, {
  262. instanceOf: ContainerElement,
  263. name: 'fake',
  264. children: [
  265. {
  266. instanceOf: AttributeElement,
  267. name: 'b',
  268. priority: 1,
  269. children: [
  270. { instanceOf: Text, data: 'foo' }
  271. ]
  272. },
  273. { instanceOf: Text, data: 'bar' }
  274. ]
  275. } ).node.getChildren();
  276. const position = new Position( root, 0 );
  277. const newRange = writer.insert( position, toInsert );
  278. test( writer, newRange, root, {
  279. instanceOf: ContainerElement,
  280. name: 'p',
  281. rangeStart: 0,
  282. rangeEnd: 2,
  283. children: [
  284. {
  285. instanceOf: AttributeElement,
  286. name: 'b',
  287. priority: 1,
  288. children: [
  289. { instanceOf: Text, data: 'foo' }
  290. ]
  291. },
  292. { instanceOf: Text, data: 'bar' }
  293. ]
  294. } );
  295. } );
  296. it( 'should merge after inserting multiple nodes', () => {
  297. // <p><b>{qux}</b>|{baz}</p> insert <b>{foo}</b>{bar}
  298. // <p><b>{qux[foo}</b>{bar]baz}</p>
  299. const created = create( writer, {
  300. instanceOf: ContainerElement,
  301. name: 'p',
  302. position: 1,
  303. children: [
  304. {
  305. instanceOf: AttributeElement,
  306. name: 'b',
  307. priority: 1,
  308. children: [
  309. { instanceOf: Text, data: 'qux' }
  310. ]
  311. },
  312. { instanceOf: Text, data: 'baz' }
  313. ]
  314. } );
  315. const toInsert = create( writer, {
  316. instanceOf: ContainerElement,
  317. name: 'fake',
  318. children: [
  319. {
  320. instanceOf: AttributeElement,
  321. name: 'b',
  322. priority: 1,
  323. children: [
  324. { instanceOf: Text, data: 'foo' }
  325. ]
  326. },
  327. { instanceOf: Text, data: 'bar' }
  328. ]
  329. } ).node.getChildren();
  330. const newRange = writer.insert( created.position, toInsert );
  331. test( writer, newRange, created.node, {
  332. instanceOf: ContainerElement,
  333. name: 'p',
  334. children: [
  335. {
  336. instanceOf: AttributeElement,
  337. name: 'b',
  338. priority: 1,
  339. children: [
  340. { instanceOf: Text, data: 'quxfoo', rangeStart: 3 }
  341. ]
  342. },
  343. { instanceOf: Text, data: 'barbaz', rangeEnd: 3 }
  344. ]
  345. } );
  346. } );
  347. } );
  348. } );