view.js 25 KB

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