public void ColonFilter()
{
NeedThenDrop(
':');
var children =
new List<HtmlElement>();
SelectedHandle handle =
null;
if (ExpectThenDrop(
"animated"))
{
throw
new Exception(
"不支持的元素选择器:animated");
}
else
if (ExpectThenDrop(
"button"))
{
handle = (element, index) => element.TagName.ToLower() ==
"button" || (element.TagName.ToLower() ==
"input" && GetAttribute(element,
"type") ==
"button");
}
else
if (ExpectThenDrop(
"checkbox"))
{
handle = (element, index) => element.TagName.ToLower() ==
"input" && GetAttribute(element,
"type") ==
"checkbox";
}
else
if (ExpectThenDrop(
"checked"))
{
handle = (element, index) => element.TagName.ToLower() ==
"input" && GetAttribute(element,
"type") ==
"checkbox" && GetAttribute(element,
"checked") ==
"checked";
}
else
if (ExpectThenDrop(
"contains"))
{
NeedThenDrop(
'(');
var text = ReadUntil(
')');
NeedThenDrop(
')');
handle = (element, index) => element.InnerText.Contains(text);
}
else
if (ExpectThenDrop(
"disabled"))
{
handle = (element, index) => element.Enabled =
false;
}
else
if (ExpectThenDrop(
"empty"))
{
handle = (element, index) =>
string.IsNullOrEmpty(element.InnerText) &&
string.IsNullOrEmpty(element.InnerHtml);
}
else
if (ExpectThenDrop(
"enabled"))
{
handle = (element, index) => element.Enabled =
true;
}
else
if (ExpectThenDrop(
"eq") || ExpectThenDrop(
"nth"))
{
NeedThenDrop(
'(');
var text = ReadUntil(
')');
NeedThenDrop(
')');
var n =
int.Parse(text);
handle = (element, index) => index == n;
}
else
if (ExpectThenDrop(
"even"))
{
handle = (element, index) => index %
2 ==
0;
}
else
if (ExpectThenDrop(
"file"))
{
handle = (element, index) => element.TagName.ToLower() ==
"input" && GetAttribute(element,
"type") ==
"file";
}
else
if (ExpectThenDrop(
"first"))
{
handle = (element, index) => index ==
0;
}
else
if (ExpectThenDrop(
"first-child"))
{
handle = (element, index) =>
{
if (element.Children.Count >
0) children.Add(element.Children[
0]);
return
false;
};
}
else
if (ExpectThenDrop(
"gt"))
{
NeedThenDrop(
'(');
var text = ReadUntil(
')');
NeedThenDrop(
')');
var n =
int.Parse(text);
handle = (element, index) => index > n;
}
else
if (ExpectThenDrop(
"has"))
{
NeedThenDrop(
'(');
var sel = ReadInnerText(
'(',
')');
NeedThenDrop(
')');
var innerSelector =
new Selector(WebBrowser);
var innerElements = innerSelector.SelectElements(sel);
handle = (element, index) => innerElements.Contains(element);
}
else
if (ExpectThenDrop(
"header"))
{
var headers =
new List<
string> {
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"h7" };
handle = (element, index) => headers.Contains(element.TagName.ToLower());
}
else
if (ExpectThenDrop(
"hidden"))
{
throw
new Exception(
"暂不支持该功能.");
}
else
if (ExpectThenDrop(
"image"))
{
handle = (element, index) => element.TagName.ToLower() ==
"input" && GetAttribute(element,
"type") ==
"image";
}
else
if (ExpectThenDrop(
"input"))
{
var inputs =
new List<
string> {
"input",
"textarea",
"select",
"button" };
handle = (element, index) => inputs.Contains(element.TagName.ToLower());
}
else
if (ExpectThenDrop(
"last"))
{
var last = Elements.Count -
1;
handle = (element, index) => index == last;
}
else
if (ExpectThenDrop(
"last-child"))
{
handle = (element, index) =>
{
if (element.Children.Count >
0) children.Add(element.Children[element.Children.Count -
1]);
return
false;
};
}
else
if (ExpectThenDrop(
"lt"))
{
NeedThenDrop(
'(');
var text = ReadUntil(
')');
NeedThenDrop(
')');
var n =
int.Parse(text);
handle = (element, index) => index < n;
}
else
if (ExpectThenDrop(
"not"))
{
NeedThenDrop(
'(');
var sel = ReadInnerText(
'(',
')');
NeedThenDrop(
')');
var innerSelector =
new Selector(WebBrowser);
var innerElements = innerSelector.SelectElements(sel);
handle = (element, index) => !innerElements.Contains(element);
}
else
if (ExpectThenDrop(
"nth-child"))
{
NeedThenDrop(
'(');
var text = ReadUntil(
')');
NeedThenDrop(
')');
var n =
int.Parse(text);
handle = (element, index) =>
{
if (element.Children.Count > n)
{
children.Add(element.Children[n]);
}
return
false;
};
}
else
if (ExpectThenDrop(
"odd"))
{
handle = (element, index) => index %
2 ==
1;
}
else
if (ExpectThenDrop(
"only-child"))
{
handle = (element, index) =>
{
if (element.Children.Count ==
1) children.Add(element.Children[
0]);
return
false;
};
}
else
if (ExpectThenDrop(
"parent"))
{
handle = (element, index) => element.Children.Count >
0;
}
else
if (ExpectThenDrop(
"password"))
{
handle = (element, index) => element.TagName.ToLower() ==
"input" && GetAttribute(element,
"type") ==
"password";
}
else
if (ExpectThenDrop(
"radio"))
{
handle = (element, index) => element.TagName.ToLower() ==
"input" && GetAttribute(element,
"type") ==
"radio";
}
else
if (ExpectThenDrop(
"reset"))
{
handle = (element, index) => (element.TagName.ToLower() ==
"input" || element.TagName.ToLower() ==
"button") && GetAttribute(element,
"type") ==
"reset";
}
else
if (ExpectThenDrop(
"selected"))
{
handle = (element, index) => element.TagName.ToLower() ==
"option " && GetAttribute(element,
"selected") ==
"selected";
}
else
if (ExpectThenDrop(
"reset"))
{
handle = (element, index) => (element.TagName.ToLower() ==
"input" || element.TagName.ToLower() ==
"button") && GetAttribute(element,
"type") ==
"submit";
}
else
if (ExpectThenDrop(
"text"))
{
handle = (element, index) => element.TagName.ToLower() ==
"input" && GetAttribute(element,
"type") ==
"text";
}
else
if (ExpectThenDrop(
"visible"))
{
throw
new Exception(
"暂不支持该功能.");
}
else
throw
new Exception(
"过滤器不能为空.");
var ls =
new List<HtmlElement>();
for (
var i =
0; i < Elements.Count; i++)
{
var element = Elements[i];
if (handle(element, i)) ls.Add(element);
}
ls.AddRange(children);
Elements = ls;
}