8
0

element.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 foo = new Text( 'foo' );
  140. const image = new Element( 'image' );
  141. const element = new Element( 'elem', [], [
  142. new Element( 'elem', [], [
  143. foo,
  144. image
  145. ] )
  146. ] );
  147. expect( element.getNodeByPath( [ 0, 0 ] ) ).to.equal( foo );
  148. expect( element.getNodeByPath( [ 0, 1 ] ) ).to.equal( foo );
  149. expect( element.getNodeByPath( [ 0, 2 ] ) ).to.equal( foo );
  150. expect( element.getNodeByPath( [ 0, 3 ] ) ).to.equal( image );
  151. } );
  152. it( 'works fine with offsets', () => {
  153. const bar = new Text( 'bar' );
  154. const foo = new Text( 'foo' );
  155. const bom = new Text( 'bom' );
  156. const bold = new Element( 'b', [], [
  157. bar
  158. ] );
  159. const paragraph = new Element( 'paragraph', [], [
  160. foo,
  161. bold,
  162. bom
  163. ] );
  164. // <paragraph>foo<bold>bar</bold>bom</paragraph>
  165. expect( paragraph.getNodeByPath( [ 0 ] ) ).to.equal( foo );
  166. expect( paragraph.getNodeByPath( [ 1 ] ) ).to.equal( foo );
  167. expect( paragraph.getNodeByPath( [ 2 ] ) ).to.equal( foo );
  168. expect( paragraph.getNodeByPath( [ 3 ] ) ).to.equal( bold );
  169. expect( paragraph.getNodeByPath( [ 3, 0 ] ) ).to.equal( bar );
  170. expect( paragraph.getNodeByPath( [ 3, 1 ] ) ).to.equal( bar );
  171. expect( paragraph.getNodeByPath( [ 3, 2 ] ) ).to.equal( bar );
  172. expect( paragraph.getNodeByPath( [ 3, 3 ] ) ).to.equal( null );
  173. expect( paragraph.getNodeByPath( [ 4 ] ) ).to.equal( bom );
  174. expect( paragraph.getNodeByPath( [ 5 ] ) ).to.equal( bom );
  175. expect( paragraph.getNodeByPath( [ 6 ] ) ).to.equal( bom );
  176. expect( paragraph.getNodeByPath( [ 7 ] ) ).to.equal( null );
  177. } );
  178. } );
  179. describe( 'getChildIndex', () => {
  180. it( 'should return child index', () => {
  181. const element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  182. const p = element.getChild( 0 );
  183. const bar = element.getChild( 1 );
  184. const h = element.getChild( 2 );
  185. expect( element.getChildIndex( p ) ).to.equal( 0 );
  186. expect( element.getChildIndex( bar ) ).to.equal( 1 );
  187. expect( element.getChildIndex( h ) ).to.equal( 2 );
  188. } );
  189. } );
  190. describe( 'getChildStartOffset', () => {
  191. it( 'should return child offset', () => {
  192. const element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  193. const p = element.getChild( 0 );
  194. const bar = element.getChild( 1 );
  195. const h = element.getChild( 2 );
  196. expect( element.getChildStartOffset( p ) ).to.equal( 0 );
  197. expect( element.getChildStartOffset( bar ) ).to.equal( 1 );
  198. expect( element.getChildStartOffset( h ) ).to.equal( 4 );
  199. } );
  200. } );
  201. describe( 'getChildCount', () => {
  202. it( 'should return number of children', () => {
  203. const element = new Element( 'elem', [], new Text( 'bar' ) );
  204. expect( element.childCount ).to.equal( 1 );
  205. } );
  206. } );
  207. describe( 'getMaxOffset', () => {
  208. it( 'should return offset number after the last child', () => {
  209. const element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  210. expect( element.maxOffset ).to.equal( 5 );
  211. } );
  212. } );
  213. describe( 'isEmpty', () => {
  214. it( 'checks whether element has no children', () => {
  215. expect( new Element( 'a' ).isEmpty ).to.be.true;
  216. expect( new Element( 'a', null, new Text( 'x' ) ).isEmpty ).to.be.false;
  217. } );
  218. } );
  219. describe( 'offsetToIndex', () => {
  220. let element;
  221. beforeEach( () => {
  222. element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  223. } );
  224. it( 'should return index of a node that occupies given offset in this element', () => {
  225. expect( element.offsetToIndex( 0 ) ).to.equal( 0 );
  226. expect( element.offsetToIndex( 1 ) ).to.equal( 1 );
  227. expect( element.offsetToIndex( 2 ) ).to.equal( 1 );
  228. expect( element.offsetToIndex( 3 ) ).to.equal( 1 );
  229. expect( element.offsetToIndex( 4 ) ).to.equal( 2 );
  230. } );
  231. it( 'should throw if given offset is too high or too low', () => {
  232. expect( () => {
  233. element.offsetToIndex( -1 );
  234. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  235. expect( () => {
  236. element.offsetToIndex( 55 );
  237. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  238. } );
  239. it( 'should return length if given offset is equal to maxOffset', () => {
  240. expect( element.offsetToIndex( 5 ) ).to.equal( 3 );
  241. } );
  242. } );
  243. describe( 'toJSON', () => {
  244. it( 'should serialize empty element', () => {
  245. const element = new Element( 'one' );
  246. expect( jsonParseStringify( element ) ).to.deep.equal( { name: 'one' } );
  247. } );
  248. it( 'should serialize element with attributes', () => {
  249. const element = new Element( 'one', { foo: true, bar: false } );
  250. expect( jsonParseStringify( element ) ).to.deep.equal( {
  251. attributes: [ [ 'foo', true ], [ 'bar', false ] ],
  252. name: 'one'
  253. } );
  254. } );
  255. it( 'should serialize node with children', () => {
  256. const img = new Element( 'img' );
  257. const one = new Element( 'one' );
  258. const two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  259. const three = new Element( 'three' );
  260. const node = new Element( null, null, [ one, two, three ] );
  261. expect( jsonParseStringify( node ) ).to.deep.equal( {
  262. children: [
  263. { name: 'one' },
  264. {
  265. children: [
  266. { data: 'ba' },
  267. { name: 'img' },
  268. { data: 'r' }
  269. ],
  270. name: 'two'
  271. },
  272. { name: 'three' }
  273. ],
  274. name: null
  275. } );
  276. } );
  277. } );
  278. describe( 'fromJSON', () => {
  279. it( 'should create element without attributes', () => {
  280. const el = new Element( 'el' );
  281. const serialized = jsonParseStringify( el );
  282. const deserialized = Element.fromJSON( serialized );
  283. expect( deserialized.parent ).to.be.null;
  284. expect( deserialized.name ).to.equal( 'el' );
  285. expect( deserialized.childCount ).to.equal( 0 );
  286. } );
  287. it( 'should create element with attributes', () => {
  288. const el = new Element( 'el', { foo: true } );
  289. const serialized = jsonParseStringify( el );
  290. const deserialized = Element.fromJSON( serialized );
  291. expect( deserialized.parent ).to.be.null;
  292. expect( deserialized.name ).to.equal( 'el' );
  293. expect( deserialized.childCount ).to.equal( 0 );
  294. expect( deserialized.hasAttribute( 'foo' ) ).to.be.true;
  295. expect( deserialized.getAttribute( 'foo' ) ).to.be.true;
  296. } );
  297. it( 'should create element with children', () => {
  298. const p = new Element( 'p' );
  299. const text = new Text( 'foo' );
  300. const el = new Element( 'el', null, [ p, text ] );
  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( 2 );
  306. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  307. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  308. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  309. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  310. } );
  311. } );
  312. } );