using System;
using System.ComponentModel;
using System.Globalization;
namespace LightWebTestFramework.Rules
{
[LocalizableDescription("ValidationRuleDescriptionTagInnerText"), LocalizableDisplayName("ValidationRuleNameTagInnerText")]
public class ValidateHtmlTagInnerText : ValidationRule
{
private string m_tagName;
private string m_attributeName;
private string m_attributeValue;
private string m_expectedInnerText;
private bool m_hasClosingTag;
private bool m_removeInnerTags;
private bool m_collapseWhiteSpace;
private int m_index;
private bool m_ignoreCase;
[LocalizableDescription("PropertyDescriptionHtmlTagName"), LocalizableDisplayName("PropertyNameHtmlTagName")]
public string TagName
{
get
{
return this.m_tagName;
}
set
{
this.m_tagName = value;
}
}
[LocalizableDescription("PropertyDescriptionAttributeName"), LocalizableDisplayName("PropertyNameAttributeName")]
public string AttributeName
{
get
{
return this.m_attributeName;
}
set
{
this.m_attributeName = value;
}
}
[LocalizableDescription("PropertyDescriptionAttributeValue"), LocalizableDisplayName("PropertyNameAttributeValue")]
public string AttributeValue
{
get
{
return this.m_attributeValue;
}
set
{
this.m_attributeValue = value;
}
}
[LocalizableDescription("PropertyDescriptionExpectedInnerText"), LocalizableDisplayName("PropertyNameExpectedInnerText")]
public string ExpectedInnerText
{
get
{
return this.m_expectedInnerText;
}
set
{
this.m_expectedInnerText = value;
}
}
[LocalizableDescription("PropertyDescriptionRemoveInnerTags"), LocalizableDisplayName("PropertyNameRemoveInnerTags"), DefaultValue(true)]
public bool RemoveInnerTags
{
get
{
return this.m_removeInnerTags;
}
set
{
this.m_removeInnerTags = value;
}
}
[LocalizableDescription("PropertyDescriptionClosingTag"), LocalizableDisplayName("PropertyNameClosingTag"), DefaultValue(true)]
public bool HasClosingTags
{
get
{
return this.m_hasClosingTag;
}
set
{
this.m_hasClosingTag = value;
}
}
[LocalizableDescription("PropertyDescriptionCollapseExtraWhiteSpace"), LocalizableDisplayName("PropertyNameCollapseExtraWhiteSpace"), DefaultValue(true)]
public bool CollapseWhiteSpace
{
get
{
return this.m_collapseWhiteSpace;
}
set
{
this.m_collapseWhiteSpace = value;
}
}
[LocalizableDescription("PropertyDescriptionIndexForValidateInnerText"), LocalizableDisplayName("PropertyNameIndex"), DefaultValue(-1)]
public int Index
{
get
{
return this.m_index;
}
set
{
this.m_index = value;
}
}
[LocalizableDescription("PropertyDescriptionIgnoreCaseForInnerText"), LocalizableDisplayName("PropertyNameIgnoreCaseForInnerText"), DefaultValue(true)]
public bool IgnoreCase
{
get
{
return this.m_ignoreCase;
}
set
{
this.m_ignoreCase = value;
}
}
public override void Validate(object sender, ValidationEventArgs e)
{
int num = 0;
HtmlTagInnerTextParser htmlTagInnerTextParser = new HtmlTagInnerTextParser(e.Response.BodyString);
foreach (HtmlTagInnerText current in htmlTagInnerTextParser.GetInnerTextForHtmlTags(this.TagName, this.AttributeName, this.AttributeValue, this.HasClosingTags, this.RemoveInnerTags, this.CollapseWhiteSpace))
{
if (this.Index == -1 || this.Index == num)
{
if (this.IgnoreCase)
{
e.IsValid = string.Equals(current.Value, this.ExpectedInnerText, StringComparison.OrdinalIgnoreCase);
}
else
{
e.IsValid = string.Equals(current.Value, this.ExpectedInnerText, StringComparison.Ordinal);
}
if (e.IsValid)
{
return;
}
}
num++;
}
e.IsValid = false;
if (!e.Response.IsHtml)
{
e.Message = string.Format(CultureInfo.InvariantCulture, Resources.ErrorResponseNotHtmlForValidationRules, new object[0]);
return;
}
e.Message = string.Format(CultureInfo.InvariantCulture, Resources.ValidationErrorInnerTextNotFound, new object[]
{
this.ExpectedInnerText,
this.TagName
});
}
}
}