model.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 '/tests/engine/_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.registerItem( 'b', '$inline' );
  24. document.schema.allow( { name: 'b', inside: '$root' } );
  25. document.schema.registerItem( 'c', '$inline' );
  26. document.schema.allow( { name: 'c', inside: '$root' } );
  27. document.schema.registerItem( 'paragraph', '$block' );
  28. document.schema.allow( { name: '$text', inside: '$root' } );
  29. } );
  30. afterEach( () => {
  31. sandbox.restore();
  32. } );
  33. describe( 'getData', () => {
  34. it( 'should use stringify method', () => {
  35. const stringifySpy = sandbox.spy( getData, '_stringify' );
  36. root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
  37. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<b>btext</b>' );
  38. sinon.assert.calledOnce( stringifySpy );
  39. sinon.assert.calledWithExactly( stringifySpy, root );
  40. } );
  41. it( 'should use stringify method with selection', () => {
  42. const stringifySpy = sandbox.spy( getData, '_stringify' );
  43. root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
  44. document.selection.addRange( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  45. expect( getData( document ) ).to.equal( '[<b>btext</b>]' );
  46. sinon.assert.calledOnce( stringifySpy );
  47. sinon.assert.calledWithExactly( stringifySpy, root, document.selection );
  48. } );
  49. it( 'should support unicode', () => {
  50. root.appendChildren( 'நிலைக்கு' );
  51. document.selection.addRange( Range.createFromParentsAndOffsets( root, 2, root, 6 ) );
  52. expect( getData( document ) ).to.equal( 'நி[லைக்]கு' );
  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 engine.model.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 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 support unicode', () => {
  114. test( 'நி[லைக்]கு' );
  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 engine.model.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: JSON.stringify( { 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. describe( 'selection', () => {
  188. let elA, elB;
  189. beforeEach( () => {
  190. elA = new Element( 'a' );
  191. elB = new Element( 'b' );
  192. root.appendChildren( [
  193. elA,
  194. new Text( 'foo' ),
  195. new Text( 'bar', { bold: true } ),
  196. elB
  197. ] );
  198. } );
  199. it( 'writes selection in an empty root', () => {
  200. const root = document.createRoot( '$root', 'empty' );
  201. selection.collapse( root );
  202. expect( stringify( root, selection ) ).to.equal(
  203. '[]'
  204. );
  205. } );
  206. it( 'writes only requested element', () => {
  207. expect( stringify( elA ) ).to.equal( '<a></a>' );
  208. } );
  209. it( 'writes selection collapsed in an element', () => {
  210. selection.collapse( root );
  211. expect( stringify( root, selection ) ).to.equal(
  212. '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
  213. );
  214. } );
  215. it( 'writes selection collapsed in a text', () => {
  216. selection.collapse( root, 3 );
  217. expect( stringify( root, selection ) ).to.equal(
  218. '<a></a>fo[]o<$text bold="true">bar</$text><b></b>'
  219. );
  220. } );
  221. it( 'writes selection collapsed at the text left boundary', () => {
  222. selection.collapse( elA, 'after' );
  223. expect( stringify( root, selection ) ).to.equal(
  224. '<a></a>[]foo<$text bold="true">bar</$text><b></b>'
  225. );
  226. } );
  227. it( 'writes selection collapsed at the text right boundary', () => {
  228. selection.collapse( elB, 'before' );
  229. expect( stringify( root, selection ) ).to.equal(
  230. '<a></a>foo<$text bold="true">bar[]</$text><b></b>'
  231. );
  232. expect( selection.getAttribute( 'bold' ) ).to.true;
  233. expect( count( selection.getAttributes() ) ).to.equal( 1 );
  234. } );
  235. it( 'writes selection collapsed at the end of the root', () => {
  236. selection.collapse( root, 'end' );
  237. // Needed due to https://github.com/ckeditor/ckeditor5-engine/issues/320.
  238. selection.clearAttributes();
  239. expect( stringify( root, selection ) ).to.equal(
  240. '<a></a>foo<$text bold="true">bar</$text><b></b>[]'
  241. );
  242. } );
  243. it( 'writes selection collapsed selection in a text with attributes', () => {
  244. selection.collapse( root, 5 );
  245. expect( stringify( root, selection ) ).to.equal(
  246. '<a></a>foo<$text bold="true">b[]ar</$text><b></b>'
  247. );
  248. expect( selection.getAttribute( 'bold' ) ).to.true;
  249. expect( count( selection.getAttributes() ) ).to.equal( 1 );
  250. } );
  251. it( 'writes flat selection containing couple of nodes', () => {
  252. selection.addRange(
  253. Range.createFromParentsAndOffsets( root, 0, root, 4 )
  254. );
  255. expect( stringify( root, selection ) ).to.equal(
  256. '[<a></a>foo]<$text bold="true">bar</$text><b></b>'
  257. );
  258. } );
  259. it( 'writes flat selection within text', () => {
  260. selection.addRange(
  261. Range.createFromParentsAndOffsets( root, 2, root, 3 )
  262. );
  263. expect( stringify( root, selection ) ).to.equal(
  264. '<a></a>f[o]o<$text bold="true">bar</$text><b></b>'
  265. );
  266. } );
  267. it( 'writes multi-level selection', () => {
  268. selection.addRange(
  269. Range.createFromParentsAndOffsets( elA, 0, elB, 0 )
  270. );
  271. expect( stringify( root, selection ) ).to.equal(
  272. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  273. );
  274. } );
  275. it( 'writes selection when is backward', () => {
  276. selection.addRange(
  277. Range.createFromParentsAndOffsets( elA, 0, elB, 0 ),
  278. true
  279. );
  280. expect( stringify( root, selection ) ).to.equal(
  281. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  282. );
  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( 'sets elements attributes', {
  338. data: '<a foo="1" bar="true" car="x y"><b x="y"></b></a>',
  339. output: '<a bar="true" car="x y" foo="1"><b x="y"></b></a>',
  340. check( a ) {
  341. expect( a.getAttribute( 'bar' ) ).to.equal( 'true' );
  342. expect( a.getAttribute( 'car' ) ).to.equal( 'x y' );
  343. expect( a.getAttribute( 'foo' ) ).to.equal( '1' );
  344. }
  345. } );
  346. test( 'sets text attributes', {
  347. data: '<$text bold="true" italic="true">foo</$text><$text bold="true">bar</$text>bom',
  348. check( root ) {
  349. expect( root.childCount ).to.equal( 3 );
  350. expect( root.maxOffset ).to.equal( 9 );
  351. expect( root.getChild( 0 ) ).to.have.property( 'data', 'foo' );
  352. expect( root.getChild( 0 ).getAttribute( 'italic' ) ).to.equal( 'true' );
  353. expect( root.getChild( 1 ) ).to.have.property( 'data', 'bar' );
  354. expect( root.getChild( 1 ).getAttribute( 'bold' ) ).to.equal( 'true' );
  355. }
  356. } );
  357. test( 'returns single parsed element', {
  358. data: '<paragraph></paragraph>',
  359. check( p ) {
  360. expect( p instanceof Element ).to.be.true;
  361. }
  362. } );
  363. test( 'returns DocumentFragment for multiple parsed elements', {
  364. data: '<paragraph></paragraph><paragraph></paragraph>',
  365. check( fragment ) {
  366. expect( fragment instanceof DocumentFragment ).to.be.true;
  367. }
  368. } );
  369. it( 'throws when invalid XML', () => {
  370. expect( () => {
  371. parse( '<a><b></a></b>' );
  372. } ).to.throw( Error );
  373. } );
  374. describe( 'selection', () => {
  375. test( 'sets collapsed selection in an element', {
  376. data: '<a>[]</a>',
  377. check( root, selection ) {
  378. expect( selection.getFirstPosition().parent ).to.have.property( 'name', 'a' );
  379. }
  380. } );
  381. test( 'sets collapsed selection between elements', {
  382. data: '<a></a>[]<b></b>'
  383. } );
  384. test( 'sets collapsed selection before a text', {
  385. data: '<a></a>[]foo'
  386. } );
  387. test( 'sets collapsed selection after a text', {
  388. data: 'foo[]'
  389. } );
  390. test( 'sets collapsed selection within a text', {
  391. data: 'foo[]bar',
  392. check( text, selection ) {
  393. expect( text.offsetSize ).to.equal( 6 );
  394. expect( text.getPath() ).to.deep.equal( [ 0 ] );
  395. expect( selection.getFirstRange().start.path ).to.deep.equal( [ 3 ] );
  396. expect( selection.getFirstRange().end.path ).to.deep.equal( [ 3 ] );
  397. }
  398. } );
  399. it( 'sets selection attributes', () => {
  400. const result = parse( 'foo[]bar', document.schema, { selectionAttributes: {
  401. bold: true,
  402. italic: true
  403. } } );
  404. expect( stringify( result.model, result.selection ) ).to.equal( 'foo<$text bold="true" italic="true">[]</$text>bar' );
  405. } );
  406. test( 'sets collapsed selection between text and text with attributes', {
  407. data: 'foo[]<$text bold="true">bar</$text>',
  408. check( root, selection ) {
  409. expect( root.maxOffset ).to.equal( 6 );
  410. expect( selection.getAttribute( 'bold' ) ).to.be.undefined;
  411. }
  412. } );
  413. test( 'sets selection containing an element', {
  414. data: 'x[<a></a>]'
  415. } );
  416. it( 'sets selection with attribute containing an element', () => {
  417. const result = parse( 'x[<a></a>]', document.schema, { selectionAttributes: {
  418. bold: true
  419. } } );
  420. expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
  421. expect( result.selection.getAttribute( 'bold' ) ).to.be.true;
  422. } );
  423. it( 'sets a backward selection containing an element', () => {
  424. const result = parse( 'x[<a></a>]', document.schema, {
  425. lastRangeBackward: true
  426. } );
  427. expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
  428. expect( result.selection.isBackward ).to.true;
  429. } );
  430. test( 'sets selection within a text', {
  431. data: 'x[y]z'
  432. } );
  433. it( 'sets selection within a text with different attributes', () => {
  434. const result = parse( '<$text bold="true">fo[o</$text>ba]r', document.schema, {
  435. selectionAttributes: { bold: true }
  436. } );
  437. expect( stringify( result.model, result.selection ) ).to.equal( '<$text bold="true">fo[o</$text>ba]r' );
  438. expect( result.selection.getAttribute( 'bold' ) ).to.true;
  439. } );
  440. it( 'throws when missing selection start', () => {
  441. expect( () => {
  442. parse( 'foo]' );
  443. } ).to.throw( Error );
  444. } );
  445. it( 'throws when missing selection end', () => {
  446. expect( () => {
  447. parse( '[foo' );
  448. } ).to.throw( Error );
  449. } );
  450. } );
  451. function test( title, options ) {
  452. it( title, () => {
  453. const output = options.output || options.data;
  454. const data = parse( options.data, document.schema );
  455. let model, selection;
  456. if ( data.selection && data.model ) {
  457. model = data.model;
  458. selection = data.selection;
  459. } else {
  460. model = data;
  461. }
  462. expect( stringify( model, selection ) ).to.equal( output );
  463. if ( options.check ) {
  464. options.check( model, selection );
  465. }
  466. } );
  467. }
  468. } );
  469. } );