model.js 19 KB

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