element.js 34 KB

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