viewconsumable.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treecontroller */
  6. 'use strict';
  7. import ViewElement from '/ckeditor5/engine/treeview/element.js';
  8. import ViewConsumable from '/ckeditor5/engine/treecontroller/viewconsumable.js';
  9. describe( 'ViewConsumable', () => {
  10. let viewConsumable;
  11. let el;
  12. beforeEach( () => {
  13. viewConsumable = new ViewConsumable();
  14. el = new ViewElement( 'p' );
  15. } );
  16. describe( 'add', () => {
  17. it( 'should allow to add element', () => {
  18. viewConsumable.add( el );
  19. expect( viewConsumable.test( el ) ).to.be.true;
  20. } );
  21. it( 'should allow to add element inside description object', () => {
  22. viewConsumable.add( { element: el } );
  23. expect( viewConsumable.test( el ) ).to.be.true;
  24. } );
  25. it( 'should allow to add attributes classes and styles', () => {
  26. viewConsumable.add( { element: el, attribute: 'href' } );
  27. viewConsumable.add( { element: el, class: 'foobar' } );
  28. viewConsumable.add( { element: el, style: 'color' } );
  29. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.true;
  30. expect( viewConsumable.test( { element: el, class: 'foobar' } ) ).to.be.true;
  31. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  32. expect( viewConsumable.test( el ) ).to.be.null;
  33. } );
  34. it( 'should allow to add attributes classes and styles in one call', () => {
  35. viewConsumable.add( { element: el, attribute: 'href', class: 'foobar', style: 'color' } );
  36. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.true;
  37. expect( viewConsumable.test( { element: el, class: 'foobar' } ) ).to.be.true;
  38. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  39. expect( viewConsumable.test( el ) ).to.be.null;
  40. } );
  41. it( 'should allow to add multiple attributes in one call', () => {
  42. viewConsumable.add( { element: el, attribute: [ 'href', 'target', 'title' ] } );
  43. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.true;
  44. expect( viewConsumable.test( { element: el, attribute: 'target' } ) ).to.be.true;
  45. expect( viewConsumable.test( { element: el, attribute: 'title' } ) ).to.be.true;
  46. expect( viewConsumable.test( el ) ).to.be.null;
  47. } );
  48. it( 'should allow to add multiple classes in one call', () => {
  49. viewConsumable.add( { element: el, class: [ 'foo', 'bar', 'baz' ] } );
  50. expect( viewConsumable.test( { element: el, class: 'foo' } ) ).to.be.true;
  51. expect( viewConsumable.test( { element: el, class: 'bar' } ) ).to.be.true;
  52. expect( viewConsumable.test( { element: el, class: 'baz' } ) ).to.be.true;
  53. expect( viewConsumable.test( el ) ).to.be.null;
  54. } );
  55. it( 'should allow to add multiple styles in one call', () => {
  56. viewConsumable.add( { element: el, style: [ 'color', 'position', 'top' ] } );
  57. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  58. expect( viewConsumable.test( { element: el, style: 'position' } ) ).to.be.true;
  59. expect( viewConsumable.test( { element: el, style: 'top' } ) ).to.be.true;
  60. expect( viewConsumable.test( el ) ).to.be.null;
  61. } );
  62. it( 'should allow to add multiple consumables in one call', () => {
  63. viewConsumable.add( el, { element: el, style: 'color' } );
  64. expect( viewConsumable.test( el ) ).to.be.true;
  65. expect( viewConsumable.test( { element: el, style: 'color' } ) );
  66. } );
  67. it( 'should throw an error when element is not provided', () => {
  68. expect( () => {
  69. viewConsumable.add( { style: 'color' } );
  70. } ).to.throw( 'viewconsumable-element-missing' );
  71. } );
  72. it( 'should throw if class attribute is added', () => {
  73. expect( () => {
  74. viewConsumable.add( { element: el, attribute: 'class' } );
  75. } ).to.throw( 'viewconsumable-invalid-attribute' );
  76. } );
  77. it( 'should throw if style attribute is added', () => {
  78. expect( () => {
  79. viewConsumable.add( { element: el, attribute: 'style' } );
  80. } ).to.throw( 'viewconsumable-invalid-attribute' );
  81. } );
  82. } );
  83. describe( 'test', () => {
  84. it( 'should test added element', () => {
  85. const el2 = new ViewElement( 'p' );
  86. viewConsumable.add( el );
  87. expect( viewConsumable.test( el ) ).to.be.true;
  88. expect( viewConsumable.test( { element: el } ) ).to.be.true;
  89. expect( viewConsumable.test( el2 ) ).to.be.null;
  90. expect( viewConsumable.test( { element: el2 } ) ).to.be.null;
  91. } );
  92. it( 'should test attributes, classes and styles', () => {
  93. const el = new ViewElement( 'p' );
  94. viewConsumable.add( { element: el, attribute: 'href', class: 'foobar', style: 'color' } );
  95. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.true;
  96. expect( viewConsumable.test( { element: el, class: 'foobar' } ) ).to.be.true;
  97. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  98. expect( viewConsumable.test( { element: el, attribute: 'href', class: 'foobar', style: 'color' } ) ).to.be.true;
  99. expect( viewConsumable.test( el ) ).to.be.null;
  100. } );
  101. it( 'should allow to test multiple attributes in one call', () => {
  102. viewConsumable.add( { element: el, attribute: [ 'href', 'title', 'target' ] } );
  103. expect( viewConsumable.test( { element: el, attribute: [ 'href', 'title', 'target' ] } ) ).to.be.true;
  104. } );
  105. it( 'should allow to test multiple classes in one call', () => {
  106. viewConsumable.add( { element: el, class: [ 'foo', 'bar', 'baz' ] } );
  107. expect( viewConsumable.test( { element: el, class: [ 'foo', 'bar', 'baz' ] } ) ).to.be.true;
  108. } );
  109. it( 'should allow to test multiple styles in one call', () => {
  110. viewConsumable.add( { element: el, style: [ 'color', 'position', 'top' ] } );
  111. expect( viewConsumable.test( { element: el, style: [ 'color', 'position', 'top' ] } ) ).to.be.true;
  112. } );
  113. it( 'should allow to test with multiple parameters', () => {
  114. viewConsumable.add( el, { element: el, class: 'foobar' }, { element: el, 'style': 'red' } );
  115. expect( viewConsumable.test( el, { element: el, style: 'red' }, { element: el, class: 'foobar' } ) ).to.be.true;
  116. } );
  117. it( 'should return null if not consumable', () => {
  118. expect( viewConsumable.test( el ) ).to.be.null;
  119. } );
  120. it( 'should return false if already consumed', () => {
  121. viewConsumable.add( el );
  122. viewConsumable.consume( el );
  123. expect( viewConsumable.test( el ) ).to.be.false;
  124. } );
  125. it( 'should return null if first non-consumable item is found', () => {
  126. viewConsumable.add( { element: el, attribute: 'foo' } );
  127. expect( viewConsumable.test( { element: el, attribute: [ 'foo', 'bar' ] } ) ).to.be.null;
  128. expect( viewConsumable.test( { element: el, attribute: 'foo' }, el ) ).to.be.null;
  129. } );
  130. it( 'should return false if first already consumed item is found', () => {
  131. viewConsumable.add( { element: el, attribute: [ 'foo', 'bar' ] }, el );
  132. viewConsumable.consume( { element: el, attribute: 'bar' } );
  133. viewConsumable.consume( el );
  134. expect( viewConsumable.test( { element: el, attribute: [ 'foo', 'bar' ] } ) ).to.be.false;
  135. expect( viewConsumable.test( el ) ).to.be.false;
  136. } );
  137. it( 'should throw an error when element is not provided', () => {
  138. expect( () => {
  139. viewConsumable.test( { style: 'color' } );
  140. } ).to.throw( 'viewconsumable-element-missing' );
  141. } );
  142. it( 'should throw if class attribute is tested', () => {
  143. viewConsumable.add( { element: el, class: 'foobar' } );
  144. expect( () => {
  145. viewConsumable.test( { element: el, attribute: 'class' } );
  146. } ).to.throw( 'viewconsumable-invalid-attribute' );
  147. } );
  148. it( 'should throw if style attribute is tested', () => {
  149. viewConsumable.add( { element: el, style: 'color' } );
  150. expect( () => {
  151. viewConsumable.test( { element: el, attribute: 'style' } );
  152. } ).to.throw( 'viewconsumable-invalid-attribute' );
  153. } );
  154. } );
  155. describe( 'consume', () => {
  156. it( 'should consume element', () => {
  157. viewConsumable.add( el );
  158. const consumed = viewConsumable.consume( el );
  159. expect( viewConsumable.test( el ) ).to.be.false;
  160. expect( consumed ).to.be.true;
  161. } );
  162. it( 'should not consume element not marked for consumption', () => {
  163. expect( viewConsumable.consume( el ) ).to.be.false;
  164. } );
  165. it( 'should not consume element already consumed', () => {
  166. viewConsumable.add( el );
  167. expect( viewConsumable.consume( el ) ).to.be.true;
  168. expect( viewConsumable.consume( el ) ).to.be.false;
  169. } );
  170. it( 'should consume attributes, classes and styles', () => {
  171. viewConsumable.add( { element: el, class: 'foobar', attribute: 'href', style: 'color' } );
  172. const consumed1 = viewConsumable.consume( { element: el, class: 'foobar' } );
  173. const consumed2 = viewConsumable.consume( { element: el, attribute: 'href' } );
  174. const consumed3 = viewConsumable.consume( { element: el, style: 'color' } );
  175. expect( consumed1 ).to.be.true;
  176. expect( consumed2 ).to.be.true;
  177. expect( consumed3 ).to.be.true;
  178. expect( viewConsumable.test( { element: el, class: 'foobar' } ) ).to.be.false;
  179. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.false;
  180. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.false;
  181. } );
  182. it( 'should consume multiple attributes', () => {
  183. viewConsumable.add( { element: el, attribute: [ 'href', 'title', 'name' ] } );
  184. const consumed = viewConsumable.consume( { element: el, attribute: [ 'href', 'title' ] } );
  185. expect( consumed ).to.be.true;
  186. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.false;
  187. expect( viewConsumable.test( { element: el, attribute: 'title' } ) ).to.be.false;
  188. expect( viewConsumable.test( { element: el, attribute: 'name' } ) ).to.be.true;
  189. } );
  190. it( 'should consume multiple styles', () => {
  191. viewConsumable.add( { element: el, style: [ 'color', 'top', 'position' ] } );
  192. const consumed = viewConsumable.consume( { element: el, style: [ 'color', 'position' ] } );
  193. expect( consumed ).to.be.true;
  194. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.false;
  195. expect( viewConsumable.test( { element: el, style: 'position' } ) ).to.be.false;
  196. expect( viewConsumable.test( { element: el, style: 'top' } ) ).to.be.true;
  197. } );
  198. it( 'should consume multiple classes', () => {
  199. viewConsumable.add( { element: el, class: [ 'foo', 'bar', 'baz' ] } );
  200. const consumed = viewConsumable.consume( { element: el, class: [ 'bar', 'baz' ] } );
  201. expect( consumed ).to.be.true;
  202. expect( viewConsumable.test( { element: el, class: 'bar' } ) ).to.be.false;
  203. expect( viewConsumable.test( { element: el, class: 'baz' } ) ).to.be.false;
  204. expect( viewConsumable.test( { element: el, class: 'foo' } ) ).to.be.true;
  205. } );
  206. it( 'should consume multiple items', () => {
  207. viewConsumable.add( {
  208. element: el,
  209. attribute: [ 'name', 'href', 'title' ],
  210. class: [ 'foo', 'bar', 'baz' ],
  211. style: [ 'color', 'position' ]
  212. } );
  213. const consumed = viewConsumable.consume(
  214. { element: el, attribute: 'name', class: 'foo' } ,
  215. { element: el, attribute: 'href', class: [ 'bar', 'baz' ] } ,
  216. { element: el, attribute: 'title', style: 'color' }
  217. );
  218. expect( consumed ).to.be.true;
  219. expect( viewConsumable.test( { element: el, attribute: 'name' } ) ).to.be.false;
  220. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.false;
  221. expect( viewConsumable.test( { element: el, attribute: 'title' } ) ).to.be.false;
  222. expect( viewConsumable.test( { element: el, class: 'foo' } ) ).to.be.false;
  223. expect( viewConsumable.test( { element: el, class: 'bar' } ) ).to.be.false;
  224. expect( viewConsumable.test( { element: el, class: 'baz' } ) ).to.be.false;
  225. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.false;
  226. expect( viewConsumable.test( { element: el, style: 'position' } ) ).to.be.true;
  227. } );
  228. it( 'should consume element provided as only item in object', () => {
  229. viewConsumable.add( el );
  230. const consumed = viewConsumable.consume( { element: el } );
  231. expect( viewConsumable.test( el ) ).to.be.false;
  232. expect( consumed ).to.be.true;
  233. } );
  234. it( 'should consume only if all items can be consumed', () => {
  235. viewConsumable.add( { element: el, style: [ 'position', 'color' ], attribute: [ 'href', 'title' ] } );
  236. let consumed = viewConsumable.consume( el, { element: el, style: 'color' } );
  237. expect( consumed ).to.be.false;
  238. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  239. consumed = viewConsumable.consume( { element: el, style: [ 'color', 'top' ] } );
  240. expect( consumed ).to.be.false;
  241. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  242. } );
  243. it( 'should throw an error when element is not provided', () => {
  244. expect( () => {
  245. viewConsumable.consume( { style: 'color' } );
  246. } ).to.throw( 'viewconsumable-element-missing' );
  247. } );
  248. it( 'should throw if class attribute is provided', () => {
  249. viewConsumable.add( { element: el, class: 'foobar' } );
  250. expect( () => {
  251. viewConsumable.consume( { element: el, attribute: 'class' } );
  252. } ).to.throw( 'viewconsumable-invalid-attribute' );
  253. } );
  254. it( 'should throw if style attribute is provided', () => {
  255. viewConsumable.add( { element: el, style: 'color' } );
  256. expect( () => {
  257. viewConsumable.consume( { element: el, attribute: 'style' } );
  258. } ).to.throw( 'viewconsumable-invalid-attribute' );
  259. } );
  260. } );
  261. describe( 'revert', () => {
  262. it( 'should revert single element', () => {
  263. viewConsumable.add( el );
  264. viewConsumable.consume( el );
  265. expect( viewConsumable.test( el ) ).to.be.false;
  266. viewConsumable.revert( el );
  267. expect( viewConsumable.test( el ) ).to.be.true;
  268. } );
  269. it( 'should not revert element that was never added', () => {
  270. viewConsumable.revert( el );
  271. expect( viewConsumable.test( el ) ).to.be.null;
  272. } );
  273. it( 'should do nothing on not consumed element', () => {
  274. viewConsumable.add( el );
  275. viewConsumable.revert( el );
  276. expect( viewConsumable.test( el ) ).to.be.true;
  277. } );
  278. it( 'should revert classes, attributes and styles', () => {
  279. viewConsumable.add( { element: el, class: 'foobar', style: 'color', attribute: 'name' } );
  280. viewConsumable.consume( { element: el, class: 'foobar', style: 'color', attribute: 'name' } );
  281. viewConsumable.revert( { element: el, class: 'foobar' } );
  282. viewConsumable.revert( { element: el, style: 'color' } );
  283. viewConsumable.revert( { element: el, attribute: 'name' } );
  284. expect( viewConsumable.test( { element: el, class: 'foobar', style: 'color', attribute: 'name' } ) ).to.be.true;
  285. } );
  286. it( 'should revert multiple classes, attributes and styles in one call #1', () => {
  287. viewConsumable.add( {
  288. element: el,
  289. class: 'foobar',
  290. style: 'color',
  291. attribute: 'name'
  292. } );
  293. viewConsumable.consume( { element: el, class: 'foobar', style: 'color', attribute: 'name' } );
  294. viewConsumable.revert( { element: el, class: 'foobar', style: 'color', attribute: 'name' } );
  295. expect( viewConsumable.test( { element: el, class: 'foobar', style: 'color', attribute: 'name' } ) ).to.be.true;
  296. } );
  297. it( 'should revert multiple classes, attributes and styles in one call #2', () => {
  298. const description = {
  299. element: el,
  300. class: [ 'foobar', 'baz' ],
  301. style: [ 'color', 'position' ],
  302. attribute: [ 'name', 'href' ]
  303. };
  304. viewConsumable.add( description );
  305. viewConsumable.consume( description );
  306. viewConsumable.revert( description );
  307. expect( viewConsumable.test( description ) ).to.be.true;
  308. } );
  309. it( 'should not revert non consumable items', () => {
  310. viewConsumable.add( { element: el, class: 'foobar' } );
  311. viewConsumable.consume( { element: el, class: 'foobar' } );
  312. viewConsumable.revert( { element: el, class: 'foobar', attribute: 'name' } );
  313. expect( viewConsumable.test( { element: el, class: 'foobar' } ) ).to.be.true;
  314. expect( viewConsumable.test( { element: el, attribute: 'name' } ) ).to.be.null;
  315. } );
  316. it( 'should allow to use additional parameters in one call', () => {
  317. const el2 = new ViewElement( 'p' );
  318. viewConsumable.add( el, el2 );
  319. expect( viewConsumable.consume( el, el2 ) ).to.be.true;
  320. viewConsumable.revert( el, el2 );
  321. expect( viewConsumable.test( el, el2 ) ).to.be.true;
  322. } );
  323. it( 'should throw an error when element is not provided', () => {
  324. expect( () => {
  325. viewConsumable.revert( { style: 'color' } );
  326. } ).to.throw( 'viewconsumable-element-missing' );
  327. } );
  328. it( 'should throw if class attribute is provided', () => {
  329. viewConsumable.add( { element: el, class: 'foobar' } );
  330. viewConsumable.consume( { element: el, class: 'foobar' } );
  331. expect( () => {
  332. viewConsumable.revert( { element: el, attribute: 'class' } );
  333. } ).to.throw( 'viewconsumable-invalid-attribute' );
  334. } );
  335. it( 'should throw if style attribute is provided', () => {
  336. viewConsumable.add( { element: el, style: 'color' } );
  337. viewConsumable.consume( { element: el, style: 'color' } );
  338. expect( () => {
  339. viewConsumable.revert( { element: el, attribute: 'style' } );
  340. } ).to.throw( 'viewconsumable-invalid-attribute' );
  341. } );
  342. } );
  343. } );