8
0

element.js 14 KB

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