element.js 33 KB

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