entities

  • Version 8.0.0
  • Published
  • 236 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: (input: 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: (input: string, options?: DecodingOptions | EntityLevel) => string;
  • Decodes a string with entities.

    Parameter input

    String to decode.

    Parameter options

    Decoding options.

function decodeHTML

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

    Parameter htmlString

    The string to decode.

    Parameter mode

    The decoding mode.

    Returns

    The decoded string.

function decodeHTMLAttribute

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

    Parameter htmlAttribute

    The string to decode.

    Returns

    The decoded string.

function decodeHTMLStrict

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

    Parameter htmlString

    The string to decode.

    Returns

    The decoded string.

function decodeXML

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

    Parameter xmlString

    The string to decode.

    Returns

    The decoded string.

function decodeXMLStrict

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

    Parameter xmlString

    The string to decode.

    Returns

    The decoded string.

function encode

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

    Parameter input

    String to encode.

    Parameter options

    Encoding options.

function encodeHTML

encodeHTML: (input: 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.

    Parameter input

    Input string to encode or decode.

function encodeNonAsciiHTML

encodeNonAsciiHTML: (input: 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.

    Parameter input

    Input string to encode or decode.

function encodeXML

encodeXML: (input: string) => string;
  • Encodes all non-ASCII characters, as well as characters not valid in XML documents using XML entities. Uses a fast bitset scan instead of RegExp.

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

    Parameter input

    Input string to encode or decode.

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.

      Parameter decodeMode

      Entity decoding mode to use.

    method write

    write: (input: 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 input

      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 {}
    • Options for decode.

    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. 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,
    }
    • Decoding mode for named entities.

    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,
    }
    • Encoding strategy used by encode.

    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 (17)

    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>