本文整理汇总了C#中HtmlTag.GetAttributeValue方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlTag.GetAttributeValue方法的具体用法?C# HtmlTag.GetAttributeValue怎么用?C# HtmlTag.GetAttributeValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HtmlTag
的用法示例。
在下文中一共展示了HtmlTag.GetAttributeValue方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TagWithEqualsSignInValueIsValid
public void TagWithEqualsSignInValueIsValid()
{
const String orig = "<IMG src = \"keywords\" content = \"1 + 1 = 2\">";
const String normalized = "<img src=\"keywords\" content=\"1 + 1 = 2\">";
HtmlTag testTag = new HtmlTag(orig);
Assert.IsTrue(testTag.AttrCount == 2, "Tag count is incorrect. Got {0}, expected {1}", testTag.AttrCount, 2);
Assert.IsTrue("\"keywords\"" == testTag.GetAttributeValue("src"), "Attribute value mismatch");
Assert.IsTrue("\"1 + 1 = 2\"" == testTag.GetAttributeValue("content"), "Attribute value mismatch");
// ensure modified string is what we expect. In this case, one change.
Assert.IsTrue(normalized == testTag.TagString, "Modified string does not match expected!");
}
示例2: EmptyAttributeIsValid
public void EmptyAttributeIsValid()
{
const String orig = "<IMG src=\"blah.gif\" alt=\"\" />";
const String updated = "<img src=\"blah.gif\" alt=\"\" />";
HtmlTag testTag = new HtmlTag(orig);
Assert.AreEqual(2, testTag.AttrCount, "Tag count is incorrect. Got {0}, expected {1}", testTag.AttrCount, 2);
Assert.AreEqual("\"blah.gif\"", testTag.GetAttributeValue("src"), "Attribute value mismatch");
Assert.AreEqual("\"\"", testTag.GetAttributeValue("alt"), "Attribute value mismatch");
// ensure modified string is what we expect. In this case, one change.
Assert.AreEqual(updated, testTag.TagString, "Modified string does not match expected!");
}
示例3: TagWithLotsOfAbnormalSpacingIsValid
public void TagWithLotsOfAbnormalSpacingIsValid()
{
const String orig =
"<META name = \"keywords\" content = \"wmap, cmb, hole, radio, universe, matter, dark matter, galaxies, huge hole\">";
const String normalized =
"<meta name=\"keywords\" content=\"wmap, cmb, hole, radio, universe, matter, dark matter, galaxies, huge hole\">";
HtmlTag testTag = new HtmlTag(orig);
Assert.IsTrue(testTag.AttrCount == 2, "Tag count is incorrect. Got {0}, expected {1}", testTag.AttrCount, 2);
Assert.IsTrue("\"keywords\"" == testTag.GetAttributeValue("name"), "Attribute value mismatch");
Assert.IsTrue(
"\"wmap, cmb, hole, radio, universe, matter, dark matter, galaxies, huge hole\"" ==
testTag.GetAttributeValue("content"), "Attribute value mismatch");
// ensure modified string is what we expect. In this case, one change.
Assert.IsTrue(normalized == testTag.TagString, "Modified string does not match expected!");
}
示例4: TagWithMultipleAttributesAndNiceSpacingIsValid
public void TagWithMultipleAttributesAndNiceSpacingIsValid()
{
const String orig =
"<IMG src=\"http://a52.g.akamaitech.net/f/52/827/1d/www.space.com/template_images/common_topmenu08_968x28.gif\" border=\"0\" usemap=\"#common_topmenu\"/>";
const String normalized =
"<img src=\"http://a52.g.akamaitech.net/f/52/827/1d/www.space.com/template_images/common_topmenu08_968x28.gif\" border=\"0\" usemap=\"#common_topmenu\" />";
HtmlTag testTag = new HtmlTag(orig);
Assert.IsTrue(testTag.AttrCount == 3, "Tag count is incorrect. Got {0}, expected {1}", testTag.AttrCount, 3);
Assert.IsTrue(
"\"http://a52.g.akamaitech.net/f/52/827/1d/www.space.com/template_images/common_topmenu08_968x28.gif\"" ==
testTag.GetAttributeValue("src"), "Attribute value mismatch");
Assert.IsTrue("\"0\"" == testTag.GetAttributeValue("border"), "Attribute value mismatch");
Assert.IsTrue("\"#common_topmenu\"" == testTag.GetAttributeValue("usemap"), "Attribute value mismatch");
// ensure modified string is what we expect. In this case, one change.
Assert.AreEqual(normalized, testTag.TagString, "Modified string {0} does not match expected {1}!",
testTag.TagString, normalized);
}
示例5: UpdatingAttributeUpdatesTagIsValid
public void UpdatingAttributeUpdatesTagIsValid()
{
const String orig = "<IMG src = \"keywords\" content = \"1 + 1 = 2\">";
const String updated = "<img src=\"keywords\" content=\"2 + 2 = 4\">";
HtmlTag testTag = new HtmlTag(orig);
Assert.IsTrue(testTag.AttrCount == 2, "Tag count is incorrect. Got {0}, expected {1}", testTag.AttrCount, 2);
Assert.IsTrue("\"1 + 1 = 2\"" == testTag.GetAttributeValue("content"), "Attribute value mismatch");
testTag.SetAttributeValue("content", "\"2 + 2 = 4\"");
Assert.IsTrue("\"2 + 2 = 4\"" == testTag.GetAttributeValue("content"), "Attribute value mismatch");
// ensure modified string is what we expect. In this case, one change.
Assert.IsTrue(updated == testTag.TagString, "Modified string does not match expected!");
}
示例6: InjectURLParameters
private String InjectURLParameters(String htmlText)
{
StringBuilder newHtmlText = new StringBuilder();
_log.Debug(App.Configuration.ExtensionWhitelistPattern);
Regex extensionWhitelistRegex =
new Regex(App.Configuration.ExtensionWhitelistPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
Regex skipJavascriptRegex = new Regex("^javascript:", RegexOptions.Compiled | RegexOptions.IgnoreCase);
for (int i = 0; i < htmlText.Length; i++)
{
switch (htmlText[i])
{
case '<':
// got a tag start. Let's grab the whole tag
String tagStr = Util.CaptureFromStartToStopChar(htmlText, i, '<', '>');
i += (tagStr.Length - 1); // advance the loop value ahead past this tag.
HtmlTag tagObj = new HtmlTag(tagStr);
String value;
if ((value = tagObj.GetAttributeValue("src")) != null)
{
// SECURITY NOTE: enforce the same-origin policy for rewrites. Only links for this site get rewritten!
if (Util.IsUrlSameOriginAsServer(Util.StripQuotes(value))
&& !extensionWhitelistRegex.IsMatch(Util.StripQuotes(value)))
{
_log.Debug("Injecting token for src url '" + value + "'");
tagObj.SetAttributeValue("src",
InjectURLToken(value, _CSRFTokenName, _CSRFSesssionToken));
}
}
else if ((value = tagObj.GetAttributeValue("href")) != null)
{
// SECURITY NOTE: enforce the same-origin policy for rewrites. Only links for this site get rewritten!
if (Util.IsUrlSameOriginAsServer(Util.StripQuotes(value))
&& !extensionWhitelistRegex.IsMatch(Util.StripQuotes(value))
&& !skipJavascriptRegex.IsMatch(Util.StripQuotes(value)))
// don't break javascript hrefs
{
_log.Debug("Injecting token for href url " + value);
tagObj.SetAttributeValue("href",
InjectURLToken(value, _CSRFTokenName, _CSRFSesssionToken));
}
}
newHtmlText.Append(tagObj.TagString);
break;
case '>':
// should never get here since the end tag gets gobbled up by the CaptureFromStartToStopChar() method
_log.Warn("Supposedly unreachable parse error encountered in RegexFilter at char #" + i);
break;
default:
// anything else passes through without modification
newHtmlText.Append(htmlText[i]);
break;
}
}
return newHtmlText.ToString();
}