element.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. } );
  130. describe( 'children manipulation methods', () => {
  131. let parent, el1, el2, el3, el4;
  132. beforeEach( () => {
  133. parent = new ViewElement( 'p' );
  134. el1 = new ViewElement( 'el1' );
  135. el2 = new ViewElement( 'el2' );
  136. el3 = new ViewElement( 'el3' );
  137. el4 = new ViewElement( 'el4' );
  138. } );
  139. describe( 'insertion', () => {
  140. it( 'should insert children', () => {
  141. const count1 = parent.insertChildren( 0, [ el1, el3 ] );
  142. const count2 = parent.insertChildren( 1, el2 );
  143. expect( parent.getChildCount() ).to.equal( 3 );
  144. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  145. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  146. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  147. expect( count1 ).to.equal( 2 );
  148. expect( count2 ).to.equal( 1 );
  149. } );
  150. it( 'should append children', () => {
  151. const count1 = parent.insertChildren( 0, el1 );
  152. const count2 = parent.appendChildren( el2 );
  153. const count3 = parent.appendChildren( el3 );
  154. expect( parent.getChildCount() ).to.equal( 3 );
  155. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  156. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  157. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  158. expect( count1 ).to.equal( 1 );
  159. expect( count2 ).to.equal( 1 );
  160. expect( count3 ).to.equal( 1 );
  161. } );
  162. } );
  163. describe( 'getChildIndex', () => {
  164. it( 'should return child index', () => {
  165. parent.appendChildren( el1 );
  166. parent.appendChildren( el2 );
  167. parent.appendChildren( el3 );
  168. expect( parent.getChildCount() ).to.equal( 3 );
  169. expect( parent.getChildIndex( el1 ) ).to.equal( 0 );
  170. expect( parent.getChildIndex( el2 ) ).to.equal( 1 );
  171. expect( parent.getChildIndex( el3 ) ).to.equal( 2 );
  172. } );
  173. } );
  174. describe( 'getChildren', () => {
  175. it( 'should renturn children iterator', () => {
  176. parent.appendChildren( el1 );
  177. parent.appendChildren( el2 );
  178. parent.appendChildren( el3 );
  179. const expected = [ el1, el2, el3 ];
  180. let i = 0;
  181. for ( let child of parent.getChildren() ) {
  182. expect( child ).to.equal( expected[ i ] );
  183. i++;
  184. }
  185. expect( i ).to.equal( 3 );
  186. } );
  187. } );
  188. describe( 'removeChildren', () => {
  189. it( 'should remove children', () => {
  190. parent.appendChildren( el1 );
  191. parent.appendChildren( el2 );
  192. parent.appendChildren( el3 );
  193. parent.appendChildren( el4 );
  194. parent.removeChildren( 1, 2 );
  195. expect( parent.getChildCount() ).to.equal( 2 );
  196. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  197. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  198. expect( el1.parent ).to.equal( parent );
  199. expect( el2.parent ).to.be.null;
  200. expect( el3.parent ).to.be.null;
  201. expect( el4.parent ).equal( parent );
  202. } );
  203. it( 'should remove one child when second parameter is not specified', () => {
  204. parent.appendChildren( el1 );
  205. parent.appendChildren( el2 );
  206. parent.appendChildren( el3 );
  207. const removed = parent.removeChildren( 1 );
  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( 'el3' );
  211. expect( removed.length ).to.equal( 1 );
  212. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  213. } );
  214. } );
  215. } );
  216. describe( 'attributes manipulation methods', () => {
  217. let el;
  218. beforeEach( () => {
  219. el = new ViewElement( 'p' );
  220. } );
  221. describe( 'setAttribute', () => {
  222. it( 'should set attribute', () => {
  223. el.setAttribute( 'foo', 'bar' );
  224. expect( el._attrs.has( 'foo' ) ).to.be.true;
  225. expect( el._attrs.get( 'foo' ) ).to.equal( 'bar' );
  226. } );
  227. it( 'should set class', () => {
  228. el.setAttribute( 'class', 'foo bar' );
  229. expect( el._attrs.has( 'class' ) ).to.be.false;
  230. expect( el._classes.has( 'foo' ) ).to.be.true;
  231. expect( el._classes.has( 'bar' ) ).to.be.true;
  232. } );
  233. it( 'should replace all existing classes', () => {
  234. el.setAttribute( 'class', 'foo bar baz' );
  235. el.setAttribute( 'class', 'qux' );
  236. expect( el._classes.has( 'foo' ) ).to.be.false;
  237. expect( el._classes.has( 'bar' ) ).to.be.false;
  238. expect( el._classes.has( 'baz' ) ).to.be.false;
  239. expect( el._classes.has( 'qux' ) ).to.be.true;
  240. } );
  241. } );
  242. describe( 'getAttribute', () => {
  243. it( 'should return attribute', () => {
  244. el.setAttribute( 'foo', 'bar' );
  245. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  246. expect( el.getAttribute( 'bom' ) ).to.not.be.ok;
  247. } );
  248. } );
  249. describe( 'hasAttribute', () => {
  250. it( 'should return true if element has attribute', () => {
  251. el.setAttribute( 'foo', 'bar' );
  252. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  253. expect( el.hasAttribute( 'bom' ) ).to.be.false;
  254. } );
  255. it( 'should return true if element has class attribute', () => {
  256. expect( el.hasAttribute( 'class' ) ).to.be.false;
  257. el.addClass( 'foo' );
  258. expect( el.hasAttribute( 'class' ) ).to.be.true;
  259. } );
  260. } );
  261. describe( 'getAttributeKeys', () => {
  262. it( 'should return keys', () => {
  263. el.setAttribute( 'foo', true );
  264. el.setAttribute( 'bar', true );
  265. const expected = [ 'foo', 'bar' ];
  266. let i = 0;
  267. for ( let key of el.getAttributeKeys() ) {
  268. expect( key ).to.equal( expected[ i ] );
  269. i++;
  270. }
  271. expect( i ).to.equal( 2 );
  272. } );
  273. it( 'should return class key', () => {
  274. el.addClass( 'foo' );
  275. el.setAttribute( 'bar', true );
  276. const expected = [ 'class', 'bar' ];
  277. let i = 0;
  278. for ( let key of el.getAttributeKeys() ) {
  279. expect( key ).to.equal( expected[ i ] );
  280. i++;
  281. }
  282. } );
  283. } );
  284. describe( 'removeAttribute', () => {
  285. it( 'should remove attributes', () => {
  286. el.setAttribute( 'foo', true );
  287. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  288. el.removeAttribute( 'foo' );
  289. expect( el.hasAttribute( 'foo' ) ).to.be.false;
  290. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 0 );
  291. } );
  292. } );
  293. } );
  294. describe( 'addClass', () => {
  295. it( 'should add single class', () => {
  296. const el = new ViewElement( 'foo' );
  297. el.addClass( 'one' );
  298. expect( el._classes.has( 'one' ) ).to.be.true;
  299. } );
  300. it( 'should add multiple classes', () => {
  301. const el = new ViewElement( 'foo' );
  302. el.addClass( 'one', 'two', 'three' );
  303. expect( el._classes.has( 'one' ) ).to.be.true;
  304. expect( el._classes.has( 'two' ) ).to.be.true;
  305. expect( el._classes.has( 'three' ) ).to.be.true;
  306. } );
  307. } );
  308. describe( 'removeClass', () => {
  309. it( 'should remove single class', () => {
  310. const el = new ViewElement( 'foo', { class: 'one two three' } );
  311. el.removeClass( 'one' );
  312. expect( el._classes.has( 'one' ) ).to.be.false;
  313. expect( el._classes.has( 'two' ) ).to.be.true;
  314. expect( el._classes.has( 'three' ) ).to.be.true;
  315. } );
  316. it( 'should remove multiple classes', () => {
  317. const el = new ViewElement( 'foo', { class: 'one two three four' } );
  318. el.removeClass( 'one', 'two', 'three' );
  319. expect( el._classes.has( 'one' ) ).to.be.false;
  320. expect( el._classes.has( 'two' ) ).to.be.false;
  321. expect( el._classes.has( 'three' ) ).to.be.false;
  322. expect( el._classes.has( 'four' ) ).to.be.true;
  323. } );
  324. } );
  325. describe( 'hasClass', () => {
  326. it( 'should check if element has a class', () => {
  327. const el = new ViewElement( 'foo' );
  328. el.addClass( 'one', 'two', 'three' );
  329. expect( el.hasClass( 'one' ) ).to.be.true;
  330. expect( el.hasClass( 'two' ) ).to.be.true;
  331. expect( el.hasClass( 'three' ) ).to.be.true;
  332. expect( el.hasClass( 'four' ) ).to.be.false;
  333. } );
  334. it( 'should check if element has multiple classes', () => {
  335. const el = new ViewElement( 'foo' );
  336. el.addClass( 'one', 'two', 'three' );
  337. expect( el.hasClass( 'one', 'two' ) ).to.be.true;
  338. expect( el.hasClass( 'three', 'two' ) ).to.be.true;
  339. expect( el.hasClass( 'three', 'one', 'two' ) ).to.be.true;
  340. expect( el.hasClass( 'three', 'one', 'two', 'zero' ) ).to.be.false;
  341. } );
  342. } );
  343. } );