element.js 32 KB

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