element.js 27 KB

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