1 package de.juplo.thymeleaf;
4 import org.thymeleaf.Arguments;
5 import org.thymeleaf.Configuration;
6 import org.thymeleaf.dom.Element;
7 import org.thymeleaf.processor.ProcessorResult;
8 import org.thymeleaf.processor.attr.AbstractAttrProcessor;
9 import org.thymeleaf.standard.expression.IStandardExpression;
10 import org.thymeleaf.standard.expression.IStandardExpressionParser;
11 import org.thymeleaf.standard.expression.StandardExpressions;
15 * Subsitutes the element, that is marked with this attribute-processor.
19 public abstract class AbstractSubstituteAttrProcessor extends AbstractAttrProcessor
21 public static final int ATTR_PRECEDENCE = 100;
23 private final String substituteName;
26 public AbstractSubstituteAttrProcessor(String attribute, String substitute)
29 this.substituteName = substitute;
34 protected final ProcessorResult processAttribute(
40 Configuration configuration = arguments.getConfiguration();
41 IStandardExpressionParser parser =
42 StandardExpressions.getExpressionParser(configuration);
43 String value = element.getAttributeValue(name);
44 IStandardExpression expression =
45 parser.parseExpression(configuration, arguments, value);
46 element.removeAttribute(name);
47 if ((Boolean)expression.execute(configuration, arguments))
49 // We must not clone the processors, because we remove attributes
50 Element substituteElement =
51 element.cloneElementNodeWithNewName(element, substituteName, false);
52 // Remove attributes, that are not allowed for <span> or <strong>
53 substituteElement.removeAttribute("charset");
54 substituteElement.removeAttribute("th:charset");
55 substituteElement.removeAttribute("coords");
56 substituteElement.removeAttribute("href");
57 substituteElement.removeAttribute("th:href");
58 substituteElement.removeAttribute("hreflang");
59 substituteElement.removeAttribute("th:hreflang");
60 substituteElement.removeAttribute("media");
61 substituteElement.removeAttribute("th:media");
62 substituteElement.removeAttribute("name");
63 substituteElement.removeAttribute("th:name");
64 substituteElement.removeAttribute("rel");
65 substituteElement.removeAttribute("th:rel");
66 substituteElement.removeAttribute("ref");
67 substituteElement.removeAttribute("th:ref");
68 substituteElement.removeAttribute("shape");
69 substituteElement.removeAttribute("target");
70 substituteElement.removeAttribute("th:target");
71 substituteElement.removeAttribute("type");
72 substituteElement.removeAttribute("th:type");
73 // Also remove the title-attribute, because the mouse-over is confusing
74 substituteElement.removeAttribute("title");
75 substituteElement.removeAttribute("th:title");
76 // Replace the element
77 element.clearChildren();
78 element.addChild(substituteElement);
79 element.getParent().extractChild(element);
81 return ProcessorResult.OK;
85 public final int getPrecedence()
87 return ATTR_PRECEDENCE;