Files
web-security/XSSAttachs/StyleSheetsParser/CssSelector.cs
JacksonBruce 868088c104 web 安全技术首次发布
跨站脚本攻击防御
2015-02-20 13:02:57 +08:00

57 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace StyleSheetsParser
{
public class CssSelector:CssRule,ICssRulesContainer
{
public CssSelector(string name, IList<CssAttribute> attributes)
: base(name)
{
Attributes = attributes;
}
public IList<CssAttribute> Attributes { get; private set; }
protected override string GetCssString()
{
if (Attributes == null || Attributes.Count == 0) return string.Empty;
StringBuilder sb = new StringBuilder();
foreach (var attr in Attributes)
{
string s = attr != null ? attr.ToString() : null;
if (s != "" && s != null && s != string.Empty)
{
sb.AppendFormat("{0}{1}", sb.Length > 0 ? ";" : null, attr);
}
}
if (sb.Length > 0)
{
sb.Insert(0, Name + "{");
sb.Append("}");
}
return sb.ToString();
}
bool? _isValid;
public override bool IsValid {
get {
if (!_isValid.HasValue)
{
//选择器必须是以下字符开始,否则视为为无效的
_isValid = Regex.IsMatch(Name, @"^[\*\[#\.a-z]", RegexOptions.IgnoreCase);
}
return _isValid.Value; }
set { _isValid = value; } }
public IEnumerable<CssRule> Selectors
{
get { return Attributes; }
}
}
}