element.js 22 KB

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