element.js 28 KB

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