model.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/dev-utils/model
  7. */
  8. /**
  9. * Collection of methods for manipulating the {@link module:engine/model/model model} for testing purposes.
  10. */
  11. import RootElement from '../model/rootelement';
  12. import Model from '../model/model';
  13. import ModelRange from '../model/range';
  14. import ModelPosition from '../model/position';
  15. import ModelSelection from '../model/selection';
  16. import ModelDocumentFragment from '../model/documentfragment';
  17. import DocumentSelection from '../model/documentselection';
  18. import View from '../view/view';
  19. import ViewContainerElement from '../view/containerelement';
  20. import ViewRootEditableElement from '../view/rooteditableelement';
  21. import { parse as viewParse, stringify as viewStringify } from '../../src/dev-utils/view';
  22. import DowncastDispatcher from '../conversion/downcastdispatcher';
  23. import UpcastDispatcher from '../conversion/upcastdispatcher';
  24. import Mapper from '../conversion/mapper';
  25. import {
  26. convertCollapsedSelection,
  27. convertRangeSelection,
  28. insertElement,
  29. insertText,
  30. insertUIElement,
  31. wrap
  32. } from '../conversion/downcasthelpers';
  33. import { isPlainObject } from 'lodash-es';
  34. import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
  35. /**
  36. * Writes the content of a model {@link module:engine/model/document~Document document} to an HTML-like string.
  37. *
  38. * **Note:** A {@link module:engine/model/text~Text text} node that contains attributes will be represented as:
  39. *
  40. * <$text attribute="value">Text data</$text>
  41. *
  42. * @param {module:engine/model/model~Model} model
  43. * @param {Object} [options]
  44. * @param {Boolean} [options.withoutSelection=false] Whether to write the selection. When set to `true`, the selection will
  45. * not be included in the returned string.
  46. * @param {String} [options.rootName='main'] The name of the root from which the data should be stringified. If not provided,
  47. * the default `main` name will be used.
  48. * @param {Boolean} [options.convertMarkers=false] Whether to include markers in the returned string.
  49. * @returns {String} The stringified data.
  50. */
  51. export function getData( model, options = {} ) {
  52. if ( !( model instanceof Model ) ) {
  53. throw new TypeError( 'Model needs to be an instance of module:engine/model/model~Model.' );
  54. }
  55. const rootName = options.rootName || 'main';
  56. const root = model.document.getRoot( rootName );
  57. return getData._stringify(
  58. root,
  59. options.withoutSelection ? null : model.document.selection,
  60. options.convertMarkers ? model.markers : null
  61. );
  62. }
  63. // Set stringify as getData private method - needed for testing/spying.
  64. getData._stringify = stringify;
  65. /**
  66. * Sets the content of a model {@link module:engine/model/document~Document document} provided as an HTML-like string.
  67. *
  68. * **Note:** Remember to register elements in the {@link module:engine/model/model~Model#schema model's schema} before
  69. * trying to use them.
  70. *
  71. * **Note:** To create a {@link module:engine/model/text~Text text} node that contains attributes use:
  72. *
  73. * <$text attribute="value">Text data</$text>
  74. *
  75. * @param {module:engine/model/model~Model} model
  76. * @param {String} data HTML-like string to write into the document.
  77. * @param {Object} options
  78. * @param {String} [options.rootName='main'] Root name where parsed data will be stored. If not provided, the default `main`
  79. * name will be used.
  80. * @param {Array<Object>} [options.selectionAttributes] A list of attributes which will be passed to the selection.
  81. * @param {Boolean} [options.lastRangeBackward=false] If set to `true`, the last range will be added as backward.
  82. * @param {String} [options.batchType='transparent'] Batch type used for inserting elements.
  83. * See {@link module:engine/model/batch~Batch#type}.
  84. */
  85. export function setData( model, data, options = {} ) {
  86. if ( !( model instanceof Model ) ) {
  87. throw new TypeError( 'Model needs to be an instance of module:engine/model/model~Model.' );
  88. }
  89. let modelDocumentFragment, selection;
  90. const modelRoot = model.document.getRoot( options.rootName || 'main' );
  91. // Parse data string to model.
  92. const parsedResult = setData._parse( data, model.schema, {
  93. lastRangeBackward: options.lastRangeBackward,
  94. selectionAttributes: options.selectionAttributes,
  95. context: [ modelRoot.name ]
  96. } );
  97. // Retrieve DocumentFragment and Selection from parsed model.
  98. if ( parsedResult.model ) {
  99. modelDocumentFragment = parsedResult.model;
  100. selection = parsedResult.selection;
  101. } else {
  102. modelDocumentFragment = parsedResult;
  103. }
  104. model.change( writer => {
  105. // Replace existing model in document by new one.
  106. writer.remove( writer.createRangeIn( modelRoot ) );
  107. writer.insert( modelDocumentFragment, modelRoot );
  108. // Clean up previous document selection.
  109. writer.setSelection( null );
  110. writer.removeSelectionAttribute( model.document.selection.getAttributeKeys() );
  111. // Update document selection if specified.
  112. if ( selection ) {
  113. const ranges = [];
  114. for ( const range of selection.getRanges() ) {
  115. const start = new ModelPosition( modelRoot, range.start.path );
  116. const end = new ModelPosition( modelRoot, range.end.path );
  117. ranges.push( new ModelRange( start, end ) );
  118. }
  119. writer.setSelection( ranges, { backward: selection.isBackward } );
  120. if ( options.selectionAttributes ) {
  121. writer.setSelectionAttribute( selection.getAttributes() );
  122. }
  123. }
  124. } );
  125. }
  126. // Set parse as setData private method - needed for testing/spying.
  127. setData._parse = parse;
  128. /**
  129. * Converts model nodes to HTML-like string representation.
  130. *
  131. * **Note:** A {@link module:engine/model/text~Text text} node that contains attributes will be represented as:
  132. *
  133. * <$text attribute="value">Text data</$text>
  134. *
  135. * @param {module:engine/model/rootelement~RootElement|module:engine/model/element~Element|module:engine/model/text~Text|
  136. * module:engine/model/documentfragment~DocumentFragment} node A node to stringify.
  137. * @param {module:engine/model/selection~Selection|module:engine/model/position~Position|
  138. * module:engine/model/range~Range} [selectionOrPositionOrRange=null]
  139. * A selection instance whose ranges will be included in the returned string data. If a range instance is provided, it will be
  140. * converted to a selection containing this range. If a position instance is provided, it will be converted to a selection
  141. * containing one range collapsed at this position.
  142. * @param {Iterable.<module:engine/model/markercollection~Marker>|null} markers Markers to include.
  143. * @returns {String} An HTML-like string representing the model.
  144. */
  145. export function stringify( node, selectionOrPositionOrRange = null, markers = null ) {
  146. const model = new Model();
  147. const mapper = new Mapper();
  148. let selection, range;
  149. // Create a range witch wraps passed node.
  150. if ( node instanceof RootElement || node instanceof ModelDocumentFragment ) {
  151. range = model.createRangeIn( node );
  152. } else {
  153. // Node is detached - create new document fragment.
  154. if ( !node.parent ) {
  155. const fragment = new ModelDocumentFragment( node );
  156. range = model.createRangeIn( fragment );
  157. } else {
  158. range = new ModelRange(
  159. model.createPositionBefore( node ),
  160. model.createPositionAfter( node )
  161. );
  162. }
  163. }
  164. // Get selection from passed selection or position or range if at least one is specified.
  165. if ( selectionOrPositionOrRange instanceof ModelSelection ) {
  166. selection = selectionOrPositionOrRange;
  167. } else if ( selectionOrPositionOrRange instanceof DocumentSelection ) {
  168. selection = selectionOrPositionOrRange;
  169. } else if ( selectionOrPositionOrRange instanceof ModelRange ) {
  170. selection = new ModelSelection( selectionOrPositionOrRange );
  171. } else if ( selectionOrPositionOrRange instanceof ModelPosition ) {
  172. selection = new ModelSelection( selectionOrPositionOrRange );
  173. }
  174. // Set up conversion.
  175. // Create a temporary view controller.
  176. const view = new View();
  177. const viewDocument = view.document;
  178. const viewRoot = new ViewRootEditableElement( 'div' );
  179. // Create a temporary root element in view document.
  180. viewRoot._document = view.document;
  181. viewRoot.rootName = 'main';
  182. viewDocument.roots.add( viewRoot );
  183. // Create and setup downcast dispatcher.
  184. const downcastDispatcher = new DowncastDispatcher( { mapper } );
  185. // Bind root elements.
  186. mapper.bindElements( node.root, viewRoot );
  187. downcastDispatcher.on( 'insert:$text', insertText() );
  188. downcastDispatcher.on( 'attribute', ( evt, data, conversionApi ) => {
  189. if ( data.item instanceof ModelSelection || data.item instanceof DocumentSelection || data.item.is( 'textProxy' ) ) {
  190. const converter = wrap( ( modelAttributeValue, viewWriter ) => {
  191. return viewWriter.createAttributeElement(
  192. 'model-text-with-attributes',
  193. { [ data.attributeKey ]: stringifyAttributeValue( modelAttributeValue ) }
  194. );
  195. } );
  196. converter( evt, data, conversionApi );
  197. }
  198. } );
  199. downcastDispatcher.on( 'insert', insertElement( modelItem => {
  200. // Stringify object types values for properly display as an output string.
  201. const attributes = convertAttributes( modelItem.getAttributes(), stringifyAttributeValue );
  202. return new ViewContainerElement( modelItem.name, attributes );
  203. } ) );
  204. downcastDispatcher.on( 'selection', convertRangeSelection() );
  205. downcastDispatcher.on( 'selection', convertCollapsedSelection() );
  206. downcastDispatcher.on( 'addMarker', insertUIElement( ( data, writer ) => {
  207. const name = data.markerName + ':' + ( data.isOpening ? 'start' : 'end' );
  208. return writer.createUIElement( name );
  209. } ) );
  210. // Convert model to view.
  211. const writer = view._writer;
  212. downcastDispatcher.convertInsert( range, writer );
  213. // Convert model selection to view selection.
  214. if ( selection ) {
  215. downcastDispatcher.convertSelection( selection, markers || model.markers, writer );
  216. }
  217. if ( markers ) {
  218. // To provide stable results, sort markers by name.
  219. markers = Array.from( markers ).sort( ( a, b ) => a.name < b.name ? 1 : -1 );
  220. for ( const marker of markers ) {
  221. downcastDispatcher.convertMarkerAdd( marker.name, marker.getRange(), writer );
  222. }
  223. }
  224. // Parse view to data string.
  225. let data = viewStringify( viewRoot, viewDocument.selection, { sameSelectionCharacters: true } );
  226. // Removing unneccessary <div> and </div> added because `viewRoot` was also stringified alongside input data.
  227. data = data.substr( 5, data.length - 11 );
  228. view.destroy();
  229. // Replace valid XML `model-text-with-attributes` element name to `$text`.
  230. return data.replace( new RegExp( 'model-text-with-attributes', 'g' ), '$text' );
  231. }
  232. /**
  233. * Parses an HTML-like string and returns the model {@link module:engine/model/rootelement~RootElement rootElement}.
  234. *
  235. * **Note:** To create a {@link module:engine/model/text~Text text} node that contains attributes use:
  236. *
  237. * <$text attribute="value">Text data</$text>
  238. *
  239. * @param {String} data HTML-like string to be parsed.
  240. * @param {module:engine/model/schema~Schema} schema A schema instance used by converters for element validation.
  241. * @param {Object} [options={}] Additional configuration.
  242. * @param {Array<Object>} [options.selectionAttributes] A list of attributes which will be passed to the selection.
  243. * @param {Boolean} [options.lastRangeBackward=false] If set to `true`, the last range will be added as backward.
  244. * @param {module:engine/model/schema~SchemaContextDefinition} [options.context='$root'] The conversion context.
  245. * If not provided, the default `'$root'` will be used.
  246. * @returns {module:engine/model/element~Element|module:engine/model/text~Text|
  247. * module:engine/model/documentfragment~DocumentFragment|Object} Returns the parsed model node or
  248. * an object with two fields: `model` and `selection`, when selection ranges were included in the data to parse.
  249. */
  250. export function parse( data, schema, options = {} ) {
  251. const mapper = new Mapper();
  252. // Replace not accepted by XML `$text` tag name by valid one `model-text-with-attributes`.
  253. data = data.replace( new RegExp( '\\$text', 'g' ), 'model-text-with-attributes' );
  254. // Parse data to view using view utils.
  255. const parsedResult = viewParse( data, {
  256. sameSelectionCharacters: true,
  257. lastRangeBackward: !!options.lastRangeBackward
  258. } );
  259. // Retrieve DocumentFragment and Selection from parsed view.
  260. let viewDocumentFragment, viewSelection, selection;
  261. if ( parsedResult.view && parsedResult.selection ) {
  262. viewDocumentFragment = parsedResult.view;
  263. viewSelection = parsedResult.selection;
  264. } else {
  265. viewDocumentFragment = parsedResult;
  266. }
  267. // Set up upcast dispatcher.
  268. const modelController = new Model();
  269. const upcastDispatcher = new UpcastDispatcher( { schema, mapper } );
  270. upcastDispatcher.on( 'documentFragment', convertToModelFragment() );
  271. upcastDispatcher.on( 'element:model-text-with-attributes', convertToModelText( true ) );
  272. upcastDispatcher.on( 'element', convertToModelElement() );
  273. upcastDispatcher.on( 'text', convertToModelText() );
  274. upcastDispatcher.isDebug = true;
  275. // Convert view to model.
  276. let model = modelController.change(
  277. writer => upcastDispatcher.convert( viewDocumentFragment.root, writer, options.context || '$root' )
  278. );
  279. mapper.bindElements( model, viewDocumentFragment.root );
  280. // If root DocumentFragment contains only one element - return that element.
  281. if ( model.childCount == 1 ) {
  282. model = model.getChild( 0 );
  283. }
  284. // Convert view selection to model selection.
  285. if ( viewSelection ) {
  286. const ranges = [];
  287. // Convert ranges.
  288. for ( const viewRange of viewSelection.getRanges() ) {
  289. ranges.push( mapper.toModelRange( viewRange ) );
  290. }
  291. // Create new selection.
  292. selection = new ModelSelection( ranges, { backward: viewSelection.isBackward } );
  293. // Set attributes to selection if specified.
  294. for ( const [ key, value ] of toMap( options.selectionAttributes || [] ) ) {
  295. selection.setAttribute( key, value );
  296. }
  297. }
  298. // Return model end selection when selection was specified.
  299. if ( selection ) {
  300. return { model, selection };
  301. }
  302. // Otherwise return model only.
  303. return model;
  304. }
  305. // -- Converters view -> model -----------------------------------------------------
  306. function convertToModelFragment() {
  307. return ( evt, data, conversionApi ) => {
  308. const childrenResult = conversionApi.convertChildren( data.viewItem, data.modelCursor );
  309. conversionApi.mapper.bindElements( data.modelCursor.parent, data.viewItem );
  310. data = Object.assign( data, childrenResult );
  311. evt.stop();
  312. };
  313. }
  314. function convertToModelElement() {
  315. return ( evt, data, conversionApi ) => {
  316. const elementName = data.viewItem.name;
  317. if ( !conversionApi.schema.checkChild( data.modelCursor, elementName ) ) {
  318. throw new Error( `Element '${ elementName }' was not allowed in given position.` );
  319. }
  320. // View attribute value is a string so we want to typecast it to the original type.
  321. // E.g. `bold="true"` - value will be parsed from string `"true"` to boolean `true`.
  322. const attributes = convertAttributes( data.viewItem.getAttributes(), parseAttributeValue );
  323. const element = conversionApi.writer.createElement( data.viewItem.name, attributes );
  324. conversionApi.writer.insert( element, data.modelCursor );
  325. conversionApi.mapper.bindElements( element, data.viewItem );
  326. conversionApi.convertChildren( data.viewItem, ModelPosition._createAt( element, 0 ) );
  327. data.modelRange = ModelRange._createOn( element );
  328. data.modelCursor = data.modelRange.end;
  329. evt.stop();
  330. };
  331. }
  332. function convertToModelText( withAttributes = false ) {
  333. return ( evt, data, conversionApi ) => {
  334. if ( !conversionApi.schema.checkChild( data.modelCursor, '$text' ) ) {
  335. throw new Error( 'Text was not allowed in given position.' );
  336. }
  337. let node;
  338. if ( withAttributes ) {
  339. // View attribute value is a string so we want to typecast it to the original type.
  340. // E.g. `bold="true"` - value will be parsed from string `"true"` to boolean `true`.
  341. const attributes = convertAttributes( data.viewItem.getAttributes(), parseAttributeValue );
  342. node = conversionApi.writer.createText( data.viewItem.getChild( 0 ).data, attributes );
  343. } else {
  344. node = conversionApi.writer.createText( data.viewItem.data );
  345. }
  346. conversionApi.writer.insert( node, data.modelCursor );
  347. data.modelRange = ModelRange._createFromPositionAndShift( data.modelCursor, node.offsetSize );
  348. data.modelCursor = data.modelRange.end;
  349. evt.stop();
  350. };
  351. }
  352. // Tries to get original type of attribute value using JSON parsing:
  353. //
  354. // `'true'` => `true`
  355. // `'1'` => `1`
  356. // `'{"x":1,"y":2}'` => `{ x: 1, y: 2 }`
  357. //
  358. // Parse error means that value should be a string:
  359. //
  360. // `'foobar'` => `'foobar'`
  361. function parseAttributeValue( attribute ) {
  362. try {
  363. return JSON.parse( attribute );
  364. } catch ( e ) {
  365. return attribute;
  366. }
  367. }
  368. // When value is an Object stringify it.
  369. function stringifyAttributeValue( data ) {
  370. if ( isPlainObject( data ) ) {
  371. return JSON.stringify( data );
  372. }
  373. return data;
  374. }
  375. // Loop trough attributes map and converts each value by passed converter.
  376. function* convertAttributes( attributes, converter ) {
  377. for ( const [ key, value ] of attributes ) {
  378. yield [ key, converter( value ) ];
  379. }
  380. }