8
0

element.js 26 KB

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