8
0

view-to-model-converters.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/conversion/view-to-model-converters
  7. */
  8. import ModelDocumentFragment from '../model/documentfragment.js';
  9. import ModelText from '../model/text.js';
  10. import { normalizeNodes } from '../model/writer.js';
  11. /**
  12. * Contains {@link module:engine/view/view view} to {@link module:engine/model/model model} converters for
  13. * {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher}.
  14. *
  15. * @namespace viewToModel
  16. */
  17. /**
  18. * Function factory, creates a converter that converts {@link module:engine/view/documentfragment~DocumentFragment view document fragment}
  19. * or all children of {@link module:engine/view/element~Element} into
  20. * {@link module:engine/model/documentfragment~DocumentFragment model document fragment}.
  21. * This is the "entry-point" converter for view to model conversion. This converter starts the conversion of all children
  22. * of passed view document fragment. Those children {@link module:engine/view/node~Node view nodes} are then handled by other converters.
  23. *
  24. * This also a "default", last resort converter for all view elements that has not been converted by other converters.
  25. * When a view element is being converted to the model but it does not have converter specified, that view element
  26. * will be converted to {@link module:engine/model/documentfragment~DocumentFragment model document fragment} and returned.
  27. *
  28. * @external module:engine/conversion/view-to-model-converters~viewToModel
  29. * @function module:engine/conversion/view-to-model-converters~viewToModel.convertToModelFragment
  30. * @returns {Function} Universal converter for view {@link module:engine/view/documentfragment~DocumentFragment fragments} and
  31. * {@link module:engine/view/element~Element elements} that returns
  32. * {@link module:engine/model/documentfragment~DocumentFragment model fragment} with children of converted view item.
  33. */
  34. export function convertToModelFragment() {
  35. return ( evt, data, consumable, conversionApi ) => {
  36. // Second argument in `consumable.consume` is discarded for ViewDocumentFragment but is needed for ViewElement.
  37. if ( !data.output && consumable.consume( data.input, { name: true } ) ) {
  38. const convertedChildren = conversionApi.convertChildren( data.input, consumable, data );
  39. data.output = new ModelDocumentFragment( normalizeNodes( convertedChildren ) );
  40. }
  41. };
  42. }
  43. /**
  44. * Function factory, creates a converter that converts {@link module:engine/view/text~Text} to {@link module:engine/model/text~Text}.
  45. *
  46. * @external module:engine/conversion/view-to-model-converters~viewToModel
  47. * @function module:engine/conversion/view-to-model-converters~viewToModel.convertText
  48. * @returns {Function} {@link module:engine/view/text~Text View text} converter.
  49. */
  50. export function convertText() {
  51. return ( evt, data, consumable, conversionApi ) => {
  52. const schemaQuery = {
  53. name: '$text',
  54. inside: data.context
  55. };
  56. if ( conversionApi.schema.check( schemaQuery ) ) {
  57. if ( consumable.consume( data.input ) ) {
  58. data.output = new ModelText( data.input.data );
  59. }
  60. }
  61. };
  62. }