8
0

element.js 33 KB

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