use of com.google.template.soy.data.SanitizedContent.ContentKind in project closure-templates by google.
the class BidiDirectivesRuntime method bidiUnicodeWrap.
public static SoyString bidiUnicodeWrap(BidiGlobalDir dir, SoyValue value) {
// normalize null between tofu and jbcsrc
value = value == null ? NullData.INSTANCE : value;
ContentKind valueKind = null;
Dir valueDir = null;
if (value instanceof SanitizedContent) {
SanitizedContent sanitizedContent = (SanitizedContent) value;
valueKind = sanitizedContent.getContentKind();
valueDir = sanitizedContent.getContentDirection();
}
BidiFormatter bidiFormatter = BidiFormatter.getInstance(dir.toDir());
// We treat the value as HTML if and only if it says it's HTML, even though in legacy usage, we
// sometimes have an HTML string (not SanitizedContent) that is passed to an autoescape="false"
// template or a {print $foo |noAutoescape}, with the output going into an HTML context without
// escaping. We simply have no way of knowing if this is what is happening when we get
// non-SanitizedContent input, and most of the time it isn't.
boolean isHtml = valueKind == ContentKind.HTML;
String wrappedValue = bidiFormatter.unicodeWrap(valueDir, value.coerceToString(), isHtml);
// Unicode-wrapping safe HTML.
if (valueKind == ContentKind.TEXT || valueKind == ContentKind.HTML) {
// run.
return UnsafeSanitizedContentOrdainer.ordainAsSafe(wrappedValue, valueKind, dir.toDir());
}
// TEXT, or HTML.
if (valueKind != null) {
return StringData.forValue(wrappedValue);
}
// The input was not SanitizedContent, so our output isn't SanitizedContent either.
return StringData.forValue(wrappedValue);
}
use of com.google.template.soy.data.SanitizedContent.ContentKind in project closure-templates by google.
the class RenderVisitor method visitCallNodeHelper.
// for IntelliJ
@SuppressWarnings("ConstantConditions")
private void visitCallNodeHelper(CallNode node, TemplateNode callee) {
// ------ Build the call data. ------
SoyRecord dataToPass;
if (node.isPassingAllData()) {
dataToPass = data;
} else if (node.isPassingData()) {
SoyValue dataRefValue = eval(node.getDataExpr(), node);
if (!(dataRefValue instanceof SoyRecord)) {
throw RenderException.create("In 'call' command " + node.toSourceString() + ", the data reference does not resolve to a SoyRecord.").addStackTraceElement(node);
}
dataToPass = (SoyRecord) dataRefValue;
} else {
dataToPass = null;
}
SoyRecord callData;
int numChildren = node.numChildren();
if (numChildren == 0) {
// --- Cases 1 and 2: Not passing params. ---
if (dataToPass == null) {
// Case 1: Not passing data and not passing params.
callData = ParamStore.EMPTY_INSTANCE;
} else {
// Case 2: Passing data and not passing params.
callData = dataToPass;
}
} else {
// --- Cases 3 and 4: Passing params. ---
ParamStore mutableCallData;
if (dataToPass == null) {
// Case 3: Not passing data and passing params.
mutableCallData = new BasicParamStore(numChildren);
} else {
// Case 4: Passing data and passing params.
mutableCallData = new AugmentedParamStore(dataToPass, numChildren);
}
for (CallParamNode child : node.getChildren()) {
if (child instanceof CallParamValueNode) {
mutableCallData.setField(child.getKey().identifier(), lazyEval(((CallParamValueNode) child).getExpr(), child));
} else if (child instanceof CallParamContentNode) {
mutableCallData.setField(child.getKey().identifier(), renderRenderUnitNode((CallParamContentNode) child));
} else {
throw new AssertionError();
}
}
callData = mutableCallData;
}
if (node.getEscapingDirectives().isEmpty()) {
// No escaping at the call site -- render directly into the output buffer.
RenderVisitor rv = this.createHelperInstance(currOutputBuf, callData);
try {
rv.renderTemplate(callee, node.getParamsToRuntimeCheck(callee));
} catch (RenderException re) {
// this template call.
throw re.addStackTraceElement(node);
}
} else {
// Escaping the call site's result, such as at a strict template boundary.
// TODO: Some optimization is needed here before Strict Soy can be widely used:
// - Only create this temporary buffer when contexts mismatch. We could run a pre-pass that
// eliminates escaping directives when all callers are known.
// - Instead of creating a temporary buffer and copying, wrap with an escaping StringBuilder.
StringBuilder calleeBuilder = new StringBuilder();
RenderVisitor rv = this.createHelperInstance(calleeBuilder, callData);
try {
rv.renderTemplate(callee, node.getParamsToRuntimeCheck(callee));
} catch (RenderException re) {
// this template call.
throw re.addStackTraceElement(node);
}
ContentKind calleeKind = fromSanitizedContentKind(callee.getContentKind());
SoyValue resultData = calleeKind != null ? UnsafeSanitizedContentOrdainer.ordainAsSafe(calleeBuilder.toString(), calleeKind) : StringData.forValue(calleeBuilder.toString());
for (SoyPrintDirective directive : node.getEscapingDirectives()) {
resultData = applyDirective(directive, resultData, ImmutableList.<SoyValue>of(), node);
}
append(currOutputBuf, resultData, node);
}
}
Aggregations