8
0

element.js 11 KB

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