element.js 33 KB

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