-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ministryofjustice/story/CCMSPUI-466-creat…
…e-details-component CCMSPUI-466 | create details component
- Loading branch information
Showing
7 changed files
with
165 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DetailsElementTagProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package uk.gov.laa.ccms.springboot.dialect; | ||
|
||
import java.util.Map; | ||
import org.thymeleaf.context.ITemplateContext; | ||
import org.thymeleaf.model.IModel; | ||
import org.thymeleaf.model.IModelFactory; | ||
import org.thymeleaf.model.IProcessableElementTag; | ||
import org.thymeleaf.processor.element.AbstractElementTagProcessor; | ||
import org.thymeleaf.processor.element.IElementTagStructureHandler; | ||
import org.thymeleaf.templatemode.TemplateMode; | ||
|
||
/** | ||
* Transforms <govuk:button/> elements into standard HTML button elements. | ||
*/ | ||
public class DetailsElementTagProcessor extends AbstractElementTagProcessor { | ||
|
||
private static final String TAG_NAME = "details"; | ||
private static final int PRECEDENCE = 900; | ||
|
||
public DetailsElementTagProcessor() { | ||
super(TemplateMode.HTML, "govuk", TAG_NAME, true, null, false, PRECEDENCE); | ||
} | ||
|
||
@Override | ||
protected void doProcess(ITemplateContext context, IProcessableElementTag tag, | ||
IElementTagStructureHandler structureHandler) { | ||
|
||
// Parse attributes | ||
Map<String, String> attributes = ProcessorUtils.parseAttributes(context, tag); | ||
String summaryText = attributes.getOrDefault("summaryText", ""); | ||
String text = attributes.getOrDefault("text", ""); | ||
|
||
// Build the HTML structure | ||
String detailsHtml = buildDetailsHtml(summaryText, text); | ||
|
||
// Create the model and replace the tag | ||
final IModelFactory modelFactory = context.getModelFactory(); | ||
final IModel model = modelFactory.parse(context.getTemplateData(), detailsHtml); | ||
structureHandler.replaceWith(model, false); | ||
} | ||
|
||
private String buildDetailsHtml(String summaryText, String text) { | ||
return new StringBuilder() | ||
.append("<details class=\"").append("govuk-details").append("\">") | ||
.append("<summary class=\"").append("govuk-details__summary").append("\">") | ||
.append("<span class=\"").append("govuk-details__summary-text").append("\">") | ||
.append(summaryText) | ||
.append("</span>") | ||
.append("</summary>") | ||
.append("<div class=\"").append("govuk-details__text").append("\">") | ||
.append(text) | ||
.append("</div>") | ||
.append("</details>") | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...tarter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/ProcessorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package uk.gov.laa.ccms.springboot.dialect; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.thymeleaf.context.ITemplateContext; | ||
import org.thymeleaf.model.IProcessableElementTag; | ||
import org.thymeleaf.standard.expression.IStandardExpression; | ||
import org.thymeleaf.standard.expression.IStandardExpressionParser; | ||
import org.thymeleaf.standard.expression.StandardExpressions; | ||
|
||
/** | ||
* ProcessorUtils for common code. | ||
*/ | ||
public class ProcessorUtils { | ||
|
||
private ProcessorUtils() { | ||
} | ||
|
||
/** | ||
* Evaluate thymeleaf expressions. | ||
*/ | ||
public static Map<String, String> parseAttributes(ITemplateContext context, | ||
IProcessableElementTag tag) { | ||
Map<String, String> attributes = tag.getAttributeMap(); | ||
Map<String, String> resolvedAttributes = new HashMap<>(); | ||
IStandardExpressionParser parser = | ||
StandardExpressions.getExpressionParser(context.getConfiguration()); | ||
|
||
for (Map.Entry<String, String> entry : attributes.entrySet()) { | ||
String key = entry.getKey(); | ||
String value = entry.getValue(); | ||
if (key.startsWith("th:")) { | ||
IStandardExpression expression = parser.parseExpression(context, value); | ||
resolvedAttributes.put(key.replace("th:", ""), (String) expression.execute(context)); | ||
} else { | ||
resolvedAttributes.put(key, value); | ||
} | ||
} | ||
|
||
return resolvedAttributes; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...lect/src/test/java/uk/gov/laa/ccms/springboot/dialect/DetailsElementTagProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package uk.gov.laa.ccms.springboot.dialect; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.thymeleaf.context.Context; | ||
import org.thymeleaf.spring6.SpringTemplateEngine; | ||
|
||
@SpringBootTest(classes = ThymeleafTestConfig.class) | ||
class DetailsElementTagProcessorTest { | ||
|
||
@Autowired | ||
private SpringTemplateEngine templateEngine; | ||
|
||
@Test | ||
void shouldRenderGovukButton() { | ||
|
||
Context context = new Context(); | ||
String renderedHtml = templateEngine.process("test-details", context); | ||
assertThat(renderedHtml) | ||
.contains( | ||
"<details class=\"govuk-details\"><summary class=\"govuk-details__summary\">" + | ||
"<span class=\"govuk-details__summary-text\">Help with nationality</span>" + | ||
"</summary><div class=\"govuk-details__text\">We need to know your nationality " + | ||
"so we can work out which elections you're entitled to vote in.</div></details>"); | ||
|
||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...laa-ccms-spring-boot-starter-govuk-dialect/src/test/resources/templates/test-details.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:govuk="http://www.thymeleaf.org" lang="EN"> | ||
<head> | ||
<title>Details Test</title> | ||
</head> | ||
<body> | ||
|
||
<govuk:details summaryText="Help with nationality" | ||
text="We need to know your nationality so we can work out which elections you're entitled to vote in."/> | ||
|
||
</body> | ||
</html> |