Search in sources :

Example 1 with ImageStyle

use of com.enonic.xp.style.ImageStyle in project xp by enonic.

the class PortalUrlServiceImpl_processHtmlTest method process_image_with_styles.

@Test
public void process_image_with_styles() {
    // Creates a content
    final Media media = ContentFixtures.newMedia();
    Mockito.when(this.contentService.getById(media.getId())).thenReturn(media);
    Mockito.when(this.contentService.getBinaryKey(media.getId(), media.getMediaAttachment().getBinaryReference())).thenReturn("binaryHash");
    final ImageStyle imageStyle = ImageStyle.create().name("mystyle").aspectRatio("2:1").filter("myfilter").build();
    final StyleDescriptor styleDescriptor = StyleDescriptor.create().application(ApplicationKey.from("myapp")).addStyleElement(imageStyle).build();
    Mockito.when(styleDescriptorService.getByApplications(Mockito.any())).thenReturn(StyleDescriptors.from(styleDescriptor));
    // Process an html text containing a style
    final String link1 = "<a href=\"image://" + media.getId() + "?style=mystyle\">Image</a>";
    final String link2 = "<a href=\"image://" + media.getId() + "?style=missingstyle\">Image</a>";
    final ProcessHtmlParams params1 = new ProcessHtmlParams().portalRequest(this.portalRequest).value(link1);
    final ProcessHtmlParams params2 = new ProcessHtmlParams().portalRequest(this.portalRequest).value(link2);
    final String processedLink1 = this.service.processHtml(params1);
    final String processedLink2 = this.service.processHtml(params2);
    // Checks that the page URL of the content is returned
    final String expectedResult1 = "<a href=\"/site/default/draft/context/path/_/image/" + media.getId() + ":8cf45815bba82c9711c673c9bb7304039a790026/" + "block-768-384" + "/" + media.getName() + "?filter=myfilter\">Image</a>";
    final String expectedResult2 = "<a href=\"/site/default/draft/context/path/_/image/" + media.getId() + ":8cf45815bba82c9711c673c9bb7304039a790026/" + "width-768" + "/" + media.getName() + "\">Image</a>";
    assertEquals(expectedResult1, processedLink1);
    assertEquals(expectedResult2, processedLink2);
}
Also used : StyleDescriptor(com.enonic.xp.style.StyleDescriptor) Media(com.enonic.xp.content.Media) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams) ImageStyle(com.enonic.xp.style.ImageStyle) Test(org.junit.jupiter.api.Test)

Example 2 with ImageStyle

use of com.enonic.xp.style.ImageStyle in project xp by enonic.

the class HtmlLinkProcessor method getImageStyleMap.

private ImmutableMap<String, ImageStyle> getImageStyleMap(final PortalRequest portalRequest) {
    final ImmutableMap.Builder<String, ImageStyle> imageStyleMap = ImmutableMap.builder();
    final StyleDescriptors styleDescriptors = getStyleDescriptors(portalRequest);
    styleDescriptors.stream().flatMap(styleDescriptor -> styleDescriptor.getElements().stream()).filter(elementStyle -> ImageStyle.STYLE_ELEMENT_NAME.equals(elementStyle.getElement())).forEach(elementStyle -> imageStyleMap.put(elementStyle.getName(), (ImageStyle) elementStyle));
    return imageStyleMap.build();
}
Also used : PortalUrlService(com.enonic.xp.portal.url.PortalUrlService) ImmutableSet(com.google.common.collect.ImmutableSet) ImageStyle(com.enonic.xp.style.ImageStyle) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) PageUrlParams(com.enonic.xp.portal.url.PageUrlParams) ApplicationKey(com.enonic.xp.app.ApplicationKey) Objects(java.util.Objects) AttachmentUrlParams(com.enonic.xp.portal.url.AttachmentUrlParams) StyleDescriptors(com.enonic.xp.style.StyleDescriptors) List(java.util.List) Site(com.enonic.xp.site.Site) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) PortalRequest(com.enonic.xp.portal.PortalRequest) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) StyleDescriptorService(com.enonic.xp.style.StyleDescriptorService) Map(java.util.Map) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Splitter(com.google.common.base.Splitter) ApplicationKeys(com.enonic.xp.app.ApplicationKeys) StyleDescriptors(com.enonic.xp.style.StyleDescriptors) ImageStyle(com.enonic.xp.style.ImageStyle) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with ImageStyle

use of com.enonic.xp.style.ImageStyle in project xp by enonic.

