8
0

element.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import utils from '/ckeditor5/utils/utils.js';
  8. import Node from '/ckeditor5/core/treeview/node.js';
  9. import ViewElement from '/ckeditor5/core/treeview/element.js';
  10. describe( 'Element', () => {
  11. describe( 'constructor', () => {
  12. it( 'should create element without attributes', () => {
  13. const el = new ViewElement( 'p' );
  14. expect( el ).to.be.an.instanceof( Node );
  15. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  16. expect( el ).to.have.property( 'parent' ).that.is.null;
  17. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 0 );
  18. } );
  19. it( 'should create element with attributes as plain object', () => {
  20. const el = new ViewElement( 'p', { foo: 'bar' } );
  21. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  22. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 1 );
  23. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  24. } );
  25. it( 'should create element with attributes as map', () => {
  26. const attrs = new Map();
  27. attrs.set( 'foo', 'bar' );
  28. const el = new ViewElement( 'p', attrs );
  29. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  30. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 1 );
  31. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  32. } );
  33. it( 'should create element with children', () => {
  34. const child = new ViewElement( 'p', { foo: 'bar' } );
  35. const parent = new ViewElement( 'div', [], [ child ] );
  36. expect( parent ).to.have.property( 'name' ).that.equals( 'div' );
  37. expect( parent.getChildCount() ).to.equal( 1 );
  38. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  39. } );
  40. it( 'should move class attribute to class set ', () => {
  41. const el = new ViewElement( 'p', { id: 'test', class: 'one two three' } );
  42. expect( el._attrs.has( 'class' ) ).to.be.false;
  43. expect( el._attrs.has( 'id' ) ).to.be.true;
  44. expect( el._classes.has( 'one' ) ).to.be.true;
  45. expect( el._classes.has( 'two' ) ).to.be.true;
  46. expect( el._classes.has( 'three' ) ).to.be.true;
  47. } );
  48. it( 'should move style attribute to style map', () => {
  49. const el = new ViewElement( 'p', { id: 'test', style: 'one: style1; two:style2 ; three : url(http://ckeditor.com)' } );
  50. expect( el._attrs.has( 'style' ) ).to.be.false;
  51. expect( el._attrs.has( 'id' ) ).to.be.true;
  52. expect( el._styles.has( 'one' ) ).to.be.true;
  53. expect( el._styles.get( 'one' ) ).to.equal( 'style1' );
  54. expect( el._styles.has( 'two' ) ).to.be.true;
  55. expect( el._styles.get( 'two' ) ).to.equal( 'style2' );
  56. expect( el._styles.has( 'three' ) ).to.be.true;
  57. expect( el._styles.get( 'three' ) ).to.equal( 'url(http://ckeditor.com)' );
  58. } );
  59. } );
  60. describe( 'clone', () => {
  61. it( 'should clone element', () => {
  62. const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' } );
  63. const clone = el.clone();
  64. expect( clone ).to.not.equal( el );
  65. expect( clone.name ).to.equal( el.name );
  66. expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
  67. expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
  68. } );
  69. it( 'should deeply clone element', () => {
  70. const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' }, [
  71. new ViewElement( 'b', { attr: 'baz' } ),
  72. new ViewElement( 'span', { attr: 'qux' } )
  73. ] );
  74. const count = el.getChildCount();
  75. const clone = el.clone( true );
  76. expect( clone ).to.not.equal( el );
  77. expect( clone.name ).to.equal( el.name );
  78. expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
  79. expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
  80. expect( clone.getChildCount() ).to.equal( count );
  81. for ( let i = 0; i < count; i++ ) {
  82. const child = el.getChild( i );
  83. const clonedChild = clone.getChild( i );
  84. expect( clonedChild ).to.not.equal( child );
  85. expect( clonedChild.name ).to.equal( child.name );
  86. expect( clonedChild.getAttribute( 'attr' ) ).to.equal( child.getAttribute( 'attr' ) );
  87. }
  88. } );
  89. it( 'shouldn\'t clone any children when deep copy is not performed', () => {
  90. const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' }, [
  91. new ViewElement( 'b', { attr: 'baz' } ),
  92. new ViewElement( 'span', { attr: 'qux' } )
  93. ] );
  94. const clone = el.clone( false );
  95. expect( clone ).to.not.equal( el );
  96. expect( clone.name ).to.equal( el.name );
  97. expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
  98. expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
  99. expect( clone.getChildCount() ).to.equal( 0 );
  100. } );
  101. it( 'should clone class attribute', () => {
  102. const el = new ViewElement( 'p', { foo: 'bar' } );
  103. el.addClass( 'baz', 'qux' );
  104. const clone = el.clone( false );
  105. expect( clone ).to.not.equal( el );
  106. expect( clone.name ).to.equal( el.name );
  107. expect( clone.getAttribute( 'foo' ) ).to.equal( 'bar' );
  108. expect( clone.getAttribute( 'class' ) ).to.equal( 'baz qux' );
  109. } );
  110. it( 'should clone style attribute', () => {
  111. const el = new ViewElement( 'p', { style: 'color: red; font-size: 12px;' } );
  112. const clone = el.clone( false );
  113. expect( clone ).to.not.equal( el );
  114. expect( clone.name ).to.equal( el.name );
  115. expect( clone._styles.has( 'color' ) ).to.be.true;
  116. expect( clone._styles.get( 'color' ) ).to.equal( 'red' );
  117. expect( clone._styles.has( 'font-size' ) ).to.be.true;
  118. expect( clone._styles.get( 'font-size' ) ).to.equal( '12px' );
  119. } );
  120. } );
  121. describe( 'isSimilar', () => {
  122. const el = new ViewElement( 'p', { foo: 'bar' } );
  123. it( 'should return false when comparing to non-element', () => {
  124. expect( el.isSimilar( null ) ).to.be.false;
  125. expect( el.isSimilar( {} ) ).to.be.false;
  126. } );
  127. it( 'should return true when the same node is provided', () => {
  128. expect( el.isSimilar( el ) ).to.be.true;
  129. } );
  130. it( 'should return true for element with same attributes and name', () => {
  131. const other = new ViewElement( 'p', { foo: 'bar' } );
  132. expect( el.isSimilar( other ) ).to.be.true;
  133. } );
  134. it( 'sould return false when name is not the same', () => {
  135. const other = el.clone();
  136. other.name = 'div';
  137. expect( el.isSimilar( other ) ).to.be.false;
  138. } );
  139. it( 'should return false when attributes are not the same', () => {
  140. const other1 = el.clone();
  141. const other2 = el.clone();
  142. const other3 = el.clone();
  143. other1.setAttribute( 'baz', 'qux' );
  144. other2.setAttribute( 'foo', 'not-bar' );
  145. other3.removeAttribute( 'foo' );
  146. expect( el.isSimilar( other1 ) ).to.be.false;
  147. expect( el.isSimilar( other2 ) ).to.be.false;
  148. expect( el.isSimilar( other3 ) ).to.be.false;
  149. } );
  150. it( 'should compare class attribute', () => {
  151. const el1 = new ViewElement( 'p' );
  152. const el2 = new ViewElement( 'p' );
  153. const el3 = new ViewElement( 'p' );
  154. const el4 = new ViewElement( 'p' );
  155. el1.addClass( 'foo', 'bar' );
  156. el2.addClass( 'bar', 'foo' );
  157. el3.addClass( 'baz' );
  158. el4.addClass( 'baz', 'bar' );
  159. expect( el1.isSimilar( el2 ) ).to.be.true;
  160. expect( el1.isSimilar( el3 ) ).to.be.false;
  161. expect( el1.isSimilar( el4 ) ).to.be.false;
  162. } );
  163. it( 'should compare styles attribute', () => {
  164. const el1 = new ViewElement( 'p' );
  165. const el2 = new ViewElement( 'p' );
  166. const el3 = new ViewElement( 'p' );
  167. const el4 = new ViewElement( 'p' );
  168. el1.setStyle( 'color', 'red' );
  169. el1.setStyle( 'top', '10px' );
  170. el2.setStyle( 'top', '20px' );
  171. el3.setStyle( 'top', '10px' );
  172. el3.setStyle( 'color', 'red' );
  173. el4.setStyle( 'color', 'blue' );
  174. el4.setStyle( 'top', '10px' );
  175. expect( el1.isSimilar( el2 ) ).to.be.false;
  176. expect( el1.isSimilar( el3 ) ).to.be.true;
  177. expect( el2.isSimilar( el3 ) ).to.be.false;
  178. expect( el3.isSimilar( el4 ) ).to.be.false;
  179. } );
  180. } );
  181. describe( 'children manipulation methods', () => {
  182. let parent, el1, el2, el3, el4;
  183. beforeEach( () => {
  184. parent = new ViewElement( 'p' );
  185. el1 = new ViewElement( 'el1' );
  186. el2 = new ViewElement( 'el2' );
  187. el3 = new ViewElement( 'el3' );
  188. el4 = new ViewElement( 'el4' );
  189. } );
  190. describe( 'insertion', () => {
  191. it( 'should insert children', () => {
  192. const count1 = parent.insertChildren( 0, [ el1, el3 ] );
  193. const count2 = parent.insertChildren( 1, el2 );
  194. expect( parent.getChildCount() ).to.equal( 3 );
  195. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  196. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  197. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  198. expect( count1 ).to.equal( 2 );
  199. expect( count2 ).to.equal( 1 );
  200. } );
  201. it( 'should append children', () => {
  202. const count1 = parent.insertChildren( 0, el1 );
  203. const count2 = parent.appendChildren( el2 );
  204. const count3 = parent.appendChildren( el3 );
  205. expect( parent.getChildCount() ).to.equal( 3 );
  206. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  207. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  208. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  209. expect( count1 ).to.equal( 1 );
  210. expect( count2 ).to.equal( 1 );
  211. expect( count3 ).to.equal( 1 );
  212. } );
  213. } );
  214. describe( 'getChildIndex', () => {
  215. it( 'should return child index', () => {
  216. parent.appendChildren( el1 );
  217. parent.appendChildren( el2 );
  218. parent.appendChildren( el3 );
  219. expect( parent.getChildCount() ).to.equal( 3 );
  220. expect( parent.getChildIndex( el1 ) ).to.equal( 0 );
  221. expect( parent.getChildIndex( el2 ) ).to.equal( 1 );
  222. expect( parent.getChildIndex( el3 ) ).to.equal( 2 );
  223. } );
  224. } );
  225. describe( 'getChildren', () => {
  226. it( 'should renturn children iterator', () => {
  227. parent.appendChildren( el1 );
  228. parent.appendChildren( el2 );
  229. parent.appendChildren( el3 );
  230. const expected = [ el1, el2, el3 ];
  231. let i = 0;
  232. for ( let child of parent.getChildren() ) {
  233. expect( child ).to.equal( expected[ i ] );
  234. i++;
  235. }
  236. expect( i ).to.equal( 3 );
  237. } );
  238. } );
  239. describe( 'removeChildren', () => {
  240. it( 'should remove children', () => {
  241. parent.appendChildren( el1 );
  242. parent.appendChildren( el2 );
  243. parent.appendChildren( el3 );
  244. parent.appendChildren( el4 );
  245. parent.removeChildren( 1, 2 );
  246. expect( parent.getChildCount() ).to.equal( 2 );
  247. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  248. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  249. expect( el1.parent ).to.equal( parent );
  250. expect( el2.parent ).to.be.null;
  251. expect( el3.parent ).to.be.null;
  252. expect( el4.parent ).equal( parent );
  253. } );
  254. it( 'should remove one child when second parameter is not specified', () => {
  255. parent.appendChildren( el1 );
  256. parent.appendChildren( el2 );
  257. parent.appendChildren( el3 );
  258. const removed = parent.removeChildren( 1 );
  259. expect( parent.getChildCount() ).to.equal( 2 );
  260. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  261. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  262. expect( removed.length ).to.equal( 1 );
  263. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  264. } );
  265. } );
  266. } );
  267. describe( 'attributes manipulation methods', () => {
  268. let el;
  269. beforeEach( () => {
  270. el = new ViewElement( 'p' );
  271. } );
  272. describe( 'setAttribute', () => {
  273. it( 'should set attribute', () => {
  274. el.setAttribute( 'foo', 'bar' );
  275. expect( el._attrs.has( 'foo' ) ).to.be.true;
  276. expect( el._attrs.get( 'foo' ) ).to.equal( 'bar' );
  277. } );
  278. it( 'should set class', () => {
  279. el.setAttribute( 'class', 'foo bar' );
  280. expect( el._attrs.has( 'class' ) ).to.be.false;
  281. expect( el._classes.has( 'foo' ) ).to.be.true;
  282. expect( el._classes.has( 'bar' ) ).to.be.true;
  283. } );
  284. it( 'should replace all existing classes', () => {
  285. el.setAttribute( 'class', 'foo bar baz' );
  286. el.setAttribute( 'class', 'qux' );
  287. expect( el._classes.has( 'foo' ) ).to.be.false;
  288. expect( el._classes.has( 'bar' ) ).to.be.false;
  289. expect( el._classes.has( 'baz' ) ).to.be.false;
  290. expect( el._classes.has( 'qux' ) ).to.be.true;
  291. } );
  292. it( 'should replace all styles', () => {
  293. el.setStyle( 'color', 'red' );
  294. el.setStyle( 'top', '10px' );
  295. el.setAttribute( 'style', 'border:none' );
  296. expect( el.hasStyle( 'color' ) ).to.be.false;
  297. expect( el.hasStyle( 'top' ) ).to.be.false;
  298. expect( el.hasStyle( 'border' ) ).to.be.true;
  299. expect( el.getStyle( 'border' ) ).to.equal( 'none' );
  300. } );
  301. } );
  302. describe( 'getAttribute', () => {
  303. it( 'should return attribute', () => {
  304. el.setAttribute( 'foo', 'bar' );
  305. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  306. expect( el.getAttribute( 'bom' ) ).to.not.be.ok;
  307. } );
  308. it( 'should return class attribute', () => {
  309. el.addClass( 'foo', 'bar' );
  310. expect( el.getAttribute( 'class' ) ).to.equal( 'foo bar' );
  311. } );
  312. it( 'should return undefined if no class attribute', () => {
  313. expect( el.getAttribute( 'class' ) ).to.be.undefined;
  314. } );
  315. it( 'should return style attribute', () => {
  316. el.setStyle( 'color', 'red' );
  317. el.setStyle( 'top', '10px' );
  318. expect( el.getAttribute( 'style' ) ).to.equal( 'color:red;top:10px;' );
  319. } );
  320. it( 'should return undefined if no style attribute', () => {
  321. expect( el.getAttribute( 'style' ) ).to.be.undefined;
  322. } );
  323. } );
  324. describe( 'hasAttribute', () => {
  325. it( 'should return true if element has attribute', () => {
  326. el.setAttribute( 'foo', 'bar' );
  327. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  328. expect( el.hasAttribute( 'bom' ) ).to.be.false;
  329. } );
  330. it( 'should return true if element has class attribute', () => {
  331. expect( el.hasAttribute( 'class' ) ).to.be.false;
  332. el.addClass( 'foo' );
  333. expect( el.hasAttribute( 'class' ) ).to.be.true;
  334. } );
  335. it( 'should return true if element has style attribute', () => {
  336. expect( el.hasAttribute( 'style' ) ).to.be.false;
  337. el.setStyle( 'border', '1px solid red' );
  338. expect( el.hasAttribute( 'style' ) ).to.be.true;
  339. } );
  340. } );
  341. describe( 'getAttributeKeys', () => {
  342. it( 'should return keys', () => {
  343. el.setAttribute( 'foo', true );
  344. el.setAttribute( 'bar', true );
  345. const expected = [ 'foo', 'bar' ];
  346. let i = 0;
  347. for ( let key of el.getAttributeKeys() ) {
  348. expect( key ).to.equal( expected[ i ] );
  349. i++;
  350. }
  351. expect( i ).to.equal( 2 );
  352. } );
  353. it( 'should return class key', () => {
  354. el.addClass( 'foo' );
  355. el.setAttribute( 'bar', true );
  356. const expected = [ 'class', 'bar' ];
  357. let i = 0;
  358. for ( let key of el.getAttributeKeys() ) {
  359. expect( key ).to.equal( expected[ i ] );
  360. i++;
  361. }
  362. } );
  363. it( 'should return style key', () => {
  364. el.setStyle( 'color', 'black' );
  365. el.setAttribute( 'bar', true );
  366. const expected = [ 'style', 'bar' ];
  367. let i = 0;
  368. for ( let key of el.getAttributeKeys() ) {
  369. expect( key ).to.equal( expected[ i ] );
  370. i++;
  371. }
  372. } );
  373. } );
  374. describe( 'removeAttribute', () => {
  375. it( 'should remove attributes', () => {
  376. el.setAttribute( 'foo', true );
  377. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  378. el.removeAttribute( 'foo' );
  379. expect( el.hasAttribute( 'foo' ) ).to.be.false;
  380. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 0 );
  381. } );
  382. it( 'should remove class attribute', () => {
  383. el.addClass( 'foo', 'bar' );
  384. const el2 = new ViewElement( 'p' );
  385. const removed1 = el.removeAttribute( 'class' );
  386. const removed2 = el2.removeAttribute( 'class' );
  387. expect( el.hasAttribute( 'class' ) ).to.be.false;
  388. expect( el.hasClass( 'foo' ) ).to.be.false;
  389. expect( el.hasClass( 'bar' ) ).to.be.false;
  390. expect( removed1 ).to.be.true;
  391. expect( removed2 ).to.be.false;
  392. } );
  393. it( 'should remove style attribute', () => {
  394. el.setStyle( 'color', 'red' );
  395. el.setStyle( 'position', 'fixed' );
  396. const el2 = new ViewElement( 'p' );
  397. const removed1 = el.removeAttribute( 'style' );
  398. const removed2 = el2.removeAttribute( 'style' );
  399. expect( el.hasAttribute( 'style' ) ).to.be.false;
  400. expect( el.hasStyle( 'color' ) ).to.be.false;
  401. expect( el.hasStyle( 'position' ) ).to.be.false;
  402. expect( removed1 ).to.be.true;
  403. expect( removed2 ).to.be.false;
  404. } );
  405. // it( 'should fire event', () => {
  406. //
  407. // } );
  408. } );
  409. } );
  410. describe( 'addClass', () => {
  411. it( 'should add single class', () => {
  412. const el = new ViewElement( 'foo' );
  413. el.addClass( 'one' );
  414. expect( el._classes.has( 'one' ) ).to.be.true;
  415. } );
  416. it( 'should add multiple classes', () => {
  417. const el = new ViewElement( 'foo' );
  418. el.addClass( 'one', 'two', 'three' );
  419. expect( el._classes.has( 'one' ) ).to.be.true;
  420. expect( el._classes.has( 'two' ) ).to.be.true;
  421. expect( el._classes.has( 'three' ) ).to.be.true;
  422. } );
  423. } );
  424. describe( 'removeClass', () => {
  425. it( 'should remove single class', () => {
  426. const el = new ViewElement( 'foo', { class: 'one two three' } );
  427. el.removeClass( 'one' );
  428. expect( el._classes.has( 'one' ) ).to.be.false;
  429. expect( el._classes.has( 'two' ) ).to.be.true;
  430. expect( el._classes.has( 'three' ) ).to.be.true;
  431. } );
  432. it( 'should remove multiple classes', () => {
  433. const el = new ViewElement( 'foo', { class: 'one two three four' } );
  434. el.removeClass( 'one', 'two', 'three' );
  435. expect( el._classes.has( 'one' ) ).to.be.false;
  436. expect( el._classes.has( 'two' ) ).to.be.false;
  437. expect( el._classes.has( 'three' ) ).to.be.false;
  438. expect( el._classes.has( 'four' ) ).to.be.true;
  439. } );
  440. } );
  441. describe( 'hasClass', () => {
  442. it( 'should check if element has a class', () => {
  443. const el = new ViewElement( 'foo' );
  444. el.addClass( 'one', 'two', 'three' );
  445. expect( el.hasClass( 'one' ) ).to.be.true;
  446. expect( el.hasClass( 'two' ) ).to.be.true;
  447. expect( el.hasClass( 'three' ) ).to.be.true;
  448. expect( el.hasClass( 'four' ) ).to.be.false;
  449. } );
  450. it( 'should check if element has multiple classes', () => {
  451. const el = new ViewElement( 'foo' );
  452. el.addClass( 'one', 'two', 'three' );
  453. expect( el.hasClass( 'one', 'two' ) ).to.be.true;
  454. expect( el.hasClass( 'three', 'two' ) ).to.be.true;
  455. expect( el.hasClass( 'three', 'one', 'two' ) ).to.be.true;
  456. expect( el.hasClass( 'three', 'one', 'two', 'zero' ) ).to.be.false;
  457. } );
  458. } );
  459. describe( 'setStyle', () => {
  460. it( 'should set element style', () => {
  461. const el = new ViewElement( 'p' );
  462. el.setStyle( 'color', 'red' );
  463. expect( el._styles.has( 'color' ) ).to.be.true;
  464. expect( el._styles.get( 'color' ) ).to.equal( 'red' );
  465. } );
  466. } );
  467. describe( 'getStyle', () => {
  468. it( 'should get style', () => {
  469. const el = new ViewElement( 'p' );
  470. el.setStyle( 'color', 'red' );
  471. el.setStyle( 'border', '1px solid red' );
  472. expect( el.getStyle( 'color' ) ).to.equal( 'red' );
  473. expect( el.getStyle( 'border' ) ).to.equal( '1px solid red' );
  474. } );
  475. } );
  476. describe( 'hasStyle', () => {
  477. it( 'should check if element has a style', () => {
  478. const el = new ViewElement( 'p' );
  479. el.setStyle( 'padding-top', '10px' );
  480. expect( el.hasStyle( 'padding-top' ) ).to.be.true;
  481. expect( el.hasStyle( 'padding-left' ) ).to.be.false;
  482. } );
  483. it( 'should check if element has multiple styles', () => {
  484. const el = new ViewElement( 'p' );
  485. el.setStyle( 'padding-top', '10px' );
  486. el.setStyle( 'margin-left', '10px' );
  487. el.setStyle( 'color', '10px;' );
  488. expect( el.hasStyle( 'padding-top', 'margin-left' ) ).to.be.true;
  489. expect( el.hasStyle( 'padding-top', 'margin-left', 'color' ) ).to.be.true;
  490. expect( el.hasStyle( 'padding-top', 'padding-left' ) ).to.be.false;
  491. } );
  492. } );
  493. describe( 'removeStyle', () => {
  494. it( 'should remove style', () => {
  495. const el = new ViewElement();
  496. el.setStyle( 'padding-top', '10px' );
  497. el.removeStyle( 'padding-top' );
  498. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  499. } );
  500. it( 'should remove multiple styles', () => {
  501. const el = new ViewElement();
  502. el.setStyle( 'padding-top', '10px' );
  503. el.setStyle( 'margin-top', '10px' );
  504. el.setStyle( 'color', 'red' );
  505. el.removeStyle( 'padding-top', 'margin-top' );
  506. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  507. expect( el.hasStyle( 'margin-top' ) ).to.be.false;
  508. expect( el.hasStyle( 'color' ) ).to.be.true;
  509. } );
  510. } );
  511. } );