Search in sources :

Example 1 with TemplateData

use of com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData in project flow by vaadin.

the class TemplateDataAnalyzer method parseTemplate.

/**
 * Gets the template data for the template initializer.
 *
 * @return the template data
 */
ParserData parseTemplate() {
    TemplateData templateData = parser.getTemplateContent(templateClass, tag);
    templateRoot = templateData.getTemplateElement();
    htmlImportUri = templateData.getHtmlImportUri();
    Elements templates = templateRoot.getElementsByTag("template");
    for (org.jsoup.nodes.Element element : templates) {
        org.jsoup.nodes.Element parent = element.parent();
        if (parent != null && tag.equals(parent.id())) {
            inspectCustomElements(element, element);
            inspectTwoWayBindings(element);
        }
    }
    collectInjectedIds(templateClass);
    return readData();
}
Also used : TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) Elements(org.jsoup.select.Elements)

Example 2 with TemplateData

use of com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData in project flow by vaadin.

the class TemplateInitializerTest method setUp.

@Before
public void setUp() throws NoSuchFieldException {
    String parentTemplateId = InTemplateClass.class.getAnnotation(Tag.class).value();
    assertThat("Both classes should have the same '@Tag' annotation", OutsideTemplateClass.class.getAnnotation(Tag.class).value(), is(parentTemplateId));
    String inTemplateElementId = InTemplateClass.class.getField("element").getAnnotation(Id.class).value();
    String outsideTemplateElementId = OutsideTemplateClass.class.getField("element").getAnnotation(Id.class).value();
    templateParser = (clazz, tag) -> new TemplateData("", Jsoup.parse(String.format("<dom-module id='%s'><template>" + "    <template><div id='%s'>Test</div></template>" + "    <div id='%s'></div>" + "    <div a='{{twoWay}}' b='{{invalid}} syntax' c='{{two.way}}'" + "        d='{{invalidSyntax' e='{{withEvent::eventName}}' f='[[oneWay]]'></div>" + "</template></dom-module>", parentTemplateId, inTemplateElementId, outsideTemplateElementId)));
}
Also used : TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) Tag(com.vaadin.flow.component.Tag) Before(org.junit.Before)

Example 3 with TemplateData

use of com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData in project flow by vaadin.

the class DefaultTemplateParserTest method defaultParser_hasTemplate_returnsContent.

@Test
public void defaultParser_hasTemplate_returnsContent() {
    TemplateData data = DefaultTemplateParser.getInstance().getTemplateContent(ImportsInspectTemplate.class, "foo");
    Element element = data.getTemplateElement();
    Assert.assertTrue(element.getElementById("foo") != null);
    Assert.assertEquals("/bar1.html", data.getHtmlImportUri());
}
Also used : TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) Element(org.jsoup.nodes.Element) Test(org.junit.Test)

Example 4 with TemplateData

use of com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData in project flow by vaadin.

the class PolymerTemplateTest method parseCachedTemplate_twoTemplatesWithInjetions_injectionsAreRegisteredInFeature.

@Test
public void parseCachedTemplate_twoTemplatesWithInjetions_injectionsAreRegisteredInFeature() {
    Mockito.when(configuration.isProductionMode()).thenReturn(true);
    AtomicInteger parserCallCount = new AtomicInteger();
    TemplateParser parser = new TemplateParser() {

        @Override
        public TemplateData getTemplateContent(Class<? extends PolymerTemplate<?>> clazz, String tag) {
            String content;
            parserCallCount.incrementAndGet();
            if (clazz.equals(TemplateInitialization.class)) {
                content = "<dom-module id='" + tag + "'><template>" + "<ffs id='foo'></ffs>" + "<child-template></child-template>" + "</template></dom-module>";
            } else {
                content = "<dom-module id='" + tag + "'><template>" + "<child-template id='bar'></child-template> <ffs></ffs>" + "</template></dom-module>";
            }
            return new TemplateData("", Jsoup.parse(content));
        }
    };
    // run in the production mode (with caching enabled) for the first time
    TemplateInitialization template1 = new TemplateInitialization(parser);
    assertEquals(1, parserCallCount.get());
    assertTemplateInitialization(template1);
    // run in the production mode (with caching enabled) for the second time
    template1 = new TemplateInitialization(parser);
    // parser shouldn't be called
    assertEquals(1, parserCallCount.get());
    assertTemplateInitialization(template1);
    parserCallCount.set(0);
    // Now initialize another template
    // run in the production mode (with caching enabled) for the first time
    AnotherTemplateInitialization template2 = new AnotherTemplateInitialization(parser);
    assertEquals(1, parserCallCount.get());
    assertAnotherTemplateInitialization(template2);
    // run in the production mode (with caching enabled) for the second time
    template2 = new AnotherTemplateInitialization(parser);
    // parser shouldn't be called
    assertEquals(1, parserCallCount.get());
    assertAnotherTemplateInitialization(template2);
}
Also used : TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

TemplateData (com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData)4 Test (org.junit.Test)2 Tag (com.vaadin.flow.component.Tag)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1 Before (org.junit.Before)1