Trilium Frontend API
    Preparing search index...

    Class ViewConsumable

    Class used for handling consumption of view module:engine/view/element~ViewElement elements, module:engine/view/text~ViewText text nodes and module:engine/view/documentfragment~ViewDocumentFragment document fragments. Element's name and its parts (attributes, classes and styles) can be consumed separately. Consuming an element's name does not consume its attributes, classes and styles. To add items for consumption use module:engine/conversion/viewconsumable~ViewConsumable#add add method. To test items use module:engine/conversion/viewconsumable~ViewConsumable#test test method. To consume items use module:engine/conversion/viewconsumable~ViewConsumable#consume consume method. To revert already consumed items use module:engine/conversion/viewconsumable~ViewConsumable#revert revert method.

    viewConsumable.add( element, { name: true } ); // Adds element's name as ready to be consumed.
    viewConsumable.add( textNode ); // Adds text node for consumption.
    viewConsumable.add( docFragment ); // Adds document fragment for consumption.
    viewConsumable.test( element, { name: true } ); // Tests if element's name can be consumed.
    viewConsumable.test( textNode ); // Tests if text node can be consumed.
    viewConsumable.test( docFragment ); // Tests if document fragment can be consumed.
    viewConsumable.consume( element, { name: true } ); // Consume element's name.
    viewConsumable.consume( textNode ); // Consume text node.
    viewConsumable.consume( docFragment ); // Consume document fragment.
    viewConsumable.revert( element, { name: true } ); // Revert already consumed element's name.
    viewConsumable.revert( textNode ); // Revert already consumed text node.
    viewConsumable.revert( docFragment ); // Revert already consumed document fragment.
    Index

    Constructors

    Methods

    • Adds view module:engine/view/element~ViewElement element, module:engine/view/text~ViewText text node or module:engine/view/documentfragment~ViewDocumentFragment document fragment as ready to be consumed.

      viewConsumable.add( p, { name: true } ); // Adds element's name to consume.
      viewConsumable.add( p, { attributes: 'name' } ); // Adds element's attribute.
      viewConsumable.add( p, { classes: 'foobar' } ); // Adds element's class.
      viewConsumable.add( p, { styles: 'color' } ); // Adds element's style
      viewConsumable.add( p, { attributes: 'name', styles: 'color' } ); // Adds attribute and style.
      viewConsumable.add( p, { classes: [ 'baz', 'bar' ] } ); // Multiple consumables can be provided.
      viewConsumable.add( textNode ); // Adds text node to consume.
      viewConsumable.add( docFragment ); // Adds document fragment to consume.

      Throws module:utils/ckeditorerror~CKEditorError CKEditorError viewconsumable-invalid-attribute when class or style attribute is provided - it should be handled separately by providing actual style/class.

      viewConsumable.add( p, { attributes: 'style' } ); // This call will throw an exception.
      viewConsumable.add( p, { styles: 'color' } ); // This is properly handled style.

      Parameters

      • element: ViewDocumentFragment | ViewElement | ViewText
      • Optionalconsumables: Consumables | ViewNormalizedConsumables

        Used only if first parameter is module:engine/view/element~ViewElement view element instance.

        • Consumables

          Object describing all features of a view element that could be consumed and converted individually. This is a non-normalized form of module:engine/view/element~ViewNormalizedConsumables generated from the view Element.

          Example element:

          <a class="foo bar" style="color: red; margin: 5px" href="https://ckeditor.com" rel="nofollow noreferrer" target="_blank">
          

          The Consumables would include:

          {
          name: true,
          classes: [ "foo", "bar" ],
          styles: [ "color", "margin" ]
          }

          You could convert a Consumable into a module:engine/view/element~ViewNormalizedConsumables using the module:engine/conversion/viewconsumable~normalizeConsumables helper.

          • Optionalattributes?: string | string[]

            Attribute name or array of attribute names.

          • Optionalclasses?: string | string[]

            Class name or array of class names.

          • Optionalname?: boolean

            If set to true element's name will be included in a consumable. Depending on the usage context it would be added as consumable, tested for being available for consume or consumed.

          • Optionalstyles?: string | string[]

            Style name or array of style names.

        • ViewNormalizedConsumables

          Object describing all features of a view element that could be consumed and converted individually. This is a normalized form of module:engine/conversion/viewconsumable~Consumables generated from the view Element.

          Example element:

          <a class="foo bar" style="color: red; margin: 5px" href="https://ckeditor.com" rel="nofollow noreferrer" target="_blank">
          

          The ViewNormalizedConsumables would include:

          {
          name: true,
          attributes: [
          [ "class", "foo" ],
          [ "class", "bar" ],
          [ "style", "color" ],
          [ "style", "margin-top" ],
          [ "style", "margin-right" ],
          [ "style", "margin-bottom" ],
          [ "style", "margin-left" ],
          [ "style", "margin" ],
          [ "href" ],
          [ "rel", "nofollow" ],
          [ "rel", "noreferrer" ],
          [ "target" ]
          ]
          }
          • attributes: [string, string?][]

            Array of tuples - an attribute name, and optional token for tokenized attributes. Note that there could be multiple entries for the same attribute with different tokens (class names or style properties).

          • name: boolean

            If set to true element's name will be included in a consumable. Depending on the usage context it would be added as consumable, tested for being available for consume or consumed.

      Returns void

    • Consumes module:engine/view/element~ViewElement view element, module:engine/view/text~ViewText text node or module:engine/view/documentfragment~ViewDocumentFragment document fragment. It returns true when all items included in method's call can be consumed, otherwise returns false.

      viewConsumable.consume( p, { name: true } ); // Consumes element's name.
      viewConsumable.consume( p, { attributes: 'name' } ); // Consumes element's attribute.
      viewConsumable.consume( p, { classes: 'foobar' } ); // Consumes element's class.
      viewConsumable.consume( p, { styles: 'color' } ); // Consumes element's style.
      viewConsumable.consume( p, { attributes: 'name', styles: 'color' } ); // Consumes attribute and style.
      viewConsumable.consume( p, { classes: [ 'baz', 'bar' ] } ); // Multiple consumables can be consumed.
      viewConsumable.consume( textNode ); // Consumes text node.
      viewConsumable.consume( docFragment ); // Consumes document fragment.

      Consuming classes and styles as attribute will test if all added classes/styles can be consumed.

      viewConsumable.consume( p, { attributes: 'class' } ); // Consume only if all added classes can be consumed.
      viewConsumable.consume( p, { attributes: 'style' } ); // Consume only if all added styles can be consumed.

      Parameters

      • element: ViewDocumentFragment | ViewNode
      • Optionalconsumables: Match | Consumables

        Used only if first parameter is module:engine/view/element~ViewElement view element instance.

        • Match

          An object representing matched element parts.

          • Optionalattributes?: [string, string?][]

            Array of matching tuples: attribute name, and optional token for tokenized attributes. Note that there could be multiple entries for the same attribute with different tokens (class names or style properties).

          • Optionalname?: boolean

            True if name of the element was matched.

        • Consumables

          Object describing all features of a view element that could be consumed and converted individually. This is a non-normalized form of module:engine/view/element~ViewNormalizedConsumables generated from the view Element.

          Example element:

          <a class="foo bar" style="color: red; margin: 5px" href="https://ckeditor.com" rel="nofollow noreferrer" target="_blank">
          

          The Consumables would include:

          {
          name: true,
          classes: [ "foo", "bar" ],
          styles: [ "color", "margin" ]
          }

          You could convert a Consumable into a module:engine/view/element~ViewNormalizedConsumables using the module:engine/conversion/viewconsumable~normalizeConsumables helper.

          • Optionalattributes?: string | string[]

            Attribute name or array of attribute names.

          • Optionalclasses?: string | string[]

            Class name or array of class names.

          • Optionalname?: boolean

            If set to true element's name will be included in a consumable. Depending on the usage context it would be added as consumable, tested for being available for consume or consumed.

          • Optionalstyles?: string | string[]

            Style name or array of style names.

      Returns boolean

      Returns true when all items included in method's call can be consumed, otherwise returns false.

    • Reverts module:engine/view/element~ViewElement view element, module:engine/view/text~ViewText text node or module:engine/view/documentfragment~ViewDocumentFragment document fragment so they can be consumed once again. Method does not revert items that were never previously added for consumption, even if they are included in method's call.

      viewConsumable.revert( p, { name: true } ); // Reverts element's name.
      viewConsumable.revert( p, { attributes: 'name' } ); // Reverts element's attribute.
      viewConsumable.revert( p, { classes: 'foobar' } ); // Reverts element's class.
      viewConsumable.revert( p, { styles: 'color' } ); // Reverts element's style.
      viewConsumable.revert( p, { attributes: 'name', styles: 'color' } ); // Reverts attribute and style.
      viewConsumable.revert( p, { classes: [ 'baz', 'bar' ] } ); // Multiple names can be reverted.
      viewConsumable.revert( textNode ); // Reverts text node.
      viewConsumable.revert( docFragment ); // Reverts document fragment.

      Reverting classes and styles as attribute will revert all classes/styles that were previously added for consumption.

      viewConsumable.revert( p, { attributes: 'class' } ); // Reverts all classes added for consumption.
      viewConsumable.revert( p, { attributes: 'style' } ); // Reverts all styles added for consumption.

      Parameters

      • element: ViewNode
      • consumables: Match | Consumables

        Used only if first parameter is module:engine/view/element~ViewElement view element instance.

        • Match

          An object representing matched element parts.

          • Optionalattributes?: [string, string?][]

            Array of matching tuples: attribute name, and optional token for tokenized attributes. Note that there could be multiple entries for the same attribute with different tokens (class names or style properties).

          • Optionalname?: boolean

            True if name of the element was matched.

        • Consumables

          Object describing all features of a view element that could be consumed and converted individually. This is a non-normalized form of module:engine/view/element~ViewNormalizedConsumables generated from the view Element.

          Example element:

          <a class="foo bar" style="color: red; margin: 5px" href="https://ckeditor.com" rel="nofollow noreferrer" target="_blank">
          

          The Consumables would include:

          {
          name: true,
          classes: [ "foo", "bar" ],
          styles: [ "color", "margin" ]
          }

          You could convert a Consumable into a module:engine/view/element~ViewNormalizedConsumables using the module:engine/conversion/viewconsumable~normalizeConsumables helper.

          • Optionalattributes?: string | string[]

            Attribute name or array of attribute names.

          • Optionalclasses?: string | string[]

            Class name or array of class names.

          • Optionalname?: boolean

            If set to true element's name will be included in a consumable. Depending on the usage context it would be added as consumable, tested for being available for consume or consumed.

          • Optionalstyles?: string | string[]

            Style name or array of style names.

      Returns void

    • Tests if module:engine/view/element~ViewElement view element, module:engine/view/text~ViewText text node or module:engine/view/documentfragment~ViewDocumentFragment document fragment can be consumed. It returns true when all items included in method's call can be consumed. Returns false when first already consumed item is found and null when first non-consumable item is found.

      viewConsumable.test( p, { name: true } ); // Tests element's name.
      viewConsumable.test( p, { attributes: 'name' } ); // Tests attribute.
      viewConsumable.test( p, { classes: 'foobar' } ); // Tests class.
      viewConsumable.test( p, { styles: 'color' } ); // Tests style.
      viewConsumable.test( p, { attributes: 'name', styles: 'color' } ); // Tests attribute and style.
      viewConsumable.test( p, { classes: [ 'baz', 'bar' ] } ); // Multiple consumables can be tested.
      viewConsumable.test( textNode ); // Tests text node.
      viewConsumable.test( docFragment ); // Tests document fragment.

      Testing classes and styles as attribute will test if all added classes/styles can be consumed.

      viewConsumable.test( p, { attributes: 'class' } ); // Tests if all added classes can be consumed.
      viewConsumable.test( p, { attributes: 'style' } ); // Tests if all added styles can be consumed.

      Parameters

      • element: ViewDocumentFragment | ViewNode
      • Optionalconsumables: Match | Consumables

        Used only if first parameter is module:engine/view/element~ViewElement view element instance.

        • Match

          An object representing matched element parts.

          • Optionalattributes?: [string, string?][]

            Array of matching tuples: attribute name, and optional token for tokenized attributes. Note that there could be multiple entries for the same attribute with different tokens (class names or style properties).

          • Optionalname?: boolean

            True if name of the element was matched.

        • Consumables

          Object describing all features of a view element that could be consumed and converted individually. This is a non-normalized form of module:engine/view/element~ViewNormalizedConsumables generated from the view Element.

          Example element:

          <a class="foo bar" style="color: red; margin: 5px" href="https://ckeditor.com" rel="nofollow noreferrer" target="_blank">
          

          The Consumables would include:

          {
          name: true,
          classes: [ "foo", "bar" ],
          styles: [ "color", "margin" ]
          }

          You could convert a Consumable into a module:engine/view/element~ViewNormalizedConsumables using the module:engine/conversion/viewconsumable~normalizeConsumables helper.

          • Optionalattributes?: string | string[]

            Attribute name or array of attribute names.

          • Optionalclasses?: string | string[]

            Class name or array of class names.

          • Optionalname?: boolean

            If set to true element's name will be included in a consumable. Depending on the usage context it would be added as consumable, tested for being available for consume or consumed.

          • Optionalstyles?: string | string[]

            Style name or array of style names.

      Returns boolean

      Returns true when all items included in method's call can be consumed. Returns false when first already consumed item is found and null when first non-consumable item is found.

    • Creates module:engine/conversion/viewconsumable~ViewConsumable ViewConsumable instance from module:engine/view/node~ViewNode node or module:engine/view/documentfragment~ViewDocumentFragment document fragment. Instance will contain all elements, child nodes, attributes, styles and classes added for consumption.

      Parameters

      • from: ViewDocumentFragment | ViewNode

        View node or document fragment from which ViewConsumable will be created.

      • Optionalinstance: ViewConsumable

        If provided, given ViewConsumable instance will be used to add all consumables. It will be returned instead of a new instance.

      Returns ViewConsumable