public HTMLDefRefBuilderImpl optimize(HTMLDefRefBuilderImpl builder) {
DefDescriptor<AttributeDef> bodyDesc = DefDescriptorImpl.getInstance("body", AttributeDef.class);
DefDescriptor<AttributeDef> tagDesc = DefDescriptorImpl.getInstance("tag", AttributeDef.class);
DefDescriptor<ComponentDef> textDesc = DefDescriptorImpl.getInstance("aura:text", ComponentDef.class);
DefDescriptor<ComponentDef> expDesc = DefDescriptorImpl.getInstance("aura:expression", ComponentDef.class);
AttributeDefRef bodyRef = builder.getAttributeValue(bodyDesc);
Map<DefDescriptor<AttributeDef>, Object> htmlAttributes = builder.htmlAttributes;
boolean pureHTML = true;
if (bodyRef != null) {
for (ComponentDefRef cdr : (List<ComponentDefRef>) bodyRef.getValue()) {
if (cdr.getClass().equals(HTMLDefRefBuilderImpl.class)) {
if (cdr.getAttributeDefRef("markup") == null) {
pureHTML = false;
break;
}
} else if ((!(cdr.getDescriptor().equals(textDesc) || cdr.getDescriptor().equals(expDesc)))
|| cdr.getLocalId() != null) {
pureHTML = false;
break;
}
}
}
if (builder.getLocalId() != null) {
pureHTML = false;
}
if (pureHTML) {
Set<String> booleanAttributes = HTMLComponentDefRefHandler.SPECIAL_BOOLEANS;
for (Map.Entry<DefDescriptor<AttributeDef>, Object> entry : htmlAttributes.entrySet()) {
String key = entry.getKey().getName();
if (key.toLowerCase().startsWith("on") || key.toLowerCase().equals("href")
|| booleanAttributes.contains(key.toLowerCase())) {
pureHTML = false;
break;
}
}
}
if (pureHTML) {
List<Object> markup = Lists.newArrayList();
String tag = (String) builder.getAttributeValue(tagDesc).getValue();
add(markup, "<").add(markup, tag);
if (htmlAttributes != null) {
for (Map.Entry<DefDescriptor<AttributeDef>, Object> entry : htmlAttributes.entrySet()) {
Object value = entry.getValue();
if (value != null) {
add(markup, " ").add(markup, entry.getKey().getName()).add(markup, "=").add(markup, "\"");
if (value instanceof Expression) {
add(markup, value);
} else {
add(markup, value.toString());
}
add(markup, "\"");
}
}
}
add(markup, ">");
if (bodyRef != null) {
for (ComponentDefRef cdr : (List<ComponentDefRef>) bodyRef.getValue()) {
if (cdr.getClass().equals(HTMLDefRefBuilderImpl.class)) {
for (Object childMarkup : (List<Object>) cdr.getAttributeDefRef("markup").getValue()) {
add(markup, childMarkup);
}
} else if (cdr.getDescriptor().equals(textDesc)) {