view-to-model-converters.js 2.5 KB

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