element.js 15 KB

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