element.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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.childCount ).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.childCount;
  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.childCount ).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.childCount ).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.childCount ).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 accept strings', () => {
  211. parent.insertChildren( 0, 'abc' );
  212. expect( parent.childCount ).to.equal( 1 );
  213. expect( parent.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  214. parent.removeChildren( 0, 1 );
  215. parent.insertChildren( 0, [ new Element( 'p' ), 'abc' ] );
  216. expect( parent.childCount ).to.equal( 2 );
  217. expect( parent.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  218. } );
  219. it( 'should append children', () => {
  220. const count1 = parent.insertChildren( 0, el1 );
  221. const count2 = parent.appendChildren( el2 );
  222. const count3 = parent.appendChildren( el3 );
  223. expect( parent.childCount ).to.equal( 3 );
  224. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  225. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  226. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  227. expect( count1 ).to.equal( 1 );
  228. expect( count2 ).to.equal( 1 );
  229. expect( count3 ).to.equal( 1 );
  230. } );
  231. } );
  232. describe( 'getChildIndex', () => {
  233. it( 'should return child index', () => {
  234. parent.appendChildren( el1 );
  235. parent.appendChildren( el2 );
  236. parent.appendChildren( el3 );
  237. expect( parent.childCount ).to.equal( 3 );
  238. expect( parent.getChildIndex( el1 ) ).to.equal( 0 );
  239. expect( parent.getChildIndex( el2 ) ).to.equal( 1 );
  240. expect( parent.getChildIndex( el3 ) ).to.equal( 2 );
  241. } );
  242. } );
  243. describe( 'getChildren', () => {
  244. it( 'should renturn children iterator', () => {
  245. parent.appendChildren( el1 );
  246. parent.appendChildren( el2 );
  247. parent.appendChildren( el3 );
  248. const expected = [ el1, el2, el3 ];
  249. let i = 0;
  250. for ( let child of parent.getChildren() ) {
  251. expect( child ).to.equal( expected[ i ] );
  252. i++;
  253. }
  254. expect( i ).to.equal( 3 );
  255. } );
  256. } );
  257. describe( 'removeChildren', () => {
  258. it( 'should remove children', () => {
  259. parent.appendChildren( el1 );
  260. parent.appendChildren( el2 );
  261. parent.appendChildren( el3 );
  262. parent.appendChildren( el4 );
  263. parent.removeChildren( 1, 2 );
  264. expect( parent.childCount ).to.equal( 2 );
  265. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  266. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  267. expect( el1.parent ).to.equal( parent );
  268. expect( el2.parent ).to.be.null;
  269. expect( el3.parent ).to.be.null;
  270. expect( el4.parent ).equal( parent );
  271. } );
  272. it( 'should remove one child when second parameter is not specified', () => {
  273. parent.appendChildren( el1 );
  274. parent.appendChildren( el2 );
  275. parent.appendChildren( el3 );
  276. const removed = parent.removeChildren( 1 );
  277. expect( parent.childCount ).to.equal( 2 );
  278. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  279. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  280. expect( removed.length ).to.equal( 1 );
  281. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  282. } );
  283. } );
  284. } );
  285. describe( 'attributes manipulation methods', () => {
  286. let el;
  287. beforeEach( () => {
  288. el = new Element( 'p' );
  289. } );
  290. describe( 'setAttribute', () => {
  291. it( 'should set attribute', () => {
  292. el.setAttribute( 'foo', 'bar' );
  293. expect( el._attrs.has( 'foo' ) ).to.be.true;
  294. expect( el._attrs.get( 'foo' ) ).to.equal( 'bar' );
  295. } );
  296. it( 'should fire change event with attributes type', ( done ) => {
  297. el.once( 'change:attributes', ( eventInfo ) => {
  298. expect( eventInfo.source ).to.equal( el );
  299. done();
  300. } );
  301. el.setAttribute( 'foo', 'bar' );
  302. } );
  303. it( 'should set class', () => {
  304. el.setAttribute( 'class', 'foo bar' );
  305. expect( el._attrs.has( 'class' ) ).to.be.false;
  306. expect( el._classes.has( 'foo' ) ).to.be.true;
  307. expect( el._classes.has( 'bar' ) ).to.be.true;
  308. } );
  309. it( 'should replace all existing classes', () => {
  310. el.setAttribute( 'class', 'foo bar baz' );
  311. el.setAttribute( 'class', 'qux' );
  312. expect( el._classes.has( 'foo' ) ).to.be.false;
  313. expect( el._classes.has( 'bar' ) ).to.be.false;
  314. expect( el._classes.has( 'baz' ) ).to.be.false;
  315. expect( el._classes.has( 'qux' ) ).to.be.true;
  316. } );
  317. it( 'should replace all styles', () => {
  318. el.setStyle( 'color', 'red' );
  319. el.setStyle( 'top', '10px' );
  320. el.setAttribute( 'style', 'border:none' );
  321. expect( el.hasStyle( 'color' ) ).to.be.false;
  322. expect( el.hasStyle( 'top' ) ).to.be.false;
  323. expect( el.hasStyle( 'border' ) ).to.be.true;
  324. expect( el.getStyle( 'border' ) ).to.equal( 'none' );
  325. } );
  326. } );
  327. describe( 'getAttribute', () => {
  328. it( 'should return attribute', () => {
  329. el.setAttribute( 'foo', 'bar' );
  330. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  331. expect( el.getAttribute( 'bom' ) ).to.not.be.ok;
  332. } );
  333. it( 'should return class attribute', () => {
  334. el.addClass( 'foo', 'bar' );
  335. expect( el.getAttribute( 'class' ) ).to.equal( 'foo bar' );
  336. } );
  337. it( 'should return undefined if no class attribute', () => {
  338. expect( el.getAttribute( 'class' ) ).to.be.undefined;
  339. } );
  340. it( 'should return style attribute', () => {
  341. el.setStyle( 'color', 'red' );
  342. el.setStyle( 'top', '10px' );
  343. expect( el.getAttribute( 'style' ) ).to.equal( 'color:red;top:10px;' );
  344. } );
  345. it( 'should return undefined if no style attribute', () => {
  346. expect( el.getAttribute( 'style' ) ).to.be.undefined;
  347. } );
  348. } );
  349. describe( 'getAttributes', () => {
  350. it( 'should return attributes', () => {
  351. el.setAttribute( 'foo', 'bar' );
  352. el.setAttribute( 'abc', 'xyz' );
  353. expect( Array.from( el.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  354. } );
  355. it( 'should return class and style attribute', () => {
  356. el.setAttribute( 'class', 'abc' );
  357. el.setAttribute( 'style', 'width:20px;' );
  358. el.addClass( 'xyz' );
  359. el.setStyle( 'font-weight', 'bold' );
  360. expect( Array.from( el.getAttributes() ) ).to.deep.equal( [
  361. [ 'class', 'abc xyz' ], [ 'style', 'width:20px;font-weight:bold;' ]
  362. ] );
  363. } );
  364. } );
  365. describe( 'hasAttribute', () => {
  366. it( 'should return true if element has attribute', () => {
  367. el.setAttribute( 'foo', 'bar' );
  368. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  369. expect( el.hasAttribute( 'bom' ) ).to.be.false;
  370. } );
  371. it( 'should return true if element has class attribute', () => {
  372. expect( el.hasAttribute( 'class' ) ).to.be.false;
  373. el.addClass( 'foo' );
  374. expect( el.hasAttribute( 'class' ) ).to.be.true;
  375. } );
  376. it( 'should return true if element has style attribute', () => {
  377. expect( el.hasAttribute( 'style' ) ).to.be.false;
  378. el.setStyle( 'border', '1px solid red' );
  379. expect( el.hasAttribute( 'style' ) ).to.be.true;
  380. } );
  381. } );
  382. describe( 'getAttributeKeys', () => {
  383. it( 'should return keys', () => {
  384. el.setAttribute( 'foo', true );
  385. el.setAttribute( 'bar', true );
  386. const expected = [ 'foo', 'bar' ];
  387. let i = 0;
  388. for ( let key of el.getAttributeKeys() ) {
  389. expect( key ).to.equal( expected[ i ] );
  390. i++;
  391. }
  392. expect( i ).to.equal( 2 );
  393. } );
  394. it( 'should return class key', () => {
  395. el.addClass( 'foo' );
  396. el.setAttribute( 'bar', true );
  397. const expected = [ 'class', 'bar' ];
  398. let i = 0;
  399. for ( let key of el.getAttributeKeys() ) {
  400. expect( key ).to.equal( expected[ i ] );
  401. i++;
  402. }
  403. } );
  404. it( 'should return style key', () => {
  405. el.setStyle( 'color', 'black' );
  406. el.setAttribute( 'bar', true );
  407. const expected = [ 'style', 'bar' ];
  408. let i = 0;
  409. for ( let key of el.getAttributeKeys() ) {
  410. expect( key ).to.equal( expected[ i ] );
  411. i++;
  412. }
  413. } );
  414. } );
  415. describe( 'removeAttribute', () => {
  416. it( 'should remove attributes', () => {
  417. el.setAttribute( 'foo', true );
  418. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  419. el.removeAttribute( 'foo' );
  420. expect( el.hasAttribute( 'foo' ) ).to.be.false;
  421. expect( count( el.getAttributeKeys() ) ).to.equal( 0 );
  422. } );
  423. it( 'should fire change event with attributes type', ( done ) => {
  424. el.setAttribute( 'foo', 'bar' );
  425. el.once( 'change:attributes', ( eventInfo ) => {
  426. expect( eventInfo.source ).to.equal( el );
  427. done();
  428. } );
  429. el.removeAttribute( 'foo' );
  430. } );
  431. it( 'should remove class attribute', () => {
  432. el.addClass( 'foo', 'bar' );
  433. const el2 = new Element( 'p' );
  434. const removed1 = el.removeAttribute( 'class' );
  435. const removed2 = el2.removeAttribute( 'class' );
  436. expect( el.hasAttribute( 'class' ) ).to.be.false;
  437. expect( el.hasClass( 'foo' ) ).to.be.false;
  438. expect( el.hasClass( 'bar' ) ).to.be.false;
  439. expect( removed1 ).to.be.true;
  440. expect( removed2 ).to.be.false;
  441. } );
  442. it( 'should remove style attribute', () => {
  443. el.setStyle( 'color', 'red' );
  444. el.setStyle( 'position', 'fixed' );
  445. const el2 = new Element( 'p' );
  446. const removed1 = el.removeAttribute( 'style' );
  447. const removed2 = el2.removeAttribute( 'style' );
  448. expect( el.hasAttribute( 'style' ) ).to.be.false;
  449. expect( el.hasStyle( 'color' ) ).to.be.false;
  450. expect( el.hasStyle( 'position' ) ).to.be.false;
  451. expect( removed1 ).to.be.true;
  452. expect( removed2 ).to.be.false;
  453. } );
  454. } );
  455. } );
  456. describe( 'classes manipulation methods', () => {
  457. let el;
  458. beforeEach( () => {
  459. el = new Element( 'p' );
  460. } );
  461. describe( 'addClass', () => {
  462. it( 'should add single class', () => {
  463. el.addClass( 'one' );
  464. expect( el._classes.has( 'one' ) ).to.be.true;
  465. } );
  466. it( 'should fire change event with attributes type', ( done ) => {
  467. el.once( 'change:attributes', ( eventInfo ) => {
  468. expect( eventInfo.source ).to.equal( el );
  469. done();
  470. } );
  471. el.addClass( 'one' );
  472. } );
  473. it( 'should add multiple classes', () => {
  474. el.addClass( 'one', 'two', 'three' );
  475. expect( el._classes.has( 'one' ) ).to.be.true;
  476. expect( el._classes.has( 'two' ) ).to.be.true;
  477. expect( el._classes.has( 'three' ) ).to.be.true;
  478. } );
  479. } );
  480. describe( 'removeClass', () => {
  481. it( 'should remove single class', () => {
  482. el.addClass( 'one', 'two', 'three' );
  483. el.removeClass( 'one' );
  484. expect( el._classes.has( 'one' ) ).to.be.false;
  485. expect( el._classes.has( 'two' ) ).to.be.true;
  486. expect( el._classes.has( 'three' ) ).to.be.true;
  487. } );
  488. it( 'should fire change event with attributes type', ( done ) => {
  489. el.addClass( 'one' );
  490. el.once( 'change:attributes', ( eventInfo ) => {
  491. expect( eventInfo.source ).to.equal( el );
  492. done();
  493. } );
  494. el.removeClass( 'one' );
  495. } );
  496. it( 'should remove multiple classes', () => {
  497. el.addClass( 'one', 'two', 'three', 'four' );
  498. el.removeClass( 'one', 'two', 'three' );
  499. expect( el._classes.has( 'one' ) ).to.be.false;
  500. expect( el._classes.has( 'two' ) ).to.be.false;
  501. expect( el._classes.has( 'three' ) ).to.be.false;
  502. expect( el._classes.has( 'four' ) ).to.be.true;
  503. } );
  504. } );
  505. describe( 'hasClass', () => {
  506. it( 'should check if element has a class', () => {
  507. el.addClass( 'one', 'two', 'three' );
  508. expect( el.hasClass( 'one' ) ).to.be.true;
  509. expect( el.hasClass( 'two' ) ).to.be.true;
  510. expect( el.hasClass( 'three' ) ).to.be.true;
  511. expect( el.hasClass( 'four' ) ).to.be.false;
  512. } );
  513. it( 'should check if element has multiple classes', () => {
  514. el.addClass( 'one', 'two', 'three' );
  515. expect( el.hasClass( 'one', 'two' ) ).to.be.true;
  516. expect( el.hasClass( 'three', 'two' ) ).to.be.true;
  517. expect( el.hasClass( 'three', 'one', 'two' ) ).to.be.true;
  518. expect( el.hasClass( 'three', 'one', 'two', 'zero' ) ).to.be.false;
  519. } );
  520. } );
  521. describe( 'getClassNames', () => {
  522. it( 'should return iterator with all class names', () => {
  523. const names = [ 'one', 'two', 'three' ];
  524. el.addClass( ...names );
  525. const iterator = el.getClassNames();
  526. let i = 0;
  527. for ( let name of iterator ) {
  528. expect( name ).to.equal( names[ i++ ] );
  529. }
  530. } );
  531. } );
  532. } );
  533. describe( 'styles manipulation methods', () => {
  534. let el;
  535. beforeEach( () => {
  536. el = new Element( 'p' );
  537. } );
  538. describe( 'setStyle', () => {
  539. it( 'should set element style', () => {
  540. el.setStyle( 'color', 'red' );
  541. expect( el._styles.has( 'color' ) ).to.be.true;
  542. expect( el._styles.get( 'color' ) ).to.equal( 'red' );
  543. } );
  544. it( 'should fire change event with attributes type', ( done ) => {
  545. el.once( 'change:attributes', ( eventInfo ) => {
  546. expect( eventInfo.source ).to.equal( el );
  547. done();
  548. } );
  549. el.setStyle( 'color', 'red' );
  550. } );
  551. it( 'should set multiple styles by providing an object', () => {
  552. el.setStyle( {
  553. color: 'red',
  554. position: 'fixed'
  555. } );
  556. expect( el._styles.has( 'color' ) ).to.be.true;
  557. expect( el._styles.has( 'position' ) ).to.be.true;
  558. expect( el._styles.get( 'color' ) ).to.equal( 'red' );
  559. expect( el._styles.get( 'position' ) ).to.equal( 'fixed' );
  560. } );
  561. } );
  562. describe( 'getStyle', () => {
  563. it( 'should get style', () => {
  564. el.setStyle( {
  565. color: 'red',
  566. border: '1px solid red'
  567. } );
  568. expect( el.getStyle( 'color' ) ).to.equal( 'red' );
  569. expect( el.getStyle( 'border' ) ).to.equal( '1px solid red' );
  570. } );
  571. } );
  572. describe( 'getStyleNames', () => {
  573. it( 'should return iterator with all style names', () => {
  574. const names = [ 'color', 'position' ];
  575. el.setStyle( {
  576. color: 'red',
  577. position: 'absolute'
  578. } );
  579. const iterator = el.getStyleNames();
  580. let i = 0;
  581. for ( let name of iterator ) {
  582. expect( name ).to.equal( names[ i++ ] );
  583. }
  584. } );
  585. } );
  586. describe( 'hasStyle', () => {
  587. it( 'should check if element has a style', () => {
  588. el.setStyle( 'padding-top', '10px' );
  589. expect( el.hasStyle( 'padding-top' ) ).to.be.true;
  590. expect( el.hasStyle( 'padding-left' ) ).to.be.false;
  591. } );
  592. it( 'should check if element has multiple styles', () => {
  593. el.setStyle( {
  594. 'padding-top': '10px',
  595. 'margin-left': '10px',
  596. 'color': '10px;'
  597. } );
  598. expect( el.hasStyle( 'padding-top', 'margin-left' ) ).to.be.true;
  599. expect( el.hasStyle( 'padding-top', 'margin-left', 'color' ) ).to.be.true;
  600. expect( el.hasStyle( 'padding-top', 'padding-left' ) ).to.be.false;
  601. } );
  602. } );
  603. describe( 'removeStyle', () => {
  604. it( 'should remove style', () => {
  605. el.setStyle( 'padding-top', '10px' );
  606. el.removeStyle( 'padding-top' );
  607. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  608. } );
  609. it( 'should fire change event with attributes type', ( done ) => {
  610. el.setStyle( 'color', 'red' );
  611. el.once( 'change:attributes', ( eventInfo ) => {
  612. expect( eventInfo.source ).to.equal( el );
  613. done();
  614. } );
  615. el.removeStyle( 'color' );
  616. } );
  617. it( 'should remove multiple styles', () => {
  618. el.setStyle( {
  619. 'padding-top': '10px',
  620. 'margin-top': '10px',
  621. 'color': 'red'
  622. } );
  623. el.removeStyle( 'padding-top', 'margin-top' );
  624. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  625. expect( el.hasStyle( 'margin-top' ) ).to.be.false;
  626. expect( el.hasStyle( 'color' ) ).to.be.true;
  627. } );
  628. } );
  629. } );
  630. } );