element.js 15 KB

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