8
0

model.js 18 KB

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