model.js 20 KB

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