breakattributes.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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( 'breakAttributes', () => {
  20. // <p>{|foobar}</p> -> <p>|{foobar}</p>
  21. it( '<p>{|foobar}</p>', () => {
  22. const created = create( writer, {
  23. instanceOf: ContainerElement,
  24. name: 'p',
  25. children: [
  26. { instanceOf: Text, data: 'foobar', position: 0 }
  27. ]
  28. } );
  29. const newPosition = writer.breakAttributes( created.position );
  30. test( writer, newPosition, created.node, {
  31. instanceOf: ContainerElement,
  32. name: 'p',
  33. position: 0,
  34. children: [
  35. { instanceOf: Text, data: 'foobar' }
  36. ]
  37. } );
  38. } );
  39. it( '<p>foo|bar</p>', () => {
  40. // <p>{foo|bar}</p> -> <p>{foo}|{bar}</p>
  41. const created = create( writer, {
  42. instanceOf: ContainerElement,
  43. name: 'p',
  44. children: [
  45. { instanceOf: Text, data: 'foobar', position: 3 }
  46. ]
  47. } );
  48. const newPosition = writer.breakAttributes( created.position );
  49. test( writer, newPosition, created.node, {
  50. instanceOf: ContainerElement,
  51. name: 'p',
  52. position: 1,
  53. children: [
  54. { instanceOf: Text, data: 'foo' },
  55. { instanceOf: Text, data: 'bar' }
  56. ]
  57. } );
  58. } );
  59. it( '<p>{foobar|}</p>', () => {
  60. // <p>{foobar|}</p> -> <p>{foobar}|</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 newPosition = writer.breakAttributes( created.position );
  69. test( writer, newPosition, created.node, {
  70. instanceOf: ContainerElement,
  71. name: 'p',
  72. position: 1,
  73. children: [
  74. { instanceOf: Text, data: 'foobar' }
  75. ]
  76. } );
  77. } );
  78. it( '<p><b>{foo|bar}</b></p>', () => {
  79. // <p><b>{foo|bar}</b></p> -> <p><b>{foo}</b>|<b>{bar}</b></p>
  80. const created = create( writer, {
  81. instanceOf: ContainerElement,
  82. name: 'p',
  83. children: [
  84. {
  85. instanceOf: AttributeElement,
  86. name: 'b',
  87. priority: 1,
  88. children: [
  89. { instanceOf: Text, data: 'foobar', position: 3 }
  90. ]
  91. }
  92. ]
  93. } );
  94. const newPosition = writer.breakAttributes( created.position );
  95. test( writer, newPosition, created.node, {
  96. instanceOf: ContainerElement,
  97. name: 'p',
  98. position: 1,
  99. children: [
  100. {
  101. instanceOf: AttributeElement,
  102. name: 'b',
  103. priority: 1,
  104. children: [
  105. { instanceOf: Text, data: 'foo' }
  106. ]
  107. },
  108. {
  109. instanceOf: AttributeElement,
  110. name: 'b',
  111. priority: 1,
  112. children: [
  113. { instanceOf: Text, data: 'bar' }
  114. ]
  115. }
  116. ]
  117. } );
  118. } );
  119. it( '<p><b><u>{|foobar}</u></b></p>', () => {
  120. // <p><b><u>{|foobar}</u></b></p> -> <p>|<b><u>{foobar}</u></b></p>
  121. const created = create( writer, {
  122. instanceOf: ContainerElement,
  123. name: 'p',
  124. children: [
  125. {
  126. instanceOf: AttributeElement,
  127. name: 'b',
  128. priority: 1,
  129. children: [
  130. {
  131. instanceOf: AttributeElement,
  132. name: 'u',
  133. priority: 1,
  134. children: [
  135. { instanceOf: Text, data: 'foobar', position: 0 }
  136. ]
  137. }
  138. ]
  139. }
  140. ]
  141. } );
  142. const newPosition = writer.breakAttributes( created.position );
  143. test( writer, newPosition, created.node, {
  144. instanceOf: ContainerElement,
  145. name: 'p',
  146. position: 0,
  147. children: [
  148. {
  149. instanceOf: AttributeElement,
  150. name: 'b',
  151. priority: 1,
  152. children: [
  153. {
  154. instanceOf: AttributeElement,
  155. name: 'u',
  156. priority: 1,
  157. children: [
  158. { instanceOf: Text, data: 'foobar' }
  159. ]
  160. }
  161. ]
  162. }
  163. ]
  164. } );
  165. } );
  166. // <p><b><u>{foo|ba}r</u></b></p> -> <p><b><u>{foo}</u></b>|<b></u>{bar}</u></b></p>
  167. it( '<p><b><u>{foo|bar}</u></b></p>', () => {
  168. const created = create( writer, {
  169. instanceOf: ContainerElement,
  170. name: 'p',
  171. children: [
  172. {
  173. instanceOf: AttributeElement,
  174. name: 'b',
  175. priority: 1,
  176. children: [
  177. {
  178. instanceOf: AttributeElement,
  179. name: 'u',
  180. priority: 1,
  181. children: [
  182. { instanceOf: Text, data: 'foobar', position: 3 }
  183. ]
  184. }
  185. ]
  186. }
  187. ]
  188. } );
  189. const newPosition = writer.breakAttributes( created.position );
  190. test( writer, newPosition, created.node, {
  191. instanceOf: ContainerElement,
  192. name: 'p',
  193. position: 1,
  194. children: [
  195. {
  196. instanceOf: AttributeElement,
  197. name: 'b',
  198. priority: 1,
  199. children: [
  200. {
  201. instanceOf: AttributeElement,
  202. name: 'u',
  203. priority: 1,
  204. children: [
  205. { instanceOf: Text, data: 'foo' }
  206. ]
  207. }
  208. ]
  209. },
  210. {
  211. instanceOf: AttributeElement,
  212. name: 'b',
  213. priority: 1,
  214. children: [
  215. {
  216. instanceOf: AttributeElement,
  217. name: 'u',
  218. priority: 1,
  219. children: [
  220. { instanceOf: Text, data: 'bar' }
  221. ]
  222. }
  223. ]
  224. }
  225. ]
  226. } );
  227. } );
  228. it( '<p><b><u>{foobar|}</u></b></p>', () => {
  229. // <p><b><u>{foobar|}</u></b></p> -> <p><b><u>{foobar}</u></b>|</p>
  230. const created = create( writer, {
  231. instanceOf: ContainerElement,
  232. name: 'p',
  233. children: [
  234. {
  235. instanceOf: AttributeElement,
  236. name: 'b',
  237. priority: 1,
  238. children: [
  239. {
  240. instanceOf: AttributeElement,
  241. name: 'u',
  242. priority: 1,
  243. children: [
  244. { instanceOf: Text, data: 'foobar', position: 6 }
  245. ]
  246. }
  247. ]
  248. }
  249. ]
  250. } );
  251. const newPosition = writer.breakAttributes( created.position );
  252. test( writer, newPosition, created.node, {
  253. instanceOf: ContainerElement,
  254. name: 'p',
  255. position: 1,
  256. children: [
  257. {
  258. instanceOf: AttributeElement,
  259. name: 'b',
  260. priority: 1,
  261. children: [
  262. {
  263. instanceOf: AttributeElement,
  264. name: 'u',
  265. priority: 1,
  266. children: [
  267. { instanceOf: Text, data: 'foobar' }
  268. ]
  269. }
  270. ]
  271. }
  272. ]
  273. } );
  274. } );
  275. } );
  276. } );