8
0

element.js 30 KB

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