element.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. 'use strict';
  7. import Node from '/ckeditor5/engine/model/node.js';
  8. import NodeList from '/ckeditor5/engine/model/nodelist.js';
  9. import Element from '/ckeditor5/engine/model/element.js';
  10. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  11. import { jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  12. describe( 'Element', () => {
  13. describe( 'constructor', () => {
  14. it( 'should create element without attributes', () => {
  15. let element = new Element( 'elem' );
  16. let parent = new Element( 'parent', [], [ element ] );
  17. expect( element ).to.be.an.instanceof( Node );
  18. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  19. expect( element ).to.have.property( 'parent' ).that.equals( parent );
  20. expect( element._attrs.size ).to.equal( 0 );
  21. } );
  22. it( 'should create element with attributes', () => {
  23. let attr = { foo: 'bar' };
  24. let element = new Element( 'elem', [ attr ] );
  25. let parent = new Element( 'parent', [], [ element ] );
  26. expect( element ).to.be.an.instanceof( Node );
  27. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  28. expect( element ).to.have.property( 'parent' ).that.equals( parent );
  29. expect( element._attrs.size ).to.equal( 1 );
  30. expect( element.getAttribute( attr.key ) ).to.equal( attr.value );
  31. } );
  32. it( 'should create element with children', () => {
  33. let element = new Element( 'elem', [], 'foo' );
  34. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  35. expect( element.getChildCount() ).to.equal( 3 );
  36. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  37. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  38. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  39. } );
  40. } );
  41. describe( 'insertChildren', () => {
  42. it( 'should add children to the element', () => {
  43. let element = new Element( 'elem', [], [ 'xy' ] );
  44. element.insertChildren( 1, 'foo' );
  45. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  46. expect( element.getChildCount() ).to.equal( 5 );
  47. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  48. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'f' );
  49. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  50. expect( element.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  51. expect( element.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
  52. } );
  53. it( 'should accept DocumentFragment as a parameter and clean it after it is added', () => {
  54. let p1 = new Element( 'p' );
  55. let p2 = new Element( 'p' );
  56. let frag = new DocumentFragment( [ p1, p2 ] );
  57. let element = new Element( 'div' );
  58. element.insertChildren( 0, frag );
  59. expect( element.getChildCount() ).to.equal( 2 );
  60. expect( element.getChild( 0 ) ).to.equal( p1 );
  61. expect( element.getChild( 1 ) ).to.equal( p2 );
  62. expect( frag.getChildCount() ).to.equal( 0 );
  63. } );
  64. } );
  65. describe( 'appendChildren', () => {
  66. it( 'should add children to the end of the element', () => {
  67. let element = new Element( 'elem', [], [ 'xy' ] );
  68. element.appendChildren( 'foo' );
  69. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  70. expect( element.getChildCount() ).to.equal( 5 );
  71. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  72. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'y' );
  73. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'f' );
  74. expect( element.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  75. expect( element.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'o' );
  76. } );
  77. it( 'should accept DocumentFragment as a parameter and clean it after it is added', () => {
  78. let p1 = new Element( 'p' );
  79. let p2 = new Element( 'p' );
  80. let frag = new DocumentFragment( [ p1, p2 ] );
  81. let element = new Element( 'div' );
  82. element.appendChildren( frag );
  83. expect( element.getChildCount() ).to.equal( 2 );
  84. expect( element.getChild( 0 ) ).to.equal( p1 );
  85. expect( element.getChild( 1 ) ).to.equal( p2 );
  86. expect( frag.getChildCount() ).to.equal( 0 );
  87. } );
  88. } );
  89. describe( 'removeChildren', () => {
  90. it( 'should remove children from the element and return them as a NodeList', () => {
  91. let element = new Element( 'elem', [], [ 'foobar' ] );
  92. let removed = element.removeChildren( 2, 3 );
  93. expect( element.getChildCount() ).to.equal( 3 );
  94. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  95. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  96. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
  97. expect( removed ).to.be.instanceof( NodeList );
  98. expect( removed.length ).to.equal( 3 );
  99. expect( removed.get( 0 ).character ).to.equal( 'o' );
  100. expect( removed.get( 1 ).character ).to.equal( 'b' );
  101. expect( removed.get( 2 ).character ).to.equal( 'a' );
  102. } );
  103. it( 'should remove one child when second parameter is not specified', () => {
  104. let element = new Element( 'elem', [], [ 'foo' ] );
  105. let removed = element.removeChildren( 2 );
  106. expect( element.getChildCount() ).to.equal( 2 );
  107. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  108. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  109. expect( removed ).to.be.instanceof( NodeList );
  110. expect( removed.length ).to.equal( 1 );
  111. expect( removed.get( 0 ).character ).to.equal( 'o' );
  112. } );
  113. } );
  114. describe( 'getChildIndex', () => {
  115. it( 'should return child index', () => {
  116. let element = new Element( 'elem', [], [ new Element( 'p' ), 'bar', new Element( 'h' ) ] );
  117. let p = element.getChild( 0 );
  118. let b = element.getChild( 1 );
  119. let a = element.getChild( 2 );
  120. let r = element.getChild( 3 );
  121. let h = element.getChild( 4 );
  122. expect( element.getChildIndex( p ) ).to.equal( 0 );
  123. expect( element.getChildIndex( b ) ).to.equal( 1 );
  124. expect( element.getChildIndex( a ) ).to.equal( 2 );
  125. expect( element.getChildIndex( r ) ).to.equal( 3 );
  126. expect( element.getChildIndex( h ) ).to.equal( 4 );
  127. } );
  128. } );
  129. describe( 'getChildCount', () => {
  130. it( 'should return number of children', () => {
  131. let element = new Element( 'elem', [], [ 'bar' ] );
  132. expect( element.getChildCount() ).to.equal( 3 );
  133. } );
  134. } );
  135. describe( 'attributes interface', () => {
  136. let node;
  137. beforeEach( () => {
  138. node = new Element();
  139. } );
  140. describe( 'setAttribute', () => {
  141. it( 'should set given attribute on the element', () => {
  142. node.setAttribute( 'foo', 'bar' );
  143. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  144. } );
  145. } );
  146. describe( 'setAttributesTo', () => {
  147. it( 'should remove all attributes set on element and set the given ones', () => {
  148. node.setAttribute( 'abc', 'xyz' );
  149. node.setAttributesTo( { foo: 'bar' } );
  150. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  151. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  152. } );
  153. } );
  154. describe( 'removeAttribute', () => {
  155. it( 'should remove attribute set on the element and return true', () => {
  156. node.setAttribute( 'foo', 'bar' );
  157. let result = node.removeAttribute( 'foo' );
  158. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  159. expect( result ).to.be.true;
  160. } );
  161. it( 'should return false if element does not contain given attribute', () => {
  162. let result = node.removeAttribute( 'foo' );
  163. expect( result ).to.be.false;
  164. } );
  165. } );
  166. describe( 'clearAttributes', () => {
  167. it( 'should remove all attributes from the element', () => {
  168. node.setAttribute( 'foo', 'bar' );
  169. node.setAttribute( 'abc', 'xyz' );
  170. node.clearAttributes();
  171. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  172. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  173. } );
  174. } );
  175. } );
  176. describe( 'isEmpty', () => {
  177. it( 'checks whether element has no children', () => {
  178. expect( new Element( 'a' ).isEmpty() ).to.be.true;
  179. expect( new Element( 'a', null, 'x' ).isEmpty() ).to.be.false;
  180. } );
  181. } );
  182. describe( 'getText()', () => {
  183. it( 'returns all text nodes', () => {
  184. const el = new Element( 'p', null, [
  185. new Element( 'p', null, 'abc' ),
  186. 'def',
  187. new Element( 'p', null, 'ghi' ),
  188. 'jkl'
  189. ] );
  190. expect( el.getText() ).to.equal( 'abcdefghijkl' );
  191. } );
  192. } );
  193. describe( 'toJSON', () => {
  194. it( 'should serialize empty element', () => {
  195. let element = new Element( 'one' );
  196. expect( jsonParseStringify( element ) ).to.deep.equal( { name: 'one' } );
  197. } );
  198. it( 'should serialize element with attributes', () => {
  199. let element = new Element( 'one', { foo: true, bar: false } );
  200. expect( jsonParseStringify( element ) ).to.deep.equal( {
  201. attributes: [ [ 'foo', true ], [ 'bar', false ] ],
  202. name: 'one'
  203. } );
  204. } );
  205. it( 'should serialize node with children', () => {
  206. let img = new Element( 'img' );
  207. let one = new Element( 'one' );
  208. let two = new Element( 'two', null, [ 'b', 'a', img, 'r' ] );
  209. let three = new Element( 'three' );
  210. let node = new Element( null, null, [ one, two, three ] );
  211. expect( jsonParseStringify( node ) ).to.deep.equal( {
  212. children: {
  213. nodes: [
  214. { name: 'one' },
  215. {
  216. children: {
  217. nodes: [
  218. { text: 'ba' },
  219. { name: 'img' },
  220. { text: 'r' }
  221. ]
  222. },
  223. name: 'two'
  224. },
  225. { name: 'three' }
  226. ]
  227. },
  228. name: null
  229. } );
  230. } );
  231. } );
  232. describe( 'fromJSON', () => {
  233. it( 'should create element without attributes', () => {
  234. const el = new Element( 'el' );
  235. let serialized = jsonParseStringify( el );
  236. let deserialized = Element.fromJSON( serialized );
  237. expect( deserialized.parent ).to.be.null;
  238. expect( deserialized.name ).to.equal( 'el' );
  239. expect( deserialized.getChildCount() ).to.equal( 0 );
  240. } );
  241. it( 'should create element with attributes', () => {
  242. const el = new Element( 'el', { foo: true } );
  243. let serialized = jsonParseStringify( el );
  244. let deserialized = Element.fromJSON( serialized );
  245. expect( deserialized.parent ).to.be.null;
  246. expect( deserialized.name ).to.equal( 'el' );
  247. expect( deserialized.getChildCount() ).to.equal( 0 );
  248. expect( deserialized.hasAttribute( 'foo' ) ).to.be.true;
  249. expect( deserialized.getAttribute( 'foo' ) ).to.be.true;
  250. } );
  251. it( 'should create element with children', () => {
  252. const p = new Element( 'p' );
  253. const el = new Element( 'el', null, p );
  254. let serialized = jsonParseStringify( el );
  255. let deserialized = Element.fromJSON( serialized );
  256. expect( deserialized.parent ).to.be.null;
  257. expect( deserialized.name ).to.equal( 'el' );
  258. expect( deserialized.getChildCount() ).to.equal( 1 );
  259. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  260. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  261. } );
  262. } );
  263. } );