Skip to content

Commit

Permalink
Fixing issue #132: class attribute not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
chbloemer committed Apr 18, 2016
1 parent ec8cd7f commit 9f0a55c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public List<Attribute> getAttributes() {
}

public void addAttribute(String name, String value, boolean escapedAttr) {
ValueString valueString = new ValueString(value);
valueString.setEscape(escapedAttr);

attributes.add(new Attribute(name,valueString,escapedAttr));
attributes.add(new Attribute(name,value,escapedAttr));
}

public void addExpressionAttribute(String name, String expression, boolean escapedAttr) {
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/de/neuland/jade4j/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,27 +604,18 @@ private Node tag(AttrsNode tagNode){
continue;
} else if (incomingToken instanceof AttributeList) {
if (seenAttrs) {
throw new JadeParserException(filename,line(),templateLoader,this.filename + ", line " + this.peek().getLineNumber() + ":\nYou should not have jade tags with multiple attributes.");
//console.warn(this.filename + ', line ' + this.peek().line + ':\nYou should not have jade tags with multiple attributes.');
}
seenAttrs = true;
AttributeList tok = (AttributeList) advance();
List<Attribute> attrs = tok.getAttributes();
tagNode.setSelfClosing(tok.isSelfClosing());

for (Attribute attr : attrs) {
String name = attr.getName();
Object value = attr.getValue();
if(value instanceof ValueString) {
ValueString valueString = (ValueString) value;
tagNode.setAttribute(name, valueString.getValue(),attr.isEscaped());
}else if(value instanceof ExpressionString) {
ExpressionString expressionString = (ExpressionString) value;
tagNode.setAttribute(name, value, attr.isEscaped());
}else if(value instanceof Boolean){
tagNode.setAttribute(name, value, false);
}else if(value instanceof String){
tagNode.setAttribute(name, value, false);
}

tagNode.setAttribute(name, value, attr.isEscaped());
}
continue;
} else if (incomingToken instanceof AttributesBlock) {
Expand Down
79 changes: 45 additions & 34 deletions src/main/java/de/neuland/jade4j/parser/node/AttrsNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public abstract class AttrsNode extends Node {
private boolean textOnly;


public void setAttribute(String key, Object value, boolean escaped) {
public AttrsNode setAttribute(String key, Object value, boolean escaped) {
if (!"class".equals(key) && this.attributeNames.indexOf(key) != -1) {
throw new Error("Duplicate attribute '" + key + "' is not allowed.");
} else {
this.attributeNames.add(key);
Attr attr = new Attr(key,value,escaped);
this.attributes.add(attr);
}
this.attributeNames.add(key);
Attr attr = new Attr(key,value,escaped);
this.attributes.add(attr);
return this;
}

public String getAttribute(String key) {
Expand Down Expand Up @@ -157,10 +157,11 @@ private String attrsToString(LinkedHashMap<String, String> attrs, JadeTemplate t

protected LinkedHashMap<String,String> attrs(JadeModel model, JadeTemplate template, LinkedList<Attr> attrs) {
ArrayList<String> classes = new ArrayList<String>();
ArrayList<Boolean> classEscaping = new ArrayList<Boolean>();
LinkedHashMap<String,String> newAttributes = new LinkedHashMap<String,String>();
for (Attr attribute : attrs) {
try {
addAttributesToMap(newAttributes,classes, attribute, model, template);
addAttributesToMap(newAttributes,classes,classEscaping, attribute, model, template);
} catch (ExpressionException e) {
throw new JadeCompilerException(this, template.getTemplateLoader(), e);
}
Expand All @@ -173,10 +174,9 @@ protected LinkedHashMap<String,String> attrs(JadeModel model, JadeTemplate templ
return finalAttributes;
}

private void addAttributesToMap(HashMap<String, String> newAttributes, ArrayList<String> classes, Attr attribute, JadeModel model, JadeTemplate template) throws ExpressionException {
private void addAttributesToMap(HashMap<String, String> newAttributes, ArrayList<String> classes, ArrayList<Boolean> classEscaping, Attr attribute, JadeModel model, JadeTemplate template) throws ExpressionException {
String name = attribute.getName();
String key = name;
boolean escaped = false;
boolean escaped = attribute.isEscaped();
// if ("class".equals(key)) {
// classes.push(attr.val);
// classEscaping.push(attr.escaped);
Expand Down Expand Up @@ -210,13 +210,14 @@ private void addAttributesToMap(HashMap<String, String> newAttributes, ArrayList

String value = null;
Object attributeValue = attribute.getValue();
if("class".equals(key)) {
if("class".equals(name)) {
if (attributeValue instanceof String) {
escaped = attribute.isEscaped();
value = getInterpolatedAttributeValue(name, attributeValue,escaped, model, template);
} else if (attributeValue instanceof ExpressionString) {
escaped = ((ExpressionString) attributeValue).isEscape();
Object expressionValue = evaluateExpression((ExpressionString) attributeValue, model,template.getExpressionHandler());
//Array to String
if (expressionValue != null && expressionValue.getClass().isArray()) {
StringBuffer s = new StringBuffer("");
boolean first = true;
Expand All @@ -236,39 +237,40 @@ private void addAttributesToMap(HashMap<String, String> newAttributes, ArrayList
}
}
value = s.toString();
}else if (expressionValue != null && expressionValue.getClass().isAssignableFrom(HashMap.class)) {
HashMap<String,Object> map = (HashMap<String,Object>) expressionValue;
for (Map.Entry<String,Object> entry : map.entrySet()) {
if(entry.getValue() instanceof Boolean){
if(((Boolean) entry.getValue()) == true){
classes.add(entry.getKey());
classEscaping.add(false);
}
}
}
}else if(expressionValue!=null && expressionValue instanceof Boolean){
if((Boolean) expressionValue)
value = expressionValue.toString();
}else if(expressionValue!=null){
value = expressionValue.toString();
}
}
if(!StringUtils.isBlank(value))
if(!StringUtils.isBlank(value)) {
classes.add(value);
return;
// }else if("id".equals(key)){
// value = (String) attribute;
}else if (attributeValue instanceof String) {
escaped = attribute.isEscaped();
value = getInterpolatedAttributeValue(name, attributeValue, escaped, model, template);
} else if (attributeValue instanceof Boolean) {
if ((Boolean) attributeValue) {
value = name;
} else {
return;
}
if (template.isTerse()) {
value = null;
classEscaping.add(escaped);
}
return;
} else if (attributeValue instanceof ExpressionString) {
escaped = ((ExpressionString) attributeValue).isEscape();
Object expressionValue = evaluateExpression((ExpressionString) attributeValue, model, template.getExpressionHandler());
// isConstant
ExpressionString expressionString = (ExpressionString) attributeValue;
escaped = expressionString.isEscape();
Object expressionValue = evaluateExpression(expressionString, model, template.getExpressionHandler());
if (expressionValue == null) {
return;
}
// TODO: refactor
if (expressionValue instanceof Boolean) {
if ((Boolean) expressionValue) {
Boolean booleanValue = (Boolean) expressionValue;
if (booleanValue) {
value = name;
} else {
return;
Expand All @@ -278,16 +280,25 @@ private void addAttributesToMap(HashMap<String, String> newAttributes, ArrayList
}
}else{
value = expressionValue.toString();
value = StringEscapeUtils.escapeHtml4(value);
if(escaped)
value = StringEscapeUtils.escapeHtml4(value);
}
}else if (attributeValue instanceof String) {
escaped = attribute.isEscaped();
value = getInterpolatedAttributeValue(name, attributeValue, escaped, model, template);
} else if (attributeValue instanceof Boolean) {
Boolean booleanValue = (Boolean) attributeValue;
if (booleanValue) {
value = name;
} else {
return;
}
if (template.isTerse()) {
value = null;
}
} else if (attributeValue instanceof String) {
value = (String) attributeValue;
// } else {
// return "";
}
newAttributes.put(name,value);
}

private Object evaluateExpression(ExpressionString attribute, JadeModel model, ExpressionHandler expressionHandler) throws ExpressionException {
String expression = ((ExpressionString) attribute).getValue();
Object result = expressionHandler.evaluateExpression(expression, model);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/de/neuland/jade4j/compiler/IssuesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

@RunWith(Parameterized.class)
public class IssuesTest {
private static String[] ignoredCases = new String[]{"68","78","100","131","132"};
private static String[] ignoredCases = new String[]{"68","78","100","131"};

private String file;

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/de/neuland/jade4j/filter/CustomTestFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public String convert(String source, List<Attr> attributes, Map<String, Object>
if("foo".equals(attribute.getName())){
Object foo = attribute.getValue();
String test = null;
if(foo instanceof ValueString)
test = ((ValueString) foo).getValue();
if(foo instanceof String)
test = (String) foo;
if("foo bar".equals(source) && "bar".equals(test)){
return "bar baz";
}
Expand Down

0 comments on commit 9f0a55c

Please sign in to comment.