1 package de.juplo.thymeleaf;
4 import java.util.HashMap;
6 import org.thymeleaf.context.ITemplateContext;
7 import org.thymeleaf.engine.AttributeName;
8 import org.thymeleaf.engine.EngineEventUtils;
9 import org.thymeleaf.model.IAttribute;
10 import org.thymeleaf.model.ICloseElementTag;
11 import org.thymeleaf.model.IModel;
12 import org.thymeleaf.model.IModelFactory;
13 import org.thymeleaf.model.IProcessableElementTag;
14 import org.thymeleaf.model.ITemplateEvent;
15 import org.thymeleaf.processor.element.AbstractAttributeModelProcessor;
16 import org.thymeleaf.processor.element.IElementModelStructureHandler;
17 import org.thymeleaf.templatemode.TemplateMode;
21 * Subsitutes the element, that is marked with this attribute-processor.
25 public abstract class AbstractSubstituteAttrProcessor
27 AbstractAttributeModelProcessor
29 public static final int ATTR_PRECEDENCE = 100;
32 private final String attributeName;
33 private final String substituteName;
36 public AbstractSubstituteAttrProcessor(
38 final String attribute,
39 final String substitute
52 this.attributeName = attribute;
53 this.substituteName = substitute;
58 protected void doProcess(
59 final ITemplateContext context,
61 final AttributeName attributeName,
62 final String attributeValue,
63 final IElementModelStructureHandler handler
66 final IProcessableElementTag element = (IProcessableElementTag) model.get(0);
68 EngineEventUtils.computeAttributeExpression(
76 if (result == null || !(result instanceof Boolean))
82 IModelFactory factory = context.getModelFactory();
86 Map<String, String> attributes = new HashMap<>();
88 for (IAttribute attribute : element.getAllAttributes())
92 .getAttributeDefinition()
109 // Also remove the title-attribute, because the mouse-over is confusing
113 if (name.equals(this.attributeName))
115 attributes.put(attribute.getAttributeCompleteName(), attribute.getValue());
120 tag = factory.createOpenElementTag(substituteName, attributes, null, false);
121 model.replace(0, tag);
122 for (int i = model.size(); i > 0; --i)
125 if (!(tag instanceof ICloseElementTag))
127 ICloseElementTag close = (ICloseElementTag)tag;
128 if (close.isUnmatched())
130 if (close.getElementCompleteName().equals(element.getElementCompleteName()))
132 model.replace(i, factory.createCloseElementTag(substituteName, close.isSynthetic(), close.isUnmatched()));
138 model.replace(0, factory.removeAttribute(element, attributeName));