model.js 10 KB

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