element.js 14 KB

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