the class HtmlLinkProcessor method process.

public String process(final String text, final String urlType, final PortalRequest portalRequest, final List<Integer> imageWidths, final String imageSizes) {
    String processedHtml = text;
    final ImmutableMap<String, ImageStyle> imageStyleMap = getImageStyleMap(portalRequest);
    final Matcher contentMatcher = CONTENT_PATTERN.matcher(text);
    while (contentMatcher.find()) {
        if (contentMatcher.groupCount() >= NB_GROUPS) {
            final String tagName = contentMatcher.group(TAG_NAME_INDEX);
            final String attr = contentMatcher.group(ATTR_INDEX);
            final String attrValue = contentMatcher.group(ATTR_VALUE_INDEX);
            final String link = contentMatcher.group(LINK_INDEX);
            final String type = contentMatcher.group(TYPE_INDEX);
            final String mode = contentMatcher.group(MODE_INDEX);
            final String id = contentMatcher.group(ID_INDEX);
            final String urlParamsString = contentMatcher.groupCount() == PARAMS_INDEX ? contentMatcher.group(PARAMS_INDEX) : null;
            switch(type) {
                case CONTENT_TYPE:
                    PageUrlParams pageUrlParams = new PageUrlParams().type(urlType).id(id).portalRequest(portalRequest);
                    final String pageUrl = portalUrlService.pageUrl(pageUrlParams);
                    processedHtml = processedHtml.replaceFirst(Pattern.quote(attrValue), "\"" + pageUrl + "\"");
                    break;
                case IMAGE_TYPE:
                    final Map<String, String> urlParams = extractUrlParams(urlParamsString);
                    ImageStyle imageStyle = getImageStyle(imageStyleMap, urlParams);
                    ImageUrlParams imageUrlParams = new ImageUrlParams().type(urlType).id(id).scale(getScale(imageStyle, urlParams, null)).filter(getFilter(imageStyle)).portalRequest(portalRequest);
                    final String imageUrl = portalUrlService.imageUrl(imageUrlParams);
                    final StringBuilder replacement = new StringBuilder("\"" + imageUrl + "\"");
                    if ("img".equals(tagName) && "src".equals(attr)) {
                        final String srcsetValues = Objects.requireNonNullElse(imageWidths, List.<Integer>of()).stream().map(imageWidth -> {
                            final ImageUrlParams imageParams = new ImageUrlParams().type(urlType).id(id).scale(getScale(imageStyle, urlParams, imageWidth)).filter(getFilter(imageStyle)).portalRequest(portalRequest);
                            return portalUrlService.imageUrl(imageParams) + " " + imageWidth + "w";
                        }).collect(Collectors.joining(","));
                        if (!srcsetValues.isEmpty()) {
                            replacement.append(" srcset=\"").append(srcsetValues).append("\"");
                        }
                        if (imageSizes != null && !imageSizes.isBlank()) {
                            replacement.append(" sizes=\"").append(imageSizes).append("\"");
                        }
                    }
                    processedHtml = processedHtml.replaceFirst(Pattern.quote(attrValue), replacement.toString());
                    break;
                default:
                    AttachmentUrlParams attachmentUrlParams = new AttachmentUrlParams().type(urlType).id(id).download(DOWNLOAD_MODE.equals(mode)).portalRequest(portalRequest);
                    final String attachmentUrl = portalUrlService.attachmentUrl(attachmentUrlParams);
                    processedHtml = processedHtml.replaceFirst(Pattern.quote(attrValue), "\"" + attachmentUrl + "\"");
                    break;
            }
        }
    }
    return processedHtml;
}
Also used : PortalUrlService(com.enonic.xp.portal.url.PortalUrlService) ImmutableSet(com.google.common.collect.ImmutableSet) ImageStyle(com.enonic.xp.style.ImageStyle) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) PageUrlParams(com.enonic.xp.portal.url.PageUrlParams) ApplicationKey(com.enonic.xp.app.ApplicationKey) Objects(java.util.Objects) AttachmentUrlParams(com.enonic.xp.portal.url.AttachmentUrlParams) StyleDescriptors(com.enonic.xp.style.StyleDescriptors) List(java.util.List) Site(com.enonic.xp.site.Site) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) PortalRequest(com.enonic.xp.portal.PortalRequest) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) StyleDescriptorService(com.enonic.xp.style.StyleDescriptorService) Map(java.util.Map) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Splitter(com.google.common.base.Splitter) ApplicationKeys(com.enonic.xp.app.ApplicationKeys) Matcher(java.util.regex.Matcher) PageUrlParams(com.enonic.xp.portal.url.PageUrlParams) AttachmentUrlParams(com.enonic.xp.portal.url.AttachmentUrlParams) ImageStyle(com.enonic.xp.style.ImageStyle) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams)

