Beispiel #1
0
 private String getInterpolatedAttributeValue(
     String name, Object attribute, boolean escaped, JadeModel model, JadeTemplate template)
     throws JadeCompilerException {
   //        if (!preparedAttributeValues.containsKey(name)) {
   //            preparedAttributeValues.put(name, Utils.prepareInterpolate((String) attribute,
   // escaped));
   //        }
   List<Object> prepared = Utils.prepareInterpolate((String) attribute, escaped);
   try {
     return Utils.interpolate(prepared, model, template.getExpressionHandler());
   } catch (ExpressionException e) {
     throw new JadeCompilerException(this, template.getTemplateLoader(), e);
   }
 }
Beispiel #2
0
 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);
       } catch (ExpressionException e) {
         e.printStackTrace();
       }
       if (o != null) {
         for (Map.Entry<String, String> entry : o.entrySet()) {
           Attr attr = new Attr();
           attr.setName(String.valueOf(entry.getKey()));
           attr.setValue(entry.getValue());
           newAttributes.add(attr);
         }
       }
     }
     LinkedHashMap<String, String> attrs = attrs(model, template, newAttributes);
     return attrsToString(attrs, template);
   } else {
     LinkedHashMap<String, String> attrs = attrs(model, template, newAttributes);
     return attrsToString(attrs, template);
   }
 }
Beispiel #3
0
 @Test
 public void testThatTagNodeIsNotSelfClosingIfXmlDoctype() {
   template.setMode(Jade4J.Mode.XML);
   for (int i = 0; i < selfClosing.length; i++) {
     tagNode.setName(selfClosing[i]);
     assertFalse(tagNode.isSelfClosing(template));
   }
 }
Beispiel #4
0
 @Test
 public void testThatTagNodeIsNotTerseIfTempalteSettingIsNotTerse() {
   template.setMode(Jade4J.Mode.XHTML);
   for (int i = 0; i < selfClosing.length; i++) {
     tagNode.setName(selfClosing[i]);
     assertFalse(tagNode.isTerse(template));
   }
 }
Beispiel #5
0
 @Test
 public void testThatTagNodeIsNotSelfclosingIfTheTagIsNotSelfclosing() {
   template.setMode(Jade4J.Mode.HTML);
   for (int i = 0; i < notSelfClosing.length; i++) {
     tagNode.setName(notSelfClosing[i]);
     assertFalse(tagNode.isTerse(template));
   }
 }
Beispiel #6
0
 @Test
 public void testThatTagNodeIsTerse() {
   template.setMode(Jade4J.Mode.HTML);
   for (int i = 0; i < selfClosing.length; i++) {
     tagNode.setName(selfClosing[i]);
     assertTrue(tagNode.isTerse(template));
   }
 }
Beispiel #7
0
 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 : attrs) {
     try {
       addAttributesToMap(newAttributes, classes, attribute, model, template);
     } catch (ExpressionException e) {
       throw new JadeCompilerException(this, template.getTemplateLoader(), e);
     }
   }
   LinkedHashMap<String, String> finalAttributes = new LinkedHashMap<String, String>();
   finalAttributes.putAll(newAttributes);
   if (!classes.isEmpty()) {
     finalAttributes.put("class", StringUtils.join(classes, " "));
   }
   return finalAttributes;
 }
Beispiel #8
0
 @Before
 public void setup() {
   tagNode = new TagNode();
   template = new JadeTemplate();
   template.setMode(Jade4J.Mode.XHTML);
 }
Beispiel #9
0
 public boolean isSelfClosing(JadeTemplate template) {
   return !template.isXml() && ArrayUtils.contains(selfClosingTags, name);
 }
Beispiel #10
0
 public boolean isTerse(JadeTemplate template) {
   return isSelfClosing(template) && template.isTerse();
 }
Beispiel #11
0
  private void addAttributesToMap(
      HashMap<String, String> newAttributes,
      ArrayList<String> classes,
      Attr attribute,
      JadeModel model,
      JadeTemplate template)
      throws ExpressionException {
    String name = attribute.getName();
    String key = name;
    boolean escaped = false;
    //        if ("class".equals(key)) {
    //          classes.push(attr.val);
    //          classEscaping.push(attr.escaped);
    //        } else if (isConstant(attr.val)) {
    //          if (buffer) {
    //            this.buffer(runtime.attr(key, toConstant(attr.val), escaped, this.terse));
    //          } else {
    //            var val = toConstant(attr.val);
    //            if (key === 'style') val = runtime.style(val);
    //            if (escaped && !(key.indexOf('data') === 0 && typeof val !== 'string')) {
    //              val = runtime.escape(val);
    //            }
    //            buf.push(utils.stringify(key) + ': ' + utils.stringify(val));
    //          }
    //        } else {
    //          if (buffer) {
    //            this.bufferExpression('jade.attr("' + key + '", ' + attr.val + ', ' +
    // utils.stringify(escaped) + ', ' + utils.stringify(this.terse) + ')');
    //          } else {
    //            var val = attr.val;
    //            if (key === 'style') {
    //              val = 'jade.style(' + val + ')';
    //            }
    //            if (escaped && !(key.indexOf('data') === 0)) {
    //              val = 'jade.escape(' + val + ')';
    //            } else if (escaped) {
    //              val = '(typeof (jade_interp = ' + val + ') == "string" ?
    // jade.escape(jade_interp) : jade_interp)';
    //            }
    //            buf.push(utils.stringify(key) + ': ' + val);
    //          }
    //        }

    String value = null;
    Object attributeValue = attribute.getValue();
    if ("class".equals(key)) {
      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());
        if (expressionValue != null && expressionValue.getClass().isArray()) {
          StringBuffer s = new StringBuffer("");
          boolean first = true;
          if (expressionValue instanceof int[]) {
            for (int o : (int[]) expressionValue) {
              if (!first) s.append(" ");
              s.append(o);
              first = false;
            }
          } else {
            for (Object o : (Object[]) expressionValue) {
              if (!first) s.append(" ");
              s.append(o.toString());
              first = false;
            }
          }
          value = s.toString();
        } else if (expressionValue != null && expressionValue instanceof Boolean) {
          if ((Boolean) expressionValue) value = expressionValue.toString();
        } else if (expressionValue != null) {
          value = expressionValue.toString();
        }
      }
      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;
      }
    } else if (attributeValue instanceof ExpressionString) {
      escaped = ((ExpressionString) attributeValue).isEscape();
      Object expressionValue =
          evaluateExpression(
              (ExpressionString) attributeValue, model, template.getExpressionHandler());
      if (expressionValue == null) {
        return;
      }
      // TODO: refactor
      if (expressionValue instanceof Boolean) {
        if ((Boolean) expressionValue) {
          value = name;
        } else {
          return;
        }
        if (template.isTerse()) {
          value = null;
        }
      } else {
        value = expressionValue.toString();
        value = StringEscapeUtils.escapeHtml4(value);
      }
    } else if (attributeValue instanceof String) {
      value = (String) attributeValue;
      //        } else {
      //            return "";
    }
    newAttributes.put(name, value);
  }