Skip to content

Commit

Permalink
Add a boolean parameter to flushPendingLoggingAttributes to track w…
Browse files Browse the repository at this point in the history
…hether we are writing to an `anchor` tag or not.

Add support for `flushPendingLoggingParameters` in all our appendable implementations

PiperOrigin-RevId: 708504098
  • Loading branch information
Soy Authors authored and copybara-github committed Dec 21, 2024
1 parent 2f8b96d commit f358346
Show file tree
Hide file tree
Showing 14 changed files with 14 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ public LoggingAdvisingAppendable appendLoggingFunctionInvocation(
return this;
}

@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag) {
return this;
}

@Override
public boolean softLimitReached() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.template.soy.data.restricted.NullData;
import com.google.template.soy.shared.internal.AbstractStreamingHtmlEscaper;
import com.google.template.soy.shared.internal.EscapingConventions;
import java.io.IOException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -102,16 +101,6 @@ public LoggingAdvisingAppendable exitLoggableElement() {
}
return this;
}

@CanIgnoreReturnValue
@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag)
throws IOException {
if (isInHtml()) {
delegate.flushPendingLoggingAttributes(isAnchorTag);
}
return this;
}
}

private CoreDirectivesRuntime() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@ public LoggingAdvisingAppendable appendLoggingFunctionInvocation(
return this;
}

@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag)
throws IOException {
delegate.flushPendingLoggingAttributes(isAnchorTag);
return this;
}

@Override
public void flushBuffers(int depth) throws IOException {
if (depth > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public abstract LoggingAdvisingAppendable append(CharSequence csq, int start, in
public abstract LoggingAdvisingAppendable exitLoggableElement();

/** Flushes all pending logging attributes. */
@CanIgnoreReturnValue
@Nonnull
public abstract LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag)
throws IOException;
public final LoggingAdvisingAppendable flushPendingLoggingAttributes() {
// TODO(b/383661457): Implement this.
return this;
}

/**
* Flushes all intermediate buffers stored within the appendable.
Expand Down Expand Up @@ -245,15 +245,11 @@ public String toString() {
public static class BufferingAppendable extends LoggingAdvisingAppendable {

private static final Object EXIT_LOG_STATEMENT_MARKER = new Object();
private static final Object FLUSH_PENDING_LOGGING_ATTRBUTES_TRUE_MARKER = new Object();
private static final Object FLUSH_PENDING_LOGGING_ATTRBUTES_FALSE_MARKER = new Object();
// lazily allocated list that contains one of 7 types of objects, each which corresponds to one
// of the callback methods.
// - String literal string content -> corresponds to a contiguous sequence of append calls
// - LogStatement -> corresponds to enterLoggableElement
// - EXIT_LOG_STATEMENT_MARKER -> corresponds to exitLoggableElement
// - FLUSH_PENDING_LOGGING_(TRUE|FALSE)_ATTRBUTES_MARKER -> corresponds to
// flushPendingLoggingAttributes(true|false)
// - LoggingFunctionInvocation -> corresponds to appendLoggingFunctionInvocation
private List<Object> commands;
private final StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -333,16 +329,6 @@ public LoggingAdvisingAppendable exitLoggableElement() {
return this;
}

@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchor) {
getCommandsAndAddPendingStringData()
.add(
isAnchor
? FLUSH_PENDING_LOGGING_ATTRBUTES_TRUE_MARKER
: FLUSH_PENDING_LOGGING_ATTRBUTES_FALSE_MARKER);
return this;
}

@CanIgnoreReturnValue
@Override
public LoggingAdvisingAppendable appendLoggingFunctionInvocation(
Expand Down Expand Up @@ -383,9 +369,6 @@ private static void replayCommandOn(Object o, LoggingAdvisingAppendable appendab
((LoggingFunctionCommand) o).replayOn(appendable);
} else if (o == EXIT_LOG_STATEMENT_MARKER) {
appendable.exitLoggableElement();
} else if (o == FLUSH_PENDING_LOGGING_ATTRBUTES_TRUE_MARKER
|| o == FLUSH_PENDING_LOGGING_ATTRBUTES_FALSE_MARKER) {
appendable.flushPendingLoggingAttributes(o == FLUSH_PENDING_LOGGING_ATTRBUTES_TRUE_MARKER);
} else if (o instanceof LogStatement) {
appendable.enterLoggableElement((LogStatement) o);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ static Statement concat(List<Statement> statements) {
MethodRef.createNonPure(LoggingAdvisingAppendable.class, "flushBuffers", int.class);

private static final MethodRef FLUSH_PENDING_LOGGING_ATTRBIUTES =
MethodRef.createNonPure(
LoggingAdvisingAppendable.class, "flushPendingLoggingAttributes", boolean.class);
MethodRef.createNonPure(LoggingAdvisingAppendable.class, "flushPendingLoggingAttributes");

static AppendableExpression forExpression(Expression delegate) {
return new AppendableExpression(delegate, e -> e, /* supportsSoftLimiting= */ true);
Expand Down Expand Up @@ -272,8 +271,8 @@ AppendableExpression setSanitizedContentKindAndDirectionality(SanitizedContentKi
BytecodeUtils.constantSanitizedContentKindAsContentKind(kind)));
}

