8
0

element.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 cast attribute value to a string', () => {
  348. el.setAttribute( 'foo', true );
  349. expect( el._attrs.get( 'foo' ) ).to.equal( 'true' );
  350. } );
  351. it( 'should fire change event with attributes type', done => {
  352. el.once( 'change:attributes', eventInfo => {
  353. expect( eventInfo.source ).to.equal( el );
  354. done();
  355. } );
  356. el.setAttribute( 'foo', 'bar' );
  357. } );
  358. it( 'should set class', () => {
  359. el.setAttribute( 'class', 'foo bar' );
  360. expect( el._attrs.has( 'class' ) ).to.be.false;
  361. expect( el._classes.has( 'foo' ) ).to.be.true;
  362. expect( el._classes.has( 'bar' ) ).to.be.true;
  363. } );
  364. it( 'should replace all existing classes', () => {
  365. el.setAttribute( 'class', 'foo bar baz' );
  366. el.setAttribute( 'class', 'qux' );
  367. expect( el._classes.has( 'foo' ) ).to.be.false;
  368. expect( el._classes.has( 'bar' ) ).to.be.false;
  369. expect( el._classes.has( 'baz' ) ).to.be.false;
  370. expect( el._classes.has( 'qux' ) ).to.be.true;
  371. } );
  372. it( 'should replace all styles', () => {
  373. el.setStyle( 'color', 'red' );
  374. el.setStyle( 'top', '10px' );
  375. el.setAttribute( 'style', 'border:none' );
  376. expect( el.hasStyle( 'color' ) ).to.be.false;
  377. expect( el.hasStyle( 'top' ) ).to.be.false;
  378. expect( el.hasStyle( 'border' ) ).to.be.true;
  379. expect( el.getStyle( 'border' ) ).to.equal( 'none' );
  380. } );
  381. } );
  382. describe( 'getAttribute', () => {
  383. it( 'should return attribute', () => {
  384. el.setAttribute( 'foo', 'bar' );
  385. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  386. expect( el.getAttribute( 'bom' ) ).to.not.be.ok;
  387. } );
  388. it( 'should return class attribute', () => {
  389. el.addClass( 'foo', 'bar' );
  390. expect( el.getAttribute( 'class' ) ).to.equal( 'foo bar' );
  391. } );
  392. it( 'should return undefined if no class attribute', () => {
  393. expect( el.getAttribute( 'class' ) ).to.be.undefined;
  394. } );
  395. it( 'should return style attribute', () => {
  396. el.setStyle( 'color', 'red' );
  397. el.setStyle( 'top', '10px' );
  398. expect( el.getAttribute( 'style' ) ).to.equal( 'color:red;top:10px;' );
  399. } );
  400. it( 'should return undefined if no style attribute', () => {
  401. expect( el.getAttribute( 'style' ) ).to.be.undefined;
  402. } );
  403. } );
  404. describe( 'getAttributes', () => {
  405. it( 'should return attributes', () => {
  406. el.setAttribute( 'foo', 'bar' );
  407. el.setAttribute( 'abc', 'xyz' );
  408. expect( Array.from( el.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  409. } );
  410. it( 'should return class and style attribute', () => {
  411. el.setAttribute( 'class', 'abc' );
  412. el.setAttribute( 'style', 'width:20px;' );
  413. el.addClass( 'xyz' );
  414. el.setStyle( 'font-weight', 'bold' );
  415. expect( Array.from( el.getAttributes() ) ).to.deep.equal( [
  416. [ 'class', 'abc xyz' ], [ 'style', 'width:20px;font-weight:bold;' ]
  417. ] );
  418. } );
  419. } );
  420. describe( 'hasAttribute', () => {
  421. it( 'should return true if element has attribute', () => {
  422. el.setAttribute( 'foo', 'bar' );
  423. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  424. expect( el.hasAttribute( 'bom' ) ).to.be.false;
  425. } );
  426. it( 'should return true if element has class attribute', () => {
  427. expect( el.hasAttribute( 'class' ) ).to.be.false;
  428. el.addClass( 'foo' );
  429. expect( el.hasAttribute( 'class' ) ).to.be.true;
  430. } );
  431. it( 'should return true if element has style attribute', () => {
  432. expect( el.hasAttribute( 'style' ) ).to.be.false;
  433. el.setStyle( 'border', '1px solid red' );
  434. expect( el.hasAttribute( 'style' ) ).to.be.true;
  435. } );
  436. } );
  437. describe( 'getAttributeKeys', () => {
  438. it( 'should return keys', () => {
  439. el.setAttribute( 'foo', true );
  440. el.setAttribute( 'bar', true );
  441. const expected = [ 'foo', 'bar' ];
  442. let i = 0;
  443. for ( const key of el.getAttributeKeys() ) {
  444. expect( key ).to.equal( expected[ i ] );
  445. i++;
  446. }
  447. expect( i ).to.equal( 2 );
  448. } );
  449. it( 'should return class key', () => {
  450. el.addClass( 'foo' );
  451. el.setAttribute( 'bar', true );
  452. const expected = [ 'class', 'bar' ];
  453. let i = 0;
  454. for ( const key of el.getAttributeKeys() ) {
  455. expect( key ).to.equal( expected[ i ] );
  456. i++;
  457. }
  458. } );
  459. it( 'should return style key', () => {
  460. el.setStyle( 'color', 'black' );
  461. el.setAttribute( 'bar', true );
  462. const expected = [ 'style', 'bar' ];
  463. let i = 0;
  464. for ( const key of el.getAttributeKeys() ) {
  465. expect( key ).to.equal( expected[ i ] );
  466. i++;
  467. }
  468. } );
  469. } );
  470. describe( 'removeAttribute', () => {
  471. it( 'should remove attributes', () => {
  472. el.setAttribute( 'foo', true );
  473. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  474. el.removeAttribute( 'foo' );
  475. expect( el.hasAttribute( 'foo' ) ).to.be.false;
  476. expect( count( el.getAttributeKeys() ) ).to.equal( 0 );
  477. } );
  478. it( 'should fire change event with attributes type', done => {
  479. el.setAttribute( 'foo', 'bar' );
  480. el.once( 'change:attributes', eventInfo => {
  481. expect( eventInfo.source ).to.equal( el );
  482. done();
  483. } );
  484. el.removeAttribute( 'foo' );
  485. } );
  486. it( 'should remove class attribute', () => {
  487. el.addClass( 'foo', 'bar' );
  488. const el2 = new Element( 'p' );
  489. const removed1 = el.removeAttribute( 'class' );
  490. const removed2 = el2.removeAttribute( 'class' );
  491. expect( el.hasAttribute( 'class' ) ).to.be.false;
  492. expect( el.hasClass( 'foo' ) ).to.be.false;
  493. expect( el.hasClass( 'bar' ) ).to.be.false;
  494. expect( removed1 ).to.be.true;
  495. expect( removed2 ).to.be.false;
  496. } );
  497. it( 'should remove style attribute', () => {
  498. el.setStyle( 'color', 'red' );
  499. el.setStyle( 'position', 'fixed' );
  500. const el2 = new Element( 'p' );
  501. const removed1 = el.removeAttribute( 'style' );
  502. const removed2 = el2.removeAttribute( 'style' );
  503. expect( el.hasAttribute( 'style' ) ).to.be.false;
  504. expect( el.hasStyle( 'color' ) ).to.be.false;
  505. expect( el.hasStyle( 'position' ) ).to.be.false;
  506. expect( removed1 ).to.be.true;
  507. expect( removed2 ).to.be.false;
  508. } );
  509. } );
  510. } );
  511. describe( 'classes manipulation methods', () => {
  512. let el;
  513. beforeEach( () => {
  514. el = new Element( 'p' );
  515. } );
  516. describe( 'addClass', () => {
  517. it( 'should add single class', () => {
  518. el.addClass( 'one' );
  519. expect( el._classes.has( 'one' ) ).to.be.true;
  520. } );
  521. it( 'should fire change event with attributes type', done => {
  522. el.once( 'change:attributes', eventInfo => {
  523. expect( eventInfo.source ).to.equal( el );
  524. done();
  525. } );
  526. el.addClass( 'one' );
  527. } );
  528. it( 'should add multiple classes', () => {
  529. el.addClass( 'one', 'two', 'three' );
  530. expect( el._classes.has( 'one' ) ).to.be.true;
  531. expect( el._classes.has( 'two' ) ).to.be.true;
  532. expect( el._classes.has( 'three' ) ).to.be.true;
  533. } );
  534. } );
  535. describe( 'removeClass', () => {
  536. it( 'should remove single class', () => {
  537. el.addClass( 'one', 'two', 'three' );
  538. el.removeClass( 'one' );
  539. expect( el._classes.has( 'one' ) ).to.be.false;
  540. expect( el._classes.has( 'two' ) ).to.be.true;
  541. expect( el._classes.has( 'three' ) ).to.be.true;
  542. } );
  543. it( 'should fire change event with attributes type', done => {
  544. el.addClass( 'one' );
  545. el.once( 'change:attributes', eventInfo => {
  546. expect( eventInfo.source ).to.equal( el );
  547. done();
  548. } );
  549. el.removeClass( 'one' );
  550. } );
  551. it( 'should remove multiple classes', () => {
  552. el.addClass( 'one', 'two', 'three', 'four' );
  553. el.removeClass( 'one', 'two', 'three' );
  554. expect( el._classes.has( 'one' ) ).to.be.false;
  555. expect( el._classes.has( 'two' ) ).to.be.false;
  556. expect( el._classes.has( 'three' ) ).to.be.false;
  557. expect( el._classes.has( 'four' ) ).to.be.true;
  558. } );
  559. } );
  560. describe( 'hasClass', () => {
  561. it( 'should check if element has a class', () => {
  562. el.addClass( 'one', 'two', 'three' );
  563. expect( el.hasClass( 'one' ) ).to.be.true;
  564. expect( el.hasClass( 'two' ) ).to.be.true;
  565. expect( el.hasClass( 'three' ) ).to.be.true;
  566. expect( el.hasClass( 'four' ) ).to.be.false;
  567. } );
  568. it( 'should check if element has multiple classes', () => {
  569. el.addClass( 'one', 'two', 'three' );
  570. expect( el.hasClass( 'one', 'two' ) ).to.be.true;
  571. expect( el.hasClass( 'three', 'two' ) ).to.be.true;
  572. expect( el.hasClass( 'three', 'one', 'two' ) ).to.be.true;
  573. expect( el.hasClass( 'three', 'one', 'two', 'zero' ) ).to.be.false;
  574. } );
  575. } );
  576. describe( 'getClassNames', () => {
  577. it( 'should return iterator with all class names', () => {
  578. const names = [ 'one', 'two', 'three' ];
  579. el.addClass( ...names );
  580. const iterator = el.getClassNames();
  581. let i = 0;
  582. for ( const name of iterator ) {
  583. expect( name ).to.equal( names[ i++ ] );
  584. }
  585. } );
  586. } );
  587. } );
  588. describe( 'styles manipulation methods', () => {
  589. let el;
  590. beforeEach( () => {
  591. el = new Element( 'p' );
  592. } );
  593. describe( 'setStyle', () => {
  594. it( 'should set element style', () => {
  595. el.setStyle( 'color', 'red' );
  596. expect( el._styles.has( 'color' ) ).to.be.true;
  597. expect( el._styles.get( 'color' ) ).to.equal( 'red' );
  598. } );
  599. it( 'should fire change event with attributes type', done => {
  600. el.once( 'change:attributes', eventInfo => {
  601. expect( eventInfo.source ).to.equal( el );
  602. done();
  603. } );
  604. el.setStyle( 'color', 'red' );
  605. } );
  606. it( 'should set multiple styles by providing an object', () => {
  607. el.setStyle( {
  608. color: 'red',
  609. position: 'fixed'
  610. } );
  611. expect( el._styles.has( 'color' ) ).to.be.true;
  612. expect( el._styles.has( 'position' ) ).to.be.true;
  613. expect( el._styles.get( 'color' ) ).to.equal( 'red' );
  614. expect( el._styles.get( 'position' ) ).to.equal( 'fixed' );
  615. } );
  616. } );
  617. describe( 'getStyle', () => {
  618. it( 'should get style', () => {
  619. el.setStyle( {
  620. color: 'red',
  621. border: '1px solid red'
  622. } );
  623. expect( el.getStyle( 'color' ) ).to.equal( 'red' );
  624. expect( el.getStyle( 'border' ) ).to.equal( '1px solid red' );
  625. } );
  626. } );
  627. describe( 'getStyleNames', () => {
  628. it( 'should return iterator with all style names', () => {
  629. const names = [ 'color', 'position' ];
  630. el.setStyle( {
  631. color: 'red',
  632. position: 'absolute'
  633. } );
  634. const iterator = el.getStyleNames();
  635. let i = 0;
  636. for ( const name of iterator ) {
  637. expect( name ).to.equal( names[ i++ ] );
  638. }
  639. } );
  640. } );
  641. describe( 'hasStyle', () => {
  642. it( 'should check if element has a style', () => {
  643. el.setStyle( 'padding-top', '10px' );
  644. expect( el.hasStyle( 'padding-top' ) ).to.be.true;
  645. expect( el.hasStyle( 'padding-left' ) ).to.be.false;
  646. } );
  647. it( 'should check if element has multiple styles', () => {
  648. el.setStyle( {
  649. 'padding-top': '10px',
  650. 'margin-left': '10px',
  651. 'color': '10px;'
  652. } );
  653. expect( el.hasStyle( 'padding-top', 'margin-left' ) ).to.be.true;
  654. expect( el.hasStyle( 'padding-top', 'margin-left', 'color' ) ).to.be.true;
  655. expect( el.hasStyle( 'padding-top', 'padding-left' ) ).to.be.false;
  656. } );
  657. } );
  658. describe( 'removeStyle', () => {
  659. it( 'should remove style', () => {
  660. el.setStyle( 'padding-top', '10px' );
  661. el.removeStyle( 'padding-top' );
  662. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  663. } );
  664. it( 'should fire change event with attributes type', done => {
  665. el.setStyle( 'color', 'red' );
  666. el.once( 'change:attributes', eventInfo => {
  667. expect( eventInfo.source ).to.equal( el );
  668. done();
  669. } );
  670. el.removeStyle( 'color' );
  671. } );
  672. it( 'should remove multiple styles', () => {
  673. el.setStyle( {
  674. 'padding-top': '10px',
  675. 'margin-top': '10px',
  676. 'color': 'red'
  677. } );
  678. el.removeStyle( 'padding-top', 'margin-top' );
  679. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  680. expect( el.hasStyle( 'margin-top' ) ).to.be.false;
  681. expect( el.hasStyle( 'color' ) ).to.be.true;
  682. } );
  683. } );
  684. describe( 'styles parsing edge cases and incorrect styles', () => {
  685. it( 'should not crash and not add any styles if styles attribute was empty', () => {
  686. const element = new Element( 'div', { style: '' } );
  687. const styles = Array.from( element.getStyleNames() );
  688. expect( styles ).to.deep.equal( [] );
  689. } );
  690. it( 'should be able to parse big styles definition', () => {
  691. expect( () => {
  692. // eslint-disable-next-line no-new
  693. new Element( 'div', { style: `background-image:url('data:image/jpeg;base64,${ encodedImage }')` } );
  694. } ).not.to.throw();
  695. } );
  696. it( 'should work with both types of quotes and ignore values inside quotes', () => {
  697. let element;
  698. element = new Element( 'div', { style: 'background-image:url("im;color:g.jpg")' } );
  699. expect( element.getStyle( 'background-image' ) ).to.equal( 'url("im;color:g.jpg")' );
  700. element = new Element( 'div', { style: 'background-image:url(\'im;color:g.jpg\')' } );
  701. expect( element.getStyle( 'background-image' ) ).to.equal( 'url(\'im;color:g.jpg\')' );
  702. } );
  703. it( 'should not be confused by whitespaces', () => {
  704. const element = new Element( 'div', { style: '\ncolor:\n red ' } );
  705. expect( element.getStyle( 'color' ) ).to.equal( 'red' );
  706. } );
  707. it( 'should not be confused by duplicated semicolon', () => {
  708. const element = new Element( 'div', { style: 'color: red;; display: inline' } );
  709. expect( element.getStyle( 'color' ) ).to.equal( 'red' );
  710. expect( element.getStyle( 'display' ) ).to.equal( 'inline' );
  711. } );
  712. it( 'should not throw when value is missing', () => {
  713. const element = new Element( 'div', { style: 'color:; display: inline' } );
  714. expect( element.getStyle( 'color' ) ).to.equal( '' );
  715. expect( element.getStyle( 'display' ) ).to.equal( 'inline' );
  716. } );
  717. it( 'should not throw when colon is duplicated', () => {
  718. const element = new Element( 'div', { style: 'color:: red; display: inline' } );
  719. // The result makes no sense, but here we just check that the algorithm doesn't crash.
  720. expect( element.getStyle( 'color' ) ).to.equal( ': red' );
  721. expect( element.getStyle( 'display' ) ).to.equal( 'inline' );
  722. } );
  723. it( 'should not throw when random stuff passed', () => {
  724. const element = new Element( 'div', { style: 'color: red;:; ;;" ": display: inline; \'aaa;:' } );
  725. // The result makes no sense, but here we just check that the algorithm doesn't crash.
  726. expect( element.getStyle( 'color' ) ).to.equal( 'red' );
  727. expect( element.getStyle( 'display' ) ).to.be.undefined;
  728. } );
  729. } );
  730. } );
  731. describe( 'findAncestor', () => {
  732. it( 'should return null if element have no ancestor', () => {
  733. const el = new Element( 'p' );
  734. expect( el.findAncestor( 'div' ) ).to.be.null;
  735. } );
  736. it( 'should return ancestor if matching', () => {
  737. const el1 = new Element( 'p' );
  738. const el2 = new Element( 'div', null, el1 );
  739. expect( el1.findAncestor( 'div' ) ).to.equal( el2 );
  740. } );
  741. it( 'should return parent\'s ancestor if matching', () => {
  742. const el1 = new Element( 'p' );
  743. const el2 = new Element( 'div', null, el1 );
  744. const el3 = new Element( 'div', { class: 'foo bar' }, el2 );
  745. expect( el1.findAncestor( { class: 'foo' } ) ).to.equal( el3 );
  746. } );
  747. it( 'should return null if no matches found', () => {
  748. const el1 = new Element( 'p' );
  749. new Element( 'div', null, el1 ); // eslint-disable-line no-new
  750. expect( el1.findAncestor( {
  751. name: 'div',
  752. class: 'container'
  753. } ) ).to.be.null;
  754. } );
  755. } );
  756. describe( 'custom properties', () => {
  757. it( 'should allow to set and get custom properties', () => {
  758. const el = new Element( 'p' );
  759. el.setCustomProperty( 'foo', 'bar' );
  760. expect( el.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
  761. } );
  762. it( 'should allow to add symbol property', () => {
  763. const el = new Element( 'p' );
  764. const symbol = Symbol( 'custom' );
  765. el.setCustomProperty( symbol, 'bar' );
  766. expect( el.getCustomProperty( symbol ) ).to.equal( 'bar' );
  767. } );
  768. it( 'should allow to remove custom property', () => {
  769. const el = new Element( 'foo' );
  770. const symbol = Symbol( 'quix' );
  771. el.setCustomProperty( 'bar', 'baz' );
  772. el.setCustomProperty( symbol, 'test' );
  773. expect( el.getCustomProperty( 'bar' ) ).to.equal( 'baz' );
  774. expect( el.getCustomProperty( symbol ) ).to.equal( 'test' );
  775. el.removeCustomProperty( 'bar' );
  776. el.removeCustomProperty( symbol );
  777. expect( el.getCustomProperty( 'bar' ) ).to.be.undefined;
  778. expect( el.getCustomProperty( symbol ) ).to.be.undefined;
  779. } );
  780. it( 'should allow to iterate over custom properties', () => {
  781. const el = new Element( 'p' );
  782. el.setCustomProperty( 'foo', 1 );
  783. el.setCustomProperty( 'bar', 2 );
  784. el.setCustomProperty( 'baz', 3 );
  785. const properties = [ ...el.getCustomProperties() ];
  786. expect( properties[ 0 ][ 0 ] ).to.equal( 'foo' );
  787. expect( properties[ 0 ][ 1 ] ).to.equal( 1 );
  788. expect( properties[ 1 ][ 0 ] ).to.equal( 'bar' );
  789. expect( properties[ 1 ][ 1 ] ).to.equal( 2 );
  790. expect( properties[ 2 ][ 0 ] ).to.equal( 'baz' );
  791. expect( properties[ 2 ][ 1 ] ).to.equal( 3 );
  792. } );
  793. } );
  794. describe( 'getIdentity()', () => {
  795. it( 'should return only name if no other attributes are present', () => {
  796. const el = new Element( 'foo' );
  797. expect( el.getIdentity() ).to.equal( 'foo' );
  798. } );
  799. it( 'should return classes in sorted order', () => {
  800. const el = new Element( 'fruit' );
  801. el.addClass( 'banana', 'lemon', 'apple' );
  802. expect( el.getIdentity() ).to.equal( 'fruit class="apple,banana,lemon"' );
  803. } );
  804. it( 'should return styles in sorted order', () => {
  805. const el = new Element( 'foo', {
  806. style: 'border: 1px solid red; background-color: red'
  807. } );
  808. expect( el.getIdentity() ).to.equal( 'foo style="background-color:red;border:1px solid red"' );
  809. } );
  810. it( 'should return attributes in sorted order', () => {
  811. const el = new Element( 'foo', {
  812. a: 1,
  813. d: 4,
  814. b: 3
  815. } );
  816. expect( el.getIdentity() ).to.equal( 'foo a="1" b="3" d="4"' );
  817. } );
  818. it( 'should return classes, styles and attributes', () => {
  819. const el = new Element( 'baz', {
  820. foo: 'one',
  821. bar: 'two',
  822. style: 'text-align:center;border-radius:10px'
  823. } );
  824. el.addClass( 'three', 'two', 'one' );
  825. expect( el.getIdentity() ).to.equal(
  826. 'baz class="one,three,two" style="border-radius:10px;text-align:center" bar="two" foo="one"'
  827. );
  828. } );
  829. } );
  830. } );