element.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import utils from '/ckeditor5/utils/utils.js';
  8. import Node from '/ckeditor5/core/treeview/node.js';
  9. import ViewElement from '/ckeditor5/core/treeview/element.js';
  10. describe( 'Element', () => {
  11. describe( 'constructor', () => {
  12. it( 'should create element without attributes', () => {
  13. const el = new ViewElement( 'p' );
  14. expect( el ).to.be.an.instanceof( Node );
  15. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  16. expect( el ).to.have.property( 'parent' ).that.is.null;
  17. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 0 );
  18. } );
  19. it( 'should create element with attributes as plain object', () => {
  20. const el = new ViewElement( 'p', { foo: 'bar' } );
  21. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  22. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 1 );
  23. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  24. } );
  25. it( 'should create element with attributes as map', () => {
  26. const attrs = new Map();
  27. attrs.set( 'foo', 'bar' );
  28. const el = new ViewElement( 'p', attrs );
  29. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  30. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 1 );
  31. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  32. } );
  33. it( 'should create element with children', () => {
  34. const child = new ViewElement( 'p', { foo: 'bar' } );
  35. const parent = new ViewElement( 'div', [], [ child ] );
  36. expect( parent ).to.have.property( 'name' ).that.equals( 'div' );
  37. expect( parent.getChildCount() ).to.equal( 1 );
  38. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  39. } );
  40. it( 'should move class attribute to class set ', () => {
  41. const el = new ViewElement( 'p', { id: 'test', class: 'one two three' } );
  42. expect( el._attrs.has( 'class' ) ).to.be.false;
  43. expect( el._attrs.has( 'id' ) ).to.be.true;
  44. expect( el._classes.has( 'one' ) ).to.be.true;
  45. expect( el._classes.has( 'two' ) ).to.be.true;
  46. expect( el._classes.has( 'three' ) ).to.be.true;
  47. } );
  48. } );
  49. describe( 'clone', () => {
  50. it( 'should clone element', () => {
  51. const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' } );
  52. const clone = el.clone();
  53. expect( clone ).to.not.equal( el );
  54. expect( clone.name ).to.equal( el.name );
  55. expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
  56. expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
  57. } );
  58. it( 'should deeply clone element', () => {
  59. const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' }, [
  60. new ViewElement( 'b', { attr: 'baz' } ),
  61. new ViewElement( 'span', { attr: 'qux' } )
  62. ] );
  63. const count = el.getChildCount();
  64. const clone = el.clone( true );
  65. expect( clone ).to.not.equal( el );
  66. expect( clone.name ).to.equal( el.name );
  67. expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
  68. expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
  69. expect( clone.getChildCount() ).to.equal( count );
  70. for ( let i = 0; i < count; i++ ) {
  71. const child = el.getChild( i );
  72. const clonedChild = clone.getChild( i );
  73. expect( clonedChild ).to.not.equal( child );
  74. expect( clonedChild.name ).to.equal( child.name );
  75. expect( clonedChild.getAttribute( 'attr' ) ).to.equal( child.getAttribute( 'attr' ) );
  76. }
  77. } );
  78. it( 'shouldn\'t clone any children when deep copy is not performed', () => {
  79. const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' }, [
  80. new ViewElement( 'b', { attr: 'baz' } ),
  81. new ViewElement( 'span', { attr: 'qux' } )
  82. ] );
  83. const clone = el.clone( false );
  84. expect( clone ).to.not.equal( el );
  85. expect( clone.name ).to.equal( el.name );
  86. expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
  87. expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
  88. expect( clone.getChildCount() ).to.equal( 0 );
  89. } );
  90. it( 'should clone when with class attribute', () => {
  91. const el = new ViewElement( 'p', { foo: 'bar' } );
  92. el.addClass( 'baz', 'qux' );
  93. const clone = el.clone( false );
  94. expect( clone ).to.not.equal( el );
  95. expect( clone.name ).to.equal( el.name );
  96. expect( clone.getAttribute( 'foo' ) ).to.equal( 'bar' );
  97. expect( clone.getAttribute( 'class' ) ).to.equal( 'baz qux' );
  98. } );
  99. } );
  100. describe( 'isSimilar', () => {
  101. const el = new ViewElement( 'p', { foo: 'bar' } );
  102. it( 'should return false when comparing to non-element', () => {
  103. expect( el.isSimilar( null ) ).to.be.false;
  104. expect( el.isSimilar( {} ) ).to.be.false;
  105. } );
  106. it( 'should return true when the same node is provided', () => {
  107. expect( el.isSimilar( el ) ).to.be.true;
  108. } );
  109. it( 'should return true for element with same attributes and name', () => {
  110. const other = new ViewElement( 'p', { foo: 'bar' } );
  111. expect( el.isSimilar( other ) ).to.be.true;
  112. } );
  113. it( 'sould return false when name is not the same', () => {
  114. const other = el.clone();
  115. other.name = 'div';
  116. expect( el.isSimilar( other ) ).to.be.false;
  117. } );
  118. it( 'should return false when attributes are not the same', () => {
  119. const other1 = el.clone();
  120. const other2 = el.clone();
  121. const other3 = el.clone();
  122. other1.setAttribute( 'baz', 'qux' );
  123. other2.setAttribute( 'foo', 'not-bar' );
  124. other3.removeAttribute( 'foo' );
  125. expect( el.isSimilar( other1 ) ).to.be.false;
  126. expect( el.isSimilar( other2 ) ).to.be.false;
  127. expect( el.isSimilar( other3 ) ).to.be.false;
  128. } );
  129. it( 'should compare class attribute', () => {
  130. const el1 = new ViewElement( 'p' );
  131. const el2 = new ViewElement( 'p' );
  132. const el3 = new ViewElement( 'p' );
  133. const el4 = new ViewElement( 'p' );
  134. el1.addClass( 'foo', 'bar' );
  135. el2.addClass( 'bar', 'foo' );
  136. el3.addClass( 'baz' );
  137. el4.addClass( 'baz', 'bar' );
  138. expect( el1.isSimilar( el2 ) ).to.be.true;
  139. expect( el1.isSimilar( el3 ) ).to.be.false;
  140. expect( el1.isSimilar( el4 ) ).to.be.false;
  141. } );
  142. } );
  143. describe( 'children manipulation methods', () => {
  144. let parent, el1, el2, el3, el4;
  145. beforeEach( () => {
  146. parent = new ViewElement( 'p' );
  147. el1 = new ViewElement( 'el1' );
  148. el2 = new ViewElement( 'el2' );
  149. el3 = new ViewElement( 'el3' );
  150. el4 = new ViewElement( 'el4' );
  151. } );
  152. describe( 'insertion', () => {
  153. it( 'should insert children', () => {
  154. const count1 = parent.insertChildren( 0, [ el1, el3 ] );
  155. const count2 = parent.insertChildren( 1, el2 );
  156. expect( parent.getChildCount() ).to.equal( 3 );
  157. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  158. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  159. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  160. expect( count1 ).to.equal( 2 );
  161. expect( count2 ).to.equal( 1 );
  162. } );
  163. it( 'should append children', () => {
  164. const count1 = parent.insertChildren( 0, el1 );
  165. const count2 = parent.appendChildren( el2 );
  166. const count3 = parent.appendChildren( el3 );
  167. expect( parent.getChildCount() ).to.equal( 3 );
  168. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  169. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  170. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  171. expect( count1 ).to.equal( 1 );
  172. expect( count2 ).to.equal( 1 );
  173. expect( count3 ).to.equal( 1 );
  174. } );
  175. } );
  176. describe( 'getChildIndex', () => {
  177. it( 'should return child index', () => {
  178. parent.appendChildren( el1 );
  179. parent.appendChildren( el2 );
  180. parent.appendChildren( el3 );
  181. expect( parent.getChildCount() ).to.equal( 3 );
  182. expect( parent.getChildIndex( el1 ) ).to.equal( 0 );
  183. expect( parent.getChildIndex( el2 ) ).to.equal( 1 );
  184. expect( parent.getChildIndex( el3 ) ).to.equal( 2 );
  185. } );
  186. } );
  187. describe( 'getChildren', () => {
  188. it( 'should renturn children iterator', () => {
  189. parent.appendChildren( el1 );
  190. parent.appendChildren( el2 );
  191. parent.appendChildren( el3 );
  192. const expected = [ el1, el2, el3 ];
  193. let i = 0;
  194. for ( let child of parent.getChildren() ) {
  195. expect( child ).to.equal( expected[ i ] );
  196. i++;
  197. }
  198. expect( i ).to.equal( 3 );
  199. } );
  200. } );
  201. describe( 'removeChildren', () => {
  202. it( 'should remove children', () => {
  203. parent.appendChildren( el1 );
  204. parent.appendChildren( el2 );
  205. parent.appendChildren( el3 );
  206. parent.appendChildren( el4 );
  207. parent.removeChildren( 1, 2 );
  208. expect( parent.getChildCount() ).to.equal( 2 );
  209. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  210. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  211. expect( el1.parent ).to.equal( parent );
  212. expect( el2.parent ).to.be.null;
  213. expect( el3.parent ).to.be.null;
  214. expect( el4.parent ).equal( parent );
  215. } );
  216. it( 'should remove one child when second parameter is not specified', () => {
  217. parent.appendChildren( el1 );
  218. parent.appendChildren( el2 );
  219. parent.appendChildren( el3 );
  220. const removed = parent.removeChildren( 1 );
  221. expect( parent.getChildCount() ).to.equal( 2 );
  222. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  223. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  224. expect( removed.length ).to.equal( 1 );
  225. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  226. } );
  227. } );
  228. } );
  229. describe( 'attributes manipulation methods', () => {
  230. let el;
  231. beforeEach( () => {
  232. el = new ViewElement( 'p' );
  233. } );
  234. describe( 'setAttribute', () => {
  235. it( 'should set attribute', () => {
  236. el.setAttribute( 'foo', 'bar' );
  237. expect( el._attrs.has( 'foo' ) ).to.be.true;
  238. expect( el._attrs.get( 'foo' ) ).to.equal( 'bar' );
  239. } );
  240. it( 'should set class', () => {
  241. el.setAttribute( 'class', 'foo bar' );
  242. expect( el._attrs.has( 'class' ) ).to.be.false;
  243. expect( el._classes.has( 'foo' ) ).to.be.true;
  244. expect( el._classes.has( 'bar' ) ).to.be.true;
  245. } );
  246. it( 'should replace all existing classes', () => {
  247. el.setAttribute( 'class', 'foo bar baz' );
  248. el.setAttribute( 'class', 'qux' );
  249. expect( el._classes.has( 'foo' ) ).to.be.false;
  250. expect( el._classes.has( 'bar' ) ).to.be.false;
  251. expect( el._classes.has( 'baz' ) ).to.be.false;
  252. expect( el._classes.has( 'qux' ) ).to.be.true;
  253. } );
  254. } );
  255. describe( 'getAttribute', () => {
  256. it( 'should return attribute', () => {
  257. el.setAttribute( 'foo', 'bar' );
  258. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  259. expect( el.getAttribute( 'bom' ) ).to.not.be.ok;
  260. } );
  261. it( 'should return class attribute', () => {
  262. el.addClass( 'foo', 'bar' );
  263. expect( el.getAttribute( 'class' ) ).to.equal( 'foo bar' );
  264. } );
  265. it( 'should return undefined if no class attribute', () => {
  266. expect( el.getAttribute( 'class' ) ).to.be.undefined;
  267. } );
  268. } );
  269. describe( 'hasAttribute', () => {
  270. it( 'should return true if element has attribute', () => {
  271. el.setAttribute( 'foo', 'bar' );
  272. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  273. expect( el.hasAttribute( 'bom' ) ).to.be.false;
  274. } );
  275. it( 'should return true if element has class attribute', () => {
  276. expect( el.hasAttribute( 'class' ) ).to.be.false;
  277. el.addClass( 'foo' );
  278. expect( el.hasAttribute( 'class' ) ).to.be.true;
  279. } );
  280. } );
  281. describe( 'getAttributeKeys', () => {
  282. it( 'should return keys', () => {
  283. el.setAttribute( 'foo', true );
  284. el.setAttribute( 'bar', true );
  285. const expected = [ 'foo', 'bar' ];
  286. let i = 0;
  287. for ( let key of el.getAttributeKeys() ) {
  288. expect( key ).to.equal( expected[ i ] );
  289. i++;
  290. }
  291. expect( i ).to.equal( 2 );
  292. } );
  293. it( 'should return class key', () => {
  294. el.addClass( 'foo' );
  295. el.setAttribute( 'bar', true );
  296. const expected = [ 'class', 'bar' ];
  297. let i = 0;
  298. for ( let key of el.getAttributeKeys() ) {
  299. expect( key ).to.equal( expected[ i ] );
  300. i++;
  301. }
  302. } );
  303. } );
  304. describe( 'removeAttribute', () => {
  305. it( 'should remove attributes', () => {
  306. el.setAttribute( 'foo', true );
  307. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  308. el.removeAttribute( 'foo' );
  309. expect( el.hasAttribute( 'foo' ) ).to.be.false;
  310. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 0 );
  311. } );
  312. } );
  313. } );
  314. describe( 'addClass', () => {
  315. it( 'should add single class', () => {
  316. const el = new ViewElement( 'foo' );
  317. el.addClass( 'one' );
  318. expect( el._classes.has( 'one' ) ).to.be.true;
  319. } );
  320. it( 'should add multiple classes', () => {
  321. const el = new ViewElement( 'foo' );
  322. el.addClass( 'one', 'two', 'three' );
  323. expect( el._classes.has( 'one' ) ).to.be.true;
  324. expect( el._classes.has( 'two' ) ).to.be.true;
  325. expect( el._classes.has( 'three' ) ).to.be.true;
  326. } );
  327. } );
  328. describe( 'removeClass', () => {
  329. it( 'should remove single class', () => {
  330. const el = new ViewElement( 'foo', { class: 'one two three' } );
  331. el.removeClass( 'one' );
  332. expect( el._classes.has( 'one' ) ).to.be.false;
  333. expect( el._classes.has( 'two' ) ).to.be.true;
  334. expect( el._classes.has( 'three' ) ).to.be.true;
  335. } );
  336. it( 'should remove multiple classes', () => {
  337. const el = new ViewElement( 'foo', { class: 'one two three four' } );
  338. el.removeClass( 'one', 'two', 'three' );
  339. expect( el._classes.has( 'one' ) ).to.be.false;
  340. expect( el._classes.has( 'two' ) ).to.be.false;
  341. expect( el._classes.has( 'three' ) ).to.be.false;
  342. expect( el._classes.has( 'four' ) ).to.be.true;
  343. } );
  344. } );
  345. describe( 'hasClass', () => {
  346. it( 'should check if element has a class', () => {
  347. const el = new ViewElement( 'foo' );
  348. el.addClass( 'one', 'two', 'three' );
  349. expect( el.hasClass( 'one' ) ).to.be.true;
  350. expect( el.hasClass( 'two' ) ).to.be.true;
  351. expect( el.hasClass( 'three' ) ).to.be.true;
  352. expect( el.hasClass( 'four' ) ).to.be.false;
  353. } );
  354. it( 'should check if element has multiple classes', () => {
  355. const el = new ViewElement( 'foo' );
  356. el.addClass( 'one', 'two', 'three' );
  357. expect( el.hasClass( 'one', 'two' ) ).to.be.true;
  358. expect( el.hasClass( 'three', 'two' ) ).to.be.true;
  359. expect( el.hasClass( 'three', 'one', 'two' ) ).to.be.true;
  360. expect( el.hasClass( 'three', 'one', 'two', 'zero' ) ).to.be.false;
  361. } );
  362. } );
  363. } );