AppendableExpression flushPendingLoggingAttributes(boolean isAnchorTag) {
return withNewDelegate(e -> e.invoke(FLUSH_PENDING_LOGGING_ATTRBIUTES, constant(isAnchorTag)));
AppendableExpression flushPendingLoggingAttributes() {
return withNewDelegate(e -> e.invoke(FLUSH_PENDING_LOGGING_ATTRBIUTES));
}

Statement flushBuffers(int depth) {
Expand Down
8 changes: 3 additions & 5 deletions java/src/com/google/template/soy/jbcsrc/SoyNodeCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ private Optional<Statement> tryCompileSwitchToSwitchInstruction(
asSwitchableInt(switchExpr, casesByKey.navigableKeySet()), casesByKey, defaultBlock));

} else {
// Otherwise we need more complex matching logic that we outsource to an invoke dynamic
// Otherwise we need more complex matching logic that we outsource to an invoke dyanmic
// bootstrap. Create a fake key for each case and then rely on the bootstrap to figure it
// out.
// update the map with the pseudo keys, so that the loops below can find them
Expand Down Expand Up @@ -843,9 +843,7 @@ protected Statement visitPrintNode(PrintNode node) {
return visitLoggingFunction(node, fn, (LoggingFunction) fn.getSoyFunction());
}
if (fn.getSoyFunction() == BuiltinFunction.FLUSH_PENDING_LOGGING_ATTRIBUTES) {
return appendableExpression
.flushPendingLoggingAttributes(((BooleanNode) fn.getParams().get(0)).getValue())
.toStatement();
return appendableExpression.flushPendingLoggingAttributes().toStatement();
}
}
// First check our special case where all print directives are streamable and an expression that
Expand Down Expand Up @@ -1051,7 +1049,7 @@ protected Statement visitRawTextNode(RawTextNode node) {

@Override
protected Statement visitDebuggerNode(DebuggerNode node) {
// Call JbcSrcRuntime.debugger. This logs a stack trace by default and is an obvious place to
// Call JbcSrcRuntime.debuggger. This logs a stack trace by default and is an obvious place to
// put a breakpoint.
return MethodRefs.RUNTIME_DEBUGGER.invokeVoid(
constant(node.getSourceLocation().getFilePath().path()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,8 @@ public LoggingAdvisingAppendable exitLoggableElement() {
}

@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag)
throws IOException {
// TODO: b/383661457 - implement this.
return this;
public void flushBuffers(int depth) {
throw new AssertionError("shouldn't be called");
}

private void appendDebugOutput(Optional<SafeHtml> veDebugOutput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,6 @@ public LoggingAdvisingAppendable exitLoggableElement() {
return this;
}

@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag) {
return this;
}

@Override
public LoggingAdvisingAppendable appendLoggingFunctionInvocation(
LoggingFunctionInvocation funCall, ImmutableList<Function<String, String>> escapers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import com.google.template.soy.base.internal.IdGenerator;
import com.google.template.soy.base.internal.Identifier;
import com.google.template.soy.error.ErrorReporter;
import com.google.template.soy.exprtree.BooleanNode;
import com.google.template.soy.exprtree.ExprNode;
import com.google.template.soy.exprtree.FunctionNode;
import com.google.template.soy.shared.internal.BuiltinFunction;
import com.google.template.soy.soytree.HtmlAttributeNode;
Expand Down Expand Up @@ -94,7 +92,6 @@ private void instrumentNode(IdGenerator nodeIdGen, HtmlOpenTagNode openTag) {
BuiltinFunction.FLUSH_PENDING_LOGGING_ATTRIBUTES.name(), SourceLocation.UNKNOWN),
BuiltinFunction.FLUSH_PENDING_LOGGING_ATTRIBUTES,
SourceLocation.UNKNOWN);
functionCall.addChild(getTagIsAnchorNode(openTag));
functionCall.setType(AttributesType.getInstance());
var printNode =
new PrintNode(
Expand All @@ -114,12 +111,4 @@ private void instrumentNode(IdGenerator nodeIdGen, HtmlOpenTagNode openTag) {
attributeNode.addChild(printNode);
openTag.addChild(attributeNode);
}

private static ExprNode getTagIsAnchorNode(HtmlOpenTagNode openTag) {
var tagName = openTag.getTagName();
return new BooleanNode(
// If the tag is dynamic we assume it isn't an anchor
tagName.isStatic() && tagName.getStaticTagNameAsLowerCase().equals("a"),
tagName.getTagLocation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.common.collect.ImmutableSet;
import com.google.template.soy.shared.restricted.SoyFunction;
import com.google.template.soy.types.AnyType;
import com.google.template.soy.types.BoolType;
import com.google.template.soy.types.IterableType;
import com.google.template.soy.types.NullType;
import com.google.template.soy.types.SanitizedType;
Expand Down Expand Up @@ -94,6 +93,7 @@ public Set<Integer> getValidArgsSizes() {
case IS_PRIMARY_MSG_IN_USE:
return ImmutableSet.of(3);
case DEBUG_SOY_TEMPLATE_INFO:
case FLUSH_PENDING_LOGGING_ATTRIBUTES:
return ImmutableSet.of(0);
case SOY_SERVER_KEY:
case CHECK_NOT_NULL:
Expand All @@ -110,7 +110,6 @@ public Set<Integer> getValidArgsSizes() {
case IS_TRUTHY_NON_EMPTY:
case HAS_CONTENT:
case NEW_SET:
case FLUSH_PENDING_LOGGING_ATTRIBUTES:
return ImmutableSet.of(1);
case PROTO_INIT:
throw new UnsupportedOperationException();
Expand All @@ -136,8 +135,6 @@ public Optional<List<SoyType>> getValidArgTypes() {
StringType.getInstance(),
NullType.getInstance(),
UndefinedType.getInstance())));
case FLUSH_PENDING_LOGGING_ATTRIBUTES:
return Optional.of(ImmutableList.of(BoolType.getInstance()));
case NEW_SET:
// This is further constrained in ResolveExpressionTypesPass.
return Optional.of(ImmutableList.of(IterableType.of(AnyType.getInstance())));
Expand Down
30 changes: 0 additions & 30 deletions java/src/com/google/template/soy/shared/internal/Sanitizers.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,6 @@ public LoggingAdvisingAppendable exitLoggableElement() {
return this;
}

@CanIgnoreReturnValue
@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag)
throws IOException {
if (isInHtml()) {
delegate.flushPendingLoggingAttributes(isAnchorTag);
}
return this;
}

@CanIgnoreReturnValue
@Override
public LoggingAdvisingAppendable appendLoggingFunctionInvocation(
Expand Down Expand Up @@ -339,11 +329,6 @@ public LoggingAdvisingAppendable enterLoggableElement(LogStatement statement) {
public LoggingAdvisingAppendable exitLoggableElement() {
return this;
}

@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag) {
return this;
}
}

/** Normalizes HTML to HTML making sure quotes and other specials are entity encoded. */
Expand Down Expand Up @@ -977,11 +962,6 @@ public LoggingAdvisingAppendable exitLoggableElement() {
return this;
}

@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag) {
return this;
}

@CanIgnoreReturnValue
@Override
public LoggingAdvisingAppendable appendLoggingFunctionInvocation(
Expand Down Expand Up @@ -1169,16 +1149,6 @@ public LoggingAdvisingAppendable appendLoggingFunctionInvocation(
return this;
}

@CanIgnoreReturnValue
@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag)
throws IOException {
if (getSanitizedContentKind() == ContentKind.ATTRIBUTES) {
delegate.flushPendingLoggingAttributes(isAnchorTag);
}
return this;
}

@Override
public boolean softLimitReached() {
return delegate.softLimitReached();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,6 @@ public LoggingAdvisingAppendable appendLoggingFunctionInvocation(
return this;
}

@CanIgnoreReturnValue
@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag)
throws IOException {
var buffer = this.buffer;
if (buffer == null) {
delegate.flushPendingLoggingAttributes(isAnchorTag);
}
return this;
}

@Override
public boolean softLimitReached() {
return delegate.softLimitReached();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ public LoggingAdvisingAppendable appendLoggingFunctionInvocation(
return append(escapePlaceholder(funCall.placeholderValue(), escapers));
}

@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag) {
return this;
}

@Override
public boolean softLimitReached() {
return delegate.softLimitReached();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.template.soy.data.SoyRecord;
import com.google.template.soy.data.SoyValueConverter;
import com.google.template.soy.data.SoyValueConverterUtility;
import com.google.template.soy.data.SoyValueProvider;
import com.google.template.soy.data.internal.ParamStore;
import com.google.template.soy.error.ErrorReporter;
import com.google.template.soy.jbcsrc.restricted.BytecodeUtils;
Expand Down Expand Up @@ -457,13 +458,6 @@ public LoggingAdvisingAppendable exitLoggableElement() {
return this;
}

@Override
public LoggingAdvisingAppendable flushPendingLoggingAttributes(boolean isAnchorTag)
throws IOException {
delegate.flushPendingLoggingAttributes(isAnchorTag);
return this;
}

@CanIgnoreReturnValue
@Override
public LoggingAdvisingAppendable appendLoggingFunctionInvocation(
Expand Down

0 comments on commit f358346

Please sign in to comment.