Class EntityDecoder

Token decoder with support of writing partial entities.

Constructors

  • Parameters

    • decodeTree: Uint16Array

      The tree used to decode entities.

    • emitCodePoint: ((cp, consumed) => void)

      The function that is called when a codepoint is decoded.

      For multi-byte named entities, this will be called multiple times, with the second codepoint, and the same consumed value.

      Param: codepoint

      The decoded codepoint.

      Param: consumed

      The number of bytes consumed by the decoder.

        • (cp, consumed): void
        • Parameters

          • cp: number
          • consumed: number

          Returns void

    • Optional errors: EntityErrorProducer

      An object that is used to produce errors.

    Returns EntityDecoder

Properties

consumed: number = 1

Characters that were consumed while parsing an entity.

decodeMode: DecodingMode = DecodingMode.Strict

The mode in which the decoder is operating.

decodeTree: Uint16Array

The tree used to decode entities.

emitCodePoint: ((cp, consumed) => void)

The function that is called when a codepoint is decoded.

For multi-byte named entities, this will be called multiple times, with the second codepoint, and the same consumed value.

Type declaration

    • (cp, consumed): void
    • Parameters

      • cp: number
      • consumed: number

        The number of bytes consumed by the decoder.

      Returns void

errors?: EntityErrorProducer

An object that is used to produce errors.

excess: number = 1

The number of characters that were consumed in excess.

result: number = 0

The result of the entity.

Either the result index of a numeric entity, or the codepoint of a numeric entity.

state: EntityDecoderState = EntityDecoderState.EntityStart

The current state of the decoder.

treeIndex: number = 0

The current index in the decode tree.

Methods

  • Parameters

    • str: string
    • start: number
    • end: number
    • base: number

    Returns void

  • Emit a named entity.

    Parameters

    • result: number

      The index of the entity in the decode tree.

    • valueLength: number

      The number of bytes in the entity.

    • consumed: number

      The number of characters consumed.

    Returns number

    The number of characters consumed.

  • Emit a named entity that was not terminated with a semicolon.

    Returns number

    The number of characters consumed.

  • Validate and emit a numeric entity.

    Implements the logic from the Hexademical character reference start state and Numeric character reference end state in the HTML spec.

    Parameters

    • lastCp: number

      The last code point of the entity. Used to see if the entity was terminated with a semicolon.

    • expectedLength: number

      The minimum number of characters that should be consumed. Used to validate that at least one digit was consumed.

    Returns number

    The number of characters that were consumed.

  • Signal to the parser that the end of the input was reached.

    Remaining data will be emitted and relevant errors will be produced.

    Returns number

    The number of characters consumed.

  • Resets the instance to make it reusable.

    Parameters

    Returns void

  • Parses a named entity.

    Equivalent to the Named character reference state in the HTML spec.

    Parameters

    • str: string

      The string containing the entity (or a continuation of the entity).

    • offset: number

      The current offset.

    Returns number

    The number of characters that were consumed, or -1 if the entity is incomplete.

  • Parses a decimal numeric entity.

    Equivalent to the Decimal character reference state in the HTML spec.

    Parameters

    • str: string

      The string containing the entity (or a continuation of the entity).

    • offset: number

      The current offset.

    Returns number

    The number of characters that were consumed, or -1 if the entity is incomplete.

  • Parses a hexadecimal numeric entity.

    Equivalent to the Hexademical character reference state in the HTML spec.

    Parameters

    • str: string

      The string containing the entity (or a continuation of the entity).

    • offset: number

      The current offset.

    Returns number

    The number of characters that were consumed, or -1 if the entity is incomplete.

  • Switches between the numeric decimal and hexadecimal states.

    Equivalent to the Numeric character reference state in the HTML spec.

    Parameters

    • str: string

      The string containing the entity (or a continuation of the entity).

    • offset: number

      The current offset.

    Returns number

    The number of characters that were consumed, or -1 if the entity is incomplete.

  • Write an entity to the decoder. This can be called multiple times with partial entities. If the entity is incomplete, the decoder will return -1.

    Mirrors the implementation of getDecoder, but with the ability to stop decoding if the entity is incomplete, and resume when the next string is written.

    Parameters

    • str: string
    • offset: number

      The offset at which the entity begins. Should be 0 if this is not the first call.

    Returns number

    The number of characters that were consumed, or -1 if the entity is incomplete.