8
0

element.js 33 KB

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