viewconsumable.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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( { element: el, attribute: 'href', class: 'baz' } ) ).to.be.null;
  100. expect( viewConsumable.test( el ) ).to.be.null;
  101. viewConsumable.consume( { element: el, style: 'color' } );
  102. expect( viewConsumable.test( { element: el, attribute: 'href', style: 'color' } ) ).to.be.false;
  103. } );
  104. it( 'should allow to test multiple attributes in one call', () => {
  105. viewConsumable.add( { element: el, attribute: [ 'href', 'title', 'target' ] } );
  106. expect( viewConsumable.test( { element: el, attribute: [ 'href', 'title', 'target' ] } ) ).to.be.true;
  107. expect( viewConsumable.test( { element: el, attribute: [ 'href', 'title', 'alt' ] } ) ).to.be.null;
  108. viewConsumable.consume( { element: el, attribute: 'target' } );
  109. expect( viewConsumable.test( { element: el, attribute: [ 'href', 'title', 'target' ] } ) ).to.be.false;
  110. } );
  111. it( 'should allow to test multiple classes in one call', () => {
  112. viewConsumable.add( { element: el, class: [ 'foo', 'bar', 'baz' ] } );
  113. expect( viewConsumable.test( { element: el, class: [ 'foo', 'bar', 'baz' ] } ) ).to.be.true;
  114. expect( viewConsumable.test( { element: el, class: [ 'foo', 'bar', 'qux' ] } ) ).to.be.null;
  115. viewConsumable.consume( { element: el, class: 'bar' } );
  116. expect( viewConsumable.test( { element: el, class: [ 'foo', 'bar', 'baz' ] } ) ).to.be.false;
  117. } );
  118. it( 'should allow to test multiple styles in one call', () => {
  119. viewConsumable.add( { element: el, style: [ 'color', 'position', 'top' ] } );
  120. expect( viewConsumable.test( { element: el, style: [ 'color', 'position', 'top' ] } ) ).to.be.true;
  121. expect( viewConsumable.test( { element: el, style: [ 'color', 'position', 'left' ] } ) ).to.be.null;
  122. viewConsumable.consume( { element: el, style: 'top' } );
  123. expect( viewConsumable.test( { element: el, style: [ 'color', 'position', 'top' ] } ) ).to.be.false;
  124. } );
  125. it( 'should allow to test with multiple parameters', () => {
  126. viewConsumable.add( el, { element: el, class: 'foobar' }, { element: el, 'style': 'red' } );
  127. expect( viewConsumable.test( el, { element: el, style: 'red' }, { element: el, class: 'foobar' } ) ).to.be.true;
  128. expect( viewConsumable.test( el, { element: el, style: 'red' }, { element: el, class: 'baz' } ) ).to.be.null;
  129. const el2 = new ViewElement( 'p' );
  130. viewConsumable.add( { element: el2, attribute: 'title' } );
  131. expect( viewConsumable.test( el, { element: el2, attribute: 'title' } ) ).to.be.true;
  132. viewConsumable.consume( { element: el2, attribute: 'title' } );
  133. expect( viewConsumable.test( el, { element: el2, attribute: 'title' } ) ).to.be.false;
  134. } );
  135. it( 'should return null if not consumable', () => {
  136. expect( viewConsumable.test( el ) ).to.be.null;
  137. } );
  138. it( 'should return false if already consumed', () => {
  139. viewConsumable.add( el );
  140. viewConsumable.consume( el );
  141. expect( viewConsumable.test( el ) ).to.be.false;
  142. } );
  143. it( 'should return null if first non-consumable item is found', () => {
  144. viewConsumable.add( { element: el, attribute: 'foo' } );
  145. expect( viewConsumable.test( { element: el, attribute: [ 'foo', 'bar' ] } ) ).to.be.null;
  146. expect( viewConsumable.test( { element: el, attribute: 'foo' }, el ) ).to.be.null;
  147. } );
  148. it( 'should return false if first already consumed item is found', () => {
  149. viewConsumable.add( { element: el, attribute: [ 'foo', 'bar' ] }, el );
  150. viewConsumable.consume( { element: el, attribute: 'bar' } );
  151. viewConsumable.consume( el );
  152. expect( viewConsumable.test( { element: el, attribute: [ 'foo', 'bar' ] } ) ).to.be.false;
  153. expect( viewConsumable.test( el ) ).to.be.false;
  154. } );
  155. it( 'should throw an error when element is not provided', () => {
  156. expect( () => {
  157. viewConsumable.test( { style: 'color' } );
  158. } ).to.throw( 'viewconsumable-element-missing' );
  159. } );
  160. it( 'should throw if class attribute is tested', () => {
  161. viewConsumable.add( { element: el, class: 'foobar' } );
  162. expect( () => {
  163. viewConsumable.test( { element: el, attribute: 'class' } );
  164. } ).to.throw( 'viewconsumable-invalid-attribute' );
  165. } );
  166. it( 'should throw if style attribute is tested', () => {
  167. viewConsumable.add( { element: el, style: 'color' } );
  168. expect( () => {
  169. viewConsumable.test( { element: el, attribute: 'style' } );
  170. } ).to.throw( 'viewconsumable-invalid-attribute' );
  171. } );
  172. } );
  173. describe( 'consume', () => {
  174. it( 'should consume element', () => {
  175. viewConsumable.add( el );
  176. const consumed = viewConsumable.consume( el );
  177. expect( viewConsumable.test( el ) ).to.be.false;
  178. expect( consumed ).to.be.true;
  179. } );
  180. it( 'should not consume element not marked for consumption', () => {
  181. expect( viewConsumable.consume( el ) ).to.be.false;
  182. } );
  183. it( 'should not consume element already consumed', () => {
  184. viewConsumable.add( el );
  185. expect( viewConsumable.consume( el ) ).to.be.true;
  186. expect( viewConsumable.consume( el ) ).to.be.false;
  187. } );
  188. it( 'should consume attributes, classes and styles', () => {
  189. viewConsumable.add( { element: el, class: 'foobar', attribute: 'href', style: 'color' } );
  190. const consumed1 = viewConsumable.consume( { element: el, class: 'foobar' } );
  191. const consumed2 = viewConsumable.consume( { element: el, attribute: 'href' } );
  192. const consumed3 = viewConsumable.consume( { element: el, style: 'color' } );
  193. expect( consumed1 ).to.be.true;
  194. expect( consumed2 ).to.be.true;
  195. expect( consumed3 ).to.be.true;
  196. expect( viewConsumable.test( { element: el, class: 'foobar' } ) ).to.be.false;
  197. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.false;
  198. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.false;
  199. } );
  200. it( 'should consume multiple attributes', () => {
  201. viewConsumable.add( { element: el, attribute: [ 'href', 'title', 'name' ] } );
  202. const consumed = viewConsumable.consume( { element: el, attribute: [ 'href', 'title' ] } );
  203. expect( consumed ).to.be.true;
  204. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.false;
  205. expect( viewConsumable.test( { element: el, attribute: 'title' } ) ).to.be.false;
  206. expect( viewConsumable.test( { element: el, attribute: 'name' } ) ).to.be.true;
  207. } );
  208. it( 'should consume multiple styles', () => {
  209. viewConsumable.add( { element: el, style: [ 'color', 'top', 'position' ] } );
  210. const consumed = viewConsumable.consume( { element: el, style: [ 'color', 'position' ] } );
  211. expect( consumed ).to.be.true;
  212. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.false;
  213. expect( viewConsumable.test( { element: el, style: 'position' } ) ).to.be.false;
  214. expect( viewConsumable.test( { element: el, style: 'top' } ) ).to.be.true;
  215. } );
  216. it( 'should consume multiple classes', () => {
  217. viewConsumable.add( { element: el, class: [ 'foo', 'bar', 'baz' ] } );
  218. const consumed = viewConsumable.consume( { element: el, class: [ 'bar', 'baz' ] } );
  219. expect( consumed ).to.be.true;
  220. expect( viewConsumable.test( { element: el, class: 'bar' } ) ).to.be.false;
  221. expect( viewConsumable.test( { element: el, class: 'baz' } ) ).to.be.false;
  222. expect( viewConsumable.test( { element: el, class: 'foo' } ) ).to.be.true;
  223. } );
  224. it( 'should consume multiple items', () => {
  225. viewConsumable.add( {
  226. element: el,
  227. attribute: [ 'name', 'href', 'title' ],
  228. class: [ 'foo', 'bar', 'baz' ],
  229. style: [ 'color', 'position' ]
  230. } );
  231. const consumed = viewConsumable.consume(
  232. { element: el, attribute: 'name', class: 'foo' } ,
  233. { element: el, attribute: 'href', class: [ 'bar', 'baz' ] } ,
  234. { element: el, attribute: 'title', style: 'color' }
  235. );
  236. expect( consumed ).to.be.true;
  237. expect( viewConsumable.test( { element: el, attribute: 'name' } ) ).to.be.false;
  238. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.false;
  239. expect( viewConsumable.test( { element: el, attribute: 'title' } ) ).to.be.false;
  240. expect( viewConsumable.test( { element: el, class: 'foo' } ) ).to.be.false;
  241. expect( viewConsumable.test( { element: el, class: 'bar' } ) ).to.be.false;
  242. expect( viewConsumable.test( { element: el, class: 'baz' } ) ).to.be.false;
  243. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.false;
  244. expect( viewConsumable.test( { element: el, style: 'position' } ) ).to.be.true;
  245. } );
  246. it( 'should consume element provided as only item in object', () => {
  247. viewConsumable.add( el );
  248. const consumed = viewConsumable.consume( { element: el } );
  249. expect( viewConsumable.test( el ) ).to.be.false;
  250. expect( consumed ).to.be.true;
  251. } );
  252. it( 'should consume only if all items can be consumed', () => {
  253. viewConsumable.add( { element: el, style: [ 'position', 'color' ], attribute: [ 'href', 'title' ] } );
  254. let consumed = viewConsumable.consume( el, { element: el, style: 'color' } );
  255. expect( consumed ).to.be.false;
  256. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  257. consumed = viewConsumable.consume( { element: el, style: [ 'color', 'top' ] } );
  258. expect( consumed ).to.be.false;
  259. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  260. } );
  261. it( 'should throw an error when element is not provided', () => {
  262. expect( () => {
  263. viewConsumable.consume( { style: 'color' } );
  264. } ).to.throw( 'viewconsumable-element-missing' );
  265. } );
  266. it( 'should throw if class attribute is provided', () => {
  267. viewConsumable.add( { element: el, class: 'foobar' } );
  268. expect( () => {
  269. viewConsumable.consume( { element: el, attribute: 'class' } );
  270. } ).to.throw( 'viewconsumable-invalid-attribute' );
  271. } );
  272. it( 'should throw if style attribute is provided', () => {
  273. viewConsumable.add( { element: el, style: 'color' } );
  274. expect( () => {
  275. viewConsumable.consume( { element: el, attribute: 'style' } );
  276. } ).to.throw( 'viewconsumable-invalid-attribute' );
  277. } );
  278. } );
  279. describe( 'revert', () => {
  280. it( 'should revert single element', () => {
  281. viewConsumable.add( el );
  282. viewConsumable.consume( el );
  283. expect( viewConsumable.test( el ) ).to.be.false;
  284. viewConsumable.revert( el );
  285. expect( viewConsumable.test( el ) ).to.be.true;
  286. } );
  287. it( 'should not revert element that was never added', () => {
  288. viewConsumable.revert( el );
  289. expect( viewConsumable.test( el ) ).to.be.null;
  290. } );
  291. it( 'should do nothing on not consumed element', () => {
  292. viewConsumable.add( el );
  293. viewConsumable.revert( el );
  294. expect( viewConsumable.test( el ) ).to.be.true;
  295. } );
  296. it( 'should revert classes, attributes and styles', () => {
  297. viewConsumable.add( { element: el, class: 'foobar', style: 'color', attribute: 'name' } );
  298. viewConsumable.consume( { element: el, class: 'foobar', style: 'color', attribute: 'name' } );
  299. viewConsumable.revert( { element: el, class: 'foobar' } );
  300. viewConsumable.revert( { element: el, style: 'color' } );
  301. viewConsumable.revert( { element: el, attribute: 'name' } );
  302. expect( viewConsumable.test( { element: el, class: 'foobar', style: 'color', attribute: 'name' } ) ).to.be.true;
  303. } );
  304. it( 'should revert multiple classes, attributes and styles in one call #1', () => {
  305. viewConsumable.add( {
  306. element: el,
  307. class: 'foobar',
  308. style: 'color',
  309. attribute: 'name'
  310. } );
  311. viewConsumable.consume( { element: el, class: 'foobar', style: 'color', attribute: 'name' } );
  312. viewConsumable.revert( { element: el, class: 'foobar', style: 'color', attribute: 'name' } );
  313. expect( viewConsumable.test( { element: el, class: 'foobar', style: 'color', attribute: 'name' } ) ).to.be.true;
  314. } );
  315. it( 'should revert multiple classes, attributes and styles in one call #2', () => {
  316. const description = {
  317. element: el,
  318. class: [ 'foobar', 'baz' ],
  319. style: [ 'color', 'position' ],
  320. attribute: [ 'name', 'href' ]
  321. };
  322. viewConsumable.add( description );
  323. viewConsumable.consume( description );
  324. viewConsumable.revert( description );
  325. expect( viewConsumable.test( description ) ).to.be.true;
  326. } );
  327. it( 'should not revert non consumable items', () => {
  328. viewConsumable.add( { element: el, class: 'foobar' } );
  329. viewConsumable.consume( { element: el, class: 'foobar' } );
  330. viewConsumable.revert( { element: el, class: 'foobar', attribute: 'name' } );
  331. expect( viewConsumable.test( { element: el, class: 'foobar' } ) ).to.be.true;
  332. expect( viewConsumable.test( { element: el, attribute: 'name' } ) ).to.be.null;
  333. } );
  334. it( 'should allow to use additional parameters in one call', () => {
  335. const el2 = new ViewElement( 'p' );
  336. viewConsumable.add( el, el2 );
  337. expect( viewConsumable.consume( el, el2 ) ).to.be.true;
  338. viewConsumable.revert( el, el2 );
  339. expect( viewConsumable.test( el, el2 ) ).to.be.true;
  340. } );
  341. it( 'should throw an error when element is not provided', () => {
  342. expect( () => {
  343. viewConsumable.revert( { style: 'color' } );
  344. } ).to.throw( 'viewconsumable-element-missing' );
  345. } );
  346. it( 'should throw if class attribute is provided', () => {
  347. viewConsumable.add( { element: el, class: 'foobar' } );
  348. viewConsumable.consume( { element: el, class: 'foobar' } );
  349. expect( () => {
  350. viewConsumable.revert( { element: el, attribute: 'class' } );
  351. } ).to.throw( 'viewconsumable-invalid-attribute' );
  352. } );
  353. it( 'should throw if style attribute is provided', () => {
  354. viewConsumable.add( { element: el, style: 'color' } );
  355. viewConsumable.consume( { element: el, style: 'color' } );
  356. expect( () => {
  357. viewConsumable.revert( { element: el, attribute: 'style' } );
  358. } ).to.throw( 'viewconsumable-invalid-attribute' );
  359. } );
  360. } );
  361. } );