view-to-model-converters.js 2.7 KB

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