entities
- Version 6.0.0
- Published
- 540 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
Functions
Classes
Interfaces
Enums
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 decodeHTML4
decodeHTML4: (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 decodeHTML4Strict
decodeHTML4Strict: (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 decodeHTML5
decodeHTML5: (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 decodeHTML5Strict
decodeHTML5Strict: (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 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 decodeStrict
decodeStrict: (input: string, options?: DecodingOptions | EntityLevel) => string;
Decodes a string with entities. Does not allow missing trailing semicolons for entities.
Parameter input
String to decode.
Parameter options
Decoding options.
Deprecated
Use
decode
with themode
set toStrict
.
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.
function encodeHTML4
encodeHTML4: (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.
function encodeHTML5
encodeHTML5: (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.
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.
function encodeXML
encodeXML: (input: 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;
Encodes all characters that have to be escaped in HTML attributes, following https://html.spec.whatwg.org/multipage/parsing.html#escapingString.
Parameter data
String to escape.
function escapeText
escapeText: (data: string) => string;
Encodes all characters that have to be escaped in HTML text, following https://html.spec.whatwg.org/multipage/parsing.html#escapingString.
Parameter data
String to escape.
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: (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 {}
property level
level?: EntityLevel;
The level of entities to support. EntityLevel.XML
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 totrue
if you are parsing an attribute value.The deprecated
decodeStrict
function defaults this toStrict
.
interface EncodingOptions
interface EncodingOptions {}
Options for
encode
.
property level
level?: EntityLevel;
The level of entities to support. EntityLevel.XML
property mode
mode?: EncodingMode;
Output format. EncodingMode.Extensive
Enums
enum DecodingMode
enum DecodingMode { Legacy = 0, Strict = 1, Attribute = 2,}
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
Encode all characters that have to be escaped in HTML attributes, following https://html.spec.whatwg.org/multipage/parsing.html#escapingString.
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
Encode all characters that have to be escaped in HTML text, following https://html.spec.whatwg.org/multipage/parsing.html#escapingString.
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.
Package Files (4)
Dependencies (0)
No dependencies.
Dev Dependencies (14)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto 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>
- Updated .
Package analyzed in 4050 ms. - Missing or incorrect documentation? Open an issue for this package.