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.UriPart
Describes the part of a URI reference that the context point is in.
|
static class |
Context.UriType
Describes the type or context of a URI that is currently being or about to be parsed.
|
| 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 |
HTML_PCDATA
The normal context for HTML where a less than opens a tag and an ampersand starts an HTML
entity.
|
int |
jsTemplateLiteralNestDepth
The count of
js template elements entered and not subsequently exited. |
Context.JsFollowingSlash |
slashType
Determines what we will do with a slash token
/. |
HtmlContext |
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(...). |
Context.UriType |
uriType
Determines the context in which this URI is being used.
|
| Modifier and Type | Method and Description |
|---|---|
Context |
derive(Context.JsFollowingSlash slashType)
Returns a context that differs only in the following slash.
|
Context |
derive(Context.UriPart uriPart)
Returns a context that differs only in the uri part.
|
Context |
derive(HtmlContext state)
Returns a context that differs only in the state.
|
boolean |
equals(Object o)
Checks if two states are completely identical.
|
Context |
getContextAfterDynamicValue()
The context after printing a correctly-escaped dynamic value in this context.
|
com.google.common.collect.ImmutableList<EscapingMode> |
getEscapingModes(SoyNode node,
List<PrintDirectiveNode> printDirectives)
Returns the escaping mode appropriate for dynamic content inserted in this context.
|
String |
getLikelyEndContextMismatchCause(SanitizedContentKind contentKind)
Returns a plausible human-readable description of a context mismatch;
|
SanitizedContentKind |
getMostAppropriateContentKind()
Returns the most sensible content kind for a context.
|
static Context |
getStartContextForContentKind(SanitizedContentKind contentKind)
Returns the autoescape
Context that produces sanitized content of the given SanitizedContentKind. |
int |
hashCode() |
boolean |
isCompatibleWith(EscapingMode mode)
True if the given escaping mode could make sense in this context.
|
boolean |
isValidEndContextForContentKind(SanitizedContentKind contentKind)
Determines whether a particular context is valid for the end of a block of a particular content
kind.
|
boolean |
isValidStartContextForContentKind(SanitizedContentKind contentKind)
Determines whether a particular context is valid at the start of a block of a particular
content kind.
|
boolean |
isValidStartContextForContentKindLoose(SanitizedContentKind 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() |
public final HtmlContext 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 Context.UriType uriType
public final int templateNestDepth
<template> elements entered and not subsequently exited.public final int jsTemplateLiteralNestDepth
js template elements entered and not subsequently exited.public static final Context HTML_PCDATA
public Context derive(HtmlContext state)
public Context derive(Context.JsFollowingSlash slashType)
public Context derive(Context.UriPart uriPart)
public Context getContextAfterDynamicValue()
This makes the optimistic assumption that the escaped string is not empty. This can lead to correctness behaviors, but the default is to fail closed; for example, printing an empty string at UriPart.START switches to MAYBE_VARIABLE_SCHEME, which is designed not to trust the printed value anyway. Same in JS -- we might switch to DIV_OP when we should have stayed in REGEX, but in the worse case, we'll just produce JavaScript that doesn't compile (which is safe).
public com.google.common.collect.ImmutableList<EscapingMode> getEscapingModes(SoyNode node, List<PrintDirectiveNode> printDirectives)
public boolean isCompatibleWith(EscapingMode mode)
public boolean equals(Object o)
Note it's better to compare either states, or use predicates like isValidEndContext.
public int packedBits()
public static Context getStartContextForContentKind(SanitizedContentKind contentKind)
Context that produces sanitized content of the given SanitizedContentKind.
Given a SanitizedContentKind, 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(SanitizedContentKind contentKind)
public boolean isValidStartContextForContentKindLoose(SanitizedContentKind contentKind)
This is slightly more relaxed, and used to help piecemeal transition of templates from contextual to strict.
public SanitizedContentKind 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(SanitizedContentKind contentKind)
public final String getLikelyEndContextMismatchCause(SanitizedContentKind contentKind)
This assumes that the provided context is an invalid end context for the particular content kind.