Search in sources :

Example 6 with Interpolation

use of org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation in project sling by apache.

the class ExpressionWrapperTest method testFormatOptionsRemoval.

@Test
public void testFormatOptionsRemoval() {
    Interpolation interpolation = new Interpolation();
    Map<String, ExpressionNode> options = new HashMap<>();
    List<ExpressionNode> formatArray = new ArrayList<>();
    formatArray.add(new StringConstant("John"));
    formatArray.add(new StringConstant("Doe"));
    options.put(FormatFilter.FORMAT_OPTION, new ArrayLiteral(formatArray));
    interpolation.addExpression(new Expression(new StringConstant("Hello {0} {1}"), options));
    ExpressionWrapper wrapper = new ExpressionWrapper(filters);
    Expression result = wrapper.transform(interpolation, MarkupContext.TEXT, ExpressionContext.TEXT);
    List<ExpressionNode> xssArguments = runOptionsAndXSSAssertions(result, 0);
    RuntimeCall format = (RuntimeCall) xssArguments.get(0);
    assertEquals(RuntimeFunction.FORMAT, format.getFunctionName());
}
Also used : Interpolation(org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation) HashMap(java.util.HashMap) Expression(org.apache.sling.scripting.sightly.compiler.expression.Expression) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) ArrayList(java.util.ArrayList) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall) StringConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant) ArrayLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral) ExpressionWrapper(org.apache.sling.scripting.sightly.impl.compiler.frontend.ExpressionWrapper) Test(org.junit.Test)

Example 7 with Interpolation

use of org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation in project sling by apache.

the class MarkupHandler method emitAttribute.

private void emitAttribute(String name, String content, char quoteChar, PluginInvoke invoke) {
    invoke.beforeAttribute(stream, name);
    if (content == null) {
        emitSimpleTextAttribute(name, null, quoteChar, invoke);
    } else {
        Interpolation interpolation = expressionParser.parseInterpolation(content);
        String text = tryAsSimpleText(interpolation);
        if (text != null) {
            emitSimpleTextAttribute(name, text, quoteChar, invoke);
        } else {
            emitExpressionAttribute(name, interpolation, quoteChar, invoke);
        }
    }
    invoke.afterAttribute(stream, name);
}
Also used : Interpolation(org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation)

Example 8 with Interpolation

use of org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation in project sling by apache.

the class MarkupHandler method attributeChecked.

private Interpolation attributeChecked(String attributeName, Interpolation interpolation) {
    if (!MarkupUtils.isSensitiveAttribute(attributeName)) {
        return interpolation;
    }
    Interpolation newInterpolation = new Interpolation();
    for (Fragment fragment : interpolation.getFragments()) {
        Fragment addedFragment = fragment;
        if (fragment.isExpression()) {
            Expression expression = fragment.getExpression();
            if (!expression.containsOption(Syntax.CONTEXT_OPTION)) {
                String warningMessage = String.format("Expressions within the value of attribute %s need to have an explicit context " + "option. The expression will be replaced with an empty string.", attributeName);
                stream.warn(new PushStream.StreamMessage(warningMessage, expression.getRawText()));
                addedFragment = new Fragment.Text("");
            }
        }
        newInterpolation.addFragment(addedFragment);
    }
    return newInterpolation;
}
Also used : Interpolation(org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation) Expression(org.apache.sling.scripting.sightly.compiler.expression.Expression) PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) Fragment(org.apache.sling.scripting.sightly.impl.compiler.frontend.Fragment)

Aggregations

Interpolation (org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation)8 Expression (org.apache.sling.scripting.sightly.compiler.expression.Expression)6 HashMap (java.util.HashMap)4 ExpressionNode (org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)4 RuntimeCall (org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)4 StringConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant)4 ExpressionWrapper (org.apache.sling.scripting.sightly.impl.compiler.frontend.ExpressionWrapper)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 ArrayLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral)3 NumericConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant)2 PushStream (org.apache.sling.scripting.sightly.impl.compiler.PushStream)2 Fragment (org.apache.sling.scripting.sightly.impl.compiler.frontend.Fragment)2 MapLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral)1