model.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 #2', () => {
  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. function test( data, expected ) {
  126. expected = expected || data;
  127. setData( document, data );
  128. expect( getData( document ) ).to.equal( expected );
  129. }
  130. } );
  131. describe( 'stringify', () => {
  132. it( 'should stringify text', () => {
  133. const text = new Text( 'text', { underline: true, bold: true } );
  134. expect( stringify( text ) ).to.equal( '<$text bold="true" underline="true">text</$text>' );
  135. } );
  136. it( 'should stringify element', () => {
  137. const element = new Element( 'a', null, [
  138. new Element( 'b', null, new Text( 'btext' ) ),
  139. new Text( 'atext' )
  140. ] );
  141. expect( stringify( element ) ).to.equal( '<a><b>btext</b>atext</a>' );
  142. } );
  143. it( 'should stringify document fragment', () => {
  144. const fragment = new DocumentFragment( [
  145. new Element( 'b', null, new Text( 'btext' ) ),
  146. new Text( 'atext' )
  147. ] );
  148. expect( stringify( fragment ) ).to.equal( '<b>btext</b>atext' );
  149. } );
  150. it( 'writes elements and texts', () => {
  151. root.appendChildren( [
  152. new Element( 'a', null, new Text( 'atext' ) ),
  153. new Element( 'b', null, [
  154. new Element( 'c1' ),
  155. new Text( 'ctext' ),
  156. new Element( 'c2' )
  157. ] ),
  158. new Element( 'd' )
  159. ] );
  160. expect( stringify( root ) ).to.equal(
  161. '<a>atext</a><b><c1></c1>ctext<c2></c2></b><d></d>'
  162. );
  163. } );
  164. it( 'writes element attributes', () => {
  165. root.appendChildren(
  166. new Element( 'a', { foo: true, bar: 1, car: false }, [
  167. new Element( 'b', { fooBar: 'x y', barFoo: { x: 1, y: 2 } } )
  168. ] )
  169. );
  170. // Note: attributes are written in a very simplistic way, because they are not to be parsed. They are just
  171. // to be compared in the tests with some patterns.
  172. expect( stringify( root ) ).to.equal(
  173. '<a bar="1" car="false" foo="true"><b barFoo="{"x":1,"y":2}" fooBar="x y"></b></a>'
  174. );
  175. } );
  176. it( 'writes text attributes', () => {
  177. root.appendChildren( [
  178. new Text( 'foo', { bold: true } ),
  179. new Text( 'bar' ),
  180. new Text( 'bom', { bold: true, italic: true } ),
  181. new Element( 'a', null, [
  182. new Text( 'pom', { underline: true, bold: true } )
  183. ] )
  184. ] );
  185. expect( stringify( root ) ).to.equal(
  186. // Because of https://github.com/ckeditor/ckeditor5-engine/issues/562 attributes are not merged
  187. '<$text bold="true">foo</$text>bar<$text italic="true"><$text bold="true">bom</$text></$text>' +
  188. '<a><$text bold="true" underline="true">pom</$text></a>'
  189. );
  190. } );
  191. it( 'writes unicode text', () => {
  192. root.appendChildren( new Text( 'நிலைக்கு' ) );
  193. expect( stringify( root ) ).to.equal( 'நிலைக்கு' );
  194. } );
  195. describe( 'selection', () => {
  196. let elA, elB;
  197. beforeEach( () => {
  198. elA = new Element( 'a' );
  199. elB = new Element( 'b' );
  200. root.appendChildren( [
  201. elA,
  202. new Text( 'foo' ),
  203. new Text( 'bar', { bold: true } ),
  204. elB
  205. ] );
  206. } );
  207. it( 'writes selection in an empty root', () => {
  208. const root = document.createRoot( '$root', 'empty' );
  209. selection.collapse( root );
  210. expect( stringify( root, selection ) ).to.equal(
  211. '[]'
  212. );
  213. } );
  214. it( 'writes selection collapsed in an element', () => {
  215. selection.collapse( root );
  216. expect( stringify( root, selection ) ).to.equal(
  217. '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
  218. );
  219. } );
  220. it( 'writes selection collapsed in a text', () => {
  221. selection.collapse( root, 3 );
  222. expect( stringify( root, selection ) ).to.equal(
  223. '<a></a>fo[]o<$text bold="true">bar</$text><b></b>'
  224. );
  225. } );
  226. it( 'writes selection collapsed at the text left boundary', () => {
  227. selection.collapse( elA, 'after' );
  228. expect( stringify( root, selection ) ).to.equal(
  229. '<a></a>[]foo<$text bold="true">bar</$text><b></b>'
  230. );
  231. } );
  232. it( 'writes selection collapsed at the text right boundary', () => {
  233. selection.collapse( elB, 'before' );
  234. expect( stringify( root, selection ) ).to.equal(
  235. '<a></a>foo<$text bold="true">bar[]</$text><b></b>'
  236. );
  237. } );
  238. it( 'writes selection collapsed at the end of the root', () => {
  239. selection.collapse( root, 'end' );
  240. // Needed due to https://github.com/ckeditor/ckeditor5-engine/issues/320.
  241. selection.clearAttributes();
  242. expect( stringify( root, selection ) ).to.equal(
  243. '<a></a>foo<$text bold="true">bar</$text><b></b>[]'
  244. );
  245. } );
  246. it( 'writes selection collapsed selection in a text with attributes', () => {
  247. selection.collapse( root, 5 );
  248. expect( stringify( root, selection ) ).to.equal(
  249. '<a></a>foo<$text bold="true">b[]ar</$text><b></b>'
  250. );
  251. } );
  252. it( 'writes flat selection containing couple of nodes', () => {
  253. selection.addRange(
  254. Range.createFromParentsAndOffsets( root, 0, root, 4 )
  255. );
  256. expect( stringify( root, selection ) ).to.equal(
  257. '[<a></a>foo]<$text bold="true">bar</$text><b></b>'
  258. );
  259. } );
  260. it( 'writes flat selection within text', () => {
  261. selection.addRange(
  262. Range.createFromParentsAndOffsets( root, 2, root, 3 )
  263. );
  264. expect( stringify( root, selection ) ).to.equal(
  265. '<a></a>f[o]o<$text bold="true">bar</$text><b></b>'
  266. );
  267. } );
  268. it( 'writes multi-level selection', () => {
  269. selection.addRange(
  270. Range.createFromParentsAndOffsets( elA, 0, elB, 0 )
  271. );
  272. expect( stringify( root, selection ) ).to.equal(
  273. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  274. );
  275. } );
  276. it( 'writes selection when is backward', () => {
  277. selection.addRange(
  278. Range.createFromParentsAndOffsets( elA, 0, elB, 0 ),
  279. true
  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 in unicode text', () => {
  286. const root = document.createRoot( '$root', 'empty' );
  287. root.appendChildren( new Text( 'நிலைக்கு' ) );
  288. selection.addRange( Range.createFromParentsAndOffsets( root, 2, root, 6 ) );
  289. expect( stringify( root, selection ) ).to.equal( 'நி[லைக்]கு' );
  290. } );
  291. it( 'uses range and coverts it to selection', () => {
  292. const range = Range.createFromParentsAndOffsets( elA, 0, elB, 0 );
  293. expect( stringify( root, range ) ).to.equal(
  294. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  295. );
  296. } );
  297. it( 'uses position and converts it to collapsed selection', () => {
  298. const position = new Position( root, [ 0 ] );
  299. expect( stringify( root, position ) ).to.equal(
  300. '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
  301. );
  302. } );
  303. } );
  304. } );
  305. describe( 'parse', () => {
  306. test( 'creates empty DocumentFragment from empty string', {
  307. data: '',
  308. check( fragment ) {
  309. expect( fragment ).to.be.instanceOf( DocumentFragment );
  310. }
  311. } );
  312. test( 'creates empty DocumentFragment with selection', {
  313. data: '[]',
  314. check( fragment, selection ) {
  315. expect( fragment ).to.be.instanceOf( DocumentFragment );
  316. expect( fragment.childCount ).to.equal( 0 );
  317. expect( selection.rangeCount ).to.equal( 1 );
  318. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( fragment, 0, fragment, 0 ) ) ).to.be.true;
  319. }
  320. } );
  321. test( 'returns Element if range is around single element', {
  322. data: '[<a></a>]',
  323. check( el, selection ) {
  324. const fragment = el.parent;
  325. expect( el ).to.be.instanceOf( Element );
  326. expect( fragment ).to.be.instanceOf( DocumentFragment );
  327. expect( selection.rangeCount ).to.equal( 1 );
  328. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( fragment, 0, fragment, 1 ) ) ).to.be.true;
  329. }
  330. } );
  331. test( 'returns DocumentFragment when multiple elements on root', {
  332. data: '<a></a><b></b>',
  333. check( fragment ) {
  334. expect( fragment ).to.be.instanceOf( DocumentFragment );
  335. expect( fragment.childCount ).to.equal( 2 );
  336. }
  337. } );
  338. test( 'creates elements', {
  339. data: '<a></a><b><c></c></b>'
  340. } );
  341. test( 'creates text nodes', {
  342. data: 'foo<a>bar</a>bom'
  343. } );
  344. test( 'creates text nodes with unicode text', {
  345. data: 'நிலைக்கு'
  346. } );
  347. test( 'sets elements attributes', {
  348. data: '<a bar="true" car="x y" foo="1"></a><b x="y"></b>',
  349. output: '<a bar="true" car="x y" foo="1"></a><b x="y"></b>',
  350. check( root ) {
  351. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.equal( true );
  352. expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
  353. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 1 );
  354. expect( root.getChild( 1 ).getAttribute( 'x' ) ).to.equal( 'y' );
  355. }
  356. } );
  357. test( 'sets text attributes', {
  358. data: '<$text bar="true" car="x y" foo="1">foo</$text><$text x="y">bar</$text>bom',
  359. check( root ) {
  360. expect( root.childCount ).to.equal( 3 );
  361. expect( root.maxOffset ).to.equal( 9 );
  362. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.equal( true );
  363. expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
  364. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 1 );
  365. expect( root.getChild( 1 ).getAttribute( 'x' ) ).to.equal( 'y' );
  366. expect( count( root.getChild( 2 ).getAttributes() ) ).to.equal( 0 );
  367. }
  368. } );
  369. test( 'creates element with complex attributes', {
  370. data: `<a foo='{"x":1,"y":2}'></a>`,
  371. output: '<a foo="{"x":1,"y":2}"></a>',
  372. check( a ) {
  373. expect( count( a.getAttributes() ) ).to.equal( 1 );
  374. expect( a.getAttribute( 'foo' ) ).to.have.property( 'x', 1 );
  375. expect( a.getAttribute( 'foo' ) ).to.have.property( 'y', 2 );
  376. }
  377. } );
  378. test( 'returns single parsed element', {
  379. data: '<paragraph></paragraph>',
  380. check( p ) {
  381. expect( p instanceof Element ).to.be.true;
  382. }
  383. } );
  384. test( 'returns DocumentFragment for multiple parsed elements', {
  385. data: '<paragraph></paragraph><paragraph></paragraph>',
  386. check( fragment ) {
  387. expect( fragment instanceof DocumentFragment ).to.be.true;
  388. }
  389. } );
  390. it( 'throws when invalid XML', () => {
  391. expect( () => {
  392. parse( '<a><b></a></b>', document.schema );
  393. } ).to.throw( Error, /Parse error/ );
  394. } );
  395. it( 'throws when try to set element not registered in schema', () => {
  396. expect( () => {
  397. parse( '<xyz></xyz>', document.schema );
  398. } ).to.throw( Error, `Element 'xyz' not allowed in context ["$root"].` );
  399. } );
  400. it( 'throws when try to set text directly to $root without registering it', () => {
  401. const doc = new Document();
  402. expect( () => {
  403. parse( 'text', doc.schema );
  404. } ).to.throw( Error, `Element '$text' not allowed in context ["$root"].` );
  405. } );
  406. it( 'converts data in the specified context', () => {
  407. const doc = new Document();
  408. doc.schema.registerItem( 'foo' );
  409. doc.schema.allow( { name: '$text', inside: 'foo' } );
  410. expect( () => {
  411. parse( 'text', doc.schema, { context: [ 'foo' ] } );
  412. } ).to.not.throw();
  413. } );
  414. describe( 'selection', () => {
  415. test( 'sets collapsed selection in an element', {
  416. data: '<a>[]</a>',
  417. check( root, selection ) {
  418. expect( selection.getFirstPosition().parent ).to.have.property( 'name', 'a' );
  419. }
  420. } );
  421. test( 'sets collapsed selection between elements', {
  422. data: '<a></a>[]<b></b>'
  423. } );
  424. test( 'sets collapsed selection before a text', {
  425. data: '<a></a>[]foo'
  426. } );
  427. test( 'sets collapsed selection after a text', {
  428. data: 'foo[]'
  429. } );
  430. test( 'sets collapsed selection within a text', {
  431. data: 'foo[]bar',
  432. check( text, selection ) {
  433. expect( text.offsetSize ).to.equal( 6 );
  434. expect( text.getPath() ).to.deep.equal( [ 0 ] );
  435. expect( selection.getFirstRange().start.path ).to.deep.equal( [ 3 ] );
  436. expect( selection.getFirstRange().end.path ).to.deep.equal( [ 3 ] );
  437. }
  438. } );
  439. it( 'sets selection attributes', () => {
  440. const result = parse( 'foo[]bar', document.schema, { selectionAttributes: {
  441. bold: true,
  442. italic: true
  443. } } );
  444. expect( stringify( result.model, result.selection ) ).to.equal( 'foo<$text bold="true" italic="true">[]</$text>bar' );
  445. } );
  446. test( 'sets collapsed selection between text and text with attributes', {
  447. data: 'foo[]<$text bold="true">bar</$text>',
  448. check( root, selection ) {
  449. expect( root.maxOffset ).to.equal( 6 );
  450. expect( selection.getAttribute( 'bold' ) ).to.be.undefined;
  451. }
  452. } );
  453. test( 'sets selection containing an element', {
  454. data: 'x[<a></a>]'
  455. } );
  456. it( 'sets selection with attribute containing an element', () => {
  457. const result = parse( 'x[<a></a>]', document.schema, { selectionAttributes: {
  458. bold: true
  459. } } );
  460. expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
  461. expect( result.selection.getAttribute( 'bold' ) ).to.be.true;
  462. } );
  463. it( 'sets a backward selection containing an element', () => {
  464. const result = parse( 'x[<a></a>]', document.schema, {
  465. lastRangeBackward: true
  466. } );
  467. expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
  468. expect( result.selection.isBackward ).to.true;
  469. } );
  470. test( 'sets selection within a text', {
  471. data: 'x[y]z'
  472. } );
  473. it( 'sets selection within a text with different attributes', () => {
  474. const result = parse( '<$text bold="true">fo[o</$text>ba]r', document.schema, {
  475. selectionAttributes: { bold: true }
  476. } );
  477. expect( stringify( result.model, result.selection ) ).to.equal( '<$text bold="true">fo[o</$text>ba]r' );
  478. expect( result.selection.getAttribute( 'bold' ) ).to.true;
  479. } );
  480. it( 'throws when missing selection start', () => {
  481. expect( () => {
  482. parse( 'foo]' );
  483. } ).to.throw( Error );
  484. } );
  485. it( 'throws when missing selection end', () => {
  486. expect( () => {
  487. parse( '[foo' );
  488. } ).to.throw( Error );
  489. } );
  490. } );
  491. function test( title, options ) {
  492. it( title, () => {
  493. const output = options.output || options.data;
  494. const data = parse( options.data, document.schema );
  495. let model, selection;
  496. if ( data.selection && data.model ) {
  497. model = data.model;
  498. selection = data.selection;
  499. } else {
  500. model = data;
  501. }
  502. expect( stringify( model, selection ) ).to.equal( output );
  503. if ( options.check ) {
  504. options.check( model, selection );
  505. }
  506. } );
  507. }
  508. } );
  509. } );