|
| 1 | +package uk.gov.laa.ccms.springboot.dialect; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.regex.Pattern; |
| 6 | +import org.apache.logging.log4j.util.Strings; |
| 7 | +import org.thymeleaf.context.ITemplateContext; |
| 8 | +import org.thymeleaf.model.IModel; |
| 9 | +import org.thymeleaf.model.IModelFactory; |
| 10 | +import org.thymeleaf.model.IProcessableElementTag; |
| 11 | +import org.thymeleaf.processor.element.AbstractElementTagProcessor; |
| 12 | +import org.thymeleaf.processor.element.IElementTagStructureHandler; |
| 13 | +import org.thymeleaf.standard.expression.IStandardExpression; |
| 14 | +import org.thymeleaf.standard.expression.IStandardExpressionParser; |
| 15 | +import org.thymeleaf.standard.expression.StandardExpressions; |
| 16 | +import org.thymeleaf.templatemode.TemplateMode; |
| 17 | + |
| 18 | +/** |
| 19 | + * Transforms <govuk:button/> elements into standard HTML button elements. |
| 20 | + */ |
| 21 | +public class ButtonElementTagProcessor extends AbstractElementTagProcessor { |
| 22 | + |
| 23 | + private static final String TAG_NAME = "button"; |
| 24 | + private static final int PRECEDENCE = 900; |
| 25 | + private static final Pattern CLEANER = Pattern.compile("(?m)^[ \t]*\r?\n"); |
| 26 | + |
| 27 | + public ButtonElementTagProcessor() { |
| 28 | + super(TemplateMode.HTML, "govuk", TAG_NAME, true, null, false, PRECEDENCE); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + protected void doProcess(ITemplateContext context, IProcessableElementTag tag, |
| 33 | + IElementTagStructureHandler structureHandler) { |
| 34 | + |
| 35 | + Map<String, String> attributes = parseAttributes(context, tag); |
| 36 | + String classNames = buildClassNames(attributes); |
| 37 | + String commonAttributes = buildCommonAttributes(classNames, attributes); |
| 38 | + String buttonAttributes = buildButtonAttributes(attributes); |
| 39 | + |
| 40 | + String html = attributes.containsKey("href") ? buildAnchorHtml(attributes, commonAttributes) : |
| 41 | + buildButtonHtml(attributes, commonAttributes, buttonAttributes); |
| 42 | + |
| 43 | + replaceElementWithHtml(context, structureHandler, html); |
| 44 | + } |
| 45 | + |
| 46 | + private Map<String, String> parseAttributes(ITemplateContext context, |
| 47 | + IProcessableElementTag tag) { |
| 48 | + Map<String, String> attributes = tag.getAttributeMap(); |
| 49 | + Map<String, String> resolvedAttributes = new HashMap<>(); |
| 50 | + IStandardExpressionParser parser = |
| 51 | + StandardExpressions.getExpressionParser(context.getConfiguration()); |
| 52 | + |
| 53 | + for (Map.Entry<String, String> entry : attributes.entrySet()) { |
| 54 | + String key = entry.getKey(); |
| 55 | + String value = entry.getValue(); |
| 56 | + if (key.startsWith("th:")) { |
| 57 | + IStandardExpression expression = parser.parseExpression(context, value); |
| 58 | + resolvedAttributes.put(key.replace("th:", ""), (String) expression.execute(context)); |
| 59 | + } else { |
| 60 | + resolvedAttributes.put(key, value); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return resolvedAttributes; |
| 65 | + } |
| 66 | + |
| 67 | + private String buildClassNames(Map<String, String> attributes) { |
| 68 | + String classNames = "govuk-button"; |
| 69 | + if (attributes.containsKey("classes")) { |
| 70 | + classNames += " " + attributes.get("classes"); |
| 71 | + } |
| 72 | + return classNames; |
| 73 | + } |
| 74 | + |
| 75 | + private String buildCommonAttributes(String classNames, Map<String, String> attributes) { |
| 76 | + StringBuilder commonAttributes = new StringBuilder(); |
| 77 | + commonAttributes.append("class=\"").append(classNames) |
| 78 | + .append("\" data-module=\"govuk-button\""); |
| 79 | + if (attributes.containsKey("id")) { |
| 80 | + commonAttributes.append(" id=\"").append(attributes.get("id")).append("\""); |
| 81 | + } |
| 82 | + return commonAttributes.toString(); |
| 83 | + } |
| 84 | + |
| 85 | + private String buildButtonAttributes(Map<String, String> attributes) { |
| 86 | + StringBuilder buttonAttributes = new StringBuilder(); |
| 87 | + if (attributes.containsKey("name")) { |
| 88 | + buttonAttributes.append(" name=\"").append(attributes.get("name")).append("\""); |
| 89 | + } |
| 90 | + if (attributes.containsKey("disabled")) { |
| 91 | + buttonAttributes.append(" disabled aria-disabled=\"true\""); |
| 92 | + } |
| 93 | + if (attributes.containsKey("preventDoubleClick")) { |
| 94 | + buttonAttributes.append(" data-prevent-double-click=\"") |
| 95 | + .append(attributes.get("preventDoubleClick")).append("\""); |
| 96 | + } |
| 97 | + return buttonAttributes.toString(); |
| 98 | + } |
| 99 | + |
| 100 | + private String buildAnchorHtml(Map<String, String> attributes, String commonAttributes) { |
| 101 | + return "<a href=\"" + attributes.getOrDefault("href", "#") |
| 102 | + + "\" role=\"button\" draggable=\"false\" " + commonAttributes + ">" |
| 103 | + + attributes.getOrDefault("text", "") + "</a>"; |
| 104 | + } |
| 105 | + |
| 106 | + private String buildButtonHtml(Map<String, String> attributes, String commonAttributes, |
| 107 | + String buttonAttributes) { |
| 108 | + return "<button type=\"" + attributes.getOrDefault("type", "submit") + "\" " |
| 109 | + + buttonAttributes + " " + commonAttributes + ">" + attributes.getOrDefault("text", "") |
| 110 | + + "</button>"; |
| 111 | + } |
| 112 | + |
| 113 | + private void replaceElementWithHtml(ITemplateContext context, |
| 114 | + IElementTagStructureHandler structureHandler, String html) { |
| 115 | + final String adjusted = CLEANER.matcher(html).replaceAll(Strings.EMPTY); |
| 116 | + final IModelFactory modelFactory = context.getModelFactory(); |
| 117 | + final IModel model = modelFactory.parse(context.getTemplateData(), adjusted); |
| 118 | + structureHandler.replaceWith(model, false); |
| 119 | + } |
| 120 | +} |
| 121 | + |
0 commit comments