Skip to content

Commit

Permalink
- Allow buildClassValue to take an array literal as an argument
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 631126861
  • Loading branch information
Soy Authors authored and Copybara-Service committed May 8, 2024
1 parent bd475fc commit a5aa946
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,21 @@ public static String buildAttrValue(List<SoyValue> values) {
/** Joins items with a space, filtering out falsey values. */
@Nonnull
public static String buildClassValue(List<SoyValue> values) {
return joinHelper(values, " ");
return values.stream()
.filter(v -> v != null)
.filter(s -> s.coerceToBoolean())
.map(
s -> {
if (s instanceof SoyList) {
return s.asJavaList().stream()
.filter(v -> v != null)
.filter(v -> v.resolve().coerceToBoolean())
.map(v -> v.resolve().coerceToString())
.collect(joining(" "));
}
return s.coerceToString();
})
.collect(joining(" "));
}

private static final Pattern CSS_NAME_REGEX = Pattern.compile("^\\s*[\\w-]+\\s*$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
name = "buildClassValue",
value = {
@Signature(
parameterTypes = {"string | css | bool | null | undefined"},
parameterTypes = {
"string | css | bool | null | undefined | list<string|css|bool|null|undefined>"
},
returnType = "string"),
@Signature(
parameterTypes = {
Expand Down
4 changes: 2 additions & 2 deletions javascript/soyutils_usegoog.js
Original file line number Diff line number Diff line change
Expand Up @@ -1840,12 +1840,12 @@ const $$buildAttrValue = function(...values) {

/**
* Joins items with a space, filtering out falsey values.
* @param {...(string|SanitizedCss!|boolean|null|undefined)} values The values
* @param {...(string|SanitizedCss!|boolean|null|undefined|!ReadonlyArray<?>)} values The values
* to join.
* @return {string} The joined string.
*/
const $$buildClassValue = function(...values) {
return values.filter((s) => s).join(' ');
return values.flat(1).filter((s) => s).join(' ');
};


Expand Down
6 changes: 4 additions & 2 deletions python/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def concat_css_values(l, r):
def merge_into_dict(original, secondary):
"""Merge two dictionaries into the first and return it.
This is simply a conveinence wrapper around the dictionary update method. In
This is simply a convenience wrapper around the dictionary update method. In
addition to the update it returns the original dict to allow for chaining.
Args:
Expand Down Expand Up @@ -714,7 +714,9 @@ def build_class_value(*values):
Returns:
The joined string.
"""
return ' '.join([str(x) for x in values if x])

return ' '.join([build_class_value(*x) if isinstance(x, list)
else str(x) for x in values if x])


def build_style_value(*values):
Expand Down

0 comments on commit a5aa946

Please sign in to comment.