model.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import { stringify, parse, getData, setData } from '/tests/engine/_utils/model.js';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  9. import Element from '/ckeditor5/engine/model/element.js';
  10. import Text from '/ckeditor5/engine/model/text.js';
  11. import Range from '/ckeditor5/engine/model/range.js';
  12. import Position from '/ckeditor5/engine/model/position.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. } );
  22. afterEach( () => {
  23. sandbox.restore();
  24. } );
  25. describe( 'getData', () => {
  26. it( 'should use stringify method', () => {
  27. const stringifySpy = sandbox.spy( getData, '_stringify' );
  28. root.appendChildren( new Element( 'b', null, [ 'btext' ] ) );
  29. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<b>btext</b>' );
  30. sinon.assert.calledOnce( stringifySpy );
  31. sinon.assert.calledWithExactly( stringifySpy, root );
  32. } );
  33. it( 'should use stringify method with selection', () => {
  34. const stringifySpy = sandbox.spy( getData, '_stringify' );
  35. root.appendChildren( new Element( 'b', null, [ 'btext' ] ) );
  36. document.selection.addRange( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  37. expect( getData( document ) ).to.equal( '<selection><b>btext</b></selection>' );
  38. sinon.assert.calledOnce( stringifySpy );
  39. sinon.assert.calledWithExactly( stringifySpy, root, document.selection );
  40. } );
  41. it( 'should throw an error when passing invalid document', () => {
  42. expect( () => {
  43. getData( { invalid: 'document' } );
  44. } ).to.throw( TypeError, 'Document needs to be an instance of engine.model.Document.' );
  45. } );
  46. } );
  47. describe( 'setData', () => {
  48. it( 'should use parse method', () => {
  49. const parseSpy = sandbox.spy( setData, '_parse' );
  50. const options = {};
  51. const data = '<b>btext</b>text';
  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. setData( document, data, options );
  63. expect( getData( document ) ).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 throw an error when passing invalid document', () => {
  69. expect( () => {
  70. setData( { invalid: 'document' } );
  71. } ).to.throw( TypeError, 'Document needs to be an instance of engine.model.Document.' );
  72. } );
  73. } );
  74. describe( 'stringify', () => {
  75. it( 'should stringify text', () => {
  76. const text = new Text( 'text', { underline: true, bold: true } );
  77. expect( stringify( text ) ).to.equal( '<$text bold=true underline=true>text</$text>' );
  78. } );
  79. it( 'should stringify element', () => {
  80. const element = new Element( 'a', null, [ new Element( 'b', null, 'btext' ), 'atext' ] );
  81. expect( stringify( element ) ).to.equal( '<a><b>btext</b>atext</a>' );
  82. } );
  83. it( 'should stringify document fragment', () => {
  84. const fragment = new DocumentFragment( [ new Element( 'b', null, 'btext' ), 'atext' ] );
  85. expect( stringify( fragment ) ).to.equal( '<b>btext</b>atext' );
  86. } );
  87. it( 'writes elements and texts', () => {
  88. root.appendChildren( [
  89. new Element( 'a', null, 'atext' ),
  90. new Element( 'b', null, [
  91. new Element( 'c1' ),
  92. 'ctext',
  93. new Element( 'c2' )
  94. ] ),
  95. new Element( 'd' )
  96. ] );
  97. expect( stringify( root ) ).to.equal(
  98. '<a>atext</a><b><c1></c1>ctext<c2></c2></b><d></d>'
  99. );
  100. } );
  101. it( 'writes element attributes', () => {
  102. root.appendChildren(
  103. new Element( 'a', { foo: true, bar: 1, car: false }, [
  104. new Element( 'b', { fooBar: 'x y', barFoo: { x: 1, y: 2 } } )
  105. ] )
  106. );
  107. // Note: attributes are written in a very simplistic way, because they are not to be parsed. They are just
  108. // to be compared in the tests with some patterns.
  109. expect( stringify( root ) ).to.equal(
  110. '<a bar=1 car=false foo=true><b barFoo={"x":1,"y":2} fooBar="x y"></b></a>'
  111. );
  112. } );
  113. it( 'writes text attributes', () => {
  114. root.appendChildren( [
  115. new Text( 'foo', { bold: true } ),
  116. 'bar',
  117. new Text( 'bom', { bold: true, italic: true } ),
  118. new Element( 'a', null, [
  119. new Text( 'pom', { underline: true, bold: true } )
  120. ] )
  121. ] );
  122. expect( stringify( root ) ).to.equal(
  123. '<$text bold=true>foo</$text>' +
  124. 'bar' +
  125. '<$text bold=true italic=true>bom</$text>' +
  126. '<a><$text bold=true underline=true>pom</$text></a>'
  127. );
  128. } );
  129. describe( 'selection', () => {
  130. let elA, elB;
  131. beforeEach( () => {
  132. elA = new Element( 'a' );
  133. elB = new Element( 'b' );
  134. root.appendChildren( [
  135. elA,
  136. 'foo',
  137. new Text( 'bar', { bold: true } ),
  138. elB
  139. ] );
  140. } );
  141. it( 'writes selection in an empty root', () => {
  142. const root = document.createRoot( '$root', 'empty' );
  143. selection.collapse( root );
  144. expect( stringify( root, selection ) ).to.equal(
  145. '<selection />'
  146. );
  147. } );
  148. it( 'writes selection collapsed in an element', () => {
  149. selection.collapse( root );
  150. expect( stringify( root, selection ) ).to.equal(
  151. '<selection /><a></a>foo<$text bold=true>bar</$text><b></b>'
  152. );
  153. } );
  154. it( 'writes selection collapsed in a text', () => {
  155. selection.collapse( root, 3 );
  156. expect( stringify( root, selection ) ).to.equal(
  157. '<a></a>fo<selection />o<$text bold=true>bar</$text><b></b>'
  158. );
  159. } );
  160. it( 'writes selection collapsed at the text left boundary', () => {
  161. selection.collapse( elA, 'AFTER' );
  162. expect( stringify( root, selection ) ).to.equal(
  163. '<a></a><selection />foo<$text bold=true>bar</$text><b></b>'
  164. );
  165. } );
  166. it( 'writes selection collapsed at the text right boundary', () => {
  167. selection.collapse( elB, 'BEFORE' );
  168. expect( stringify( root, selection ) ).to.equal(
  169. '<a></a>foo<$text bold=true>bar</$text><selection bold=true /><b></b>'
  170. );
  171. } );
  172. it( 'writes selection collapsed at the end of the root', () => {
  173. selection.collapse( root, 'END' );
  174. // Needed due to https://github.com/ckeditor/ckeditor5-engine/issues/320.
  175. selection.clearAttributes();
  176. expect( stringify( root, selection ) ).to.equal(
  177. '<a></a>foo<$text bold=true>bar</$text><b></b><selection />'
  178. );
  179. } );
  180. it( 'writes selection attributes', () => {
  181. selection.collapse( root );
  182. selection.setAttributesTo( { italic: true, bold: true } );
  183. expect( stringify( root, selection ) ).to.equal(
  184. '<selection bold=true italic=true /><a></a>foo<$text bold=true>bar</$text><b></b>'
  185. );
  186. } );
  187. it( 'writes selection collapsed selection in a text with attributes', () => {
  188. selection.collapse( root, 5 );
  189. expect( stringify( root, selection ) ).to.equal(
  190. '<a></a>foo<$text bold=true>b<selection bold=true />ar</$text><b></b>'
  191. );
  192. } );
  193. it( 'writes flat selection containing couple of nodes', () => {
  194. selection.addRange(
  195. Range.createFromParentsAndOffsets( root, 0, root, 4 )
  196. );
  197. expect( stringify( root, selection ) ).to.equal(
  198. '<selection><a></a>foo</selection><$text bold=true>bar</$text><b></b>'
  199. );
  200. } );
  201. it( 'writes flat selection within text', () => {
  202. selection.addRange(
  203. Range.createFromParentsAndOffsets( root, 2, root, 3 )
  204. );
  205. expect( stringify( root, selection ) ).to.equal(
  206. '<a></a>f<selection>o</selection>o<$text bold=true>bar</$text><b></b>'
  207. );
  208. } );
  209. it( 'writes multi-level selection', () => {
  210. selection.addRange(
  211. Range.createFromParentsAndOffsets( elA, 0, elB, 0 )
  212. );
  213. expect( stringify( root, selection ) ).to.equal(
  214. '<a><selection></a>foo<$text bold=true>bar</$text><b></selection></b>'
  215. );
  216. } );
  217. it( 'writes backward selection', () => {
  218. selection.addRange(
  219. Range.createFromParentsAndOffsets( elA, 0, elB, 0 ),
  220. true
  221. );
  222. expect( stringify( root, selection ) ).to.equal(
  223. '<a><selection backward></a>foo<$text bold=true>bar</$text><b></selection></b>'
  224. );
  225. } );
  226. it( 'uses range and coverts it to selection', () => {
  227. const range = Range.createFromParentsAndOffsets( elA, 0, elB, 0 );
  228. expect( stringify( root, range ) ).to.equal(
  229. '<a><selection></a>foo<$text bold=true>bar</$text><b></selection></b>'
  230. );
  231. } );
  232. it( 'uses position and converts it to collapsed selection', () => {
  233. const position = new Position( root, [ 0 ] );
  234. expect( stringify( root, position ) ).to.equal(
  235. '<selection /><a></a>foo<$text bold=true>bar</$text><b></b>'
  236. );
  237. } );
  238. } );
  239. } );
  240. describe( 'parse', () => {
  241. test( 'creates elements', {
  242. data: '<a></a><b><c></c></b>'
  243. } );
  244. test( 'creates text nodes', {
  245. data: 'foo<a>bar</a>bom'
  246. } );
  247. test( 'sets elements attributes', {
  248. data: '<a foo=1 bar=true car="x y"><b x="y"></b></a>',
  249. output: '<a bar=true car="x y" foo=1><b x="y"></b></a>',
  250. check( root ) {
  251. expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
  252. }
  253. } );
  254. test( 'sets complex attributes', {
  255. data: '<a foo={"a":1,"b":"c"}></a>',
  256. check( root ) {
  257. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.have.property( 'a', 1 );
  258. }
  259. } );
  260. test( 'sets text attributes', {
  261. data: '<$text bold=true italic=true>foo</$text><$text bold=true>bar</$text>bom',
  262. check( root ) {
  263. expect( root.getChildCount() ).to.equal( 9 );
  264. expect( root.getChild( 0 ) ).to.have.property( 'character', 'f' );
  265. expect( root.getChild( 0 ).getAttribute( 'italic' ) ).to.equal( true );
  266. }
  267. } );
  268. it( 'throws when unexpected closing tag', () => {
  269. expect( () => {
  270. parse( '<a><b></a></b>' );
  271. } ).to.throw( Error, 'Parse error - unexpected closing tag.' );
  272. } );
  273. it( 'throws when unexpected attribute', () => {
  274. expect( () => {
  275. parse( '<a ?></a>' );
  276. } ).to.throw( Error, 'Parse error - unexpected token: ?.' );
  277. } );
  278. it( 'throws when incorrect tag', () => {
  279. expect( () => {
  280. parse( '<a' );
  281. } ).to.throw( Error, 'Parse error - unexpected token: <a.' );
  282. } );
  283. it( 'throws when missing closing tag', () => {
  284. expect( () => {
  285. parse( '<a><b></b>' );
  286. } ).to.throw( Error, 'Parse error - missing closing tags: a.' );
  287. } );
  288. it( 'throws when missing opening tag for text', () => {
  289. expect( () => {
  290. parse( '</$text>' );
  291. } ).to.throw( Error, 'Parse error - unexpected closing tag.' );
  292. } );
  293. it( 'throws when missing closing tag for text', () => {
  294. expect( () => {
  295. parse( '<$text>' );
  296. } ).to.throw( Error, 'Parse error - missing closing tags: $text.' );
  297. } );
  298. describe( 'selection', () => {
  299. test( 'sets collapsed selection in an element', {
  300. data: '<a><selection /></a>',
  301. check( root, selection ) {
  302. expect( selection.getFirstPosition().parent ).to.have.property( 'name', 'a' );
  303. }
  304. } );
  305. test( 'sets collapsed selection between elements', {
  306. data: '<a></a><selection /><b></b>'
  307. } );
  308. test( 'sets collapsed selection before a text', {
  309. data: '<a></a><selection />foo'
  310. } );
  311. test( 'sets collapsed selection after a text', {
  312. data: 'foo<selection />'
  313. } );
  314. test( 'sets collapsed selection within a text', {
  315. data: 'foo<selection />bar',
  316. check( root ) {
  317. expect( root.getChildCount() ).to.equal( 6 );
  318. }
  319. } );
  320. test( 'sets selection attributes', {
  321. data: 'foo<selection bold=true italic=true />bar',
  322. check( root, selection ) {
  323. expect( selection.getAttribute( 'italic' ) ).to.be.true;
  324. }
  325. } );
  326. test( 'sets collapsed selection between text and text with attributes', {
  327. data: 'foo<selection /><$text bold=true>bar</$text>',
  328. check( root, selection ) {
  329. expect( root.getChildCount() ).to.equal( 6 );
  330. expect( selection.getAttribute( 'bold' ) ).to.be.undefined;
  331. }
  332. } );
  333. test( 'sets selection containing an element', {
  334. data: 'x<selection><a></a></selection>'
  335. } );
  336. test( 'sets selection with attribute containing an element', {
  337. data: 'x<selection bold=true><a></a></selection>'
  338. } );
  339. test( 'sets a backward selection containing an element', {
  340. data: 'x<selection backward bold=true><a></a></selection>'
  341. } );
  342. test( 'sets selection within a text', {
  343. data: 'x<selection bold=true>y</selection>z'
  344. } );
  345. test( 'sets selection within a text with different attributes', {
  346. data: '<$text bold=true>fo<selection bold=true>o</$text>ba</selection>r'
  347. } );
  348. it( 'throws when missing selection start', () => {
  349. expect( () => {
  350. parse( 'foo</selection>' );
  351. } ).to.throw( Error, 'Parse error - missing selection start.' );
  352. } );
  353. it( 'throws when missing selection end', () => {
  354. expect( () => {
  355. parse( '<selection>foo' );
  356. } ).to.throw( Error, 'Parse error - missing selection end.' );
  357. } );
  358. } );
  359. function test( title, options ) {
  360. it( title, () => {
  361. const output = options.output || options.data;
  362. const data = parse( options.data );
  363. let model, selection;
  364. if ( data.selection && data.model ) {
  365. model = data.model;
  366. selection = data.selection;
  367. } else {
  368. model = data;
  369. }
  370. expect( stringify( model, selection ) ).to.equal( output );
  371. if ( options.check ) {
  372. options.check( model, selection );
  373. }
  374. } );
  375. }
  376. } );
  377. } );