本文整理汇总了C#中HtmlTag.SetAttributeValue方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlTag.SetAttributeValue方法的具体用法?C# HtmlTag.SetAttributeValue怎么用?C# HtmlTag.SetAttributeValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HtmlTag
的用法示例。
在下文中一共展示了HtmlTag.SetAttributeValue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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!");
}
示例2: 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();
}