8
0

element.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. 'use strict';
  7. import count from '/ckeditor5/utils/count.js';
  8. import Node from '/ckeditor5/engine/view/node.js';
  9. import ViewElement from '/ckeditor5/engine/view/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( 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( 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( 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 fire change event with attributes type', ( done ) => {
  279. el.once( 'change:attributes', ( eventInfo ) => {
  280. expect( eventInfo.source ).to.equal( el );
  281. done();
  282. } );
  283. el.setAttribute( 'foo', 'bar' );
  284. } );
  285. it( 'should set class', () => {
  286. el.setAttribute( 'class', 'foo bar' );
  287. expect( el._attrs.has( 'class' ) ).to.be.false;
  288. expect( el._classes.has( 'foo' ) ).to.be.true;
  289. expect( el._classes.has( 'bar' ) ).to.be.true;
  290. } );
  291. it( 'should replace all existing classes', () => {
  292. el.setAttribute( 'class', 'foo bar baz' );
  293. el.setAttribute( 'class', 'qux' );
  294. expect( el._classes.has( 'foo' ) ).to.be.false;
  295. expect( el._classes.has( 'bar' ) ).to.be.false;
  296. expect( el._classes.has( 'baz' ) ).to.be.false;
  297. expect( el._classes.has( 'qux' ) ).to.be.true;
  298. } );
  299. it( 'should replace all styles', () => {
  300. el.setStyle( 'color', 'red' );
  301. el.setStyle( 'top', '10px' );
  302. el.setAttribute( 'style', 'border:none' );
  303. expect( el.hasStyle( 'color' ) ).to.be.false;
  304. expect( el.hasStyle( 'top' ) ).to.be.false;
  305. expect( el.hasStyle( 'border' ) ).to.be.true;
  306. expect( el.getStyle( 'border' ) ).to.equal( 'none' );
  307. } );
  308. } );
  309. describe( 'getAttribute', () => {
  310. it( 'should return attribute', () => {
  311. el.setAttribute( 'foo', 'bar' );
  312. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  313. expect( el.getAttribute( 'bom' ) ).to.not.be.ok;
  314. } );
  315. it( 'should return class attribute', () => {
  316. el.addClass( 'foo', 'bar' );
  317. expect( el.getAttribute( 'class' ) ).to.equal( 'foo bar' );
  318. } );
  319. it( 'should return undefined if no class attribute', () => {
  320. expect( el.getAttribute( 'class' ) ).to.be.undefined;
  321. } );
  322. it( 'should return style attribute', () => {
  323. el.setStyle( 'color', 'red' );
  324. el.setStyle( 'top', '10px' );
  325. expect( el.getAttribute( 'style' ) ).to.equal( 'color:red;top:10px;' );
  326. } );
  327. it( 'should return undefined if no style attribute', () => {
  328. expect( el.getAttribute( 'style' ) ).to.be.undefined;
  329. } );
  330. } );
  331. describe( 'hasAttribute', () => {
  332. it( 'should return true if element has attribute', () => {
  333. el.setAttribute( 'foo', 'bar' );
  334. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  335. expect( el.hasAttribute( 'bom' ) ).to.be.false;
  336. } );
  337. it( 'should return true if element has class attribute', () => {
  338. expect( el.hasAttribute( 'class' ) ).to.be.false;
  339. el.addClass( 'foo' );
  340. expect( el.hasAttribute( 'class' ) ).to.be.true;
  341. } );
  342. it( 'should return true if element has style attribute', () => {
  343. expect( el.hasAttribute( 'style' ) ).to.be.false;
  344. el.setStyle( 'border', '1px solid red' );
  345. expect( el.hasAttribute( 'style' ) ).to.be.true;
  346. } );
  347. } );
  348. describe( 'getAttributeKeys', () => {
  349. it( 'should return keys', () => {
  350. el.setAttribute( 'foo', true );
  351. el.setAttribute( 'bar', true );
  352. const expected = [ 'foo', 'bar' ];
  353. let i = 0;
  354. for ( let key of el.getAttributeKeys() ) {
  355. expect( key ).to.equal( expected[ i ] );
  356. i++;
  357. }
  358. expect( i ).to.equal( 2 );
  359. } );
  360. it( 'should return class key', () => {
  361. el.addClass( 'foo' );
  362. el.setAttribute( 'bar', true );
  363. const expected = [ 'class', 'bar' ];
  364. let i = 0;
  365. for ( let key of el.getAttributeKeys() ) {
  366. expect( key ).to.equal( expected[ i ] );
  367. i++;
  368. }
  369. } );
  370. it( 'should return style key', () => {
  371. el.setStyle( 'color', 'black' );
  372. el.setAttribute( 'bar', true );
  373. const expected = [ 'style', 'bar' ];
  374. let i = 0;
  375. for ( let key of el.getAttributeKeys() ) {
  376. expect( key ).to.equal( expected[ i ] );
  377. i++;
  378. }
  379. } );
  380. } );
  381. describe( 'removeAttribute', () => {
  382. it( 'should remove attributes', () => {
  383. el.setAttribute( 'foo', true );
  384. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  385. el.removeAttribute( 'foo' );
  386. expect( el.hasAttribute( 'foo' ) ).to.be.false;
  387. expect( count( el.getAttributeKeys() ) ).to.equal( 0 );
  388. } );
  389. it( 'should fire change event with attributes type', ( done ) => {
  390. el.setAttribute( 'foo', 'bar' );
  391. el.once( 'change:attributes', ( eventInfo ) => {
  392. expect( eventInfo.source ).to.equal( el );
  393. done();
  394. } );
  395. el.removeAttribute( 'foo' );
  396. } );
  397. it( 'should remove class attribute', () => {
  398. el.addClass( 'foo', 'bar' );
  399. const el2 = new ViewElement( 'p' );
  400. const removed1 = el.removeAttribute( 'class' );
  401. const removed2 = el2.removeAttribute( 'class' );
  402. expect( el.hasAttribute( 'class' ) ).to.be.false;
  403. expect( el.hasClass( 'foo' ) ).to.be.false;
  404. expect( el.hasClass( 'bar' ) ).to.be.false;
  405. expect( removed1 ).to.be.true;
  406. expect( removed2 ).to.be.false;
  407. } );
  408. it( 'should remove style attribute', () => {
  409. el.setStyle( 'color', 'red' );
  410. el.setStyle( 'position', 'fixed' );
  411. const el2 = new ViewElement( 'p' );
  412. const removed1 = el.removeAttribute( 'style' );
  413. const removed2 = el2.removeAttribute( 'style' );
  414. expect( el.hasAttribute( 'style' ) ).to.be.false;
  415. expect( el.hasStyle( 'color' ) ).to.be.false;
  416. expect( el.hasStyle( 'position' ) ).to.be.false;
  417. expect( removed1 ).to.be.true;
  418. expect( removed2 ).to.be.false;
  419. } );
  420. } );
  421. } );
  422. describe( 'classes manipulation methods', () => {
  423. let el;
  424. beforeEach( () => {
  425. el = new ViewElement( 'p' );
  426. } );
  427. describe( 'addClass', () => {
  428. it( 'should add single class', () => {
  429. el.addClass( 'one' );
  430. expect( el._classes.has( 'one' ) ).to.be.true;
  431. } );
  432. it( 'should fire change event with attributes type', ( done ) => {
  433. el.once( 'change:attributes', ( eventInfo ) => {
  434. expect( eventInfo.source ).to.equal( el );
  435. done();
  436. } );
  437. el.addClass( 'one' );
  438. } );
  439. it( 'should add multiple classes', () => {
  440. el.addClass( 'one', 'two', 'three' );
  441. expect( el._classes.has( 'one' ) ).to.be.true;
  442. expect( el._classes.has( 'two' ) ).to.be.true;
  443. expect( el._classes.has( 'three' ) ).to.be.true;
  444. } );
  445. } );
  446. describe( 'removeClass', () => {
  447. it( 'should remove single class', () => {
  448. el.addClass( 'one', 'two', 'three' );
  449. el.removeClass( 'one' );
  450. expect( el._classes.has( 'one' ) ).to.be.false;
  451. expect( el._classes.has( 'two' ) ).to.be.true;
  452. expect( el._classes.has( 'three' ) ).to.be.true;
  453. } );
  454. it( 'should fire change event with attributes type', ( done ) => {
  455. el.addClass( 'one' );
  456. el.once( 'change:attributes', ( eventInfo ) => {
  457. expect( eventInfo.source ).to.equal( el );
  458. done();
  459. } );
  460. el.removeClass( 'one' );
  461. } );
  462. it( 'should remove multiple classes', () => {
  463. el.addClass( 'one', 'two', 'three', 'four' );
  464. el.removeClass( 'one', 'two', 'three' );
  465. expect( el._classes.has( 'one' ) ).to.be.false;
  466. expect( el._classes.has( 'two' ) ).to.be.false;
  467. expect( el._classes.has( 'three' ) ).to.be.false;
  468. expect( el._classes.has( 'four' ) ).to.be.true;
  469. } );
  470. } );
  471. describe( 'hasClass', () => {
  472. it( 'should check if element has a class', () => {
  473. el.addClass( 'one', 'two', 'three' );
  474. expect( el.hasClass( 'one' ) ).to.be.true;
  475. expect( el.hasClass( 'two' ) ).to.be.true;
  476. expect( el.hasClass( 'three' ) ).to.be.true;
  477. expect( el.hasClass( 'four' ) ).to.be.false;
  478. } );
  479. it( 'should check if element has multiple classes', () => {
  480. el.addClass( 'one', 'two', 'three' );
  481. expect( el.hasClass( 'one', 'two' ) ).to.be.true;
  482. expect( el.hasClass( 'three', 'two' ) ).to.be.true;
  483. expect( el.hasClass( 'three', 'one', 'two' ) ).to.be.true;
  484. expect( el.hasClass( 'three', 'one', 'two', 'zero' ) ).to.be.false;
  485. } );
  486. } );
  487. describe( 'getClassNames', () => {
  488. it( 'should return iterator with all class names', () => {
  489. const names = [ 'one', 'two', 'three' ];
  490. el.addClass( ...names );
  491. const iterator = el.getClassNames();
  492. let i = 0;
  493. for ( let name of iterator ) {
  494. expect( name ).to.equal( names[ i++ ] );
  495. }
  496. } );
  497. } );
  498. } );
  499. describe( 'styles manipulation methods', () => {
  500. let el;
  501. beforeEach( () => {
  502. el = new ViewElement( 'p' );
  503. } );
  504. describe( 'setStyle', () => {
  505. it( 'should set element style', () => {
  506. el.setStyle( 'color', 'red' );
  507. expect( el._styles.has( 'color' ) ).to.be.true;
  508. expect( el._styles.get( 'color' ) ).to.equal( 'red' );
  509. } );
  510. it( 'should fire change event with attributes type', ( done ) => {
  511. el.once( 'change:attributes', ( eventInfo ) => {
  512. expect( eventInfo.source ).to.equal( el );
  513. done();
  514. } );
  515. el.setStyle( 'color', 'red' );
  516. } );
  517. it( 'should set multiple styles by providing an object', () => {
  518. el.setStyle( {
  519. color: 'red',
  520. position: 'fixed'
  521. } );
  522. expect( el._styles.has( 'color' ) ).to.be.true;
  523. expect( el._styles.has( 'position' ) ).to.be.true;
  524. expect( el._styles.get( 'color' ) ).to.equal( 'red' );
  525. expect( el._styles.get( 'position' ) ).to.equal( 'fixed' );
  526. } );
  527. } );
  528. describe( 'getStyle', () => {
  529. it( 'should get style', () => {
  530. el.setStyle( {
  531. color: 'red',
  532. border: '1px solid red'
  533. } );
  534. expect( el.getStyle( 'color' ) ).to.equal( 'red' );
  535. expect( el.getStyle( 'border' ) ).to.equal( '1px solid red' );
  536. } );
  537. } );
  538. describe( 'getStyleNames', () => {
  539. it( 'should return iterator with all style names', () => {
  540. const names = [ 'color', 'position' ];
  541. el.setStyle( {
  542. color: 'red',
  543. position: 'absolute'
  544. } );
  545. const iterator = el.getStyleNames();
  546. let i = 0;
  547. for ( let name of iterator ) {
  548. expect( name ).to.equal( names[ i++ ] );
  549. }
  550. } );
  551. } );
  552. describe( 'hasStyle', () => {
  553. it( 'should check if element has a style', () => {
  554. el.setStyle( 'padding-top', '10px' );
  555. expect( el.hasStyle( 'padding-top' ) ).to.be.true;
  556. expect( el.hasStyle( 'padding-left' ) ).to.be.false;
  557. } );
  558. it( 'should check if element has multiple styles', () => {
  559. el.setStyle( {
  560. 'padding-top': '10px',
  561. 'margin-left': '10px',
  562. 'color': '10px;'
  563. } );
  564. expect( el.hasStyle( 'padding-top', 'margin-left' ) ).to.be.true;
  565. expect( el.hasStyle( 'padding-top', 'margin-left', 'color' ) ).to.be.true;
  566. expect( el.hasStyle( 'padding-top', 'padding-left' ) ).to.be.false;
  567. } );
  568. } );
  569. describe( 'removeStyle', () => {
  570. it( 'should remove style', () => {
  571. el.setStyle( 'padding-top', '10px' );
  572. el.removeStyle( 'padding-top' );
  573. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  574. } );
  575. it( 'should fire change event with attributes type', ( done ) => {
  576. el.setStyle( 'color', 'red' );
  577. el.once( 'change:attributes', ( eventInfo ) => {
  578. expect( eventInfo.source ).to.equal( el );
  579. done();
  580. } );
  581. el.removeStyle( 'color' );
  582. } );
  583. it( 'should remove multiple styles', () => {
  584. el.setStyle( {
  585. 'padding-top': '10px',
  586. 'margin-top': '10px',
  587. 'color': 'red'
  588. } );
  589. el.removeStyle( 'padding-top', 'margin-top' );
  590. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  591. expect( el.hasStyle( 'margin-top' ) ).to.be.false;
  592. expect( el.hasStyle( 'color' ) ).to.be.true;
  593. } );
  594. } );
  595. } );
  596. } );