element.js 33 KB

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