Search in sources :

Example 1 with TemplateParseException

use of com.vaadin.flow.template.angular.TemplateParseException in project flow by vaadin.

the class TemplateParser method getRootElement.

private static Element getRootElement(Document bodyFragment, TemplateResolver templateResolver) {
    Elements children = bodyFragment.body().children();
    int childNodeSize = children.size();
    if (childNodeSize != 1) {
        if (childNodeSize == 0) {
            throw new TemplateParseException("AngularTemplate must not be empty. " + ROOT_CLARIFICATION);
        } else {
            throw new TemplateParseException("AngularTemplate must not have multiple root elements. " + ROOT_CLARIFICATION);
        }
    }
    Element rootElement = children.get(0);
    populateIncludes(rootElement, templateResolver);
    return rootElement;
}
Also used : TemplateParseException(com.vaadin.flow.template.angular.TemplateParseException) Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements)

Example 2 with TemplateParseException

use of com.vaadin.flow.template.angular.TemplateParseException in project flow by vaadin.

the class DefaultElementBuilderFactory method setBinding.

private void setBinding(Attribute attribute, ElementTemplateBuilder builder, Element element) {
    String name = attribute.getKey();
    if (name.startsWith("(")) {
        if (!name.endsWith(")")) {
            throw new TemplateParseException("Event listener registration should be in the form (click)='...' but template contains '" + attribute + "'.");
        }
        String key = extractKey(name, 1);
        builder.addEventHandler(key, attribute.getValue());
    } else if (name.startsWith("[")) {
        if (!name.endsWith("]")) {
            throw new TemplateParseException("Property binding should be in the form [property]='value' but template contains '" + attribute + "'.");
        }
        handlePropertyParsing(attribute, builder, element, name);
    } else {
        /*
             * Regular attribute names in the template, i.e. name not starting
             * with [ or (, are used as static attributes on the target element.
             */
        builder.setAttribute(name, new StaticBindingValueProvider(attribute.getValue()));
    }
}
Also used : TemplateParseException(com.vaadin.flow.template.angular.TemplateParseException) StaticBindingValueProvider(com.vaadin.flow.template.angular.StaticBindingValueProvider)

Example 3 with TemplateParseException

use of com.vaadin.flow.template.angular.TemplateParseException in project flow by vaadin.

the class DefaultElementBuilderFactory method handlePropertyParsing.

private void handlePropertyParsing(Attribute attribute, ElementTemplateBuilder builder, Element element, String name) {
    String key = extractKey(name, 1);
    AbstractBindingValueProvider binding = createExpressionBinding(stripForLoopVariableIfNeeded(attribute.getValue()));
    if (key.startsWith("class.")) {
        String className = key.substring("class.".length());
        String classAttribute = element.attr("class");
        if (Stream.of(classAttribute.split("\\s+")).anyMatch(className::equals)) {
            throw new TemplateParseException(String.format("The class attribute can't contain '%s' " + "when there's also a binding for [class.%s]", className, className));
        }
        builder.setClassName(className, binding);
    } else if (key.startsWith("attr.")) {
        String attributeName = key.substring("attr.".length());
        if (element.hasAttr(attributeName)) {
            throw new TemplateParseException(String.format("The '%s' attribute can't be present when there " + "is also a binding for [attr.%s]", attributeName, attributeName));
        }
        builder.setAttribute(attributeName, binding);
    } else {
        builder.setProperty(key, binding);
    }
}
Also used : TemplateParseException(com.vaadin.flow.template.angular.TemplateParseException) AbstractBindingValueProvider(com.vaadin.flow.template.angular.AbstractBindingValueProvider)

Aggregations

TemplateParseException (com.vaadin.flow.template.angular.TemplateParseException)3 AbstractBindingValueProvider (com.vaadin.flow.template.angular.AbstractBindingValueProvider)1 StaticBindingValueProvider (com.vaadin.flow.template.angular.StaticBindingValueProvider)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1