Skip to content

Commit

Permalink
Add style parameter to SvgTag (regular, solid, brands).
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Sep 22, 2022
1 parent c42a527 commit d064ceb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
58 changes: 55 additions & 3 deletions src/main/java/io/jenkins/plugins/fontawesome/api/SvgTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;

import j2html.tags.ContainerTag;

import io.jenkins.plugins.util.JenkinsFacade;
Expand All @@ -12,13 +14,22 @@
* @author Ullrich Hafner
*/
public class SvgTag {
/** Available Font Awesome styles. */
public enum FontAwesomeStyle {
SOLID,
REGULAR,
BRANDS
}

private static final String SVG_ICON = "fa-svg-icon";
private static final String ICON_PREFIX = "/plugin/font-awesome-api/sprites/solid.svg#";
private static final String ICON_PREFIX = "/plugin/font-awesome-api/sprites/";
private static final String ICON_SUFFIX = ".svg#";

private final ContainerTag container;

/**
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome.
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome using
* {@link FontAwesomeStyle#SOLID}.
*
* @param iconName
* the name of the icon (without fa- prefix), e.g. {@code chevron-circle-up}.
Expand All @@ -34,11 +45,38 @@ public static SvgTag fontAwesomeSvgIcon(final String iconName) {
*
* @param iconName
* the name of the icon (without fa- prefix), e.g. {@code chevron-circle-up}.
* @param style
* Font Awesome style of the icon
*
* @return the HTML tag
*/
public static SvgTag fontAwesomeSvgIcon(final String iconName, final FontAwesomeStyle style) {
return new SvgTag(iconName, style);
}

/**
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome using
* {@link FontAwesomeStyle#SOLID}.
*
* @param iconName
* the name of the icon (without fa- prefix), e.g. {@code chevron-circle-up}.
*/
public SvgTag(final String iconName) {
this(iconName, new JenkinsFacade());
}

/**
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome.
*
* @param iconName
* the name of the icon (without fa- prefix), e.g. {@code chevron-circle-up}.
* @param style
* Font Awesome style of the icon
*/
public SvgTag(final String iconName, final FontAwesomeStyle style) {
this(iconName, new JenkinsFacade(), style);
}

/**
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome.
*
Expand All @@ -48,9 +86,23 @@ public SvgTag(final String iconName) {
* Jenkins facade to replaced with a stub during unit tests
*/
public SvgTag(final String iconName, final JenkinsFacade jenkinsFacade) {
this(iconName, jenkinsFacade, FontAwesomeStyle.SOLID);
}

/**
* Creates a new {@link SvgTag} that renders the specified SVG icon of FontAwesome.
*
* @param iconName
* the name of the icon (without fa- prefix), e.g. {@code chevron-circle-up}.
* @param jenkinsFacade
* Jenkins facade to replaced with a stub during unit tests
* @param style
* Font Awesome style of the icon
*/
public SvgTag(final String iconName, final JenkinsFacade jenkinsFacade, final FontAwesomeStyle style) {
container = new ContainerTag("svg")
.withClasses(SVG_ICON)
.with(use().withHref(jenkinsFacade.getImagePath(ICON_PREFIX + iconName)));
.with(use().withHref(jenkinsFacade.getImagePath(ICON_PREFIX + StringUtils.lowerCase(style.name()) + ICON_SUFFIX + iconName)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.jupiter.api.Test;

import io.jenkins.plugins.fontawesome.api.SvgTag.FontAwesomeStyle;
import io.jenkins.plugins.util.JenkinsFacade;

import static org.assertj.core.api.Assertions.*;
Expand All @@ -16,9 +17,17 @@ class SvgTagCreatorTest {
@Test
void shouldCreateIcon() {
JenkinsFacade jenkins = mock(JenkinsFacade.class);
when(jenkins.getImagePath(anyString())).thenReturn("path");
when(jenkins.getImagePath("/plugin/font-awesome-api/sprites/solid.svg#hello")).thenReturn("path-solid");

assertThat(new SvgTag("hello", jenkins).render())
.isEqualTo("<svg class=\"fa-svg-icon\"><use href=\"path\"></use></svg>");
.isEqualTo("<svg class=\"fa-svg-icon\"><use href=\"path-solid\"></use></svg>");

when(jenkins.getImagePath("/plugin/font-awesome-api/sprites/regular.svg#hello")).thenReturn("path-regular");
SvgTag tag = new SvgTag("hello", jenkins, FontAwesomeStyle.REGULAR);
assertThat(tag.render())
.isEqualTo("<svg class=\"fa-svg-icon\"><use href=\"path-regular\"></use></svg>");

assertThat(tag.withClasses("icon-md").render())
.isEqualTo("<svg class=\"icon-md fa-svg-icon\"><use href=\"path-regular\"></use></svg>");
}
}

0 comments on commit d064ceb

Please sign in to comment.