8
0

element.js 33 KB

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