element.js 33 KB

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