entities

  • Version 4.5.0
  • Published
  • 413 kB
  • No dependencies
  • BSD-2-Clause license

Install

npm i entities
yarn add entities
pnpm add entities

Overview

Encode & decode XML and HTML entities with ease & speed

Index

Variables

variable escape

const escape: (str: string) => string;
  • Encodes all non-ASCII characters, as well as characters not valid in XML documents using numeric hexadecimal reference (eg. ü).

    Have a look at escapeUTF8 if you want a more concise output at the expense of reduced transportability.

    Parameter data

    String to escape.

Functions

function decode

decode: (data: string, options?: DecodingOptions | EntityLevel) => string;
  • Decodes a string with entities.

    Parameter data

    String to decode.

    Parameter options

    Decoding options.

function decodeHTML

decodeHTML: (str: string, mode?: DecodingMode) => string;
  • Decodes an HTML string.

    Parameter str

    The string to decode.

    Parameter mode

    The decoding mode.

    Returns

    The decoded string.

function decodeHTML4

decodeHTML4: (str: string, mode?: DecodingMode) => string;
  • Decodes an HTML string.

    Parameter str

    The string to decode.

    Parameter mode

    The decoding mode.

    Returns

    The decoded string.

function decodeHTML4Strict

decodeHTML4Strict: (str: string) => string;
  • Decodes an HTML string, requiring all entities to be terminated by a semicolon.

    Parameter str

    The string to decode.

    Returns

    The decoded string.

function decodeHTML5

decodeHTML5: (str: string, mode?: DecodingMode) => string;
  • Decodes an HTML string.

    Parameter str

    The string to decode.

    Parameter mode

    The decoding mode.

    Returns

    The decoded string.

function decodeHTML5Strict

decodeHTML5Strict: (str: string) => string;
  • Decodes an HTML string, requiring all entities to be terminated by a semicolon.

    Parameter str

    The string to decode.

    Returns

    The decoded string.

function decodeHTMLAttribute

decodeHTMLAttribute: (str: string) => string;
  • Decodes an HTML string in an attribute.

    Parameter str

    The string to decode.

    Returns

    The decoded string.

function decodeHTMLStrict

decodeHTMLStrict: (str: string) => string;
  • Decodes an HTML string, requiring all entities to be terminated by a semicolon.

    Parameter str

    The string to decode.

    Returns

    The decoded string.

function decodeStrict

decodeStrict: (data: string, options?: DecodingOptions | EntityLevel) => string;
  • Decodes a string with entities. Does not allow missing trailing semicolons for entities.

    Parameter data

    String to decode.

    Parameter options

    Decoding options.

    Deprecated

    Use decode with the mode set to Strict.

function decodeXML

decodeXML: (str: string) => string;
  • Decodes an XML string, requiring all entities to be terminated by a semicolon.

    Parameter str

    The string to decode.

    Returns

    The decoded string.

function decodeXMLStrict

decodeXMLStrict: (str: string) => string;
  • Decodes an XML string, requiring all entities to be terminated by a semicolon.

    Parameter str

    The string to decode.

    Returns

    The decoded string.

function encode

encode: (data: string, options?: EncodingOptions | EntityLevel) => string;
  • Encodes a string with entities.

    Parameter data

    String to encode.

    Parameter options

    Encoding options.

function encodeHTML

encodeHTML: (data: string) => string;
  • Encodes all characters in the input using HTML entities. This includes characters that are valid ASCII characters in HTML documents, such as #.

    To get a more compact output, consider using the encodeNonAsciiHTML function, which will only encode characters that are not valid in HTML documents, as well as non-ASCII characters.

    If a character has no equivalent entity, a numeric hexadecimal reference (eg. ü) will be used.

function encodeHTML4

encodeHTML4: (data: string) => string;
  • Encodes all characters in the input using HTML entities. This includes characters that are valid ASCII characters in HTML documents, such as #.

    To get a more compact output, consider using the encodeNonAsciiHTML function, which will only encode characters that are not valid in HTML documents, as well as non-ASCII characters.

    If a character has no equivalent entity, a numeric hexadecimal reference (eg. ü) will be used.

function encodeHTML5

