maxLen = args.get(0).integerValue(); } catch (SoyDataException sde) { throw new IllegalArgumentException( "Could not parse first parameter of '|truncate' as integer (value was \"" + args.get(0).stringValue() + "\")."); String str = value.toString(); if (str.length() <= maxLen) { return SoyData.createFromExistingData(str); // no need to truncate if (args.size() == 2) { try { doAddEllipsis = args.get(1).booleanValue(); } catch (SoyDataException sde) { throw new IllegalArgumentException( return SoyData.createFromExistingData(str);
/** * Precondition: The specified index contains a float. Gets the float at the given index. * * @param index The index. * @return The float at the given index, or null of the index is undefined. */ public double getFloat(int index) { return get(index).floatValue(); }
/** * Precondition: The specified index contains a long. Gets the long at the given index. * * @param index The index. * @return The long at the given index, or null of the index is undefined. */ public long getLong(int index) { return get(index).longValue(); }
@Override public SoyData apply(SoyData value, List<SoyData> args) { return value instanceof StringData ? value : SoyData.createFromExistingData(value.toString()); }
public static SoyData $$plus(SoyData operand0, SoyData operand1) { if (operand0 instanceof NumberData && operand1 instanceof NumberData) { if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return IntegerData.forValue(operand0.integerValue() + operand1.integerValue()); } else { return FloatData.forValue(operand0.numberValue() + operand1.numberValue()); } } else { // String concatenation. Note we're calling toString() instead of stringValue() in case one // of the operands needs to be coerced to a string. return StringData.forValue(operand0.toString() + operand1.toString()); } }
@Override public SoyData compute(List<SoyData> args) { SoyData arg0 = args.get(0); SoyData arg1 = args.get(1); if (arg0 instanceof IntegerData && arg1 instanceof IntegerData) { return toSoyData(Math.min(arg0.integerValue(), arg1.integerValue())); } else { return toSoyData(Math.min(arg0.numberValue(), arg1.numberValue())); } }
@Override public SoyData compute(List<SoyData> args) { SoyData arg0 = args.get(0); SoyData arg1 = args.get(1); Preconditions.checkArgument(arg0 instanceof StringData, String.format( "First argument to strContains() function is not StringData: %s", arg0.stringValue())); Preconditions.checkArgument(arg1 instanceof StringData, String.format( "Second argument to strContains() function is not StringData: %s", arg1.stringValue())); String strArg0 = arg0.stringValue(); String strArg1 = arg1.stringValue(); return toSoyData(strArg0.contains(strArg1)); }
public SoyData convertToSoyData(Object value) { return SoyData.createFromExistingData(convertObject(value)); }
@Override public SoyData compute(List<SoyData> args) { String text = args.get(0).stringValue(); //noinspection SimplifiableConditionalExpression boolean isHtml = (args.size() == 2) ? args.get(1).booleanValue() : false /* default */; return toSoyData(BidiUtils.estimateDirection(text, isHtml).ord); }
maxCharsBetweenWordBreaks = args.get(0).integerValue(); } catch (SoyDataException sde) { throw new IllegalArgumentException( int numCharsWithoutBreak = 0; // number of characters since the last word break String str = value.toString(); for (int codePoint, i = 0, n = str.length(); i < n; i += Character.charCount(codePoint)) { codePoint = str.codePointAt(i); return SoyData.createFromExistingData(result.toString());
/** * Precondition: The specified index contains an integer. Gets the integer at the given index. * * @param index The index. * @return The integer at the given index, or null of the index is undefined. */ public int getInteger(int index) { return get(index).integerValue(); }
/** * Converts the input to a JavaScript expression. The resulting expression can be a boolean, * number, string literal, or {@code null}. */ public static String escapeJsValue(SoyData value) { // We surround values with spaces so that they can't be interpolated into identifiers // by accident. We could use parentheses but those might be interpreted as a function call. if (NullData.INSTANCE == value) { // The JS counterpart of this code in soyutils.js emits " null " for both null and the special // JS value undefined. return " null "; } else if (value instanceof NumberData) { // This will emit references to NaN and Infinity. Client code should not redefine those // to store sensitive data. return " " + value.numberValue() + " "; } else if (value instanceof BooleanData) { return " " + value.booleanValue() + " "; } else if (isSanitizedContentOfKind(value, SanitizedContent.ContentKind.JS)) { return value.toString(); } else { return escapeJsValue(value.toString()); } }
renderedBlock.stringValue(), childCpcn.getContentKind()); resultData = applyDirective(directiveName, resultData, ImmutableList.<SoyData>of(), node); append(currOutputBuf, resultData.toString());
@Override protected SoyData visitNegativeOpNode(NegativeOpNode node) { SoyData operand = visit(node.getChild(0)); if (operand instanceof IntegerData) { return convertResult( - operand.integerValue() ); } else { return convertResult( - operand.floatValue() ); } }
/** * Precondition: The specified index contains a boolean. Gets the boolean at the given index. * * @param index The index. * @return The boolean at the given index, or null of the index is undefined. */ public boolean getBoolean(int index) { return get(index).booleanValue(); }
@Override protected SoyData visitNotEqualOpNode(NotEqualOpNode node) { SoyData operand0 = visit(node.getChild(0)); SoyData operand1 = visit(node.getChild(1)); return convertResult(!operand0.equals(operand1)); }
/** * Precondition: The specified index contains a string. Gets the string at the given index. * * @param index The index. * @return The string at the given index, or null of the index is undefined. */ public String getString(int index) { return get(index).stringValue(); }
@Override public final SoyData apply(SoyData value, List<SoyData> args) { return SoyData.createFromExistingData(escape(value)); }
@Override public SoyData compute(List<SoyData> args) { String text = args.get(0).stringValue(); @SuppressWarnings("SimplifiableConditionalExpression") // make IntelliJ happy boolean isHtml = (args.size() == 2) ? args.get(1).booleanValue() : false /* default */; int bidiGlobalDir = bidiGlobalDirProvider.get().getStaticValue(); return UnsafeSanitizedContentOrdainer.ordainAsSafe( SoyBidiUtils.getBidiFormatter(bidiGlobalDir).dirAttr(text, isHtml), SanitizedContent.ContentKind.ATTRIBUTES); }
@Override public SoyData apply(SoyData value, List<SoyData> args) { return SoyData.createFromExistingData( NEWLINE_PATTERN.matcher(value.toString()).replaceAll("<br>")); }