8
0

element.js 14 KB

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