8
0

element.js 23 KB

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