8
0

viewconsumable.js 16 KB

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