8
0

element.js 27 KB

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