element.js 32 KB

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