diff --git a/src/main/java/de/neuland/jade4j/lexer/token/AttributeList.java b/src/main/java/de/neuland/jade4j/lexer/token/AttributeList.java index 7e655923..f7dff6f3 100644 --- a/src/main/java/de/neuland/jade4j/lexer/token/AttributeList.java +++ b/src/main/java/de/neuland/jade4j/lexer/token/AttributeList.java @@ -21,10 +21,7 @@ public List 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) { diff --git a/src/main/java/de/neuland/jade4j/parser/Parser.java b/src/main/java/de/neuland/jade4j/parser/Parser.java index eac442e6..06f95099 100644 --- a/src/main/java/de/neuland/jade4j/parser/Parser.java +++ b/src/main/java/de/neuland/jade4j/parser/Parser.java @@ -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 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) { diff --git a/src/main/java/de/neuland/jade4j/parser/node/AttrsNode.java b/src/main/java/de/neuland/jade4j/parser/node/AttrsNode.java index 3b2ef8da..2f4aab3d 100644 --- a/src/main/java/de/neuland/jade4j/parser/node/AttrsNode.java +++ b/src/main/java/de/neuland/jade4j/parser/node/AttrsNode.java @@ -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) { @@ -157,10 +157,11 @@ private String attrsToString(LinkedHashMap attrs, JadeTemplate t protected LinkedHashMap attrs(JadeModel model, JadeTemplate template, LinkedList attrs) { ArrayList classes = new ArrayList(); + ArrayList classEscaping = new ArrayList(); LinkedHashMap newAttributes = new LinkedHashMap(); 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); } @@ -173,10 +174,9 @@ protected LinkedHashMap attrs(JadeModel model, JadeTemplate templ return finalAttributes; } - private void addAttributesToMap(HashMap newAttributes, ArrayList classes, Attr attribute, JadeModel model, JadeTemplate template) throws ExpressionException { + private void addAttributesToMap(HashMap newAttributes, ArrayList classes, ArrayList 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); @@ -210,13 +210,14 @@ private void addAttributesToMap(HashMap 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; @@ -236,6 +237,16 @@ private void addAttributesToMap(HashMap newAttributes, ArrayList } } value = s.toString(); + }else if (expressionValue != null && expressionValue.getClass().isAssignableFrom(HashMap.class)) { + HashMap map = (HashMap) expressionValue; + for (Map.Entry 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(); @@ -243,32 +254,23 @@ private void addAttributesToMap(HashMap newAttributes, ArrayList 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; @@ -278,16 +280,25 @@ private void addAttributesToMap(HashMap 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); diff --git a/src/test/java/de/neuland/jade4j/compiler/IssuesTest.java b/src/test/java/de/neuland/jade4j/compiler/IssuesTest.java index b95e5b46..ce281117 100644 --- a/src/test/java/de/neuland/jade4j/compiler/IssuesTest.java +++ b/src/test/java/de/neuland/jade4j/compiler/IssuesTest.java @@ -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; diff --git a/src/test/java/de/neuland/jade4j/filter/CustomTestFilter.java b/src/test/java/de/neuland/jade4j/filter/CustomTestFilter.java index dbf8fa3c..cc3c5926 100644 --- a/src/test/java/de/neuland/jade4j/filter/CustomTestFilter.java +++ b/src/test/java/de/neuland/jade4j/filter/CustomTestFilter.java @@ -13,8 +13,8 @@ public String convert(String source, List attributes, Map 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"; }