element.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. } );
  91. describe( 'isSimilar', () => {
  92. const el = new ViewElement( 'p', { foo: 'bar' } );
  93. it( 'should return false when comparing to non-element', () => {
  94. expect( el.isSimilar( null ) ).to.be.false;
  95. expect( el.isSimilar( {} ) ).to.be.false;
  96. } );
  97. it( 'should return true when the same node is provided', () => {
  98. expect( el.isSimilar( el ) ).to.be.true;
  99. } );
  100. it( 'should return true for element with same attributes and name', () => {
  101. const other = new ViewElement( 'p', { foo: 'bar' } );
  102. expect( el.isSimilar( other ) ).to.be.true;
  103. } );
  104. it( 'sould return false when name is not the same', () => {
  105. const other = el.clone();
  106. other.name = 'div';
  107. expect( el.isSimilar( other ) ).to.be.false;
  108. } );
  109. it( 'should return false when attributes are not the same', () => {
  110. const other1 = el.clone();
  111. const other2 = el.clone();
  112. const other3 = el.clone();
  113. other1.setAttribute( 'baz', 'qux' );
  114. other2.setAttribute( 'foo', 'not-bar' );
  115. other3.removeAttribute( 'foo' );
  116. expect( el.isSimilar( other1 ) ).to.be.false;
  117. expect( el.isSimilar( other2 ) ).to.be.false;
  118. expect( el.isSimilar( other3 ) ).to.be.false;
  119. } );
  120. } );
  121. describe( 'children manipulation methods', () => {
  122. let parent, el1, el2, el3, el4;
  123. beforeEach( () => {
  124. parent = new ViewElement( 'p' );
  125. el1 = new ViewElement( 'el1' );
  126. el2 = new ViewElement( 'el2' );
  127. el3 = new ViewElement( 'el3' );
  128. el4 = new ViewElement( 'el4' );
  129. } );
  130. describe( 'insertion', () => {
  131. it( 'should insert children', () => {
  132. const count1 = parent.insertChildren( 0, [ el1, el3 ] );
  133. const count2 = parent.insertChildren( 1, el2 );
  134. expect( parent.getChildCount() ).to.equal( 3 );
  135. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  136. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  137. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  138. expect( count1 ).to.equal( 2 );
  139. expect( count2 ).to.equal( 1 );
  140. } );
  141. it( 'should append children', () => {
  142. const count1 = parent.insertChildren( 0, el1 );
  143. const count2 = parent.appendChildren( el2 );
  144. const count3 = parent.appendChildren( el3 );
  145. expect( parent.getChildCount() ).to.equal( 3 );
  146. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  147. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  148. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  149. expect( count1 ).to.equal( 1 );
  150. expect( count2 ).to.equal( 1 );
  151. expect( count3 ).to.equal( 1 );
  152. } );
  153. } );
  154. describe( 'getChildIndex', () => {
  155. it( 'should return child index', () => {
  156. parent.appendChildren( el1 );
  157. parent.appendChildren( el2 );
  158. parent.appendChildren( el3 );
  159. expect( parent.getChildCount() ).to.equal( 3 );
  160. expect( parent.getChildIndex( el1 ) ).to.equal( 0 );
  161. expect( parent.getChildIndex( el2 ) ).to.equal( 1 );
  162. expect( parent.getChildIndex( el3 ) ).to.equal( 2 );
  163. } );
  164. } );
  165. describe( 'getChildren', () => {
  166. it( 'should renturn children iterator', () => {
  167. parent.appendChildren( el1 );
  168. parent.appendChildren( el2 );
  169. parent.appendChildren( el3 );
  170. const expected = [ el1, el2, el3 ];
  171. let i = 0;
  172. for ( let child of parent.getChildren() ) {
  173. expect( child ).to.equal( expected[ i ] );
  174. i++;
  175. }
  176. expect( i ).to.equal( 3 );
  177. } );
  178. } );
  179. describe( 'removeChildren', () => {
  180. it( 'should remove children', () => {
  181. parent.appendChildren( el1 );
  182. parent.appendChildren( el2 );
  183. parent.appendChildren( el3 );
  184. parent.appendChildren( el4 );
  185. parent.removeChildren( 1, 2 );
  186. expect( parent.getChildCount() ).to.equal( 2 );
  187. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  188. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  189. expect( el1.parent ).to.equal( parent );
  190. expect( el2.parent ).to.be.null;
  191. expect( el3.parent ).to.be.null;
  192. expect( el4.parent ).equal( parent );
  193. } );
  194. it( 'should remove one child when second parameter is not specified', () => {
  195. parent.appendChildren( el1 );
  196. parent.appendChildren( el2 );
  197. parent.appendChildren( el3 );
  198. const removed = parent.removeChildren( 1 );
  199. expect( parent.getChildCount() ).to.equal( 2 );
  200. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  201. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  202. expect( removed.length ).to.equal( 1 );
  203. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  204. } );
  205. } );
  206. } );
  207. describe( 'attributes manipulation methods', () => {
  208. let el;
  209. beforeEach( () => {
  210. el = new ViewElement( 'p' );
  211. } );
  212. describe( 'getAttribute', () => {
  213. it( 'should return attribute', () => {
  214. el.setAttribute( 'foo', 'bar' );
  215. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  216. expect( el.getAttribute( 'bom' ) ).to.not.be.ok;
  217. } );
  218. } );
  219. describe( 'hasAttribute', () => {
  220. it( 'should return true if element has attribute', () => {
  221. el.setAttribute( 'foo', 'bar' );
  222. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  223. expect( el.hasAttribute( 'bom' ) ).to.be.false;
  224. } );
  225. } );
  226. describe( 'getAttributeKeys', () => {
  227. it( 'should return keys', () => {
  228. el.setAttribute( 'foo', true );
  229. el.setAttribute( 'bar', true );
  230. const expected = [ 'foo', 'bar' ];
  231. let i = 0;
  232. for ( let key of el.getAttributeKeys() ) {
  233. expect( key ).to.equal( expected[ i ] );
  234. i++;
  235. }
  236. expect( i ).to.equal( 2 );
  237. } );
  238. } );
  239. describe( 'removeAttribute', () => {
  240. it( 'should remove attributes', () => {
  241. el.setAttribute( 'foo', true );
  242. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  243. el.removeAttribute( 'foo' );
  244. expect( el.hasAttribute( 'foo' ) ).to.be.false;
  245. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 0 );
  246. } );
  247. } );
  248. } );
  249. describe( 'addClass', () => {
  250. it( 'should add single class', () => {
  251. const el = new ViewElement( 'foo' );
  252. el.addClass( 'one' );
  253. expect( el._classes.has( 'one' ) ).to.be.true;
  254. } );
  255. it( 'should add multiple classes', () => {
  256. const el = new ViewElement( 'foo' );
  257. el.addClass( 'one', 'two', 'three' );
  258. expect( el._classes.has( 'one' ) ).to.be.true;
  259. expect( el._classes.has( 'two' ) ).to.be.true;
  260. expect( el._classes.has( 'three' ) ).to.be.true;
  261. } );
  262. } );
  263. describe( 'removeClass', () => {
  264. it( 'should remove single class', () => {
  265. const el = new ViewElement( 'foo', { class: 'one two three' } );
  266. el.removeClass( 'one' );
  267. expect( el._classes.has( 'one' ) ).to.be.false;
  268. expect( el._classes.has( 'two' ) ).to.be.true;
  269. expect( el._classes.has( 'three' ) ).to.be.true;
  270. } );
  271. it( 'should remove multiple classes', () => {
  272. const el = new ViewElement( 'foo', { class: 'one two three four' } );
  273. el.removeClass( 'one', 'two', 'three' );
  274. expect( el._classes.has( 'one' ) ).to.be.false;
  275. expect( el._classes.has( 'two' ) ).to.be.false;
  276. expect( el._classes.has( 'three' ) ).to.be.false;
  277. expect( el._classes.has( 'four' ) ).to.be.true;
  278. } );
  279. } );
  280. } );