private TemplateHtmlAttribute ReadAttribute() { var sb = new StringBuilder(); string name; while (_index < _source.Length) { if (Current == ' ' || Current == '=' || Current == '>' || (Current == '/' && Next == '>')) { PassOptionalWhitespace(); if (Current != '=') {//没有值的属性:disabled,readonly等,转换后disabled="disabled"。 name = sb.ToString().Trim(); return(new TemplateHtmlAttribute(name, name)); } _index++; break; } sb.Append(Current); _index++; } name = sb.ToString().Trim(); PassOptionalWhitespace(); //属性引号 var quote = Current; if (quote != '\'' && quote != '"') { quote = ' '; } else { _index++; } if (Current == '{' && Next == '{') { //代码段 var attribute = new TemplateHtmlCodeAttribute(name, ReadCode()); _index++; //跳过属性引号 PassOptionalWhitespace(); return(attribute); } sb = new StringBuilder(); while (_index < _source.Length) { if (Current == quote || Current == '>' || (Current == '/' && Next == '>')) { if (Current == quote) { _index++; } break; } sb.Append(Current); _index++; } PassOptionalWhitespace(); return(new TemplateHtmlAttribute(name, sb.ToString())); }
/// <summary> /// 通过当前对象执行属性所得到的字符串。 /// </summary> /// <param name="attribute">属性实例对象。</param> /// <param name="result">当前原有的属性列表。</param> /// <param name="instance">当前实例对象。</param> public void Execute(TemplateHtmlCodeAttribute attribute, Dictionary <string, string> result, object instance) { var value = attribute.Value?.Trim(); if (!string.IsNullOrWhiteSpace(value)) { var isTrue = (bool)TemplateExpression.Execute(value, instance); if (isTrue) { result[attribute.AttributeName] = attribute[0].Trim('\''); } else if (attribute.Count == 2) { result[attribute.AttributeName] = attribute[1].Trim('\''); } } }
/// <summary> /// 通过当前对象执行属性所得到的字符串。 /// </summary> /// <param name="attribute">属性实例对象。</param> /// <param name="result">当前原有的属性列表。</param> /// <returns>返回脚本代码</returns> public string Execute(TemplateHtmlCodeAttribute attribute, Dictionary <string, string> result) { var sb = new StringBuilder(); if (result.TryGetValue(attribute.AttributeName, out var value)) { result.Remove(attribute.AttributeName); } sb.Append("if(") .Append(attribute.Value) .Append("){") .MergeJsAttribute(attribute.AttributeName, attribute[0], value) .Append("}"); if (attribute.Count == 2) { sb.Append("else{") .MergeJsAttribute(attribute.AttributeName, attribute[1], value) .Append("}"); } return(sb.ToString()); }
/// <summary> /// 通过当前对象执行属性所得到的字符串。 /// </summary> /// <param name="attribute">属性实例对象。</param> /// <param name="result">当前原有的属性列表。</param> /// <returns>返回脚本代码</returns> public string Execute(TemplateHtmlCodeAttribute attribute, Dictionary <string, string> result) { var sb = new StringBuilder(); if (result.TryGetValue(attribute.AttributeName, out var value)) { result.Remove(attribute.AttributeName); } sb.Append("if(") .Append(attribute.Value) .Append("){") .SetJsAttribute(attribute.AttributeName, attribute[0]); if (attribute.Count == 2) { sb.Append("}else{").SetJsAttribute(attribute.AttributeName, attribute[1]); } else if (!string.IsNullOrWhiteSpace(value)) { sb.Append("}else{").SetJsAttribute(attribute.AttributeName, value); } sb.Append("}"); return(sb.ToString()); }