encodeHTML5: (data: string) => string;
  • Encodes all characters in the input using HTML entities. This includes characters that are valid ASCII characters in HTML documents, such as #.

    To get a more compact output, consider using the encodeNonAsciiHTML function, which will only encode characters that are not valid in HTML documents, as well as non-ASCII characters.

    If a character has no equivalent entity, a numeric hexadecimal reference (eg. ü) will be used.

function encodeNonAsciiHTML

encodeNonAsciiHTML: (data: string) => string;
  • Encodes all non-ASCII characters, as well as characters not valid in HTML documents using HTML entities. This function will not encode characters that are valid in HTML documents, such as #.

    If a character has no equivalent entity, a numeric hexadecimal reference (eg. ü) will be used.

function encodeXML

encodeXML: (str: string) => string;
  • Encodes all non-ASCII characters, as well as characters not valid in XML documents using XML entities.

    If a character has no equivalent entity, a numeric hexadecimal reference (eg. ü) will be used.

function escapeAttribute

escapeAttribute: (data: string) => string;

function escapeText

escapeText: (data: string) => string;

function escapeUTF8

escapeUTF8: (data: string) => string;
  • Encodes all characters not valid in XML documents using XML entities.

    Note that the output will be character-set dependent.

    Parameter data

    String to escape.

Classes

class EntityDecoder

class EntityDecoder {}
  • Token decoder with support of writing partial entities.

constructor

constructor(
decodeTree: Uint16Array,
emitCodePoint: (cp: number, consumed: number) => void,
errors?: EntityErrorProducer
);

    method end

    end: () => number;
    • Signal to the parser that the end of the input was reached.

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

      Returns

      The number of characters consumed.

    method startEntity

    startEntity: (decodeMode: DecodingMode) => void;
    • Resets the instance to make it reusable.

    method write

    write: (str: string, offset: number) => number;
    • 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.

      Parameter string

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

      Parameter offset

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

      Returns

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

    Interfaces

    interface DecodingOptions

    interface DecodingOptions {}

      property level

      level?: EntityLevel;

      property mode

      mode?: DecodingMode | undefined;
      • Decoding mode. If Legacy, will support legacy entities not terminated with a semicolon (;).

        Always Strict for XML. For HTML, set this to true if you are parsing an attribute value.

        The deprecated decodeStrict function defaults this to Strict.

        DecodingMode.Legacy

      interface EncodingOptions

      interface EncodingOptions {}
      • Options for encode.

      property level

      level?: EntityLevel;

      property mode

      mode?: EncodingMode;

      Enums

      enum DecodingMode

      enum DecodingMode {
      Legacy = 0,
      Strict = 1,
      Attribute = 2,
      }

        member Attribute

        Attribute = 2
        • Entities in attributes have limitations on ending characters.

        member Legacy

        Legacy = 0
        • Entities in text nodes that can end with any character.

        member Strict

        Strict = 1
        • Only allow entities terminated with a semicolon.

        enum EncodingMode

        enum EncodingMode {
        UTF8 = 0,
        ASCII = 1,
        Extensive = 2,
        Attribute = 3,
        Text = 4,
        }

          member ASCII

          ASCII = 1
          • The output consists only of ASCII characters. Characters that need escaping within HTML, and characters that aren't ASCII characters will be escaped.

          member Attribute

          Attribute = 3

          member Extensive

          Extensive = 2
          • Encode all characters that have an equivalent entity, as well as all characters that are not ASCII characters.

          member Text

          Text = 4

          member UTF8

          UTF8 = 0
          • The output is UTF-8 encoded. Only characters that need escaping within XML will be escaped.

          enum EntityLevel

          enum EntityLevel {
          XML = 0,
          HTML = 1,
          }
          • The level of entities to support.

          member HTML

          HTML = 1
          • Support HTML entities, which are a superset of XML entities.

          member XML

          XML = 0
          • Support only XML entities.

          Package Files (4)

          Dependencies (0)

          No dependencies.

          Dev Dependencies (12)

          Peer Dependencies (0)

          No peer dependencies.

          Badge

          To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

          You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/entities.

          • Markdown
            [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/entities)
          • HTML
            <a href="https://www.jsdocs.io/package/entities"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>