view.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: browser-only */
  7. import { parse, stringify, getData, setData }from 'ckeditor5/engine/dev-utils/view.js';
  8. import DocumentFragment from 'ckeditor5/engine/view/documentfragment.js';
  9. import Position from 'ckeditor5/engine/view/position.js';
  10. import Element from 'ckeditor5/engine/view/element.js';
  11. import AttributeElement from 'ckeditor5/engine/view/attributeelement.js';
  12. import ContainerElement from 'ckeditor5/engine/view/containerelement.js';
  13. import WidgetElement from 'ckeditor5/engine/view/widgetelement.js';
  14. import EmptyElement from 'ckeditor5/engine/view/emptyelement.js';
  15. import Text from 'ckeditor5/engine/view/text.js';
  16. import Selection from 'ckeditor5/engine/view/selection.js';
  17. import Range from 'ckeditor5/engine/view/range.js';
  18. import Document from 'ckeditor5/engine/view/document.js';
  19. import XmlDataProcessor from 'ckeditor5/engine/dataprocessor/xmldataprocessor.js';
  20. describe( 'view test utils', () => {
  21. describe( 'getData, setData', () => {
  22. let sandbox;
  23. beforeEach( () => {
  24. sandbox = sinon.sandbox.create();
  25. } );
  26. afterEach( () => {
  27. sandbox.restore();
  28. } );
  29. describe( 'getData', () => {
  30. it( 'should use stringify method', () => {
  31. const element = document.createElement( 'div' );
  32. const stringifySpy = sandbox.spy( getData, '_stringify' );
  33. const viewDocument = new Document();
  34. const options = { showType: false, showPriority: false, withoutSelection: true };
  35. const root = viewDocument.createRoot( element );
  36. root.appendChildren( new Element( 'p' ) );
  37. expect( getData( viewDocument, options ) ).to.equal( '<p></p>' );
  38. sinon.assert.calledOnce( stringifySpy );
  39. expect( stringifySpy.firstCall.args[ 0 ] ).to.equal( root );
  40. expect( stringifySpy.firstCall.args[ 1 ] ).to.equal( null );
  41. const stringifyOptions = stringifySpy.firstCall.args[ 2 ];
  42. expect( stringifyOptions ).to.have.property( 'showType' ).that.equals( false );
  43. expect( stringifyOptions ).to.have.property( 'showPriority' ).that.equals( false );
  44. expect( stringifyOptions ).to.have.property( 'ignoreRoot' ).that.equals( true );
  45. } );
  46. it( 'should use stringify method with selection', () => {
  47. const element = document.createElement( 'div' );
  48. const stringifySpy = sandbox.spy( getData, '_stringify' );
  49. const viewDocument = new Document();
  50. const options = { showType: false, showPriority: false };
  51. const root = viewDocument.createRoot( element );
  52. root.appendChildren( new Element( 'p' ) );
  53. viewDocument.selection.addRange( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  54. expect( getData( viewDocument, options ) ).to.equal( '[<p></p>]' );
  55. sinon.assert.calledOnce( stringifySpy );
  56. expect( stringifySpy.firstCall.args[ 0 ] ).to.equal( root );
  57. expect( stringifySpy.firstCall.args[ 1 ] ).to.equal( viewDocument.selection );
  58. const stringifyOptions = stringifySpy.firstCall.args[ 2 ];
  59. expect( stringifyOptions ).to.have.property( 'showType' ).that.equals( false );
  60. expect( stringifyOptions ).to.have.property( 'showPriority' ).that.equals( false );
  61. expect( stringifyOptions ).to.have.property( 'ignoreRoot' ).that.equals( true );
  62. } );
  63. it( 'should throw an error when passing invalid document', () => {
  64. expect( () => {
  65. getData( { invalid: 'document' } );
  66. } ).to.throw( TypeError, 'Document needs to be an instance of engine.view.Document.' );
  67. } );
  68. } );
  69. describe( 'setData', () => {
  70. it( 'should use parse method', () => {
  71. const viewDocument = new Document();
  72. const data = 'foobar<b>baz</b>';
  73. const parseSpy = sandbox.spy( setData, '_parse' );
  74. viewDocument.createRoot( document.createElement( 'div' ) );
  75. setData( viewDocument, data );
  76. expect( getData( viewDocument ) ).to.equal( 'foobar<b>baz</b>' );
  77. sinon.assert.calledOnce( parseSpy );
  78. const args = parseSpy.firstCall.args;
  79. expect( args[ 0 ] ).to.equal( data );
  80. expect( args[ 1 ] ).to.be.an( 'object' );
  81. expect( args[ 1 ].rootElement ).to.equal( viewDocument.getRoot() );
  82. } );
  83. it( 'should use parse method with selection', () => {
  84. const viewDocument = new Document();
  85. const data = '[<b>baz</b>]';
  86. const parseSpy = sandbox.spy( setData, '_parse' );
  87. viewDocument.createRoot( document.createElement( 'div' ) );
  88. setData( viewDocument, data );
  89. expect( getData( viewDocument ) ).to.equal( '[<b>baz</b>]' );
  90. const args = parseSpy.firstCall.args;
  91. expect( args[ 0 ] ).to.equal( data );
  92. expect( args[ 1 ] ).to.be.an( 'object' );
  93. expect( args[ 1 ].rootElement ).to.equal( viewDocument.getRoot() );
  94. } );
  95. it( 'should throw an error when passing invalid document', () => {
  96. expect( () => {
  97. setData( { invalid: 'document' } );
  98. } ).to.throw( TypeError, 'Document needs to be an instance of engine.view.Document.' );
  99. } );
  100. } );
  101. } );
  102. describe( 'stringify', () => {
  103. it( 'should write text', () => {
  104. const text = new Text( 'foobar' );
  105. expect( stringify( text ) ).to.equal( 'foobar' );
  106. } );
  107. it( 'should write elements and texts', () => {
  108. const text = new Text( 'foobar' );
  109. const b = new Element( 'b', null, text );
  110. const p = new Element( 'p', null, b );
  111. expect( stringify( p ) ).to.equal( '<p><b>foobar</b></p>' );
  112. } );
  113. it( 'should write elements with attributes (attributes in alphabetical order)', () => {
  114. const text = new Text( 'foobar' );
  115. const b = new Element( 'b', {
  116. foo: 'bar'
  117. }, text );
  118. const p = new Element( 'p', {
  119. baz: 'qux',
  120. bar: 'taz',
  121. class: 'short wide'
  122. }, b );
  123. expect( stringify( p ) ).to.equal( '<p bar="taz" baz="qux" class="short wide"><b foo="bar">foobar</b></p>' );
  124. } );
  125. it( 'should write selection ranges inside elements', () => {
  126. const text1 = new Text( 'foobar' );
  127. const text2 = new Text( 'bazqux' );
  128. const b1 = new Element( 'b', null, text1 );
  129. const b2 = new Element( 'b', null, text2 );
  130. const p = new Element( 'p', null, [ b1, b2 ] );
  131. const range = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  132. const selection = new Selection();
  133. selection.addRange( range );
  134. expect( stringify( p, selection ) ).to.equal( '<p><b>foobar</b>[<b>bazqux</b>]</p>' );
  135. } );
  136. it( 'should support unicode', () => {
  137. const text = new Text( 'நிலைக்கு' );
  138. const b = new Element( 'b', null, text );
  139. const p = new Element( 'p', null, b );
  140. const range = Range.createFromParentsAndOffsets( p, 0, text, 4 );
  141. const selection = new Selection();
  142. selection.addRange( range );
  143. expect( stringify( p, selection ) ).to.equal( '<p>[<b>நிலை}க்கு</b></p>' );
  144. } );
  145. it( 'should write collapsed selection ranges inside elements', () => {
  146. const text = new Text( 'foobar' );
  147. const p = new Element( 'p', null, text );
  148. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  149. const selection = new Selection();
  150. selection.addRange( range );
  151. expect( stringify( p, selection ) ).to.equal( '<p>[]foobar</p>' );
  152. } );
  153. it( 'should write selection ranges inside text', () => {
  154. const text1 = new Text( 'foobar' );
  155. const text2 = new Text( 'bazqux' );
  156. const b1 = new Element( 'b', null, text1 );
  157. const b2 = new Element( 'b', null, text2 );
  158. const p = new Element( 'p', null, [ b1, b2 ] );
  159. const range = Range.createFromParentsAndOffsets( text1, 1, text1, 5 );
  160. const selection = new Selection();
  161. selection.addRange( range );
  162. expect( stringify( p, selection ) ).to.equal( '<p><b>f{ooba}r</b><b>bazqux</b></p>' );
  163. } );
  164. it( 'should write selection ranges inside text represented by `[` and `]` characters', () => {
  165. const text1 = new Text( 'foobar' );
  166. const text2 = new Text( 'bazqux' );
  167. const b1 = new Element( 'b', null, text1 );
  168. const b2 = new Element( 'b', null, text2 );
  169. const p = new Element( 'p', null, [ b1, b2 ] );
  170. const range = Range.createFromParentsAndOffsets( text1, 1, text1, 5 );
  171. const selection = new Selection();
  172. selection.addRange( range );
  173. expect( stringify( p, selection, { sameSelectionCharacters: true } ) )
  174. .to.equal( '<p><b>f[ooba]r</b><b>bazqux</b></p>' );
  175. } );
  176. it( 'should write collapsed selection ranges inside texts', () => {
  177. const text = new Text( 'foobar' );
  178. const p = new Element( 'p', null, text );
  179. const range = Range.createFromParentsAndOffsets( text, 0, text, 0 );
  180. const selection = new Selection();
  181. selection.addRange( range );
  182. expect( stringify( p, selection ) ).to.equal( '<p>{}foobar</p>' );
  183. } );
  184. it( 'should write ranges that start inside text end ends between elements', () => {
  185. const text1 = new Text( 'foobar' );
  186. const text2 = new Text( 'bazqux' );
  187. const b1 = new Element( 'b', null, text1 );
  188. const b2 = new Element( 'b', null, text2 );
  189. const p = new Element( 'p', null, [ b1, b2 ] );
  190. const range = Range.createFromParentsAndOffsets( p, 0, text2, 5 );
  191. const selection = new Selection();
  192. selection.addRange( range );
  193. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b><b>bazqu}x</b></p>' );
  194. } );
  195. it( 'should write elements types as namespaces when needed', () => {
  196. const text = new Text( 'foobar' );
  197. const b = new AttributeElement( 'b', null, text );
  198. const p = new ContainerElement( 'p', null, b );
  199. expect( stringify( p, null, { showType: true } ) )
  200. .to.equal( '<container:p><attribute:b>foobar</attribute:b></container:p>' );
  201. } );
  202. it( 'should not write element type when type is not specified', () => {
  203. const p = new Element( 'p' );
  204. expect( stringify( p, null, { showType: true } ) ).to.equal( '<p></p>' );
  205. } );
  206. it( 'should write elements priorities when needed', () => {
  207. const text = new Text( 'foobar' );
  208. const b = new AttributeElement( 'b', null, text );
  209. const p = new ContainerElement( 'p', null, b );
  210. expect( stringify( p, null, { showPriority: true } ) )
  211. .to.equal( '<p><b view-priority="10">foobar</b></p>' );
  212. } );
  213. it( 'should parse DocumentFragment as root', () => {
  214. const text1 = new Text( 'foobar' );
  215. const text2 = new Text( 'bazqux' );
  216. const b1 = new Element( 'b', null, text1 );
  217. const b2 = new Element( 'b', null, text2 );
  218. const fragment = new DocumentFragment( [ b1, b2 ] );
  219. expect( stringify( fragment, null ) ).to.equal( '<b>foobar</b><b>bazqux</b>' );
  220. } );
  221. it( 'should not write ranges outside elements - end position outside element', () => {
  222. const text = new Text( 'foobar' );
  223. const b = new Element( 'b', null, text );
  224. const p = new Element( 'p', null, b );
  225. const range = Range.createFromParentsAndOffsets( p, 0, p, 5 );
  226. expect( stringify( p, range ) ).to.equal( '<p>[<b>foobar</b></p>' );
  227. } );
  228. it( 'should not write ranges outside elements - start position outside element', () => {
  229. const text = new Text( 'foobar' );
  230. const b = new Element( 'b', null, text );
  231. const p = new Element( 'p', null, b );
  232. const range = Range.createFromParentsAndOffsets( p, -1, p, 1 );
  233. expect( stringify( p, range ) ).to.equal( '<p><b>foobar</b>]</p>' );
  234. } );
  235. it( 'should not write ranges outside elements - end position outside text', () => {
  236. const text = new Text( 'foobar' );
  237. const b = new Element( 'b', null, text );
  238. const p = new Element( 'p', null, b );
  239. const range = Range.createFromParentsAndOffsets( text, 0, text, 7 );
  240. expect( stringify( p, range ) ).to.equal( '<p><b>{foobar</b></p>' );
  241. } );
  242. it( 'should not write ranges outside elements - start position outside text', () => {
  243. const text = new Text( 'foobar' );
  244. const b = new Element( 'b', null, text );
  245. const p = new Element( 'p', null, b );
  246. const range = Range.createFromParentsAndOffsets( text, -1, text, 2 );
  247. expect( stringify( p, range ) ).to.equal( '<p><b>fo}obar</b></p>' );
  248. } );
  249. it( 'should write multiple ranges from selection #1', () => {
  250. const text1 = new Text( 'foobar' );
  251. const text2 = new Text( 'bazqux' );
  252. const b1 = new Element( 'b', null, text1 );
  253. const b2 = new Element( 'b', null, text2 );
  254. const p = new Element( 'p', null, [ b1, b2 ] );
  255. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  256. const range2 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  257. const selection = new Selection();
  258. selection.setRanges( [ range2, range1 ] );
  259. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]<b>bazqux</b></p>' );
  260. } );
  261. it( 'should write multiple ranges from selection #2', () => {
  262. const text1 = new Text( 'foobar' );
  263. const text2 = new Text( 'bazqux' );
  264. const b = new Element( 'b', null, text1 );
  265. const p = new Element( 'p', null, [ b, text2 ] );
  266. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  267. const range2 = Range.createFromParentsAndOffsets( text2, 0, text2, 3 );
  268. const range3 = Range.createFromParentsAndOffsets( text2, 3, text2, 4 );
  269. const range4 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  270. const selection = new Selection();
  271. selection.setRanges( [ range1, range2, range3, range4 ] );
  272. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]{baz}{q}ux</p>' );
  273. } );
  274. it( 'should use Position instance instead of Selection', () => {
  275. const text = new Text( 'foobar' );
  276. const position = new Position( text, 3 );
  277. const string = stringify( text, position );
  278. expect( string ).to.equal( 'foo{}bar' );
  279. } );
  280. it( 'should use Range instance instead of Selection', () => {
  281. const text = new Text( 'foobar' );
  282. const range = Range.createFromParentsAndOffsets( text, 3, text, 4 );
  283. const string = stringify( text, range );
  284. expect( string ).to.equal( 'foo{b}ar' );
  285. } );
  286. it( 'should stringify EmptyElement', () => {
  287. const img = new EmptyElement( 'img' );
  288. const p = new ContainerElement( 'p', null, img );
  289. expect( stringify( p, null, { showType: true } ) )
  290. .to.equal( '<container:p><empty:img></empty:img></container:p>' );
  291. } );
  292. it( 'should stringify single WidgetElement', () => {
  293. const figure = new WidgetElement( 'figure' );
  294. expect( stringify( figure , null, { showType: true } ) )
  295. .to.equal( '<widget:figure contenteditable="false"></widget:figure>' );
  296. } );
  297. it( 'should stringify single WidgetElement inside ContainerElement', () => {
  298. const figure = new WidgetElement( 'figure' );
  299. const container = new ContainerElement( 'p', null, figure );
  300. expect( stringify( container , null, { showType: true } ) )
  301. .to.equal( '<container:p><widget:figure contenteditable="false"></widget:figure></container:p>' );
  302. } );
  303. it( 'should stringify single WidgetElement with children', () => {
  304. const text = new Text( 'foo bar' );
  305. const b = new AttributeElement( 'b', null, text );
  306. const figure = new WidgetElement( 'figure', null, b );
  307. expect( stringify( figure , null, { showType: true } ) )
  308. .to.equal( '<widget:figure contenteditable="false"><attribute:b>foo bar</attribute:b></widget:figure>' );
  309. } );
  310. } );
  311. describe( 'parse', () => {
  312. it( 'should return empty DocumentFragment for empty string', () => {
  313. const fragment = parse( '' );
  314. expect( fragment ).to.be.instanceOf( DocumentFragment );
  315. expect( fragment.childCount ).to.equal( 0 );
  316. } );
  317. it( 'should return empty DocumentFragment and Selection for string containing range only', () => {
  318. const { view, selection } = parse( '[]' );
  319. expect( view ).to.be.instanceOf( DocumentFragment );
  320. expect( selection.rangeCount ).to.equal( 1 );
  321. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( view, 0, view, 0 ) ) ).to.be.true;
  322. } );
  323. it( 'should return Element if range is around single element', () => {
  324. const { view, selection } = parse( '[<b>foobar</b>]' );
  325. const parent = view.parent;
  326. expect( view ).to.be.instanceOf( Element );
  327. expect( parent ).to.be.instanceOf( DocumentFragment );
  328. expect( selection.rangeCount ).to.equal( 1 );
  329. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( parent, 0, parent, 1 ) ) ).to.be.true;
  330. } );
  331. it( 'should create DocumentFragment when multiple elements on root', () => {
  332. const view = parse( '<b></b><i></i>' );
  333. expect( view ).to.be.instanceOf( DocumentFragment );
  334. expect( view.childCount ).to.equal( 2 );
  335. expect( view.getChild( 0 ).isSimilar( new Element( 'b' ) ) ).to.be.true;
  336. expect( view.getChild( 1 ).isSimilar( new Element( 'i' ) ) ).to.be.true;
  337. } );
  338. it( 'should parse text', () => {
  339. const text = parse( 'foobar' );
  340. expect( text ).to.be.instanceOf( Text );
  341. expect( text.data ).to.equal( 'foobar' );
  342. } );
  343. it( 'should parse text with spaces', () => {
  344. const text = parse( 'foo bar' );
  345. expect( text ).to.be.instanceOf( Text );
  346. expect( text.data ).to.equal( 'foo bar' );
  347. } );
  348. it( 'should parse elements and texts', () => {
  349. const view = parse( '<b>foobar</b>' );
  350. const element = new Element( 'b' );
  351. expect( view ).to.be.instanceof( Element );
  352. expect( view.isSimilar( element ) ).to.be.true;
  353. expect( view.childCount ).to.equal( 1 );
  354. const text = view.getChild( 0 );
  355. expect( text ).to.be.instanceof( Text );
  356. expect( text.data ).to.equal( 'foobar' );
  357. } );
  358. it( 'should parse element attributes', () => {
  359. const view = parse( '<b name="foo" title="bar" class="foo bar" style="color:red;"></b>' );
  360. const element = new Element( 'b', { name: 'foo', title: 'bar', class: 'foo bar', style: 'color:red;' } );
  361. expect( view ).to.be.instanceof( Element );
  362. expect( view.isSimilar( element ) ).to.be.true;
  363. expect( view.childCount ).to.equal( 0 );
  364. } );
  365. it( 'should parse element type', () => {
  366. const view1 = parse( '<attribute:b></attribute:b>' );
  367. const attribute = new AttributeElement( 'b' );
  368. const view2 = parse( '<container:p></container:p>' );
  369. const container = new ContainerElement( 'p' );
  370. expect( view1 ).to.be.instanceof( AttributeElement );
  371. expect( view1.isSimilar( attribute ) ).to.be.true;
  372. expect( view2 ).to.be.instanceof( ContainerElement );
  373. expect( view2.isSimilar( container ) ).to.be.true;
  374. } );
  375. it( 'should parse element priority', () => {
  376. const parsed1 = parse( '<b view-priority="12"></b>' );
  377. const attribute1 = new AttributeElement( 'b' );
  378. attribute1.priority = 12;
  379. const parsed2 = parse( '<attribute:b view-priority="44"></attribute:b>' );
  380. const attribute2 = new AttributeElement( 'b' );
  381. attribute2.priority = 44;
  382. parsed1.isSimilar( attribute1 );
  383. expect( parsed1.isSimilar( attribute1 ) ).to.be.true;
  384. expect( parsed2.isSimilar( attribute2 ) ).to.be.true;
  385. } );
  386. it( 'should paste nested elements and texts', () => {
  387. const parsed = parse( '<container:p>foo<b view-priority="12">bar<i view-priority="25">qux</i></b></container:p>' );
  388. expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  389. expect( parsed.childCount ).to.equal( 2 );
  390. expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  391. const b = parsed.getChild( 1 );
  392. expect( b ).to.be.instanceof( AttributeElement );
  393. expect( b.priority ).to.equal( 12 );
  394. expect( b.childCount ).to.equal( 2 );
  395. expect( b.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  396. const i = b.getChild( 1 );
  397. expect( i ).to.be.instanceof( AttributeElement );
  398. expect( i.priority ).to.equal( 25 );
  399. expect( i.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'qux' );
  400. } );
  401. it( 'should parse selection range inside text', () => {
  402. const { view, selection } = parse( 'f{oo}b{}ar' );
  403. expect( view ).to.be.instanceof( Text );
  404. expect( view.data ).to.equal( 'foobar' );
  405. expect( selection.rangeCount ).to.equal( 2 );
  406. const ranges = [ ...selection.getRanges() ];
  407. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 3 ) ) ).to.be.true;
  408. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 4, view, 4 ) ) ).to.be.true;
  409. } );
  410. it( 'should parse selection range between elements', () => {
  411. const { view, selection } = parse( '<p>[<b>foobar]</b>[]</p>' );
  412. expect( view ).to.be.instanceof( Element );
  413. expect( view.childCount ).to.equal( 1 );
  414. const b = view.getChild( 0 );
  415. expect( b ).to.be.instanceof( Element );
  416. expect( b.name ).to.equal( 'b' );
  417. expect( b.childCount ).to.equal( 1 );
  418. const text = b.getChild( 0 );
  419. expect( text ).to.be.instanceof( Text );
  420. expect( text.data ).to.equal( 'foobar' );
  421. expect( selection.rangeCount ).to.equal( 2 );
  422. const ranges = [ ...selection.getRanges() ];
  423. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, b, 1 ) ) ).to.be.true;
  424. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 1 ) ) ).to.be.true;
  425. } );
  426. it( 'should support unicode', () => {
  427. const { view, selection } = parse( '<p>[<b>நிலை}க்கு</b></p>' );
  428. expect( view ).to.be.instanceof( Element );
  429. expect( view.name ).to.equal( 'p' );
  430. expect( view.childCount ).to.equal( 1 );
  431. const b = view.getChild( 0 );
  432. expect( b.name ).to.equal( 'b' );
  433. expect( b.childCount ).to.equal( 1 );
  434. const text = b.getChild( 0 );
  435. expect( text.data ).to.equal( 'நிலைக்கு' );
  436. expect( selection.rangeCount ).to.equal( 1 );
  437. const range = selection.getFirstRange();
  438. expect( range.start.parent ).to.equal( view );
  439. expect( range.start.offset ).to.equal( 0 );
  440. expect( range.end.parent ).to.equal( text );
  441. expect( range.end.offset ).to.equal( 4 );
  442. } );
  443. it( 'should parse ranges #1', () => {
  444. const { view, selection } = parse( '<container:p>foo{bar]</container:p>' );
  445. expect( view.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  446. expect( view.childCount ).to.equal( 1 );
  447. const text = view.getChild( 0 );
  448. expect( text ).to.be.instanceof( Text );
  449. expect( text.data ).to.equal( 'foobar' );
  450. expect( selection.rangeCount ).to.equal( 1 );
  451. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( text, 3, view, 1 ) ) ).to.be.true;
  452. } );
  453. it( 'should parse ranges #2', () => {
  454. const { view, selection } = parse( '<attribute:b>[foob}ar<i>{baz</i>]</attribute:b>' );
  455. expect( view.isSimilar( new AttributeElement( 'b' ) ) ).to.be.true;
  456. expect( view.childCount ).to.equal( 2 );
  457. const text1 = view.getChild( 0 );
  458. expect( text1 ).to.be.instanceof( Text );
  459. expect( text1.data ).to.equal( 'foobar' );
  460. const i = view.getChild( 1 );
  461. expect( i.isSimilar( new Element( 'i' ) ) ).to.be.true;
  462. expect( i.childCount ).to.equal( 1 );
  463. const text2 = i.getChild( 0 );
  464. expect( text2 ).to.be.instanceof( Text );
  465. expect( text2.data ).to.equal( 'baz' );
  466. expect( selection.rangeCount ).to.equal( 2 );
  467. const ranges = [ ...selection.getRanges() ];
  468. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, text1, 4 ) ) ).to.be.true;
  469. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( text2, 0, view, 2 ) ) ).to.be.true;
  470. } );
  471. it( 'should use ranges order when provided', () => {
  472. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ] } );
  473. expect( selection.rangeCount ).to.equal( 3 );
  474. const ranges = [ ...selection.getRanges() ];
  475. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  476. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  477. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  478. expect( selection.anchor.isEqual( ranges[ 2 ].start ) ).to.be.true;
  479. expect( selection.focus.isEqual( ranges[ 2 ].end ) ).to.be.true;
  480. } );
  481. it( 'should set last range backward if needed', () => {
  482. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ], lastRangeBackward: true } );
  483. expect( selection.rangeCount ).to.equal( 3 );
  484. const ranges = [ ...selection.getRanges() ];
  485. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  486. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  487. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  488. expect( selection.anchor.isEqual( ranges[ 2 ].end ) ).to.be.true;
  489. expect( selection.focus.isEqual( ranges[ 2 ].start ) ).to.be.true;
  490. } );
  491. it( 'should throw when ranges order does not include all ranges', () => {
  492. expect( () => {
  493. parse( '{}foobar{}', { order: [ 1 ] } );
  494. } ).to.throw( Error );
  495. } );
  496. it( 'should throw when ranges order is invalid', () => {
  497. expect( () => {
  498. parse( '{}foobar{}', { order: [ 1, 4 ] } );
  499. } ).to.throw( Error );
  500. } );
  501. it( 'should throw when element range delimiter is inside text node', () => {
  502. expect( () => {
  503. parse( 'foo[bar' );
  504. } ).to.throw( Error );
  505. } );
  506. it( 'should throw when text range delimiter is inside empty text node', () => {
  507. expect( () => {
  508. parse( '<b>foo</b>}' );
  509. } ).to.throw( Error );
  510. } );
  511. it( 'should throw when end of range is found before start', () => {
  512. expect( () => {
  513. parse( 'fo}obar' );
  514. } ).to.throw( Error );
  515. } );
  516. it( 'should throw when intersecting ranges found', () => {
  517. expect( () => {
  518. parse( '[fo{o}bar]' );
  519. } ).to.throw( Error );
  520. } );
  521. it( 'should throw when opened ranges are left', () => {
  522. expect( () => {
  523. parse( 'fo{obar' );
  524. } ).to.throw( Error );
  525. } );
  526. it( 'should throw when wrong type is provided', () => {
  527. sinon.stub( XmlDataProcessor.prototype, 'toView', () => {
  528. return new ContainerElement( 'invalidType:b' );
  529. } );
  530. expect( () => {
  531. parse( 'sth' );
  532. } ).to.throw( Error, `Parse error - cannot parse element's name: invalidType:b.` );
  533. XmlDataProcessor.prototype.toView.restore();
  534. } );
  535. it( 'should use provided root element #1', () => {
  536. const root = new Element( 'p' );
  537. const data = parse( '<span>text</span>', { rootElement: root } );
  538. expect( stringify( data ) ).to.equal( '<p><span>text</span></p>' );
  539. } );
  540. it( 'should use provided root element #2', () => {
  541. const root = new Element( 'p' );
  542. const data = parse( '<span>text</span><b>test</b>', { rootElement: root } );
  543. expect( stringify( data ) ).to.equal( '<p><span>text</span><b>test</b></p>' );
  544. } );
  545. it( 'should parse EmptyElement', () => {
  546. const parsed = parse( '<empty:img></empty:img>' );
  547. expect( parsed ).to.be.instanceof( EmptyElement );
  548. } );
  549. it( 'should throw an error if EmptyElement is not empty', () => {
  550. expect( () => {
  551. parse( '<empty:img>foo bar</empty:img>' );
  552. } ).to.throw( Error, 'Parse error - cannot parse inside EmptyElement.' );
  553. } );
  554. it( 'should parse WidgetElement', () => {
  555. const data = parse( '<widget:figure></widget:figure>' );
  556. expect( stringify( data, null, { showType: true } ) )
  557. .to.equal( '<widget:figure contenteditable="false"></widget:figure>' );
  558. } );
  559. it( 'should parse WidgetElement with children', () => {
  560. const data = parse( '<widget:figure><attribute:b>foo bar</attribute:b></widget:figure>' );
  561. expect( stringify( data, null, { showType: true } ) )
  562. .to.equal( '<widget:figure contenteditable="false"><attribute:b>foo bar</attribute:b></widget:figure>' );
  563. } );
  564. } );
  565. } );