|
28 | 28 | import org.thymeleaf.extras.eclipse.dialect.xml.AttributeProcessor; |
29 | 29 | import org.thymeleaf.extras.eclipse.dialect.xml.AttributeRestrictions; |
30 | 30 | import org.w3c.dom.NamedNodeMap; |
| 31 | +import org.w3c.dom.Node; |
| 32 | + |
31 | 33 | import static org.thymeleaf.extras.eclipse.contentassist.ContentAssistPlugin.findCurrentJavaProject; |
32 | 34 |
|
33 | 35 | import java.util.ArrayList; |
@@ -134,13 +136,7 @@ else if (!tag.equals(elementname)) { |
134 | 136 |
|
135 | 137 | if (restrictions.isSetAttributes()) { |
136 | 138 | for (String attribute: restrictions.getAttributes()) { |
137 | | - if (attribute.startsWith("-")) { |
138 | | - if (existingattributes.getNamedItem(attribute.substring(1)) != null) { |
139 | | - restricted = true; |
140 | | - break; |
141 | | - } |
142 | | - } |
143 | | - else if (existingattributes.getNamedItem(attribute) == null) { |
| 139 | + if (!matchAttributeRestriction(attribute, existingattributes)) { |
144 | 140 | restricted = true; |
145 | 141 | break; |
146 | 142 | } |
@@ -200,4 +196,51 @@ private static boolean makeAttributeProcessorSuggestions(IDOMNode node, ITextReg |
200 | 196 | } |
201 | 197 | return false; |
202 | 198 | } |
| 199 | + |
| 200 | + /** |
| 201 | + * Checks if an attribute processor proposal should be made given the |
| 202 | + * attribute restriction. |
| 203 | + * |
| 204 | + * @param restriction |
| 205 | + * @param existingattributes |
| 206 | + * @return <tt>true</tt> if an attribute processor can be proposed because |
| 207 | + * it passed the current restriction. |
| 208 | + */ |
| 209 | + private static boolean matchAttributeRestriction(String restriction, |
| 210 | + NamedNodeMap existingattributes) { |
| 211 | + |
| 212 | + // Break the restriction into its parts |
| 213 | + String restrictionName; |
| 214 | + String restrictionValue; |
| 215 | + if (restriction.contains("=")) { |
| 216 | + int indexOfEq = restriction.indexOf('='); |
| 217 | + restrictionName = restriction.substring(0, indexOfEq); |
| 218 | + restrictionValue = restriction.substring(indexOfEq + 1); |
| 219 | + } |
| 220 | + else { |
| 221 | + restrictionName = restriction; |
| 222 | + restrictionValue = null; |
| 223 | + } |
| 224 | + |
| 225 | + // Flag to indicate if this is a restriction that the attribute _shouldn't_ be there |
| 226 | + boolean negate = restriction.startsWith("-"); |
| 227 | + if (negate) { |
| 228 | + restrictionName = restrictionName.substring(1); |
| 229 | + } |
| 230 | + |
| 231 | + // Check restriction against other attributes in the element |
| 232 | + Node attribute = existingattributes.getNamedItem(restrictionName); |
| 233 | + boolean allow = true; |
| 234 | + if (attribute == null) { |
| 235 | + allow = false; |
| 236 | + } |
| 237 | + else if (restrictionValue != null) { |
| 238 | + String attributeValue = attribute.getNodeValue(); |
| 239 | + if (attributeValue != null && !attributeValue.equals(restrictionValue)) { |
| 240 | + allow = false; |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + return negate ? !allow : allow; |
| 245 | + } |
203 | 246 | } |
0 commit comments