document.insert(element.getBegin(), buf.toString()); // 插入块指令
}
// ---- 指令属性处理 ----
for (int i = 0; i < directiveAttributes.size(); i++) {
Attribute attribute = (Attribute) directiveAttributes.get(i);
document.remove(new Segment(source, attribute.getBegin() - 1, attribute.getEnd())); // 移除属性
}
if (attributes != null) {
//检查扩展的ifattr指令
for (Attribute attribute : attributes) {
if (attribute != null) {
String name = attribute.getName();
if (ifattr.equals(name)) {
String val = attribute.getValue();
String[] arr = val.split(",");
String attrName = arr[0].trim();
String expression = arr[1].trim();
//修改原attribute
Attribute oriattr = attributes.get(attrName);
if (oriattr != null) {
String buf = String.format("#if(%s)%s=\"%s\"#end()", expression, oriattr.getName(), oriattr.getValue());
document.replace(new Segment(source, oriattr.getBegin(), oriattr.getEnd()), buf);
document.remove(new Segment(source, attribute.getBegin(), attribute.getEnd())); // 移除ifattr控制属性
}
}
}
}
//检查扩展的setattr指令
for (Attribute attribute : attributes) {
if (attribute != null) {
String name = attribute.getName();
if (setattr.equals(name)) {
String val = attribute.getValue();
String[] arr = val.split(",");
String attrName = arr[0].trim();
String expression = arr[1].trim();
//将控制指令直接替换为动态属性赋值
Attribute oriattr = attributes.get(attrName);
String buf = String.format("%s=\"%s\"", attrName, expression);
document.replace(new Segment(source, attribute.getBegin(), attribute.getEnd()), buf);
//如果有已经存在的静态属性,直接删去即可
if (oriattr != null) {
document.remove(new Segment(source, attribute.getBegin(), attribute.getEnd())); // 移除setattr控制属性
}
}
}
}
}