Search in sources :

Example 1 with HtmlPageItem

use of com.adobe.cq.wcm.core.components.models.HtmlPageItem in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class PageImpl method getHtmlPageItems.

@Override
@NotNull
public List<HtmlPageItem> getHtmlPageItems() {
    if (htmlPageItems == null) {
        htmlPageItems = new LinkedList<>();
        ConfigurationBuilder configurationBuilder = configurationResolver.get(resource);
        HtmlPageItemsConfig config = configurationBuilder.as(HtmlPageItemsConfig.class);
        for (HtmlPageItemConfig itemConfig : config.items()) {
            HtmlPageItem item = new HtmlPageItemImpl(StringUtils.defaultString(config.prefixPath()), itemConfig);
            if (item.getElement() != null) {
                htmlPageItems.add(item);
            }
        }
        // Support the former node structure: see com.adobe.cq.wcm.core.components.config.HtmlPageItemsConfig
        if (htmlPageItems.isEmpty()) {
            Resource configResource = configurationResourceResolver.getResource(resource, "sling:configs", HtmlPageItemsConfig.class.getName());
            if (configResource != null) {
                ValueMap properties = configResource.getValueMap();
                for (Resource child : configResource.getChildren()) {
                    HtmlPageItem item = new HtmlPageItemImpl(properties.get(HtmlPageItemsConfig.PN_PREFIX_PATH, StringUtils.EMPTY), child);
                    if (item.getElement() != null) {
                        htmlPageItems.add(item);
                    }
                }
            }
        }
    }
    return htmlPageItems;
}
Also used : ConfigurationBuilder(org.apache.sling.caconfig.ConfigurationBuilder) HtmlPageItemsConfig(com.adobe.cq.wcm.core.components.config.HtmlPageItemsConfig) HtmlPageItemConfig(com.adobe.cq.wcm.core.components.config.HtmlPageItemConfig) HtmlPageItem(com.adobe.cq.wcm.core.components.models.HtmlPageItem) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with HtmlPageItem

use of com.adobe.cq.wcm.core.components.models.HtmlPageItem in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class PageImplTest method testHtmlPageItemsConfig.

@Test
void testHtmlPageItemsConfig() {
    loadHtmlPageItemsConfig(true);
    Page page = getPageUnderTest(PAGE);
    assertNotNull(page.getHtmlPageItems());
    assertEquals(3, page.getHtmlPageItems().size(), "Unexpected number of HTML page items");
    Map<String, Object> cssAttributes = new HashMap<>();
    Map<String, Object> jsAttributes = new HashMap<>();
    Map<String, Object> metaAttributes = new HashMap<>();
    cssAttributes.put("href", "/_theme/theme.css");
    cssAttributes.put("rel", "preload");
    cssAttributes.put("as", "style");
    jsAttributes.put("async", true);
    jsAttributes.put("defer", false);
    jsAttributes.put("src", "/_theme/theme.js");
    metaAttributes.put("charset", "UTF-8");
    Object[] attributes = { cssAttributes, jsAttributes, metaAttributes };
    int index = 0;
    for (HtmlPageItem item : page.getHtmlPageItems()) {
        assertEquals(attributes[index], item.getAttributes(), "Wrong attributes");
        index++;
    }
}
Also used : HashMap(java.util.HashMap) HtmlPageItem(com.adobe.cq.wcm.core.components.models.HtmlPageItem) Page(com.adobe.cq.wcm.core.components.models.Page) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with HtmlPageItem

use of com.adobe.cq.wcm.core.components.models.HtmlPageItem in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class PageImplTest method testHtmlPageItemsConfigWithDeprecatedCaconfig.

@Test
void testHtmlPageItemsConfigWithDeprecatedCaconfig() throws Exception {
    Page page = getPageUnderTest(PAGE);
    loadHtmlPageItemsConfig(false);
    assertNotNull(page.getHtmlPageItems());
    assertEquals(3, page.getHtmlPageItems().size(), "Unexpected number of HTML page items");
    int[] attributeCounts = { 3, 3, 1 };
    int index = 0;
    for (HtmlPageItem item : page.getHtmlPageItems()) {
        assertEquals(attributeCounts[index], item.getAttributes().size());
        index++;
    }
}
Also used : HtmlPageItem(com.adobe.cq.wcm.core.components.models.HtmlPageItem) Page(com.adobe.cq.wcm.core.components.models.Page) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HtmlPageItem (com.adobe.cq.wcm.core.components.models.HtmlPageItem)3 Page (com.adobe.cq.wcm.core.components.models.Page)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 HtmlPageItemConfig (com.adobe.cq.wcm.core.components.config.HtmlPageItemConfig)1 HtmlPageItemsConfig (com.adobe.cq.wcm.core.components.config.HtmlPageItemsConfig)1 HashMap (java.util.HashMap)1 Resource (org.apache.sling.api.resource.Resource)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 ConfigurationBuilder (org.apache.sling.caconfig.ConfigurationBuilder)1 NotNull (org.jetbrains.annotations.NotNull)1