Skip to content

Commit

Permalink
Convert internal print directives to using the JS DSL, which is more …
Browse files Browse the repository at this point in the history
…modern and correct, and necessary for sourcemaps/kythe support inside such expressions.

PiperOrigin-RevId: 708641155
  • Loading branch information
Jesse-Good authored and copybara-github committed Dec 23, 2024
1 parent 8ac5cb5 commit 089a9df
Show file tree
Hide file tree
Showing 41 changed files with 360 additions and 322 deletions.
3 changes: 2 additions & 1 deletion java/src/com/google/template/soy/basicdirectives/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ java_library(
"//java/src/com/google/template/soy/data:unsafesanitizedcontentordainer",
"//java/src/com/google/template/soy/internal/targetexpr",
"//java/src/com/google/template/soy/jbcsrc/restricted",
"//java/src/com/google/template/soy/jssrc/restricted",
"//java/src/com/google/template/soy/jssrc/dsl",
"//java/src/com/google/template/soy/jssrc/restricted:internal",
"//java/src/com/google/template/soy/pysrc/restricted",
"//java/src/com/google/template/soy/shared/internal:escaping_library",
"//java/src/com/google/template/soy/shared/internal:short_circuitable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import com.google.template.soy.jbcsrc.restricted.SoyExpression;
import com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective;
import com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective.Streamable.AppendableAndOptions;
import com.google.template.soy.jssrc.restricted.JsExpr;
import com.google.template.soy.jssrc.restricted.SoyLibraryAssistedJsSrcPrintDirective;
import com.google.template.soy.jssrc.restricted.ModernSoyJsSrcPrintDirective;
import com.google.template.soy.pysrc.restricted.PyExpr;
import com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective;
import com.google.template.soy.shared.internal.Sanitizers;
Expand All @@ -53,7 +52,7 @@
*/
public abstract class BasicEscapeDirective
implements SoyJavaPrintDirective,
SoyLibraryAssistedJsSrcPrintDirective,
ModernSoyJsSrcPrintDirective,
SoyPySrcPrintDirective,
SoyJbcSrcPrintDirective {

Expand All @@ -65,7 +64,9 @@ public abstract class BasicEscapeDirective
@LazyInit private IdentityHashMap<Class<?>, MethodRef> javaSanitizerByParamType;
@LazyInit private MethodRef javaStreamingSanitizer;

/** @param name E.g. {@code |escapeUri}. */
/**
* @param name E.g. {@code |escapeUri}.
*/
public BasicEscapeDirective(String name) {
this.name = name;
}
Expand Down Expand Up @@ -99,14 +100,10 @@ public SoyValue applyForJava(SoyValue value, List<SoyValue> args) {
}

@Override
public JsExpr applyForJsSrc(JsExpr value, List<JsExpr> args) {
return new JsExpr(
"soy.$$" + name.substring(1) + "(" + value.getText() + ")", Integer.MAX_VALUE);
}

@Override
public ImmutableSet<String> getRequiredJsLibNames() {
return ImmutableSet.of("soy");
public com.google.template.soy.jssrc.dsl.Expression applyForJsSrc(
com.google.template.soy.jssrc.dsl.Expression value,
List<com.google.template.soy.jssrc.dsl.Expression> args) {
return SOY.dotAccess("$$" + name.substring(1)).call(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import com.google.template.soy.jbcsrc.restricted.MethodRef;
import com.google.template.soy.jbcsrc.restricted.SoyExpression;
import com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective;
import com.google.template.soy.jssrc.restricted.JsExpr;
import com.google.template.soy.jssrc.restricted.SoyLibraryAssistedJsSrcPrintDirective;
import com.google.template.soy.jssrc.restricted.ModernSoyJsSrcPrintDirective;
import com.google.template.soy.pysrc.restricted.PyExpr;
import com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective;
import com.google.template.soy.shared.restricted.SoyJavaPrintDirective;
Expand All @@ -39,14 +38,12 @@
import java.util.Set;
import javax.annotation.Nonnull;

/**
* A directive that replaces newlines (\n, \r, or \r\n) with HTML line breaks (&lt;br&gt;).
*/
/** A directive that replaces newlines (\n, \r, or \r\n) with HTML line breaks (&lt;br&gt;). */
@SoyPurePrintDirective
final class ChangeNewlineToBrDirective
implements SanitizedContentOperator,
SoyJavaPrintDirective,
SoyLibraryAssistedJsSrcPrintDirective,
ModernSoyJsSrcPrintDirective,
SoyPySrcPrintDirective,
SoyJbcSrcPrintDirective.Streamable {

Expand Down Expand Up @@ -99,13 +96,10 @@ public AppendableAndOptions applyForJbcSrcStreaming(
}

@Override
public JsExpr applyForJsSrc(JsExpr value, List<JsExpr> args) {
return new JsExpr("soy.$$changeNewlineToBr(" + value.getText() + ")", Integer.MAX_VALUE);
}

@Override
public ImmutableSet<String> getRequiredJsLibNames() {
return ImmutableSet.of("soy");
public com.google.template.soy.jssrc.dsl.Expression applyForJsSrc(
com.google.template.soy.jssrc.dsl.Expression value,
List<com.google.template.soy.jssrc.dsl.Expression> args) {
return SOY.dotAccess("$$changeNewlineToBr").call(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import com.google.template.soy.jbcsrc.restricted.MethodRef;
import com.google.template.soy.jbcsrc.restricted.SoyExpression;
import com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective;
import com.google.template.soy.jssrc.restricted.JsExpr;
import com.google.template.soy.jssrc.restricted.SoyLibraryAssistedJsSrcPrintDirective;
import com.google.template.soy.jssrc.dsl.Expressions;
import com.google.template.soy.jssrc.dsl.FormatOptions;
import com.google.template.soy.jssrc.dsl.StringLiteral;
import com.google.template.soy.jssrc.restricted.ModernSoyJsSrcPrintDirective;
import com.google.template.soy.pysrc.restricted.PyExpr;
import com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective;
import com.google.template.soy.shared.internal.Sanitizers;
Expand All @@ -60,7 +62,7 @@
@SoyPurePrintDirective
final class CleanHtmlDirective
implements SoyJavaPrintDirective,
SoyLibraryAssistedJsSrcPrintDirective,
ModernSoyJsSrcPrintDirective,
SoyPySrcPrintDirective,
SoyJbcSrcPrintDirective.Streamable {

Expand Down Expand Up @@ -132,15 +134,28 @@ private Expression fromTagNameList(List<SoyExpression> args) {
}

@Override
public JsExpr applyForJsSrc(JsExpr value, List<JsExpr> args) {
String optionalSafeTagsArg = generateOptionalSafeTagsArg(args);
return new JsExpr(
"soy.$$cleanHtml(" + value.getText() + optionalSafeTagsArg + ")", Integer.MAX_VALUE);
}

@Override
public ImmutableSet<String> getRequiredJsLibNames() {
return ImmutableSet.of("soy");
public com.google.template.soy.jssrc.dsl.Expression applyForJsSrc(
com.google.template.soy.jssrc.dsl.Expression value,
List<com.google.template.soy.jssrc.dsl.Expression> args) {
List<com.google.template.soy.jssrc.dsl.Expression> callArgs = new ArrayList<>();
callArgs.add(value);
if (!args.isEmpty()) {
for (com.google.template.soy.jssrc.dsl.Expression tagName : args) {
if (tagName instanceof StringLiteral) {
OptionalSafeTag.fromTagName(((StringLiteral) tagName).literalValue());
} else {
throw new IllegalArgumentException(
String.format(
"The cleanHtml directive expects arguments to be tag name string "
+ "literals, such as 'span'. Encountered: %s",
tagName.getClass().getSimpleName()
+ ": "
+ tagName.singleExprOrName(FormatOptions.JSSRC).getText()));
}
}
callArgs.add(Expressions.arrayLiteral(args));
}
return SOY.dotAccess("$$cleanHtml").call(callArgs);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import com.google.template.soy.jbcsrc.restricted.MethodRef;
import com.google.template.soy.jbcsrc.restricted.SoyExpression;
import com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective;
import com.google.template.soy.jssrc.restricted.JsExpr;
import com.google.template.soy.jssrc.restricted.SoyLibraryAssistedJsSrcPrintDirective;
import com.google.template.soy.jssrc.dsl.Expression;
import com.google.template.soy.jssrc.restricted.ModernSoyJsSrcPrintDirective;
import com.google.template.soy.pysrc.restricted.PyExpr;
import com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective;
import com.google.template.soy.shared.internal.Sanitizers;
Expand All @@ -43,7 +43,7 @@
@SoyPurePrintDirective
final class FilterImageDataUriDirective
implements SoyJavaPrintDirective,
SoyLibraryAssistedJsSrcPrintDirective,
ModernSoyJsSrcPrintDirective,
SoyPySrcPrintDirective,
SoyJbcSrcPrintDirective {

Expand Down Expand Up @@ -77,13 +77,8 @@ public SoyExpression applyForJbcSrc(
}

@Override
public JsExpr applyForJsSrc(JsExpr value, List<JsExpr> args) {
return new JsExpr("soy.$$filterImageDataUri(" + value.getText() + ")", Integer.MAX_VALUE);
}

@Override
public ImmutableSet<String> getRequiredJsLibNames() {
return ImmutableSet.of("soy");
public Expression applyForJsSrc(Expression value, List<Expression> args) {
return SOY.dotAccess("$$filterImageDataUri").call(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import com.google.template.soy.jbcsrc.restricted.MethodRef;
import com.google.template.soy.jbcsrc.restricted.SoyExpression;
import com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective;
import com.google.template.soy.jssrc.restricted.JsExpr;
import com.google.template.soy.jssrc.restricted.SoyLibraryAssistedJsSrcPrintDirective;
import com.google.template.soy.jssrc.dsl.Expression;
import com.google.template.soy.jssrc.restricted.ModernSoyJsSrcPrintDirective;
import com.google.template.soy.pysrc.restricted.PyExpr;
import com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective;
import com.google.template.soy.shared.internal.Sanitizers;
Expand All @@ -42,7 +42,7 @@
@SoyPurePrintDirective
final class FilterSipUriDirective
implements SoyJavaPrintDirective,
SoyLibraryAssistedJsSrcPrintDirective,
ModernSoyJsSrcPrintDirective,
SoyPySrcPrintDirective,
SoyJbcSrcPrintDirective {

Expand Down Expand Up @@ -76,13 +76,8 @@ public SoyExpression applyForJbcSrc(
}

@Override
public JsExpr applyForJsSrc(JsExpr value, List<JsExpr> args) {
return new JsExpr("soy.$$filterSipUri(" + value.getText() + ")", Integer.MAX_VALUE);
}

@Override
public ImmutableSet<String> getRequiredJsLibNames() {
return ImmutableSet.of("soy");
public Expression applyForJsSrc(Expression value, List<Expression> args) {
return SOY.dotAccess("$$filterSipUri").call(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import com.google.template.soy.jbcsrc.restricted.MethodRef;
import com.google.template.soy.jbcsrc.restricted.SoyExpression;
import com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective;
import com.google.template.soy.jssrc.restricted.JsExpr;
import com.google.template.soy.jssrc.restricted.SoyLibraryAssistedJsSrcPrintDirective;
import com.google.template.soy.jssrc.dsl.Expression;
import com.google.template.soy.jssrc.restricted.ModernSoyJsSrcPrintDirective;
import com.google.template.soy.pysrc.restricted.PyExpr;
import com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective;
import com.google.template.soy.shared.internal.Sanitizers;
Expand All @@ -42,7 +42,7 @@
@SoyPurePrintDirective
final class FilterTelUriDirective
implements SoyJavaPrintDirective,
SoyLibraryAssistedJsSrcPrintDirective,
ModernSoyJsSrcPrintDirective,
SoyPySrcPrintDirective,
SoyJbcSrcPrintDirective {

Expand Down Expand Up @@ -76,13 +76,8 @@ public SoyExpression applyForJbcSrc(
}

@Override
public JsExpr applyForJsSrc(JsExpr value, List<JsExpr> args) {
return new JsExpr("soy.$$filterTelUri(" + value.getText() + ")", Integer.MAX_VALUE);
}

@Override
public ImmutableSet<String> getRequiredJsLibNames() {
return ImmutableSet.of("soy");
public Expression applyForJsSrc(Expression value, List<Expression> args) {
return SOY.dotAccess("$$filterTelUri").call(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import com.google.template.soy.jbcsrc.restricted.MethodRef;
import com.google.template.soy.jbcsrc.restricted.SoyExpression;
import com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective;
import com.google.template.soy.jssrc.restricted.JsExpr;
import com.google.template.soy.jssrc.restricted.SoyLibraryAssistedJsSrcPrintDirective;
import com.google.template.soy.jssrc.restricted.ModernSoyJsSrcPrintDirective;
import com.google.template.soy.pysrc.restricted.PyExpr;
import com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective;
import com.google.template.soy.shared.restricted.SoyJavaPrintDirective;
Expand All @@ -52,7 +51,7 @@
final class InsertWordBreaksDirective
implements SanitizedContentOperator,
SoyJavaPrintDirective,
SoyLibraryAssistedJsSrcPrintDirective,
ModernSoyJsSrcPrintDirective,
SoyPySrcPrintDirective,
SoyJbcSrcPrintDirective.Streamable {

Expand Down Expand Up @@ -118,16 +117,10 @@ public AppendableAndOptions applyForJbcSrcStreaming(
}

@Override
public JsExpr applyForJsSrc(JsExpr value, List<JsExpr> args) {

return new JsExpr(
"soy.$$insertWordBreaks(" + value.getText() + ", " + args.get(0).getText() + ")",
Integer.MAX_VALUE);
}

@Override
public ImmutableSet<String> getRequiredJsLibNames() {
return ImmutableSet.of("soy");
public com.google.template.soy.jssrc.dsl.Expression applyForJsSrc(
com.google.template.soy.jssrc.dsl.Expression value,
List<com.google.template.soy.jssrc.dsl.Expression> args) {
return SOY.dotAccess("$$insertWordBreaks").call(value, args.get(0));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

package com.google.template.soy.basicdirectives;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.template.soy.data.SoyValue;
import com.google.template.soy.jbcsrc.restricted.Expression;
import com.google.template.soy.jbcsrc.restricted.JbcSrcPluginContext;
import com.google.template.soy.jbcsrc.restricted.SoyExpression;
import com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective;
import com.google.template.soy.jssrc.restricted.JsExpr;
import com.google.template.soy.jssrc.restricted.JsExprUtils;
import com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective;
import com.google.template.soy.jssrc.dsl.Expressions;
import com.google.template.soy.jssrc.restricted.ModernSoyJsSrcPrintDirective;
import com.google.template.soy.pysrc.restricted.PyExpr;
import com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective;
import com.google.template.soy.shared.restricted.SoyJavaPrintDirective;
Expand All @@ -42,7 +40,7 @@
@SoyPurePrintDirective
final class TextDirective
implements SoyJavaPrintDirective,
SoyJsSrcPrintDirective,
ModernSoyJsSrcPrintDirective,
SoyPySrcPrintDirective,
SoyJbcSrcPrintDirective.Streamable {

Expand Down Expand Up @@ -77,10 +75,10 @@ public AppendableAndOptions applyForJbcSrcStreaming(
}

@Override
public JsExpr applyForJsSrc(JsExpr value, List<JsExpr> args) {
// Coerce to string, since sometimes this will be the root of an expression and will be used as
// a return value or let-block assignment.
return JsExprUtils.concatJsExprs(ImmutableList.of(new JsExpr("''", Integer.MAX_VALUE), value));
public com.google.template.soy.jssrc.dsl.Expression applyForJsSrc(
com.google.template.soy.jssrc.dsl.Expression value,
List<com.google.template.soy.jssrc.dsl.Expression> args) {
return Expressions.stringLiteral("").plus(value);
}

@Override
Expand Down
Loading

0 comments on commit 089a9df

Please sign in to comment.