8
0

viewconsumable.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/core/treeview/element.js';
  8. import ViewConsumable from '/ckeditor5/core/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 consume attributes, classes and styles', () => {
  166. viewConsumable.add( { element: el, class: 'foobar', attribute: 'href', style: 'color' } );
  167. const consumed1 = viewConsumable.consume( { element: el, class: 'foobar' } );
  168. const consumed2 = viewConsumable.consume( { element: el, attribute: 'href' } );
  169. const consumed3 = viewConsumable.consume( { element: el, style: 'color' } );
  170. expect( consumed1 ).to.be.true;
  171. expect( consumed2 ).to.be.true;
  172. expect( consumed3 ).to.be.true;
  173. expect( viewConsumable.test( { element: el, class: 'foobar' } ) ).to.be.false;
  174. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.false;
  175. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.false;
  176. } );
  177. it( 'should consume multiple attributes', () => {
  178. viewConsumable.add( { element: el, attribute: [ 'href', 'title', 'name' ] } );
  179. const consumed = viewConsumable.consume( { element: el, attribute: [ 'href', 'title' ] } );
  180. expect( consumed ).to.be.true;
  181. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.false;
  182. expect( viewConsumable.test( { element: el, attribute: 'title' } ) ).to.be.false;
  183. expect( viewConsumable.test( { element: el, attribute: 'name' } ) ).to.be.true;
  184. } );
  185. it( 'should consume multiple styles', () => {
  186. viewConsumable.add( { element: el, style: [ 'color', 'top', 'position' ] } );
  187. const consumed = viewConsumable.consume( { element: el, style: [ 'color', 'position' ] } );
  188. expect( consumed ).to.be.true;
  189. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.false;
  190. expect( viewConsumable.test( { element: el, style: 'position' } ) ).to.be.false;
  191. expect( viewConsumable.test( { element: el, style: 'top' } ) ).to.be.true;
  192. } );
  193. it( 'should consume multiple classes', () => {
  194. viewConsumable.add( { element: el, class: [ 'foo', 'bar', 'baz' ] } );
  195. const consumed = viewConsumable.consume( { element: el, class: [ 'bar', 'baz' ] } );
  196. expect( consumed ).to.be.true;
  197. expect( viewConsumable.test( { element: el, class: 'bar' } ) ).to.be.false;
  198. expect( viewConsumable.test( { element: el, class: 'baz' } ) ).to.be.false;
  199. expect( viewConsumable.test( { element: el, class: 'foo' } ) ).to.be.true;
  200. } );
  201. it( 'should consume multiple items', () => {
  202. viewConsumable.add( {
  203. element: el,
  204. attribute: [ 'name', 'href', 'title' ],
  205. class: [ 'foo', 'bar', 'baz' ],
  206. style: [ 'color', 'position' ]
  207. } );
  208. const consumed = viewConsumable.consume(
  209. { element: el, attribute: 'name', class: 'foo' } ,
  210. { element: el, attribute: 'href', class: [ 'bar', 'baz' ] } ,
  211. { element: el, attribute: 'title', style: 'color' }
  212. );
  213. expect( consumed ).to.be.true;
  214. expect( viewConsumable.test( { element: el, attribute: 'name' } ) ).to.be.false;
  215. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.false;
  216. expect( viewConsumable.test( { element: el, attribute: 'title' } ) ).to.be.false;
  217. expect( viewConsumable.test( { element: el, class: 'foo' } ) ).to.be.false;
  218. expect( viewConsumable.test( { element: el, class: 'bar' } ) ).to.be.false;
  219. expect( viewConsumable.test( { element: el, class: 'baz' } ) ).to.be.false;
  220. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.false;
  221. expect( viewConsumable.test( { element: el, style: 'position' } ) ).to.be.true;
  222. } );
  223. it( 'should consume element provided as only item in object', () => {
  224. viewConsumable.add( el );
  225. const consumed = viewConsumable.consume( { element: el } );
  226. expect( viewConsumable.test( el ) ).to.be.false;
  227. expect( consumed ).to.be.true;
  228. } );
  229. it( 'should consume only if all items can be consumed', () => {
  230. viewConsumable.add( { element: el, style: [ 'position', 'color' ], attribute: [ 'href', 'title' ] } );
  231. let consumed = viewConsumable.consume( el, { element: el, style: 'color' } );
  232. expect( consumed ).to.be.false;
  233. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  234. consumed = viewConsumable.consume( { element: el, style: [ 'color', 'top' ] } );
  235. expect( consumed ).to.be.false;
  236. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  237. } );
  238. } );
  239. } );