8
0

element.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Node from '../../src/model/node';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import { jsonParseStringify } from '../../tests/model/_utils/utils';
  9. import count from '@ckeditor/ckeditor5-utils/src/count';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. describe( 'Element', () => {
  12. describe( 'constructor()', () => {
  13. it( 'should create empty element', () => {
  14. const element = new Element( 'elem' );
  15. expect( element ).to.be.an.instanceof( Node );
  16. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  17. expect( count( element.getAttributes() ) ).to.equal( 0 );
  18. expect( count( element.getChildren() ) ).to.equal( 0 );
  19. } );
  20. it( 'should create element with attributes', () => {
  21. const element = new Element( 'elem', { foo: 'bar' } );
  22. expect( count( element.getAttributes() ) ).to.equal( 1 );
  23. expect( element.getAttribute( 'foo' ) ).to.equal( 'bar' );
  24. } );
  25. it( 'should create element with children', () => {
  26. const element = new Element( 'elem', [], new Text( 'foo' ) );
  27. expect( element.childCount ).to.equal( 1 );
  28. expect( element.maxOffset ).to.equal( 3 );
  29. expect( element.getChild( 0 ).data ).to.equal( 'foo' );
  30. } );
  31. } );
  32. describe( 'is', () => {
  33. let element;
  34. before( () => {
  35. element = new Element( 'paragraph' );
  36. } );
  37. it( 'should return true for element, element with same name and element name', () => {
  38. expect( element.is( 'element' ) ).to.be.true;
  39. expect( element.is( 'element', 'paragraph' ) ).to.be.true;
  40. expect( element.is( 'paragraph' ) ).to.be.true;
  41. } );
  42. it( 'should return false for other accept values', () => {
  43. expect( element.is( 'element', 'image' ) ).to.be.false;
  44. expect( element.is( 'image' ) ).to.be.false;
  45. expect( element.is( 'text' ) ).to.be.false;
  46. expect( element.is( 'textProxy' ) ).to.be.false;
  47. expect( element.is( 'documentFragment' ) ).to.be.false;
  48. expect( element.is( 'rootElement' ) ).to.be.false;
  49. } );
  50. } );
  51. describe( 'clone', () => {
  52. it( 'should return an element with same name, attributes and same instances of children if clone was not deep', () => {
  53. const p = new Element( 'p' );
  54. const foo = new Text( 'foo' );
  55. const element = new Element( 'elem', { bold: true, italic: true }, [ p, foo ] );
  56. const copy = element.clone();
  57. expect( copy.name ).to.equal( 'elem' );
  58. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ], [ 'italic', true ] ] );
  59. expect( Array.from( copy.getChildren() ) ).to.deep.equal( [] );
  60. } );
  61. it( 'should clone children (deeply), if clone is deep', () => {
  62. const foo = new Text( 'foo' );
  63. const bar = new Text( 'bar' );
  64. const p = new Element( 'p', null, bar );
  65. const element = new Element( 'elem', { bold: true, italic: true }, [ p, foo ] );
  66. const copy = element.clone( true );
  67. expect( copy.name ).to.equal( 'elem' );
  68. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ], [ 'italic', true ] ] );
  69. expect( copy.childCount ).to.equal( 2 );
  70. expect( copy.getChild( 0 ) ).not.to.equal( p );
  71. expect( copy.getChild( 0 ).getChild( 0 ) ).not.to.equal( bar );
  72. expect( copy.getChild( 1 ) ).not.to.equal( foo );
  73. expect( copy.getChild( 0 ).name ).to.equal( 'p' );
  74. expect( copy.getChild( 0 ).getChild( 0 ).data ).to.equal( 'bar' );
  75. expect( copy.getChild( 1 ).data ).to.equal( 'foo' );
  76. } );
  77. } );
  78. describe( 'insertChildren', () => {
  79. it( 'should add a child to the element', () => {
  80. const element = new Element( 'elem', [], new Text( 'xy' ) );
  81. element.insertChildren( 1, new Text( 'foo' ) );
  82. expect( element.childCount ).to.equal( 2 );
  83. expect( element.maxOffset ).to.equal( 5 );
  84. expect( element.getChild( 0 ).data ).to.equal( 'xy' );
  85. expect( element.getChild( 1 ).data ).to.equal( 'foo' );
  86. } );
  87. it( 'should accept arrays and strings', () => {
  88. const element = new Element( 'elem' );
  89. element.insertChildren( 0, [ new Element( 'image' ), 'xy', new Element( 'list' ) ] );
  90. expect( element.childCount ).to.equal( 3 );
  91. expect( element.maxOffset ).to.equal( 4 );
  92. expect( element.getChild( 0 ).name ).to.equal( 'image' );
  93. expect( element.getChild( 1 ).data ).to.equal( 'xy' );
  94. expect( element.getChild( 2 ).name ).to.equal( 'list' );
  95. } );
  96. it( 'should accept strings', () => {
  97. const element = new Element( 'div' );
  98. element.insertChildren( 0, 'abc' );
  99. expect( element.childCount ).to.equal( 1 );
  100. expect( element.maxOffset ).to.equal( 3 );
  101. expect( element.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  102. } );
  103. } );
  104. describe( 'appendChildren', () => {
  105. it( 'should use insertChildren to add children at the end of the element', () => {
  106. const element = new Element( 'elem', [], new Text( 'xy' ) );
  107. sinon.spy( element, 'insertChildren' );
  108. const text = new Text( 'foo' );
  109. element.appendChildren( text );
  110. expect( element.insertChildren.calledWithExactly( 0, text ) );
  111. } );
  112. } );
  113. describe( 'removeChildren', () => {
  114. it( 'should remove children from the element and return them as an array', () => {
  115. const element = new Element( 'elem', [], [ new Text( 'foobar' ), new Element( 'image' ) ] );
  116. const removed = element.removeChildren( 1, 1 );
  117. expect( element.childCount ).to.equal( 1 );
  118. expect( element.maxOffset ).to.equal( 6 );
  119. expect( element.getChild( 0 ).data ).to.equal( 'foobar' );
  120. expect( removed.length ).to.equal( 1 );
  121. expect( removed[ 0 ].name ).to.equal( 'image' );
  122. } );
  123. it( 'should remove one child when second parameter is not specified', () => {
  124. const element = new Element( 'element', [], [ new Text( 'foo' ), new Element( 'image' ) ] );
  125. const removed = element.removeChildren( 0 );
  126. expect( element.childCount ).to.equal( 1 );
  127. expect( element.maxOffset ).to.equal( 1 );
  128. expect( element.getChild( 0 ).name ).to.equal( 'image' );
  129. expect( removed.length ).to.equal( 1 );
  130. expect( removed[ 0 ].data ).to.equal( 'foo' );
  131. } );
  132. } );
  133. describe( 'getNodeByPath', () => {
  134. it( 'should return this node if path is empty', () => {
  135. const element = new Element( 'elem' );
  136. expect( element.getNodeByPath( [] ) ).to.equal( element );
  137. } );
  138. it( 'should return a descendant of this node', () => {
  139. const image = new Element( 'image' );
  140. const element = new Element( 'elem', [], [
  141. new Element( 'elem', [], [
  142. new Text( 'foo' ),
  143. image
  144. ] )
  145. ] );
  146. expect( element.getNodeByPath( [ 0, 1 ] ) ).to.equal( image );
  147. } );
  148. } );
  149. describe( 'getChildIndex', () => {
  150. it( 'should return child index', () => {
  151. const element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  152. const p = element.getChild( 0 );
  153. const bar = element.getChild( 1 );
  154. const h = element.getChild( 2 );
  155. expect( element.getChildIndex( p ) ).to.equal( 0 );
  156. expect( element.getChildIndex( bar ) ).to.equal( 1 );
  157. expect( element.getChildIndex( h ) ).to.equal( 2 );
  158. } );
  159. } );
  160. describe( 'getChildStartOffset', () => {
  161. it( 'should return child offset', () => {
  162. const element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  163. const p = element.getChild( 0 );
  164. const bar = element.getChild( 1 );
  165. const h = element.getChild( 2 );
  166. expect( element.getChildStartOffset( p ) ).to.equal( 0 );
  167. expect( element.getChildStartOffset( bar ) ).to.equal( 1 );
  168. expect( element.getChildStartOffset( h ) ).to.equal( 4 );
  169. } );
  170. } );
  171. describe( 'getChildCount', () => {
  172. it( 'should return number of children', () => {
  173. const element = new Element( 'elem', [], new Text( 'bar' ) );
  174. expect( element.childCount ).to.equal( 1 );
  175. } );
  176. } );
  177. describe( 'getMaxOffset', () => {
  178. it( 'should return offset number after the last child', () => {
  179. const element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  180. expect( element.maxOffset ).to.equal( 5 );
  181. } );
  182. } );
  183. describe( 'isEmpty', () => {
  184. it( 'checks whether element has no children', () => {
  185. expect( new Element( 'a' ).isEmpty ).to.be.true;
  186. expect( new Element( 'a', null, new Text( 'x' ) ).isEmpty ).to.be.false;
  187. } );
  188. } );
  189. describe( 'offsetToIndex', () => {
  190. let element;
  191. beforeEach( () => {
  192. element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  193. } );
  194. it( 'should return index of a node that occupies given offset in this element', () => {
  195. expect( element.offsetToIndex( 0 ) ).to.equal( 0 );
  196. expect( element.offsetToIndex( 1 ) ).to.equal( 1 );
  197. expect( element.offsetToIndex( 2 ) ).to.equal( 1 );
  198. expect( element.offsetToIndex( 3 ) ).to.equal( 1 );
  199. expect( element.offsetToIndex( 4 ) ).to.equal( 2 );
  200. } );
  201. it( 'should throw if given offset is too high or too low', () => {
  202. expect( () => {
  203. element.offsetToIndex( -1 );
  204. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  205. expect( () => {
  206. element.offsetToIndex( 55 );
  207. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  208. } );
  209. it( 'should return length if given offset is equal to maxOffset', () => {
  210. expect( element.offsetToIndex( 5 ) ).to.equal( 3 );
  211. } );
  212. } );
  213. describe( 'toJSON', () => {
  214. it( 'should serialize empty element', () => {
  215. const element = new Element( 'one' );
  216. expect( jsonParseStringify( element ) ).to.deep.equal( { name: 'one' } );
  217. } );
  218. it( 'should serialize element with attributes', () => {
  219. const element = new Element( 'one', { foo: true, bar: false } );
  220. expect( jsonParseStringify( element ) ).to.deep.equal( {
  221. attributes: [ [ 'foo', true ], [ 'bar', false ] ],
  222. name: 'one'
  223. } );
  224. } );
  225. it( 'should serialize node with children', () => {
  226. const img = new Element( 'img' );
  227. const one = new Element( 'one' );
  228. const two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  229. const three = new Element( 'three' );
  230. const node = new Element( null, null, [ one, two, three ] );
  231. expect( jsonParseStringify( node ) ).to.deep.equal( {
  232. children: [
  233. { name: 'one' },
  234. {
  235. children: [
  236. { data: 'ba' },
  237. { name: 'img' },
  238. { data: 'r' }
  239. ],
  240. name: 'two'
  241. },
  242. { name: 'three' }
  243. ],
  244. name: null
  245. } );
  246. } );
  247. } );
  248. describe( 'fromJSON', () => {
  249. it( 'should create element without attributes', () => {
  250. const el = new Element( 'el' );
  251. const serialized = jsonParseStringify( el );
  252. const deserialized = Element.fromJSON( serialized );
  253. expect( deserialized.parent ).to.be.null;
  254. expect( deserialized.name ).to.equal( 'el' );
  255. expect( deserialized.childCount ).to.equal( 0 );
  256. } );
  257. it( 'should create element with attributes', () => {
  258. const el = new Element( 'el', { foo: true } );
  259. const serialized = jsonParseStringify( el );
  260. const deserialized = Element.fromJSON( serialized );
  261. expect( deserialized.parent ).to.be.null;
  262. expect( deserialized.name ).to.equal( 'el' );
  263. expect( deserialized.childCount ).to.equal( 0 );
  264. expect( deserialized.hasAttribute( 'foo' ) ).to.be.true;
  265. expect( deserialized.getAttribute( 'foo' ) ).to.be.true;
  266. } );
  267. it( 'should create element with children', () => {
  268. const p = new Element( 'p' );
  269. const text = new Text( 'foo' );
  270. const el = new Element( 'el', null, [ p, text ] );
  271. const serialized = jsonParseStringify( el );
  272. const deserialized = Element.fromJSON( serialized );
  273. expect( deserialized.parent ).to.be.null;
  274. expect( deserialized.name ).to.equal( 'el' );
  275. expect( deserialized.childCount ).to.equal( 2 );
  276. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  277. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  278. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  279. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  280. } );
  281. } );
  282. } );