element.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /**
  2. * @license Copyright (c) 2003-2019, 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.hasProperty( 'one' ) ).to.be.true;
  59. expect( el._styles.getInlineProperty( 'one' ) ).to.equal( 'style1' );
  60. expect( el._styles.hasProperty( 'two' ) ).to.be.true;
  61. expect( el._styles.getInlineProperty( 'two' ) ).to.equal( 'style2' );
  62. expect( el._styles.hasProperty( 'three' ) ).to.be.true;
  63. expect( el._styles.getInlineProperty( '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.hasProperty( 'color' ) ).to.be.true;
  165. expect( clone._styles.getInlineProperty( 'color' ) ).to.equal( 'red' );
  166. expect( clone._styles.hasProperty( 'font-size' ) ).to.be.true;
  167. expect( clone._styles.getInlineProperty( '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( 'sould 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. it( 'should compare styles attribute', () => {
  230. const el1 = new Element( 'p' );
  231. const el2 = new Element( 'p' );
  232. const el3 = new Element( 'p' );
  233. const el4 = new Element( 'p' );
  234. el1._setStyle( 'color', 'red' );
  235. el1._setStyle( 'top', '10px' );
  236. el2._setStyle( 'top', '20px' );
  237. el3._setStyle( 'top', '10px' );
  238. el3._setStyle( 'color', 'red' );
  239. el4._setStyle( 'color', 'blue' );
  240. el4._setStyle( 'top', '10px' );
  241. expect( el1.isSimilar( el2 ) ).to.be.false;
  242. expect( el1.isSimilar( el3 ) ).to.be.true;
  243. expect( el2.isSimilar( el3 ) ).to.be.false;
  244. expect( el3.isSimilar( el4 ) ).to.be.false;
  245. } );
  246. } );
  247. describe( 'children manipulation methods', () => {
  248. let parent, el1, el2, el3, el4;
  249. beforeEach( () => {
  250. parent = new Element( 'p' );
  251. el1 = new Element( 'el1' );
  252. el2 = new Element( 'el2' );
  253. el3 = new Element( 'el3' );
  254. el4 = new Element( 'el4' );
  255. } );
  256. describe( 'insertion', () => {
  257. it( 'should insert children', () => {
  258. const count1 = parent._insertChild( 0, [ el1, el3 ] );
  259. const count2 = parent._insertChild( 1, el2 );
  260. expect( parent.childCount ).to.equal( 3 );
  261. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  262. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  263. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  264. expect( count1 ).to.equal( 2 );
  265. expect( count2 ).to.equal( 1 );
  266. } );
  267. it( 'should accept strings', () => {
  268. parent._insertChild( 0, 'abc' );
  269. expect( parent.childCount ).to.equal( 1 );
  270. expect( parent.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  271. parent._removeChildren( 0, 1 );
  272. parent._insertChild( 0, [ new Element( 'p' ), 'abc' ] );
  273. expect( parent.childCount ).to.equal( 2 );
  274. expect( parent.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  275. } );
  276. it( 'should append children', () => {
  277. const count1 = parent._insertChild( 0, el1 );
  278. const count2 = parent._appendChild( el2 );
  279. const count3 = parent._appendChild( el3 );
  280. expect( parent.childCount ).to.equal( 3 );
  281. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  282. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  283. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  284. expect( count1 ).to.equal( 1 );
  285. expect( count2 ).to.equal( 1 );
  286. expect( count3 ).to.equal( 1 );
  287. } );
  288. it( 'should accept and correctly handle text proxies', () => {
  289. const element = new Element( 'div' );
  290. const text = new Text( 'abcxyz' );
  291. const textProxy = new TextProxy( text, 2, 3 );
  292. element._insertChild( 0, textProxy );
  293. expect( element.childCount ).to.equal( 1 );
  294. expect( element.getChild( 0 ) ).to.be.instanceof( Text );
  295. expect( element.getChild( 0 ).data ).to.equal( 'cxy' );
  296. } );
  297. } );
  298. describe( 'getChildIndex', () => {
  299. it( 'should return child index', () => {
  300. parent._appendChild( el1 );
  301. parent._appendChild( el2 );
  302. parent._appendChild( el3 );
  303. expect( parent.childCount ).to.equal( 3 );
  304. expect( parent.getChildIndex( el1 ) ).to.equal( 0 );
  305. expect( parent.getChildIndex( el2 ) ).to.equal( 1 );
  306. expect( parent.getChildIndex( el3 ) ).to.equal( 2 );
  307. } );
  308. } );
  309. describe( 'getChildren', () => {
  310. it( 'should renturn children iterator', () => {
  311. parent._appendChild( el1 );
  312. parent._appendChild( el2 );
  313. parent._appendChild( el3 );
  314. const expected = [ el1, el2, el3 ];
  315. let i = 0;
  316. for ( const child of parent.getChildren() ) {
  317. expect( child ).to.equal( expected[ i ] );
  318. i++;
  319. }
  320. expect( i ).to.equal( 3 );
  321. } );
  322. } );
  323. describe( '_removeChildren', () => {
  324. it( 'should remove children', () => {
  325. parent._appendChild( el1 );
  326. parent._appendChild( el2 );
  327. parent._appendChild( el3 );
  328. parent._appendChild( el4 );
  329. parent._removeChildren( 1, 2 );
  330. expect( parent.childCount ).to.equal( 2 );
  331. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  332. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  333. expect( el1.parent ).to.equal( parent );
  334. expect( el2.parent ).to.be.null;
  335. expect( el3.parent ).to.be.null;
  336. expect( el4.parent ).equal( parent );
  337. } );
  338. it( 'should remove one child when second parameter is not specified', () => {
  339. parent._appendChild( el1 );
  340. parent._appendChild( el2 );
  341. parent._appendChild( el3 );
  342. const removed = parent._removeChildren( 1 );
  343. expect( parent.childCount ).to.equal( 2 );
  344. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  345. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  346. expect( removed.length ).to.equal( 1 );
  347. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  348. } );
  349. } );
  350. } );
  351. describe( 'attributes manipulation methods', () => {
  352. let el;
  353. beforeEach( () => {
  354. el = new Element( 'p' );
  355. } );
  356. describe( '_setAttribute', () => {
  357. it( 'should set attribute', () => {
  358. el._setAttribute( 'foo', 'bar' );
  359. expect( el._attrs.has( 'foo' ) ).to.be.true;
  360. expect( el._attrs.get( 'foo' ) ).to.equal( 'bar' );
  361. } );
  362. it( 'should cast attribute value to a string', () => {
  363. el._setAttribute( 'foo', true );
  364. expect( el._attrs.get( 'foo' ) ).to.equal( 'true' );
  365. } );
  366. it( 'should fire change event with attributes type', done => {
  367. el.once( 'change:attributes', eventInfo => {
  368. expect( eventInfo.source ).to.equal( el );
  369. done();
  370. } );
  371. el._setAttribute( 'foo', 'bar' );
  372. } );
  373. it( 'should set class', () => {
  374. el._setAttribute( 'class', 'foo bar' );
  375. expect( el._attrs.has( 'class' ) ).to.be.false;
  376. expect( el._classes.has( 'foo' ) ).to.be.true;
  377. expect( el._classes.has( 'bar' ) ).to.be.true;
  378. } );
  379. it( 'should replace all existing classes', () => {
  380. el._setAttribute( 'class', 'foo bar baz' );
  381. el._setAttribute( 'class', 'qux' );
  382. expect( el._classes.has( 'foo' ) ).to.be.false;
  383. expect( el._classes.has( 'bar' ) ).to.be.false;
  384. expect( el._classes.has( 'baz' ) ).to.be.false;
  385. expect( el._classes.has( 'qux' ) ).to.be.true;
  386. } );
  387. it( 'should replace all styles', () => {
  388. el._setStyle( 'color', 'red' );
  389. el._setStyle( 'top', '10px' );
  390. el._setAttribute( 'style', 'margin-top:2em;' );
  391. expect( el.hasStyle( 'color' ) ).to.be.false;
  392. expect( el.hasStyle( 'top' ) ).to.be.false;
  393. expect( el.hasStyle( 'margin-top' ) ).to.be.true;
  394. expect( el.getStyle( 'margin-top' ) ).to.equal( '2em' );
  395. } );
  396. } );
  397. describe( 'getAttribute', () => {
  398. it( 'should return attribute', () => {
  399. el._setAttribute( 'foo', 'bar' );
  400. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  401. expect( el.getAttribute( 'bom' ) ).to.not.be.ok;
  402. } );
  403. it( 'should return class attribute', () => {
  404. el._addClass( [ 'foo', 'bar' ] );
  405. expect( el.getAttribute( 'class' ) ).to.equal( 'foo bar' );
  406. } );
  407. it( 'should return undefined if no class attribute', () => {
  408. expect( el.getAttribute( 'class' ) ).to.be.undefined;
  409. } );
  410. it( 'should return style attribute', () => {
  411. el._setStyle( 'color', 'red' );
  412. el._setStyle( 'top', '10px' );
  413. expect( el.getAttribute( 'style' ) ).to.equal( 'color:red;top:10px;' );
  414. } );
  415. it( 'should return undefined if no style attribute', () => {
  416. expect( el.getAttribute( 'style' ) ).to.be.undefined;
  417. } );
  418. } );
  419. describe( 'getAttributes', () => {
  420. it( 'should return attributes', () => {
  421. el._setAttribute( 'foo', 'bar' );
  422. el._setAttribute( 'abc', 'xyz' );
  423. expect( Array.from( el.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  424. } );
  425. it( 'should return class and style attribute', () => {
  426. el._setAttribute( 'class', 'abc' );
  427. el._setAttribute( 'style', 'width:20px;' );
  428. el._addClass( 'xyz' );
  429. el._setStyle( 'font-weight', 'bold' );
  430. expect( Array.from( el.getAttributes() ) ).to.deep.equal( [
  431. [ 'class', 'abc xyz' ], [ 'style', 'font-weight:bold;width:20px;' ]
  432. ] );
  433. } );
  434. } );
  435. describe( 'hasAttribute', () => {
  436. it( 'should return true if element has attribute', () => {
  437. el._setAttribute( 'foo', 'bar' );
  438. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  439. expect( el.hasAttribute( 'bom' ) ).to.be.false;
  440. } );
  441. it( 'should return true if element has class attribute', () => {
  442. expect( el.hasAttribute( 'class' ) ).to.be.false;
  443. el._addClass( 'foo' );
  444. expect( el.hasAttribute( 'class' ) ).to.be.true;
  445. } );
  446. it( 'should return true if element has style attribute', () => {
  447. expect( el.hasAttribute( 'style' ) ).to.be.false;
  448. el._setStyle( 'border', '1px solid red' );
  449. expect( el.hasAttribute( 'style' ) ).to.be.true;
  450. } );
  451. } );
  452. describe( 'getAttributeKeys', () => {
  453. it( 'should return keys', () => {
  454. el._setAttribute( 'foo', true );
  455. el._setAttribute( 'bar', true );
  456. const expected = [ 'foo', 'bar' ];
  457. let i = 0;
  458. for ( const key of el.getAttributeKeys() ) {
  459. expect( key ).to.equal( expected[ i ] );
  460. i++;
  461. }
  462. expect( i ).to.equal( 2 );
  463. } );
  464. it( 'should return class key', () => {
  465. el._addClass( 'foo' );
  466. el._setAttribute( 'bar', true );
  467. const expected = [ 'class', 'bar' ];
  468. let i = 0;
  469. for ( const key of el.getAttributeKeys() ) {
  470. expect( key ).to.equal( expected[ i ] );
  471. i++;
  472. }
  473. } );
  474. it( 'should return style key', () => {
  475. el._setStyle( 'color', 'black' );
  476. el._setAttribute( 'bar', true );
  477. const expected = [ 'style', 'bar' ];
  478. let i = 0;
  479. for ( const key of el.getAttributeKeys() ) {
  480. expect( key ).to.equal( expected[ i ] );
  481. i++;
  482. }
  483. } );
  484. } );
  485. describe( '_removeAttribute', () => {
  486. it( 'should remove attributes', () => {
  487. el._setAttribute( 'foo', true );
  488. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  489. el._removeAttribute( 'foo' );
  490. expect( el.hasAttribute( 'foo' ) ).to.be.false;
  491. expect( count( el.getAttributeKeys() ) ).to.equal( 0 );
  492. } );
  493. it( 'should fire change event with attributes type', done => {
  494. el._setAttribute( 'foo', 'bar' );
  495. el.once( 'change:attributes', eventInfo => {
  496. expect( eventInfo.source ).to.equal( el );
  497. done();
  498. } );
  499. el._removeAttribute( 'foo' );
  500. } );
  501. it( 'should remove class attribute', () => {
  502. el._addClass( [ 'foo', 'bar' ] );
  503. const el2 = new Element( 'p' );
  504. const removed1 = el._removeAttribute( 'class' );
  505. const removed2 = el2._removeAttribute( 'class' );
  506. expect( el.hasAttribute( 'class' ) ).to.be.false;
  507. expect( el.hasClass( 'foo' ) ).to.be.false;
  508. expect( el.hasClass( 'bar' ) ).to.be.false;
  509. expect( removed1 ).to.be.true;
  510. expect( removed2 ).to.be.false;
  511. } );
  512. it( 'should remove style attribute', () => {
  513. el._setStyle( 'color', 'red' );
  514. el._setStyle( 'position', 'fixed' );
  515. const el2 = new Element( 'p' );
  516. const removed1 = el._removeAttribute( 'style' );
  517. const removed2 = el2._removeAttribute( 'style' );
  518. expect( el.hasAttribute( 'style' ) ).to.be.false;
  519. expect( el.hasStyle( 'color' ) ).to.be.false;
  520. expect( el.hasStyle( 'position' ) ).to.be.false;
  521. expect( removed1 ).to.be.true;
  522. expect( removed2 ).to.be.false;
  523. } );
  524. } );
  525. } );
  526. describe( 'classes manipulation methods', () => {
  527. let el;
  528. beforeEach( () => {
  529. el = new Element( 'p' );
  530. } );
  531. describe( '_addClass()', () => {
  532. it( 'should add single class', () => {
  533. el._addClass( 'one' );
  534. expect( el._classes.has( 'one' ) ).to.be.true;
  535. } );
  536. it( 'should fire change event with attributes type', done => {
  537. el.once( 'change:attributes', eventInfo => {
  538. expect( eventInfo.source ).to.equal( el );
  539. done();
  540. } );
  541. el._addClass( 'one' );
  542. } );
  543. it( 'should add multiple classes', () => {
  544. el._addClass( [ 'one', 'two', 'three' ] );
  545. expect( el._classes.has( 'one' ) ).to.be.true;
  546. expect( el._classes.has( 'two' ) ).to.be.true;
  547. expect( el._classes.has( 'three' ) ).to.be.true;
  548. } );
  549. } );
  550. describe( '_removeClass()', () => {
  551. it( 'should remove single class', () => {
  552. el._addClass( [ 'one', 'two', 'three' ] );
  553. el._removeClass( 'one' );
  554. expect( el._classes.has( 'one' ) ).to.be.false;
  555. expect( el._classes.has( 'two' ) ).to.be.true;
  556. expect( el._classes.has( 'three' ) ).to.be.true;
  557. } );
  558. it( 'should fire change event with attributes type', done => {
  559. el._addClass( 'one' );
  560. el.once( 'change:attributes', eventInfo => {
  561. expect( eventInfo.source ).to.equal( el );
  562. done();
  563. } );
  564. el._removeClass( 'one' );
  565. } );
  566. it( 'should remove multiple classes', () => {
  567. el._addClass( [ 'one', 'two', 'three', 'four' ] );
  568. el._removeClass( [ 'one', 'two', 'three' ] );
  569. expect( el._classes.has( 'one' ) ).to.be.false;
  570. expect( el._classes.has( 'two' ) ).to.be.false;
  571. expect( el._classes.has( 'three' ) ).to.be.false;
  572. expect( el._classes.has( 'four' ) ).to.be.true;
  573. } );
  574. } );
  575. describe( 'hasClass', () => {
  576. it( 'should check if element has a class', () => {
  577. el._addClass( [ 'one', 'two', 'three' ] );
  578. expect( el.hasClass( 'one' ) ).to.be.true;
  579. expect( el.hasClass( 'two' ) ).to.be.true;
  580. expect( el.hasClass( 'three' ) ).to.be.true;
  581. expect( el.hasClass( 'four' ) ).to.be.false;
  582. } );
  583. it( 'should check if element has multiple classes', () => {
  584. el._addClass( [ 'one', 'two', 'three' ] );
  585. expect( el.hasClass( 'one', 'two' ) ).to.be.true;
  586. expect( el.hasClass( 'three', 'two' ) ).to.be.true;
  587. expect( el.hasClass( 'three', 'one', 'two' ) ).to.be.true;
  588. expect( el.hasClass( 'three', 'one', 'two', 'zero' ) ).to.be.false;
  589. } );
  590. } );
  591. describe( 'getClassNames', () => {
  592. it( 'should return iterator with all class names', () => {
  593. const names = [ 'one', 'two', 'three' ];
  594. el._addClass( names );
  595. const iterator = el.getClassNames();
  596. let i = 0;
  597. for ( const name of iterator ) {
  598. expect( name ).to.equal( names[ i++ ] );
  599. }
  600. } );
  601. } );
  602. } );
  603. describe( 'styles manipulation methods', () => {
  604. let el;
  605. beforeEach( () => {
  606. el = new Element( 'p' );
  607. } );
  608. describe( '_setStyle()', () => {
  609. it( 'should set element style', () => {
  610. el._setStyle( 'color', 'red' );
  611. expect( el._styles._styles.color ).to.equal( 'red' );
  612. } );
  613. it( 'should fire change event with attributes type', done => {
  614. el.once( 'change:attributes', eventInfo => {
  615. expect( eventInfo.source ).to.equal( el );
  616. done();
  617. } );
  618. el._setStyle( 'color', 'red' );
  619. } );
  620. it( 'should set multiple styles by providing an object', () => {
  621. el._setStyle( {
  622. color: 'red',
  623. position: 'fixed'
  624. } );
  625. expect( el._styles._styles.color ).to.equal( 'red' );
  626. expect( el._styles._styles.position ).to.equal( 'fixed' );
  627. } );
  628. } );
  629. describe( 'getStyle', () => {
  630. it( 'should get style', () => {
  631. el._setStyle( {
  632. color: 'red',
  633. 'margin-top': '1px'
  634. } );
  635. expect( el.getStyle( 'color' ) ).to.equal( 'red' );
  636. expect( el.getStyle( 'margin-top' ) ).to.equal( '1px' );
  637. } );
  638. } );
  639. describe( 'getStyleNames', () => {
  640. it( 'should return iterator with all style names', () => {
  641. const names = [ 'color', 'position' ];
  642. el._setStyle( {
  643. color: 'red',
  644. position: 'absolute'
  645. } );
  646. const iterator = el.getStyleNames();
  647. let i = 0;
  648. for ( const name of iterator ) {
  649. expect( name ).to.equal( names[ i++ ] );
  650. }
  651. } );
  652. } );
  653. describe( 'hasStyle', () => {
  654. it( 'should check if element has a style', () => {
  655. el._setStyle( 'padding-top', '10px' );
  656. expect( el.hasStyle( 'padding-top' ) ).to.be.true;
  657. expect( el.hasStyle( 'padding-left' ) ).to.be.false;
  658. } );
  659. it( 'should check if element has multiple styles', () => {
  660. el._setStyle( {
  661. 'padding-top': '10px',
  662. 'margin-left': '10px',
  663. 'color': '10px;'
  664. } );
  665. expect( el.hasStyle( 'padding-top', 'margin-left' ) ).to.be.true;
  666. expect( el.hasStyle( 'padding-top', 'margin-left', 'color' ) ).to.be.true;
  667. expect( el.hasStyle( 'padding-top', 'padding-left' ) ).to.be.false;
  668. } );
  669. } );
  670. describe( '_removeStyle()', () => {
  671. it( 'should remove style', () => {
  672. el._setStyle( 'padding-top', '10px' );
  673. el._removeStyle( 'padding-top' );
  674. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  675. } );
  676. it( 'should fire change event with attributes type', done => {
  677. el._setStyle( 'color', 'red' );
  678. el.once( 'change:attributes', eventInfo => {
  679. expect( eventInfo.source ).to.equal( el );
  680. done();
  681. } );
  682. el._removeStyle( 'color' );
  683. } );
  684. it( 'should remove multiple styles', () => {
  685. el._setStyle( {
  686. 'padding-top': '10px',
  687. 'margin-top': '10px',
  688. 'color': 'red'
  689. } );
  690. el._removeStyle( [ 'padding-top', 'margin-top' ] );
  691. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  692. expect( el.hasStyle( 'margin-top' ) ).to.be.false;
  693. expect( el.hasStyle( 'color' ) ).to.be.true;
  694. } );
  695. } );
  696. } );
  697. describe( 'findAncestor', () => {
  698. it( 'should return null if element have no ancestor', () => {
  699. const el = new Element( 'p' );
  700. expect( el.findAncestor( 'div' ) ).to.be.null;
  701. } );
  702. it( 'should return ancestor if matching', () => {
  703. const el1 = new Element( 'p' );
  704. const el2 = new Element( 'div', null, el1 );
  705. expect( el1.findAncestor( 'div' ) ).to.equal( el2 );
  706. } );
  707. it( 'should return parent\'s ancestor if matching', () => {
  708. const el1 = new Element( 'p' );
  709. const el2 = new Element( 'div', null, el1 );
  710. const el3 = new Element( 'div', { class: 'foo bar' }, el2 );
  711. expect( el1.findAncestor( { classes: 'foo' } ) ).to.equal( el3 );
  712. } );
  713. it( 'should return null if no matches found', () => {
  714. const el1 = new Element( 'p' );
  715. new Element( 'div', null, el1 ); // eslint-disable-line no-new
  716. expect( el1.findAncestor( {
  717. name: 'div',
  718. classes: 'container'
  719. } ) ).to.be.null;
  720. } );
  721. } );
  722. describe( 'custom properties', () => {
  723. it( 'should allow to set and get custom properties', () => {
  724. const el = new Element( 'p' );
  725. el._setCustomProperty( 'foo', 'bar' );
  726. expect( el.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
  727. } );
  728. it( 'should allow to add symbol property', () => {
  729. const el = new Element( 'p' );
  730. const symbol = Symbol( 'custom' );
  731. el._setCustomProperty( symbol, 'bar' );
  732. expect( el.getCustomProperty( symbol ) ).to.equal( 'bar' );
  733. } );
  734. it( 'should allow to remove custom property', () => {
  735. const el = new Element( 'foo' );
  736. const symbol = Symbol( 'quix' );
  737. el._setCustomProperty( 'bar', 'baz' );
  738. el._setCustomProperty( symbol, 'test' );
  739. expect( el.getCustomProperty( 'bar' ) ).to.equal( 'baz' );
  740. expect( el.getCustomProperty( symbol ) ).to.equal( 'test' );
  741. el._removeCustomProperty( 'bar' );
  742. el._removeCustomProperty( symbol );
  743. expect( el.getCustomProperty( 'bar' ) ).to.be.undefined;
  744. expect( el.getCustomProperty( symbol ) ).to.be.undefined;
  745. } );
  746. it( 'should allow to iterate over custom properties', () => {
  747. const el = new Element( 'p' );
  748. el._setCustomProperty( 'foo', 1 );
  749. el._setCustomProperty( 'bar', 2 );
  750. el._setCustomProperty( 'baz', 3 );
  751. const properties = [ ...el.getCustomProperties() ];
  752. expect( properties[ 0 ][ 0 ] ).to.equal( 'foo' );
  753. expect( properties[ 0 ][ 1 ] ).to.equal( 1 );
  754. expect( properties[ 1 ][ 0 ] ).to.equal( 'bar' );
  755. expect( properties[ 1 ][ 1 ] ).to.equal( 2 );
  756. expect( properties[ 2 ][ 0 ] ).to.equal( 'baz' );
  757. expect( properties[ 2 ][ 1 ] ).to.equal( 3 );
  758. } );
  759. } );
  760. describe( 'getIdentity()', () => {
  761. it( 'should return only name if no other attributes are present', () => {
  762. const el = new Element( 'foo' );
  763. expect( el.getIdentity() ).to.equal( 'foo' );
  764. } );
  765. it( 'should return classes in sorted order', () => {
  766. const el = new Element( 'fruit' );
  767. el._addClass( [ 'banana', 'lemon', 'apple' ] );
  768. expect( el.getIdentity() ).to.equal( 'fruit class="apple,banana,lemon"' );
  769. } );
  770. it( 'should return styles in sorted order', () => {
  771. const el = new Element( 'foo', {
  772. style: 'margin-top: 2em; background-color: red'
  773. } );
  774. expect( el.getIdentity() ).to.equal( 'foo style="background-color:red;margin-top:2em;"' );
  775. } );
  776. it( 'should return attributes in sorted order', () => {
  777. const el = new Element( 'foo', {
  778. a: 1,
  779. d: 4,
  780. b: 3
  781. } );
  782. expect( el.getIdentity() ).to.equal( 'foo a="1" b="3" d="4"' );
  783. } );
  784. it( 'should return classes, styles and attributes', () => {
  785. const el = new Element( 'baz', {
  786. foo: 'one',
  787. bar: 'two',
  788. style: 'text-align:center;border-radius:10px'
  789. } );
  790. el._addClass( [ 'three', 'two', 'one' ] );
  791. expect( el.getIdentity() ).to.equal(
  792. 'baz class="one,three,two" style="border-radius:10px;text-align:center;" bar="two" foo="one"'
  793. );
  794. } );
  795. } );
  796. } );