JavaScriptEncoding
Namespace: SnowBank.Text · class
Methods
Encode
static string Encode(string text, bool includeQuotes)
Encode a JavaScript string
text— Text to encodeincludeQuotes— Sitrue, automatically add quotes around the string ('...')
Returns: Encoded string
This method will not allocate memory if the original string is printable as-is, and is false
EncodePropertyName
static string EncodePropertyName(string name)
Encode the name of an object's property or field
name— Name of a property or field of a Javascript object
Returns: If the name is "clean", it will be written as-is. Otherwise, it will be escaped
EncodePropertyName("foo") => "foo" EncodePropertyName("foo bar") => "'foo bar'" EncodePropertyName("foo'bar") => "'foo'bar'" EncodePropertyName("") => "''" EncodePropertyName(null) => ArgumentNullException
EncodePropertyNameTo
static void EncodePropertyNameTo(ref ValueStringWriter output, ReadOnlySpan<char> name)
writes the encoded name of an object's property or field
output— Destinationname— Name of a property or field of a Javascript object
Returns: If the name is "clean", it will be written as-is. Otherwise, it will be escaped
EncodePropertyName("foo") => "foo" EncodePropertyName("foo bar") => "'foo bar'" EncodePropertyName("foo'bar") => "'foo'bar'" EncodePropertyName("") => "''" EncodePropertyName(null) => ArgumentNullException
EncodeSlow
static StringBuilder EncodeSlow(StringBuilder sb, ReadOnlySpan<char> s, bool includeQuotes)
Encode a Javascript string known to contain at least one invalid character
static void EncodeSlow(ref ValueStringWriter sb, ReadOnlySpan<char> s, bool includeQuotes)
Encode a Javascript string known to contain at least one invalid character
EncodeTo
static void EncodeTo(ref ValueStringWriter output, ReadOnlySpan<char> text, bool includeQuotes)
Writes an encoded a JavaScript string literal
output— Destinationtext— Text to encodeincludeQuotes— Sitrue, automatically add quotes around the string ('...')
Returns: Encoded string
This method will not allocate memory if the original string is printable as-is, and is false
IsCleanJavaScript
static bool IsCleanJavaScript(ReadOnlySpan<char> s)
Test if a javascript string would require escaping or not
s— String to inspect
Returns: true if all characters are valid
IsCleanJavaScriptPropertyName
static bool IsCleanJavaScriptPropertyName(ReadOnlySpan<char> s)
Test if a javascript string would require escaping or not
s— String to inspect
Returns: true if all characters are valid
IsValidIdentifier
static bool IsValidIdentifier(string name)
Quickly test if a name can be used as a Javascript object's property name without escaping, or if it must be escaped first
name— Name of a property or field
Returns: Return true if this is a valid name, and that it only contains ASCII. false it is invalid or if it contains Unicode that requires escaping
This can return false even for valid identifiers! The goal is to decide QUICKLY if escaping is required. A false negative would only use a bit more cpu but still produce a correct result.
static bool IsValidIdentifier(ReadOnlySpan<char> name)
Quickly test if a name can be used as a Javascript object's property name without escaping, or if it must be escaped first
name— Name of a property or field
Returns: Return true if this is a valid name, and that it only contains ASCII. false it is invalid or if it contains Unicode that requires escaping
This can return false even for valid identifiers! The goal is to decide QUICKLY if escaping is required. A false negative would only use a bit more cpu but still produce a correct result.