element.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. } );
  324. describe( 'getChildIndex', () => {
  325. it( 'should return child index', () => {
  326. parent._appendChild( el1 );
  327. parent._appendChild( el2 );
  328. parent._appendChild( el3 );
  329. expect( parent.childCount ).to.equal( 3 );
  330. expect( parent.getChildIndex( el1 ) ).to.equal( 0 );
  331. expect( parent.getChildIndex( el2 ) ).to.equal( 1 );
  332. expect( parent.getChildIndex( el3 ) ).to.equal( 2 );
  333. } );
  334. } );
  335. describe( 'getChildren', () => {
  336. it( 'should renturn children iterator', () => {
  337. parent._appendChild( el1 );
  338. parent._appendChild( el2 );
  339. parent._appendChild( el3 );
  340. const expected = [ el1, el2, el3 ];
  341. let i = 0;
  342. for ( const child of parent.getChildren() ) {
  343. expect( child ).to.equal( expected[ i ] );
  344. i++;
  345. }
  346. expect( i ).to.equal( 3 );
  347. } );
  348. } );
  349. describe( '_removeChildren', () => {
  350. it( 'should remove children', () => {
  351. parent._appendChild( el1 );
  352. parent._appendChild( el2 );
  353. parent._appendChild( el3 );
  354. parent._appendChild( el4 );
  355. parent._removeChildren( 1, 2 );
  356. expect( parent.childCount ).to.equal( 2 );
  357. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  358. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  359. expect( el1.parent ).to.equal( parent );
  360. expect( el2.parent ).to.be.null;
  361. expect( el3.parent ).to.be.null;
  362. expect( el4.parent ).equal( parent );
  363. } );
  364. it( 'should remove one child when second parameter is not specified', () => {
  365. parent._appendChild( el1 );
  366. parent._appendChild( el2 );
  367. parent._appendChild( el3 );
  368. const removed = parent._removeChildren( 1 );
  369. expect( parent.childCount ).to.equal( 2 );
  370. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  371. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  372. expect( removed.length ).to.equal( 1 );
  373. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  374. } );
  375. } );
  376. } );
  377. describe( 'attributes manipulation methods', () => {
  378. let el;
  379. beforeEach( () => {
  380. el = new Element( document, 'p' );
  381. } );
  382. describe( '_setAttribute', () => {
  383. it( 'should set attribute', () => {
  384. el._setAttribute( 'foo', 'bar' );
  385. expect( el._attrs.has( 'foo' ) ).to.be.true;
  386. expect( el._attrs.get( 'foo' ) ).to.equal( 'bar' );
  387. } );
  388. it( 'should cast attribute value to a string', () => {
  389. el._setAttribute( 'foo', true );
  390. expect( el._attrs.get( 'foo' ) ).to.equal( 'true' );
  391. } );
  392. it( 'should fire change event with attributes type', done => {
  393. el.once( 'change:attributes', eventInfo => {
  394. expect( eventInfo.source ).to.equal( el );
  395. done();
  396. } );
  397. el._setAttribute( 'foo', 'bar' );
  398. } );
  399. it( 'should set class', () => {
  400. el._setAttribute( 'class', 'foo bar' );
  401. expect( el._attrs.has( 'class' ) ).to.be.false;
  402. expect( el._classes.has( 'foo' ) ).to.be.true;
  403. expect( el._classes.has( 'bar' ) ).to.be.true;
  404. } );
  405. it( 'should replace all existing classes', () => {
  406. el._setAttribute( 'class', 'foo bar baz' );
  407. el._setAttribute( 'class', 'qux' );
  408. expect( el._classes.has( 'foo' ) ).to.be.false;
  409. expect( el._classes.has( 'bar' ) ).to.be.false;
  410. expect( el._classes.has( 'baz' ) ).to.be.false;
  411. expect( el._classes.has( 'qux' ) ).to.be.true;
  412. } );
  413. it( 'should replace all styles', () => {
  414. el._setStyle( 'color', 'red' );
  415. el._setStyle( 'top', '10px' );
  416. el._setAttribute( 'style', 'margin-top:2em;' );
  417. expect( el.hasStyle( 'color' ) ).to.be.false;
  418. expect( el.hasStyle( 'top' ) ).to.be.false;
  419. expect( el.hasStyle( 'margin-top' ) ).to.be.true;
  420. expect( el.getStyle( 'margin-top' ) ).to.equal( '2em' );
  421. } );
  422. } );
  423. describe( 'getAttribute', () => {
  424. it( 'should return attribute', () => {
  425. el._setAttribute( 'foo', 'bar' );
  426. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  427. expect( el.getAttribute( 'bom' ) ).to.not.be.ok;
  428. } );
  429. it( 'should return class attribute', () => {
  430. el._addClass( [ 'foo', 'bar' ] );
  431. expect( el.getAttribute( 'class' ) ).to.equal( 'foo bar' );
  432. } );
  433. it( 'should return undefined if no class attribute', () => {
  434. expect( el.getAttribute( 'class' ) ).to.be.undefined;
  435. } );
  436. it( 'should return style attribute', () => {
  437. el._setStyle( 'color', 'red' );
  438. el._setStyle( 'top', '10px' );
  439. expect( el.getAttribute( 'style' ) ).to.equal( 'color:red;top:10px;' );
  440. } );
  441. it( 'should return undefined if no style attribute', () => {
  442. expect( el.getAttribute( 'style' ) ).to.be.undefined;
  443. } );
  444. } );
  445. describe( 'getAttributes', () => {
  446. it( 'should return attributes', () => {
  447. el._setAttribute( 'foo', 'bar' );
  448. el._setAttribute( 'abc', 'xyz' );
  449. expect( Array.from( el.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  450. } );
  451. it( 'should return class and style attribute', () => {
  452. el._setAttribute( 'class', 'abc' );
  453. el._setAttribute( 'style', 'width:20px;' );
  454. el._addClass( 'xyz' );
  455. el._setStyle( 'font-weight', 'bold' );
  456. expect( Array.from( el.getAttributes() ) ).to.deep.equal( [
  457. [ 'class', 'abc xyz' ], [ 'style', 'font-weight:bold;width:20px;' ]
  458. ] );
  459. } );
  460. } );
  461. describe( 'hasAttribute', () => {
  462. it( 'should return true if element has attribute', () => {
  463. el._setAttribute( 'foo', 'bar' );
  464. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  465. expect( el.hasAttribute( 'bom' ) ).to.be.false;
  466. } );
  467. it( 'should return true if element has class attribute', () => {
  468. expect( el.hasAttribute( 'class' ) ).to.be.false;
  469. el._addClass( 'foo' );
  470. expect( el.hasAttribute( 'class' ) ).to.be.true;
  471. } );
  472. it( 'should return true if element has style attribute', () => {
  473. expect( el.hasAttribute( 'style' ) ).to.be.false;
  474. el._setStyle( 'border', '1px solid red' );
  475. expect( el.hasAttribute( 'style' ) ).to.be.true;
  476. } );
  477. } );
  478. describe( 'getAttributeKeys', () => {
  479. it( 'should return keys', () => {
  480. el._setAttribute( 'foo', true );
  481. el._setAttribute( 'bar', true );
  482. const expected = [ 'foo', 'bar' ];
  483. let i = 0;
  484. for ( const key of el.getAttributeKeys() ) {
  485. expect( key ).to.equal( expected[ i ] );
  486. i++;
  487. }
  488. expect( i ).to.equal( 2 );
  489. } );
  490. it( 'should return class key', () => {
  491. el._addClass( 'foo' );
  492. el._setAttribute( 'bar', true );
  493. const expected = [ 'class', 'bar' ];
  494. let i = 0;
  495. for ( const key of el.getAttributeKeys() ) {
  496. expect( key ).to.equal( expected[ i ] );
  497. i++;
  498. }
  499. } );
  500. it( 'should return style key', () => {
  501. el._setStyle( 'color', 'black' );
  502. el._setAttribute( 'bar', true );
  503. const expected = [ 'style', 'bar' ];
  504. let i = 0;
  505. for ( const key of el.getAttributeKeys() ) {
  506. expect( key ).to.equal( expected[ i ] );
  507. i++;
  508. }
  509. } );
  510. } );
  511. describe( '_removeAttribute', () => {
  512. it( 'should remove attributes', () => {
  513. el._setAttribute( 'foo', true );
  514. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  515. el._removeAttribute( 'foo' );
  516. expect( el.hasAttribute( 'foo' ) ).to.be.false;
  517. expect( count( el.getAttributeKeys() ) ).to.equal( 0 );
  518. } );
  519. it( 'should fire change event with attributes type', done => {
  520. el._setAttribute( 'foo', 'bar' );
  521. el.once( 'change:attributes', eventInfo => {
  522. expect( eventInfo.source ).to.equal( el );
  523. done();
  524. } );
  525. el._removeAttribute( 'foo' );
  526. } );
  527. it( 'should remove class attribute', () => {
  528. el._addClass( [ 'foo', 'bar' ] );
  529. const el2 = new Element( document, 'p' );
  530. const removed1 = el._removeAttribute( 'class' );
  531. const removed2 = el2._removeAttribute( 'class' );
  532. expect( el.hasAttribute( 'class' ) ).to.be.false;
  533. expect( el.hasClass( 'foo' ) ).to.be.false;
  534. expect( el.hasClass( 'bar' ) ).to.be.false;
  535. expect( removed1 ).to.be.true;
  536. expect( removed2 ).to.be.false;
  537. } );
  538. it( 'should remove style attribute', () => {
  539. el._setStyle( 'color', 'red' );
  540. el._setStyle( 'position', 'fixed' );
  541. const el2 = new Element( document, 'p' );
  542. const removed1 = el._removeAttribute( 'style' );
  543. const removed2 = el2._removeAttribute( 'style' );
  544. expect( el.hasAttribute( 'style' ) ).to.be.false;
  545. expect( el.hasStyle( 'color' ) ).to.be.false;
  546. expect( el.hasStyle( 'position' ) ).to.be.false;
  547. expect( removed1 ).to.be.true;
  548. expect( removed2 ).to.be.false;
  549. } );
  550. } );
  551. } );
  552. describe( 'classes manipulation methods', () => {
  553. let el;
  554. beforeEach( () => {
  555. el = new Element( document, 'p' );
  556. } );
  557. describe( '_addClass()', () => {
  558. it( 'should add single class', () => {
  559. el._addClass( 'one' );
  560. expect( el._classes.has( 'one' ) ).to.be.true;
  561. } );
  562. it( 'should fire change event with attributes type', done => {
  563. el.once( 'change:attributes', eventInfo => {
  564. expect( eventInfo.source ).to.equal( el );
  565. done();
  566. } );
  567. el._addClass( 'one' );
  568. } );
  569. it( 'should add multiple classes', () => {
  570. el._addClass( [ 'one', 'two', 'three' ] );
  571. expect( el._classes.has( 'one' ) ).to.be.true;
  572. expect( el._classes.has( 'two' ) ).to.be.true;
  573. expect( el._classes.has( 'three' ) ).to.be.true;
  574. } );
  575. } );
  576. describe( '_removeClass()', () => {
  577. it( 'should remove single class', () => {
  578. el._addClass( [ 'one', 'two', 'three' ] );
  579. el._removeClass( 'one' );
  580. expect( el._classes.has( 'one' ) ).to.be.false;
  581. expect( el._classes.has( 'two' ) ).to.be.true;
  582. expect( el._classes.has( 'three' ) ).to.be.true;
  583. } );
  584. it( 'should fire change event with attributes type', done => {
  585. el._addClass( 'one' );
  586. el.once( 'change:attributes', eventInfo => {
  587. expect( eventInfo.source ).to.equal( el );
  588. done();
  589. } );
  590. el._removeClass( 'one' );
  591. } );
  592. it( 'should remove multiple classes', () => {
  593. el._addClass( [ 'one', 'two', 'three', 'four' ] );
  594. el._removeClass( [ 'one', 'two', 'three' ] );
  595. expect( el._classes.has( 'one' ) ).to.be.false;
  596. expect( el._classes.has( 'two' ) ).to.be.false;
  597. expect( el._classes.has( 'three' ) ).to.be.false;
  598. expect( el._classes.has( 'four' ) ).to.be.true;
  599. } );
  600. } );
  601. describe( 'hasClass', () => {
  602. it( 'should check if element has a class', () => {
  603. el._addClass( [ 'one', 'two', 'three' ] );
  604. expect( el.hasClass( 'one' ) ).to.be.true;
  605. expect( el.hasClass( 'two' ) ).to.be.true;
  606. expect( el.hasClass( 'three' ) ).to.be.true;
  607. expect( el.hasClass( 'four' ) ).to.be.false;
  608. } );
  609. it( 'should check if element has multiple classes', () => {
  610. el._addClass( [ 'one', 'two', 'three' ] );
  611. expect( el.hasClass( 'one', 'two' ) ).to.be.true;
  612. expect( el.hasClass( 'three', 'two' ) ).to.be.true;
  613. expect( el.hasClass( 'three', 'one', 'two' ) ).to.be.true;
  614. expect( el.hasClass( 'three', 'one', 'two', 'zero' ) ).to.be.false;
  615. } );
  616. } );
  617. describe( 'getClassNames', () => {
  618. it( 'should return iterator with all class names', () => {
  619. const names = [ 'one', 'two', 'three' ];
  620. el._addClass( names );
  621. const iterator = el.getClassNames();
  622. let i = 0;
  623. for ( const name of iterator ) {
  624. expect( name ).to.equal( names[ i++ ] );
  625. }
  626. } );
  627. } );
  628. } );
  629. describe( 'styles manipulation methods', () => {
  630. let el;
  631. beforeEach( () => {
  632. el = new Element( document, 'p' );
  633. } );
  634. describe( '_setStyle()', () => {
  635. it( 'should set element style', () => {
  636. el._setStyle( 'color', 'red' );
  637. expect( el._styles._styles.color ).to.equal( 'red' );
  638. } );
  639. it( 'should fire change event with attributes type', done => {
  640. el.once( 'change:attributes', eventInfo => {
  641. expect( eventInfo.source ).to.equal( el );
  642. done();
  643. } );
  644. el._setStyle( 'color', 'red' );
  645. } );
  646. it( 'should set multiple styles by providing an object', () => {
  647. el._setStyle( {
  648. color: 'red',
  649. position: 'fixed'
  650. } );
  651. expect( el._styles._styles.color ).to.equal( 'red' );
  652. expect( el._styles._styles.position ).to.equal( 'fixed' );
  653. } );
  654. } );
  655. describe( 'getStyle', () => {
  656. it( 'should get style', () => {
  657. el._setStyle( {
  658. color: 'red',
  659. 'margin-top': '1px'
  660. } );
  661. expect( el.getStyle( 'color' ) ).to.equal( 'red' );
  662. expect( el.getStyle( 'margin-top' ) ).to.equal( '1px' );
  663. } );
  664. } );
  665. describe( 'getNormalizedStyle', () => {
  666. it( 'should get normalized style', () => {
  667. el._setStyle( {
  668. color: 'red',
  669. 'margin-top': '1px'
  670. } );
  671. expect( el.getNormalizedStyle( 'color' ) ).to.equal( 'red' );
  672. expect( el.getNormalizedStyle( 'margin-top' ) ).to.equal( '1px' );
  673. } );
  674. } );
  675. describe( 'getStyleNames', () => {
  676. it( 'should return iterator with all style names', () => {
  677. const names = [ 'color', 'position' ];
  678. el._setStyle( {
  679. color: 'red',
  680. position: 'absolute'
  681. } );
  682. const iterator = el.getStyleNames();
  683. let i = 0;
  684. for ( const name of iterator ) {
  685. expect( name ).to.equal( names[ i++ ] );
  686. }
  687. } );
  688. } );
  689. describe( 'hasStyle', () => {
  690. it( 'should check if element has a style', () => {
  691. el._setStyle( 'padding-top', '10px' );
  692. expect( el.hasStyle( 'padding-top' ) ).to.be.true;
  693. expect( el.hasStyle( 'padding-left' ) ).to.be.false;
  694. } );
  695. it( 'should check if element has multiple styles', () => {
  696. el._setStyle( {
  697. 'padding-top': '10px',
  698. 'margin-left': '10px',
  699. 'color': '10px;'
  700. } );
  701. expect( el.hasStyle( 'padding-top', 'margin-left' ) ).to.be.true;
  702. expect( el.hasStyle( 'padding-top', 'margin-left', 'color' ) ).to.be.true;
  703. expect( el.hasStyle( 'padding-top', 'padding-left' ) ).to.be.false;
  704. } );
  705. } );
  706. describe( '_removeStyle()', () => {
  707. it( 'should remove style', () => {
  708. el._setStyle( 'padding-top', '10px' );
  709. el._removeStyle( 'padding-top' );
  710. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  711. } );
  712. it( 'should fire change event with attributes type', done => {
  713. el._setStyle( 'color', 'red' );
  714. el.once( 'change:attributes', eventInfo => {
  715. expect( eventInfo.source ).to.equal( el );
  716. done();
  717. } );
  718. el._removeStyle( 'color' );
  719. } );
  720. it( 'should remove multiple styles', () => {
  721. el._setStyle( {
  722. 'padding-top': '10px',
  723. 'margin-top': '10px',
  724. 'color': 'red'
  725. } );
  726. el._removeStyle( [ 'padding-top', 'margin-top' ] );
  727. expect( el.hasStyle( 'padding-top' ) ).to.be.false;
  728. expect( el.hasStyle( 'margin-top' ) ).to.be.false;
  729. expect( el.hasStyle( 'color' ) ).to.be.true;
  730. } );
  731. } );
  732. } );
  733. describe( 'findAncestor', () => {
  734. it( 'should return null if element have no ancestor', () => {
  735. const el = new Element( document, 'p' );
  736. expect( el.findAncestor( 'div' ) ).to.be.null;
  737. } );
  738. it( 'should return ancestor if matching', () => {
  739. const el1 = new Element( document, 'p' );
  740. const el2 = new Element( document, 'div', null, el1 );
  741. expect( el1.findAncestor( 'div' ) ).to.equal( el2 );
  742. } );
  743. it( 'should return parent\'s ancestor if matching', () => {
  744. const el1 = new Element( document, 'p' );
  745. const el2 = new Element( document, 'div', null, el1 );
  746. const el3 = new Element( document, 'div', { class: 'foo bar' }, el2 );
  747. expect( el1.findAncestor( { classes: 'foo' } ) ).to.equal( el3 );
  748. } );
  749. it( 'should return null if no matches found', () => {
  750. const el1 = new Element( document, 'p' );
  751. new Element( document, 'div', null, el1 ); // eslint-disable-line no-new
  752. expect( el1.findAncestor( {
  753. name: 'div',
  754. classes: 'container'
  755. } ) ).to.be.null;
  756. } );
  757. } );
  758. describe( 'custom properties', () => {
  759. it( 'should allow to set and get custom properties', () => {
  760. const el = new Element( document, 'p' );
  761. el._setCustomProperty( 'foo', 'bar' );
  762. expect( el.getCustomProperty( 'foo' ) ).to.equal( 'bar' );
  763. } );
  764. it( 'should allow to add symbol property', () => {
  765. const el = new Element( document, 'p' );
  766. const symbol = Symbol( 'custom' );
  767. el._setCustomProperty( symbol, 'bar' );
  768. expect( el.getCustomProperty( symbol ) ).to.equal( 'bar' );
  769. } );
  770. it( 'should allow to remove custom property', () => {
  771. const el = new Element( document, 'foo' );
  772. const symbol = Symbol( 'quix' );
  773. el._setCustomProperty( 'bar', 'baz' );
  774. el._setCustomProperty( symbol, 'test' );
  775. expect( el.getCustomProperty( 'bar' ) ).to.equal( 'baz' );
  776. expect( el.getCustomProperty( symbol ) ).to.equal( 'test' );
  777. el._removeCustomProperty( 'bar' );
  778. el._removeCustomProperty( symbol );
  779. expect( el.getCustomProperty( 'bar' ) ).to.be.undefined;
  780. expect( el.getCustomProperty( symbol ) ).to.be.undefined;
  781. } );
  782. it( 'should allow to iterate over custom properties', () => {
  783. const el = new Element( document, 'p' );
  784. el._setCustomProperty( 'foo', 1 );
  785. el._setCustomProperty( 'bar', 2 );
  786. el._setCustomProperty( 'baz', 3 );
  787. const properties = [ ...el.getCustomProperties() ];
  788. expect( properties[ 0 ][ 0 ] ).to.equal( 'foo' );
  789. expect( properties[ 0 ][ 1 ] ).to.equal( 1 );
  790. expect( properties[ 1 ][ 0 ] ).to.equal( 'bar' );
  791. expect( properties[ 1 ][ 1 ] ).to.equal( 2 );
  792. expect( properties[ 2 ][ 0 ] ).to.equal( 'baz' );
  793. expect( properties[ 2 ][ 1 ] ).to.equal( 3 );
  794. } );
  795. } );
  796. describe( 'getIdentity()', () => {
  797. it( 'should return only name if no other attributes are present', () => {
  798. const el = new Element( document, 'foo' );
  799. expect( el.getIdentity() ).to.equal( 'foo' );
  800. } );
  801. it( 'should return classes in sorted order', () => {
  802. const el = new Element( document, 'fruit' );
  803. el._addClass( [ 'banana', 'lemon', 'apple' ] );
  804. expect( el.getIdentity() ).to.equal( 'fruit class="apple,banana,lemon"' );
  805. } );
  806. it( 'should return styles in sorted order', () => {
  807. const el = new Element( document, 'foo', {
  808. style: 'margin-top: 2em; background-color: red'
  809. } );
  810. expect( el.getIdentity() ).to.equal( 'foo style="background-color:red;margin-top:2em;"' );
  811. } );
  812. it( 'should return attributes in sorted order', () => {
  813. const el = new Element( document, 'foo', {
  814. a: 1,
  815. d: 4,
  816. b: 3
  817. } );
  818. expect( el.getIdentity() ).to.equal( 'foo a="1" b="3" d="4"' );
  819. } );
  820. it( 'should return classes, styles and attributes', () => {
  821. const el = new Element( document, 'baz', {
  822. foo: 'one',
  823. bar: 'two',
  824. style: 'text-align:center;border-radius:10px'
  825. } );
  826. el._addClass( [ 'three', 'two', 'one' ] );
  827. expect( el.getIdentity() ).to.equal(
  828. 'baz class="one,three,two" style="border-radius:10px;text-align:center;" bar="two" foo="one"'
  829. );
  830. } );
  831. } );
  832. } );