model.js 11 KB

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