model.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { stringify, parse, getData, setData } from '../../src/dev-utils/model';
  6. import Document from '../../src/model/document';
  7. import DocumentFragment from '../../src/model/documentfragment';
  8. import Element from '../../src/model/element';
  9. import Text from '../../src/model/text';
  10. import Range from '../../src/model/range';
  11. import Position from '../../src/model/position';
  12. import count from '@ckeditor/ckeditor5-utils/src/count';
  13. describe( 'model test utils', () => {
  14. let document, root, selection, sandbox;
  15. beforeEach( () => {
  16. document = new Document();
  17. root = document.createRoot();
  18. selection = document.selection;
  19. sandbox = sinon.sandbox.create();
  20. selection.removeAllRanges();
  21. document.schema.registerItem( 'a', '$inline' );
  22. document.schema.allow( { name: 'a', inside: '$root' } );
  23. document.schema.allow( { name: 'a', inside: '$root', attributes: [ 'bar', 'car', 'foo' ] } );
  24. document.schema.registerItem( 'b', '$inline' );
  25. document.schema.allow( { name: 'b', inside: '$root' } );
  26. document.schema.allow( { name: 'b', inside: '$root', attributes: [ 'barFoo', 'fooBar', 'x' ] } );
  27. document.schema.registerItem( 'c', '$inline' );
  28. document.schema.allow( { name: 'c', inside: '$root' } );
  29. document.schema.registerItem( 'paragraph', '$block' );
  30. document.schema.allow( { name: '$text', inside: '$root' } );
  31. document.schema.allow( { name: '$text', inside: 'a' } );
  32. document.schema.allow( { name: '$text', inside: 'b' } );
  33. document.schema.allow( { name: 'c', inside: 'b' } );
  34. } );
  35. afterEach( () => {
  36. sandbox.restore();
  37. } );
  38. describe( 'getData', () => {
  39. it( 'should use stringify method', () => {
  40. const stringifySpy = sandbox.spy( getData, '_stringify' );
  41. root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
  42. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<b>btext</b>' );
  43. sinon.assert.calledOnce( stringifySpy );
  44. sinon.assert.calledWithExactly( stringifySpy, root );
  45. } );
  46. it( 'should use stringify method with selection', () => {
  47. const stringifySpy = sandbox.spy( getData, '_stringify' );
  48. root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
  49. document.selection.addRange( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  50. expect( getData( document ) ).to.equal( '[<b>btext</b>]' );
  51. sinon.assert.calledOnce( stringifySpy );
  52. sinon.assert.calledWithExactly( stringifySpy, root, document.selection );
  53. } );
  54. it( 'should throw an error when passing invalid document', () => {
  55. expect( () => {
  56. getData( { invalid: 'document' } );
  57. } ).to.throw( TypeError, 'Document needs to be an instance of module:engine/model/document~Document.' );
  58. } );
  59. } );
  60. describe( 'setData', () => {
  61. it( 'should use parse method', () => {
  62. const parseSpy = sandbox.spy( setData, '_parse' );
  63. const options = {};
  64. const data = '<b>btext</b>text';
  65. setData( document, data, options );
  66. expect( getData( document, { withoutSelection: true } ) ).to.equal( data );
  67. sinon.assert.calledOnce( parseSpy );
  68. const args = parseSpy.firstCall.args;
  69. expect( args[ 0 ] ).to.equal( data );
  70. } );
  71. it( 'should use parse method with selection', () => {
  72. const parseSpy = sandbox.spy( setData, '_parse' );
  73. const options = {};
  74. const data = '[<b>btext</b>]';
  75. setData( document, data, options );
  76. expect( getData( document ) ).to.equal( data );
  77. sinon.assert.calledOnce( parseSpy );
  78. const args = parseSpy.firstCall.args;
  79. expect( args[ 0 ] ).to.equal( data );
  80. } );
  81. it( 'should insert text', () => {
  82. test( 'this is test text', '[]this is test text' );
  83. } );
  84. it( 'should insert text with selection around', () => {
  85. test( '[this is test text]' );
  86. } );
  87. it( 'should insert text with selection inside #1', () => {
  88. test( 'this [is test] text' );
  89. } );
  90. it( 'should insert text with selection inside #2', () => {
  91. test( '[this is test] text' );
  92. } );
  93. it( 'should insert text with selection inside #3', () => {
  94. test( 'this is [test text]' );
  95. } );
  96. it( 'should insert unicode text with selection', () => {
  97. test( 'நி[லைக்]கு' );
  98. } );
  99. it( 'should insert element', () => {
  100. test( '<b>foo bar</b>', '[]<b>foo bar</b>' );
  101. } );
  102. it( 'should insert element with selection inside #1', () => {
  103. test( '<b>[foo ]bar</b>' );
  104. } );
  105. it( 'should insert element with selection inside #2', () => {
  106. test( '[<b>foo ]bar</b>' );
  107. } );
  108. it( 'should insert element with selection inside #3', () => {
  109. test( '<b>[foo bar</b>]' );
  110. } );
  111. it( 'should insert backward selection', () => {
  112. setData( document, '<b>[foo bar</b>]', { lastRangeBackward: true } );
  113. expect( getData( document ) ).to.equal( '<b>[foo bar</b>]' );
  114. expect( document.selection.isBackward ).to.true;
  115. } );
  116. it( 'should throw an error when passing invalid document', () => {
  117. expect( () => {
  118. setData( { invalid: 'document' } );
  119. } ).to.throw( TypeError, 'Document needs to be an instance of module:engine/model/document~Document.' );
  120. } );
  121. it( 'should set attributes to the selection', () => {
  122. setData( document, '<b>[foo bar]</b>', { selectionAttributes: { foo: 'bar' } } );
  123. expect( document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  124. } );
  125. // #815.
  126. it( 'should work in a special root', () => {
  127. const document = new Document();
  128. document.schema.registerItem( 'textOnly' );
  129. document.schema.allow( { name: '$text', inside: 'textOnly' } );
  130. document.createRoot( 'textOnly', 'textOnly' );
  131. setData( document, 'a[b]c', { rootName: 'textOnly' } );
  132. expect( getData( document, { rootName: 'textOnly' } ) ).to.equal( 'a[b]c' );
  133. } );
  134. function test( data, expected ) {
  135. expected = expected || data;
  136. setData( document, data );
  137. expect( getData( document ) ).to.equal( expected );
  138. }
  139. } );
  140. describe( 'stringify', () => {
  141. it( 'should stringify text', () => {
  142. const text = new Text( 'text', { underline: true, bold: true } );
  143. expect( stringify( text ) ).to.equal( '<$text bold="true" underline="true">text</$text>' );
  144. } );
  145. it( 'should stringify element', () => {
  146. const element = new Element( 'a', null, [
  147. new Element( 'b', null, new Text( 'btext' ) ),
  148. new Text( 'atext' )
  149. ] );
  150. expect( stringify( element ) ).to.equal( '<a><b>btext</b>atext</a>' );
  151. } );
  152. it( 'should stringify document fragment', () => {
  153. const fragment = new DocumentFragment( [
  154. new Element( 'b', null, new Text( 'btext' ) ),
  155. new Text( 'atext' )
  156. ] );
  157. expect( stringify( fragment ) ).to.equal( '<b>btext</b>atext' );
  158. } );
  159. it( 'writes elements and texts', () => {
  160. root.appendChildren( [
  161. new Element( 'a', null, new Text( 'atext' ) ),
  162. new Element( 'b', null, [
  163. new Element( 'c1' ),
  164. new Text( 'ctext' ),
  165. new Element( 'c2' )
  166. ] ),
  167. new Element( 'd' )
  168. ] );
  169. expect( stringify( root ) ).to.equal(
  170. '<a>atext</a><b><c1></c1>ctext<c2></c2></b><d></d>'
  171. );
  172. } );
  173. it( 'writes element attributes', () => {
  174. root.appendChildren(
  175. new Element( 'a', { foo: true, bar: 1, car: false }, [
  176. new Element( 'b', { fooBar: 'x y', barFoo: { x: 1, y: 2 } } )
  177. ] )
  178. );
  179. // Note: attributes are written in a very simplistic way, because they are not to be parsed. They are just
  180. // to be compared in the tests with some patterns.
  181. expect( stringify( root ) ).to.equal(
  182. '<a bar="1" car="false" foo="true"><b barFoo="{"x":1,"y":2}" fooBar="x y"></b></a>'
  183. );
  184. } );
  185. it( 'writes text attributes', () => {
  186. root.appendChildren( [
  187. new Text( 'foo', { bold: true } ),
  188. new Text( 'bar' ),
  189. new Text( 'bom', { bold: true, italic: true } ),
  190. new Element( 'a', null, [
  191. new Text( 'pom', { underline: true, bold: true } )
  192. ] )
  193. ] );
  194. expect( stringify( root ) ).to.equal(
  195. // Because of https://github.com/ckeditor/ckeditor5-engine/issues/562 attributes are not merged
  196. '<$text bold="true">foo</$text>bar<$text bold="true"><$text italic="true">bom</$text></$text>' +
  197. '<a><$text bold="true" underline="true">pom</$text></a>'
  198. );
  199. } );
  200. it( 'writes unicode text', () => {
  201. root.appendChildren( new Text( 'நிலைக்கு' ) );
  202. expect( stringify( root ) ).to.equal( 'நிலைக்கு' );
  203. } );
  204. describe( 'selection', () => {
  205. let elA, elB;
  206. beforeEach( () => {
  207. elA = new Element( 'a' );
  208. elB = new Element( 'b' );
  209. root.appendChildren( [
  210. elA,
  211. new Text( 'foo' ),
  212. new Text( 'bar', { bold: true } ),
  213. elB
  214. ] );
  215. } );
  216. it( 'writes selection in an empty root', () => {
  217. const root = document.createRoot( '$root', 'empty' );
  218. selection.setCollapsedAt( root );
  219. expect( stringify( root, selection ) ).to.equal(
  220. '[]'
  221. );
  222. } );
  223. it( 'writes selection collapsed in an element', () => {
  224. selection.setCollapsedAt( root );
  225. expect( stringify( root, selection ) ).to.equal(
  226. '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
  227. );
  228. } );
  229. it( 'writes selection collapsed in a text', () => {
  230. selection.setCollapsedAt( root, 3 );
  231. expect( stringify( root, selection ) ).to.equal(
  232. '<a></a>fo[]o<$text bold="true">bar</$text><b></b>'
  233. );
  234. } );
  235. it( 'writes selection collapsed at the text left boundary', () => {
  236. selection.setCollapsedAt( elA, 'after' );
  237. expect( stringify( root, selection ) ).to.equal(
  238. '<a></a>[]foo<$text bold="true">bar</$text><b></b>'
  239. );
  240. } );
  241. it( 'writes selection collapsed at the text right boundary', () => {
  242. selection.setCollapsedAt( elB, 'before' );
  243. expect( stringify( root, selection ) ).to.equal(
  244. '<a></a>foo<$text bold="true">bar[]</$text><b></b>'
  245. );
  246. } );
  247. it( 'writes selection collapsed at the end of the root', () => {
  248. selection.setCollapsedAt( root, 'end' );
  249. // Needed due to https://github.com/ckeditor/ckeditor5-engine/issues/320.
  250. selection.clearAttributes();
  251. expect( stringify( root, selection ) ).to.equal(
  252. '<a></a>foo<$text bold="true">bar</$text><b></b>[]'
  253. );
  254. } );
  255. it( 'writes selection collapsed selection in a text with attributes', () => {
  256. selection.setCollapsedAt( root, 5 );
  257. expect( stringify( root, selection ) ).to.equal(
  258. '<a></a>foo<$text bold="true">b[]ar</$text><b></b>'
  259. );
  260. } );
  261. it( 'writes flat selection containing couple of nodes', () => {
  262. selection.addRange(
  263. Range.createFromParentsAndOffsets( root, 0, root, 4 )
  264. );
  265. expect( stringify( root, selection ) ).to.equal(
  266. '[<a></a>foo]<$text bold="true">bar</$text><b></b>'
  267. );
  268. } );
  269. it( 'writes flat selection within text', () => {
  270. selection.addRange(
  271. Range.createFromParentsAndOffsets( root, 2, root, 3 )
  272. );
  273. expect( stringify( root, selection ) ).to.equal(
  274. '<a></a>f[o]o<$text bold="true">bar</$text><b></b>'
  275. );
  276. } );
  277. it( 'writes multi-level selection', () => {
  278. selection.addRange(
  279. Range.createFromParentsAndOffsets( elA, 0, elB, 0 )
  280. );
  281. expect( stringify( root, selection ) ).to.equal(
  282. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  283. );
  284. } );
  285. it( 'writes selection when is backward', () => {
  286. selection.addRange(
  287. Range.createFromParentsAndOffsets( elA, 0, elB, 0 ),
  288. true
  289. );
  290. expect( stringify( root, selection ) ).to.equal(
  291. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  292. );
  293. } );
  294. it( 'writes selection in unicode text', () => {
  295. const root = document.createRoot( '$root', 'empty' );
  296. root.appendChildren( new Text( 'நிலைக்கு' ) );
  297. selection.addRange( Range.createFromParentsAndOffsets( root, 2, root, 6 ) );
  298. expect( stringify( root, selection ) ).to.equal( 'நி[லைக்]கு' );
  299. } );
  300. it( 'uses range and coverts it to selection', () => {
  301. const range = Range.createFromParentsAndOffsets( elA, 0, elB, 0 );
  302. expect( stringify( root, range ) ).to.equal(
  303. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  304. );
  305. } );
  306. it( 'uses position and converts it to collapsed selection', () => {
  307. const position = new Position( root, [ 0 ] );
  308. expect( stringify( root, position ) ).to.equal(
  309. '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
  310. );
  311. } );
  312. } );
  313. } );
  314. describe( 'parse', () => {
  315. test( 'creates empty DocumentFragment from empty string', {
  316. data: '',
  317. check( fragment ) {
  318. expect( fragment ).to.be.instanceOf( DocumentFragment );
  319. }
  320. } );
  321. test( 'creates empty DocumentFragment with selection', {
  322. data: '[]',
  323. check( fragment, selection ) {
  324. expect( fragment ).to.be.instanceOf( DocumentFragment );
  325. expect( fragment.childCount ).to.equal( 0 );
  326. expect( selection.rangeCount ).to.equal( 1 );
  327. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( fragment, 0, fragment, 0 ) ) ).to.be.true;
  328. }
  329. } );
  330. test( 'returns Element if range is around single element', {
  331. data: '[<a></a>]',
  332. check( el, selection ) {
  333. const fragment = el.parent;
  334. expect( el ).to.be.instanceOf( Element );
  335. expect( fragment ).to.be.instanceOf( DocumentFragment );
  336. expect( selection.rangeCount ).to.equal( 1 );
  337. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( fragment, 0, fragment, 1 ) ) ).to.be.true;
  338. }
  339. } );
  340. test( 'returns DocumentFragment when multiple elements on root', {
  341. data: '<a></a><b></b>',
  342. check( fragment ) {
  343. expect( fragment ).to.be.instanceOf( DocumentFragment );
  344. expect( fragment.childCount ).to.equal( 2 );
  345. }
  346. } );
  347. test( 'creates elements', {
  348. data: '<a></a><b><c></c></b>'
  349. } );
  350. test( 'creates text nodes', {
  351. data: 'foo<a>bar</a>bom'
  352. } );
  353. test( 'creates text nodes with unicode text', {
  354. data: 'நிலைக்கு'
  355. } );
  356. test( 'sets elements attributes', {
  357. data: '<a bar="true" car="x y" foo="1"></a><b x="y"></b>',
  358. output: '<a bar="true" car="x y" foo="1"></a><b x="y"></b>',
  359. check( root ) {
  360. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.equal( true );
  361. expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
  362. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 1 );
  363. expect( root.getChild( 1 ).getAttribute( 'x' ) ).to.equal( 'y' );
  364. }
  365. } );
  366. test( 'sets text attributes', {
  367. data: '<$text bar="true" car="x y" foo="1">foo</$text><$text x="y">bar</$text>bom',
  368. check( root ) {
  369. expect( root.childCount ).to.equal( 3 );
  370. expect( root.maxOffset ).to.equal( 9 );
  371. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.equal( true );
  372. expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
  373. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 1 );
  374. expect( root.getChild( 1 ).getAttribute( 'x' ) ).to.equal( 'y' );
  375. expect( count( root.getChild( 2 ).getAttributes() ) ).to.equal( 0 );
  376. }
  377. } );
  378. test( 'creates element with complex attributes', {
  379. data: '<a foo=\'{"x":1,"y":2}\'></a>',
  380. output: '<a foo="{"x":1,"y":2}"></a>',
  381. check( a ) {
  382. expect( count( a.getAttributes() ) ).to.equal( 1 );
  383. expect( a.getAttribute( 'foo' ) ).to.have.property( 'x', 1 );
  384. expect( a.getAttribute( 'foo' ) ).to.have.property( 'y', 2 );
  385. }
  386. } );
  387. test( 'returns single parsed element', {
  388. data: '<paragraph></paragraph>',
  389. check( p ) {
  390. expect( p instanceof Element ).to.be.true;
  391. }
  392. } );
  393. test( 'returns DocumentFragment for multiple parsed elements', {
  394. data: '<paragraph></paragraph><paragraph></paragraph>',
  395. check( fragment ) {
  396. expect( fragment instanceof DocumentFragment ).to.be.true;
  397. }
  398. } );
  399. it( 'throws when invalid XML', () => {
  400. expect( () => {
  401. parse( '<a><b></a></b>', document.schema, document.batch() );
  402. } ).to.throw( Error, /Parse error/ );
  403. } );
  404. it( 'throws when try to set element not registered in schema', () => {
  405. expect( () => {
  406. parse( '<xyz></xyz>', document.schema, document.batch() );
  407. } ).to.throw( Error, 'Element \'xyz\' not allowed in context ["$root"].' );
  408. } );
  409. it( 'throws when try to set text directly to $root without registering it', () => {
  410. const document = new Document();
  411. expect( () => {
  412. parse( 'text', document.schema, document.batch() );
  413. } ).to.throw( Error, 'Element \'$text\' not allowed in context ["$root"].' );
  414. } );
  415. it( 'converts data in the specified context', () => {
  416. const doc = new Document();
  417. doc.schema.registerItem( 'foo' );
  418. doc.schema.allow( { name: '$text', inside: 'foo' } );
  419. expect( () => {
  420. parse( 'text', doc.schema, doc.batch(), { context: [ 'foo' ] } );
  421. } ).to.not.throw();
  422. } );
  423. describe( 'selection', () => {
  424. test( 'sets collapsed selection in an element', {
  425. data: '<a>[]</a>',
  426. check( root, selection ) {
  427. expect( selection.getFirstPosition().parent ).to.have.property( 'name', 'a' );
  428. }
  429. } );
  430. test( 'sets collapsed selection between elements', {
  431. data: '<a></a>[]<b></b>'
  432. } );
  433. test( 'sets collapsed selection before a text', {
  434. data: '<a></a>[]foo'
  435. } );
  436. test( 'sets collapsed selection after a text', {
  437. data: 'foo[]'
  438. } );
  439. test( 'sets collapsed selection within a text', {
  440. data: 'foo[]bar',
  441. check( text, selection ) {
  442. expect( text.offsetSize ).to.equal( 6 );
  443. expect( text.getPath() ).to.deep.equal( [ 0 ] );
  444. expect( selection.getFirstRange().start.path ).to.deep.equal( [ 3 ] );
  445. expect( selection.getFirstRange().end.path ).to.deep.equal( [ 3 ] );
  446. }
  447. } );
  448. it( 'sets selection attributes', () => {
  449. const result = parse( 'foo[]bar', document.schema, document.batch(), { selectionAttributes: {
  450. bold: true,
  451. italic: true
  452. } } );
  453. expect( stringify( result.model, result.selection ) ).to.equal( 'foo<$text bold="true" italic="true">[]</$text>bar' );
  454. } );
  455. test( 'sets collapsed selection between text and text with attributes', {
  456. data: 'foo[]<$text bold="true">bar</$text>',
  457. check( root, selection ) {
  458. expect( root.maxOffset ).to.equal( 6 );
  459. expect( selection.getAttribute( 'bold' ) ).to.be.undefined;
  460. }
  461. } );
  462. test( 'sets selection containing an element', {
  463. data: 'x[<a></a>]'
  464. } );
  465. it( 'sets selection with attribute containing an element', () => {
  466. const result = parse( 'x[<a></a>]', document.schema, document.batch(), { selectionAttributes: {
  467. bold: true
  468. } } );
  469. expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
  470. expect( result.selection.getAttribute( 'bold' ) ).to.be.true;
  471. } );
  472. it( 'sets a backward selection containing an element', () => {
  473. const result = parse( 'x[<a></a>]', document.schema, document.batch(), {
  474. lastRangeBackward: true
  475. } );
  476. expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
  477. expect( result.selection.isBackward ).to.true;
  478. } );
  479. test( 'sets selection within a text', {
  480. data: 'x[y]z'
  481. } );
  482. it( 'sets selection within a text with different attributes', () => {
  483. const result = parse( '<$text bold="true">fo[o</$text>ba]r', document.schema, document.batch(), {
  484. selectionAttributes: { bold: true }
  485. } );
  486. expect( stringify( result.model, result.selection ) ).to.equal( '<$text bold="true">fo[o</$text>ba]r' );
  487. expect( result.selection.getAttribute( 'bold' ) ).to.true;
  488. } );
  489. it( 'throws when missing selection start', () => {
  490. expect( () => {
  491. parse( 'foo]', document.schema, document.batch() );
  492. } ).to.throw( Error );
  493. } );
  494. it( 'throws when missing selection end', () => {
  495. expect( () => {
  496. parse( '[foo', document.schema, document.batch() );
  497. } ).to.throw( Error );
  498. } );
  499. } );
  500. function test( title, options ) {
  501. it( title, () => {
  502. const output = options.output || options.data;
  503. const data = parse( options.data, document.schema, document.batch() );
  504. let model, selection;
  505. if ( data.selection && data.model ) {
  506. model = data.model;
  507. selection = data.selection;
  508. } else {
  509. model = data;
  510. }
  511. expect( stringify( model, selection ) ).to.equal( output );
  512. if ( options.check ) {
  513. options.check( model, selection );
  514. }
  515. } );
  516. }
  517. } );
  518. } );