Example 4 with ImageStyle

use of com.enonic.xp.style.ImageStyle in project xp by enonic.

the class XmlStyleDescriptorParserTest method assertResult.

private void assertResult() throws Exception {
    final StyleDescriptor result = this.builder.build();
    assertEquals("myapplication", result.getApplicationKey().toString());
    assertEquals("assets/styles.css", result.getCssPath());
    assertEquals(4, result.getElements().size());
    final GenericStyle element0 = (GenericStyle) result.getElements().get(0);
    final ImageStyle element1 = (ImageStyle) result.getElements().get(1);
    final ImageStyle element2 = (ImageStyle) result.getElements().get(2);
    final ImageStyle element3 = (ImageStyle) result.getElements().get(3);
    assertEquals("style", element0.getElement());
    assertEquals("warning", element0.getName());
    assertEquals("Warning", element0.getDisplayName());
    assertEquals("warning.displayName", element0.getDisplayNameI18nKey());
    assertEquals("image", element1.getElement());
    assertEquals("editor-align-justify", element1.getName());
    assertEquals("Justify", element1.getDisplayName());
    assertEquals("style.editor.align.justify", element1.getDisplayNameI18nKey());
    assertEquals("image", element2.getElement());
    assertEquals("editor-width-auto", element2.getName());
    assertEquals("Override ${width}", element2.getDisplayName());
    assertEquals("editor-width-auto-text", element2.getDisplayNameI18nKey());
    assertEquals("image", element3.getElement());
    assertEquals("editor-style-cinema", element3.getName());
    assertEquals("Cinema", element3.getDisplayName());
    assertEquals("editor-style-cinema-text", element3.getDisplayNameI18nKey());
    assertEquals("21:9", element3.getAspectRatio());
    assertEquals("pixelate(10)", element3.getFilter());
}
Also used : GenericStyle(com.enonic.xp.style.GenericStyle) StyleDescriptor(com.enonic.xp.style.StyleDescriptor) ImageStyle(com.enonic.xp.style.ImageStyle)

Example 5 with ImageStyle

use of com.enonic.xp.style.ImageStyle in project xp by enonic.

the class XmlStyleDescriptorParser method toImageStyle.

private ImageStyle toImageStyle(final DomElement styleElement) {
    final ImageStyle.Builder builder = ImageStyle.create();
    builder.name(styleElement.getAttribute(ELEMENT_NAME_ATTRIBUTE_NAME));
    final DomElement displayName = styleElement.getChild(DISPLAY_NAME_TAG_NAME);
    if (displayName != null) {
        builder.displayName(displayName.getValue());
        builder.displayNameI18nKey(displayName.getAttribute(I18N_ATTRIBUTE_NAME));
    }
    final DomElement aspectRatio = styleElement.getChild(ASPECT_RATIO_TAG_NAME);
    if (aspectRatio != null) {
        builder.aspectRatio(aspectRatio.getValue().trim());
    }
    final DomElement filter = styleElement.getChild(FILTER_TAG_NAME);
    if (filter != null) {
        builder.filter(filter.getValue().trim());
    }
    return builder.build();
}
Also used : DomElement(com.enonic.xp.xml.DomElement) ImageStyle(com.enonic.xp.style.ImageStyle)

Aggregations

ImageStyle (com.enonic.xp.style.ImageStyle)5 ApplicationKey (com.enonic.xp.app.ApplicationKey)2 ApplicationKeys (com.enonic.xp.app.ApplicationKeys)2 PortalRequest (com.enonic.xp.portal.PortalRequest)2 AttachmentUrlParams (com.enonic.xp.portal.url.AttachmentUrlParams)2 ImageUrlParams (com.enonic.xp.portal.url.ImageUrlParams)2 PageUrlParams (com.enonic.xp.portal.url.PageUrlParams)2 PortalUrlService (com.enonic.xp.portal.url.PortalUrlService)2 Site (com.enonic.xp.site.Site)2 StyleDescriptor (com.enonic.xp.style.StyleDescriptor)2 StyleDescriptorService (com.enonic.xp.style.StyleDescriptorService)2 StyleDescriptors (com.enonic.xp.style.StyleDescriptors)2 Splitter (com.google.common.base.Splitter)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2