Skip to content

Commit

Permalink
Issue 113: &attributes on Mixin not working
Browse files Browse the repository at this point in the history
  • Loading branch information
chbloemer committed Nov 10, 2015
1 parent e6044c9 commit 5740075
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/main/java/de/neuland/jade4j/parser/node/AttrsNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ public boolean hasCodeNode() {
}

protected String visitAttributes(JadeModel model, JadeTemplate template) {

LinkedList<Attr> newAttributes = new LinkedList<Attr>(attributes);
if(attributeBlocks.size()>0){
//Todo: AttributesBlock needs to be evaluated
for (String attributeBlock : attributeBlocks) {
HashMap<String,String> o = null;
try {
o = (HashMap<String,String>)template.getExpressionHandler().evaluateExpression(attributeBlock, model);
o = (HashMap<String,String>) template.getExpressionHandler().evaluateExpression(attributeBlock, model);
} catch (ExpressionException e) {
e.printStackTrace();
}
Expand All @@ -128,14 +128,14 @@ protected String visitAttributes(JadeModel model, JadeTemplate template) {
Attr attr = new Attr();
attr.setName(entry.getKey());
attr.setValue(entry.getValue());
attributes.add(attr);
newAttributes.add(attr);
}
}
}
LinkedHashMap<String,String> attrs = attrs(model, template);
LinkedHashMap<String,String> attrs = attrs(model, template,newAttributes);
return attrsToString(attrs, template);
}else{
LinkedHashMap<String,String> attrs = attrs(model, template);
LinkedHashMap<String,String> attrs = attrs(model, template, newAttributes);
return attrsToString(attrs, template);
}

Expand All @@ -160,10 +160,10 @@ private String attrsToString(LinkedHashMap<String, String> attrs, JadeTemplate t
return sb.toString();
}

protected LinkedHashMap<String,String> attrs(JadeModel model, JadeTemplate template) {
protected LinkedHashMap<String,String> attrs(JadeModel model, JadeTemplate template, LinkedList<Attr> attrs) {
ArrayList<String> classes = new ArrayList<String>();
LinkedHashMap<String,String> newAttributes = new LinkedHashMap<String,String>();
for (Attr attribute : attributes) {
for (Attr attribute : attrs) {
try {
addAttributesToMap(newAttributes,classes, attribute, model, template);
} catch (ExpressionException e) {
Expand Down
38 changes: 30 additions & 8 deletions src/main/java/de/neuland/jade4j/parser/node/CallNode.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package de.neuland.jade4j.parser.node;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.*;

import de.neuland.jade4j.compiler.IndentWriter;
import de.neuland.jade4j.exceptions.ExpressionException;
Expand Down Expand Up @@ -121,13 +119,37 @@ private void writeVariables(JadeModel model, MixinNode mixin, JadeTemplate templ
private void writeAttributes(JadeModel model, MixinNode mixin, JadeTemplate template) {
// model.put("attributes", mergeInheritedAttributes(model));
// model.put("attributes", getArguments());
LinkedList<Attr> newAttributes = new LinkedList<Attr>(attributes);
if (attributeBlocks.size()>0) {
if (attributes.size()>0) {
LinkedHashMap<String,String> attrs = attrs(model, template);
//Todo: AttributesBlock needs to be evaluated

for (String attributeBlock : attributeBlocks) {
Object o = null;
try {
o = template.getExpressionHandler().evaluateExpression(attributeBlock, model);
} catch (ExpressionException e) {
e.printStackTrace();
}
if(o!=null) {
if(o instanceof HashMap) {
for (Map.Entry<String, String> entry : ((HashMap<String,String>) o).entrySet()) {
Attr attr = new Attr();
attr.setName(entry.getKey());
attr.setValue(entry.getValue());
newAttributes.add(attr);
}
}else if(o instanceof String){
System.out.print(o);
}

}
}
if (newAttributes.size()>0) {
LinkedHashMap<String,String> attrs = attrs(model, template, newAttributes);
model.put("attributes", attrs);
}
model.put("attributes", StringUtils.join(attributeBlocks, ","));
} else if (attributes.size()>0) {
LinkedHashMap<String,String> attrs = attrs(model, template);
} else if (newAttributes.size()>0) {
LinkedHashMap<String,String> attrs = attrs(model, template, newAttributes);
model.put("attributes", attrs);
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/issues/113.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

<div>
<p data-bla="foo"></p>
</div>
9 changes: 9 additions & 0 deletions src/test/resources/issues/113.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-var bla = "foo"
mixin test()
div
+test2()&attributes(attributes)

mixin test2()
p&attributes(attributes)

+test()(data-bla=bla)

0 comments on commit 5740075

Please sign in to comment.