Skip to content

Commit f6ab628

Browse files
committed
Fixes #48, allow restrictions on attribute values.
1 parent daaa80e commit f6ab628

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

bundles/thymeleaf-extras-eclipse-plugin.content-assist/src/main/java/org/thymeleaf/extras/eclipse/contentassist/autocomplete/CompletionProposalComputer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.thymeleaf.extras.eclipse.contentassist.autocomplete.generators.AttributeRestrictionProposalGenerator;
3434
import org.thymeleaf.extras.eclipse.contentassist.autocomplete.generators.ElementProcessorProposalGenerator;
3535
import org.thymeleaf.extras.eclipse.contentassist.autocomplete.generators.ExpressionObjectProposalGenerator;
36-
3736
import static org.thymeleaf.extras.eclipse.contentassist.ContentAssistPlugin.*;
3837

3938
import java.util.ArrayList;

bundles/thymeleaf-extras-eclipse-plugin.content-assist/src/main/java/org/thymeleaf/extras/eclipse/contentassist/autocomplete/generators/AttributeProcessorProposalGenerator.java

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.thymeleaf.extras.eclipse.dialect.xml.AttributeProcessor;
2929
import org.thymeleaf.extras.eclipse.dialect.xml.AttributeRestrictions;
3030
import org.w3c.dom.NamedNodeMap;
31+
import org.w3c.dom.Node;
32+
3133
import static org.thymeleaf.extras.eclipse.contentassist.ContentAssistPlugin.findCurrentJavaProject;
3234

3335
import java.util.ArrayList;
@@ -134,13 +136,7 @@ else if (!tag.equals(elementname)) {
134136

135137
if (restrictions.isSetAttributes()) {
136138
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)) {
144140
restricted = true;
145141
break;
146142
}
@@ -200,4 +196,51 @@ private static boolean makeAttributeProcessorSuggestions(IDOMNode node, ITextReg
200196
}
201197
return false;
202198
}
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+
}
203246
}

bundles/thymeleaf-extras-eclipse-plugin.content-assist/src/main/java/org/thymeleaf/extras/eclipse/contentassist/autocomplete/proposals/AbstractCompletionProposal.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,13 @@ private static StringBuilder generateDocumentationRestrictions(List<String> rest
221221
if (!yestags.isEmpty()) {
222222
doctext.append("<dt>" + yestext + "</dt>");
223223
for (String yestag: yestags) {
224-
doctext.append("<dd>&lt;" + yestag + "&gt;</dd>");
224+
doctext.append("<dd>" + yestag + "</dd>");
225225
}
226226
}
227227
if (!notags.isEmpty()) {
228228
doctext.append("<dt>" + notext + "</dt>");
229229
for (String notag: notags) {
230-
doctext.append("<dd>&lt;" + notag.substring(1) + "&gt;</dd>");
230+
doctext.append("<dd>" + notag.substring(1) + "</dd>");
231231
}
232232
}
233233

bundles/thymeleaf-extras-eclipse-plugin.content-assist/src/main/java/org/thymeleaf/extras/eclipse/contentassist/autocomplete/proposals/AttributeProcessorCompletionProposal.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.eclipse.swt.graphics.Image;
2222
import org.eclipse.swt.graphics.Point;
2323
import org.thymeleaf.extras.eclipse.dialect.xml.AttributeProcessor;
24-
2524
import static org.thymeleaf.extras.eclipse.contentassist.ContentAssistPlugin.*;
2625

2726
/**

0 commit comments

Comments
 (0)