Dictionary <string, PolicyHtmlAttribute> ParseHtmlAttributes(XElement e) { IEnumerable <XElement> elements = e.Elements("attribute"); if (elements == null) { return(null); } Dictionary <string, PolicyHtmlAttribute> attrs = new Dictionary <string, PolicyHtmlAttribute>(); Func <string, PolicyHtmlAttributeOnInvalid> ParseOnInvalid = s => { PolicyHtmlAttributeOnInvalid tmp; return(s != null && Enum.TryParse <PolicyHtmlAttributeOnInvalid>(s, out tmp) ? tmp : PolicyHtmlAttributeOnInvalid.RemoveAttribute); }; foreach (var node in elements) { String name = Attr(node, "name"); if (string.IsNullOrWhiteSpace(name)) { continue; } PolicyHtmlAttribute attr = new PolicyHtmlAttribute(this, name); attr.OnInvalid = ParseOnInvalid(Attr(node, "onInvalid")); attr.Description = Attr(node, "description"); attr.AllowedRegExp = GetRegexList(node); attr.AllowedValues = GetLiteralList(node); string key = attr.Name.ToLower(); if (!attrs.ContainsKey(key)) { attrs.Add(key, attr); } } return(attrs); }
void MergerRules(PolicyHtmlAttribute a, string[] AllowedRegExp, string[] AllowedValues) { if (a == null) { return; } if (AllowedRegExp != null) { if (a.AllowedRegExp == null) { a.AllowedRegExp = AllowedRegExp; } else { a.AllowedRegExp = MergerArray(a.AllowedRegExp, AllowedRegExp); } } if (AllowedValues != null) { if (a.AllowedValues == null) { a.AllowedValues = AllowedValues; } else { a.AllowedValues = MergerArray(a.AllowedValues, AllowedValues); } } }
public PolicyHtmlAttribute AllowedAttribute(string name) { PolicyHtmlAttribute a = allowedAttributes.ContainsKey(name) ? allowedAttributes[name] : null, g = Policy.GlobalHtmlAttribute(name), c = Policy.CommonHtmlAttribute(name); if (a == null) { a = g; } else if (g != null) { MergerRules(a, g.AllowedRegExp, g.AllowedValues); } if (a != null && c != null) { MergerRules(a, c.AllowedRegExp, c.AllowedValues); } return(a); }