viewconsumable.js 19 KB

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