element.js 33 KB

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