public final class Context extends Object
This is an immutable bit-packable struct that contains a number of enums.
These enums have their own nullish values like Context.ElementType.NONE so should always
be non-null.
The contextual autoescape rewriter propagates contexts so that it can infer an appropriate
escaping function for each {print ...} command.
To make sure it can correctly identify a unique escape convention for all paths to a particular
print command, it may clone a template for each context in which it is called, using the
bitpacked form of the context to generate a unique template name.
| Modifier and Type | Class and Description |
|---|---|
static class |
Context.AttributeEndDelimiter
Describes the content that will end the current HTML attribute.
|
static class |
Context.AttributeType
Describes the content of an HTML attribute.
|
static class |
Context.ElementType
A type of HTML element.
|
static class |
Context.JsFollowingSlash
Describes what a slash (
/) means when parsing JavaScript source code. |
static class |
Context.State
A state in the parse of an HTML document.
|
static class |
Context.UriPart
Describes the part of a URI reference that the context point is in.
|
| Modifier and Type | Field and Description |
|---|---|
Context.AttributeType |
attrType
Describes the attribute whose value the context point is in.
|
Context.AttributeEndDelimiter |
delimType
Describes the quoting convention for the attribute value that the context point is in.
|
Context.ElementType |
elType
Describes the innermost element that the text preceding the context point is in.
|
static Context |
ERROR
A special state transitioned to if the CSS/HTML/JS parser cannot compute the next context.
|
static Context |
HTML_PCDATA
The normal context for HTML where a less than opens a tag and an ampersand starts an HTML
entity.
|
Context.JsFollowingSlash |
slashType
Determines what we will do with a slash token
/. |
Context.State |
state
The state the text preceding the context point describes.
|
int |
templateNestDepth
The count of
<template> elements entered and not subsequently exited. |
Context.UriPart |
uriPart
Determines how we encode interpolations in URI attributes and CSS
uri(...). |
| Modifier and Type | Method and Description |
|---|---|
Context |
derive(Context.JsFollowingSlash slashType)
Returns a context that differs only in the following slash.
|
Context |
derive(Context.State state)
Returns a context that differs only in the state.
|
Context |
derive(Context.UriPart uriPart)
Returns a context that differs only in the uri part.
|
boolean |
equals(Object o)
Deprecated.
Prefer comparing states or predicates like isValidEndContext
|
Context |
getContextAfterEscaping(EscapingMode mode)
The context reached after escaping content using the given mode from this context.
|
com.google.common.collect.ImmutableList<EscapingMode> |
getEscapingModes()
Returns the escaping mode appropriate for dynamic content inserted in this context.
|
String |
getLikelyEndContextMismatchCause(SanitizedContent.ContentKind contentKind)
Returns a plausible human-readable description of a context mismatch;
|
SanitizedContent.ContentKind |
getMostAppropriateContentKind()
Returns the most sensible content kind for a context.
|
static Context |
getStartContextForContentKind(SanitizedContent.ContentKind contentKind)
Returns the autoescape
Context that produces sanitized content of the given
SanitizedContent.ContentKind. |
int |
hashCode() |
boolean |
isCompatibleWith(EscapingMode mode)
True if the given escaping mode could make sense in this context.
|
boolean |
isErrorContext()
True if this context is in the
error state. |
boolean |
isValidEndContextForContentKind(SanitizedContent.ContentKind contentKind)
Determines whether a particular context is valid for the end of a block of a particular
content kind.
|
boolean |
isValidStartContextForContentKind(SanitizedContent.ContentKind contentKind)
Determines whether a particular context is valid at the start of a block of a particular
content kind.
|
boolean |
isValidStartContextForContentKindLoose(SanitizedContent.ContentKind contentKind)
Determines whether a particular context is allowed for contextual to strict calls.
|
int |
packedBits()
An integer form that uniquely identifies this context.
|
String |
toString() |
static Context |
union(Context a,
Context b)
A context which is consistent with both contexts.
|
public final Context.State state
public final Context.ElementType elType
<b id="boldly-going">Hello, World!</b >
^ ^
O C
Outside an element, or in PCDATA text, this will be the nullish value Context.ElementType.NONE.public final Context.AttributeType attrType
Context.AttributeType.NONE.public final Context.AttributeEndDelimiter delimType
Context.AttributeEndDelimiter.NONE.public final Context.JsFollowingSlash slashType
/. This is irrelevant outside JavaScript
contexts, but inside JavaScript, it helps us distinguish the contexts of {$bar} in
"foo".replace(/{$bar}/i) and x/{$bar}/ipublic final Context.UriPart uriPart
uri(...).public final int templateNestDepth
<template> elements entered and not subsequently exited.public static final Context HTML_PCDATA
public static final Context ERROR
public Context derive(Context.State state)
public Context derive(Context.JsFollowingSlash slashType)
public Context derive(Context.UriPart uriPart)
public Context getContextAfterEscaping(@Nullable EscapingMode mode)
Context.UriPart.START and
Context.UriPart.PRE_QUERY.public com.google.common.collect.ImmutableList<EscapingMode> getEscapingModes()
public boolean isCompatibleWith(EscapingMode mode)
public boolean isErrorContext()
error state.ERROR@Deprecated public boolean equals(Object o)
public int packedBits()
public static Context union(Context a, Context b)
{if} command and the path through the else-clause.ERROR when there is no such context consistent with both.public static Context getStartContextForContentKind(SanitizedContent.ContentKind contentKind)
Context that produces sanitized content of the given
SanitizedContent.ContentKind.
Given a SanitizedContent.ContentKind, returns the corresponding Context such that contextual
autoescaping of a block of Soy code with that context as the start context results in a value
that adheres to the contract of SanitizedContent of the
given kind.
public boolean isValidStartContextForContentKind(SanitizedContent.ContentKind contentKind)
public boolean isValidStartContextForContentKindLoose(SanitizedContent.ContentKind contentKind)
This is slightly more relaxed, and used to help piecemeal transition of templates from contextual to strict.
public SanitizedContent.ContentKind getMostAppropriateContentKind()
This is primarily for error messages, indicating to the user what content kind can be used to mostly null out the escaping. Returns TEXT if no useful match was detected.
public final boolean isValidEndContextForContentKind(SanitizedContent.ContentKind contentKind)
public final String getLikelyEndContextMismatchCause(SanitizedContent.ContentKind contentKind)
This assumes that the provided context is an invalid end context for the particular content kind.