Package org.cthing.escapers
Class JavaScriptEscaper
java.lang.Object
org.cthing.escapers.JavaScriptEscaper
Escapes strings and character arrays according to the
ECMA-262 ECMAScript® 2023 Language Specification. ECMAScript
is typically referred to as JavaScript. Characters are escaped according to the following table:
| Character | Escape | Description |
|---|---|---|
| 0x00 - 0x07 | \uXXXX | ASCII control characters |
| 0x08 | \b | Backspace |
| 0x09 | \t | Tab |
| 0x0A | \n | Line feed |
| 0x0B | \uXXXX | ASCII control character |
| 0x0C | \f | Form feed |
| 0x0D | \r | Carriage return |
| 0x0E - 0x1F | \uXXXX | ASCII control characters |
| 0x20 - 0x21 | Unchanged | Printable ASCII characters |
| 0x22 | \" | Double quote |
| 0x23 - 0x26 | Unchanged | Printable ASCII characters |
| 0x27 | \' | Single quote |
| 0x28 - 0x2E | Unchanged | Printable ASCII characters |
| 0x2F | \/ | Forward slash |
| 0x30 - 0x5B | Unchanged | Printable ASCII characters |
| 0x5C | \\ | Backslash |
| 0x5D - 0x7E | Unchanged | Printable ASCII characters |
| 0x7F | \uXXXX | ASCII control character |
| 0x80 - 0x10FFFF | Unchanged by default or \uXXXX[\uXXXX] if JavaScriptEscaper.Option.ESCAPE_NON_ASCII specified |
ISO Latin-1, Unicode BMP and surrogate pairs |
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic voidescape(char[] charArr, int offset, int length, Writer writer, Set<JavaScriptEscaper.Option> options) Applies JavaScript escaping to the specified character array and writes the result to the specified writer.static voidescape(char[] charArr, int offset, int length, Writer writer, JavaScriptEscaper.Option... options) Applies JavaScript escaping to the specified character array and writes the result to the specified writer.static Stringescape(char[] charArr, int offset, int length, Set<JavaScriptEscaper.Option> options) Applies JavaScript escaping to the specified character array.static Stringescape(char[] charArr, int offset, int length, JavaScriptEscaper.Option... options) Applies JavaScript escaping to the specified character array.static voidescape(char[] charArr, Writer writer, Set<JavaScriptEscaper.Option> options) Applies JavaScript escaping to the specified character array and writes the result to the specified writer.static voidescape(char[] charArr, Writer writer, JavaScriptEscaper.Option... options) Applies JavaScript escaping to the specified character array and writes the result to the specified writer.static Stringescape(char[] charArr, Set<JavaScriptEscaper.Option> options) Applies JavaScript escaping to the specified character array.static Stringescape(char[] charArr, JavaScriptEscaper.Option... options) Applies JavaScript escaping to the specified character array.static voidescape(CharSequence charSequence, Writer writer, Set<JavaScriptEscaper.Option> options) Applies JavaScript escaping to the specified string and writes the result to the specified writer.static voidescape(CharSequence charSequence, Writer writer, JavaScriptEscaper.Option... options) Applies JavaScript escaping to the specified string and writes the result to the specified writer.static Stringescape(CharSequence charSequence, Set<JavaScriptEscaper.Option> options) Applies JavaScript escaping to the specified string.static Stringescape(CharSequence charSequence, JavaScriptEscaper.Option... options) Applies JavaScript escaping to the specified string.
-
Method Details
-
escape
Applies JavaScript escaping to the specified string.- Parameters:
charSequence- String to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in.
-
escape
Applies JavaScript escaping to the specified string.- Parameters:
charSequence- String to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in.
-
escape
@WillNotClose public static void escape(CharSequence charSequence, Writer writer, JavaScriptEscaper.Option... options) throws IOException Applies JavaScript escaping to the specified string and writes the result to the specified writer.- Parameters:
charSequence- String to escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
@WillNotClose public static void escape(CharSequence charSequence, Writer writer, Set<JavaScriptEscaper.Option> options) throws IOException Applies JavaScript escaping to the specified string and writes the result to the specified writer.- Parameters:
charSequence- String to escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
Applies JavaScript escaping to the specified character array.- Parameters:
charArr- Character array to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in.
-
escape
public static String escape(char[] charArr, int offset, int length, JavaScriptEscaper.Option... options) Applies JavaScript escaping to the specified character array.- Parameters:
charArr- Character array to escapeoffset- Start index in arraylength- Number of characters in arrayoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in.
-
escape
Applies JavaScript escaping to the specified character array.- Parameters:
charArr- Character array to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in.
-
escape
public static String escape(char[] charArr, int offset, int length, Set<JavaScriptEscaper.Option> options) Applies JavaScript escaping to the specified character array.- Parameters:
charArr- Character array to escapeoffset- Start index in arraylength- Number of characters in arrayoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in.
-
escape
@WillNotClose public static void escape(char[] charArr, Writer writer, JavaScriptEscaper.Option... options) throws IOException Applies JavaScript escaping to the specified character array and writes the result to the specified writer.- Parameters:
charArr- Character array to escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
@WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer, JavaScriptEscaper.Option... options) throws IOException Applies JavaScript escaping to the specified character array and writes the result to the specified writer.- Parameters:
charArr- Character array to escapeoffset- Start index in arraylength- Number of characters in arraywriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
@WillNotClose public static void escape(char[] charArr, Writer writer, Set<JavaScriptEscaper.Option> options) throws IOException Applies JavaScript escaping to the specified character array and writes the result to the specified writer.- Parameters:
charArr- Character array to escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
@WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer, Set<JavaScriptEscaper.Option> options) throws IOException Applies JavaScript escaping to the specified character array and writes the result to the specified writer.- Parameters:
charArr- Character array to escapeoffset- Start index in arraylength- Number of characters in arraywriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-