element.js 33 KB

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