view.js 26 KB

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