string ReplaceAttributeValues(string template, string content)
{
var attributesWithValues = (from attr in Tag.Attributes
group attr by attr.ID into gAttrByID
let val = (from attr in gAttrByID
let val = TryGetValue(attr)
where val != null
select new { attr, val }).FirstOrDefault()
select new { attrID = gAttrByID.Key, attrAndVal = val }).ToList();
var attrValuesByID = attributesWithValues.Where(x => x.attrAndVal != null).ToDictionary(x => x.attrID, x => x.attrAndVal.val);
if (!attrValuesByID.ContainsKey(BBTag.ContentPlaceholderName))
attrValuesByID.Add(BBTag.ContentPlaceholderName, content);
var output = template;
foreach (var x in attributesWithValues)
{
var placeholderStr = "${" + x.attrID + "}";
if (x.attrAndVal != null)
{
//replace attributes with values
var rawValue = x.attrAndVal.val;
var attribute = x.attrAndVal.attr;
output = ReplaceAttribute(output, attribute, rawValue, placeholderStr, attrValuesByID);
}
}
//replace empty attributes
var attributeIDsWithValues = new HashSet<string>(attributesWithValues.Where(x => x.attrAndVal != null).Select(x => x.attrID));
var emptyAttributes = Tag.Attributes.Where(attr => !attributeIDsWithValues.Contains(attr.ID)).ToList();
foreach (var attr in emptyAttributes)
{
var placeholderStr = "${" + attr.ID + "}";
output = ReplaceAttribute(output, attr, null, placeholderStr, attrValuesByID);
}
output = output.Replace("${" + BBTag.ContentPlaceholderName + "}", content);
return output;
}