model.js 17 KB

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