使用HtmlAgilityPack设置InnerHtml属性会产生意外结果

时间:2016-04-26 18:14:11

标签: c# html-agility-pack

我正在使用HtmlAgilityPack和C#来转换旧的IE标签以及Javascript以与其他浏览器兼容。这是一个例子:

旧代码:

<script for="thisForm" event="onsubmit()" language="JScript">

var Checked = false
var Counter = 0

for (;Counter < this.choice.length; Counter++)
{
    if (this.choice[Counter].checked)
    {
        Checked = true
        this.action = this.choice[Counter].value
    }
}

if (!Checked)
{
    alert ("Please make a selection")
    return false
}
</script>

我转换为:

<script ftype="text\JScript">
function thisForm_onsubmit(el)
{
var Checked = false
var Counter = 0

for (;Counter < el.choice.length; counter++)
{
    if (el.choice[counter].checked)
    {
        checked = true
        el.action = el.choice[counter].value
    }
}

if (!checked)
{
    alert ("please make a selection")
    return false
}
}
</script>

我在上面做的是从脚本标记中删除了for,event和language属性,添加了type =&#34; text / JScript&#34;属性并将javascript包装成功能代码。

我只需添加HtmlNode属性然后替换InnerHtml属性值即可。到目前为止它对我来说工作正常,直到我遇到上述功能。不知怎的,不是给我上面的结果,我得到以下内容:

<script type="text/JScript">
function thisForm_onsubmit(el)
{
var Checked = false
var Counter = 0

for (;Counter < el.choice.length; counter++)
{
    if (el.choice[counter].checked)
    {
        checked = true
        el.action = el.choice[counter].value
    }
}

if (!checked)
{
    alert ("please make a selection")
    return false
}

}
  el.choice.length;="" counter++)="" {="" if="" (el.choice[counter].checked)="" {="" checked="true" el.action="el.choice[Counter].value" }="" }="" if="" (!checked)="" {="" alert="" ("please="" make="" a="" selection")="" return="" false="" }="" }=""></ el.choice.length; counter++)
{
    if (el.choice[counter].checked)
    {
        checked = true
        el.action = el.choice[counter].value
    }
}

if (!checked)
{
    alert ("please make a selection")
    return false
}

}
></script>

我分配给InnerHtml的文本的奇怪部分是正确的,但scriptNode.InnerHtml显示不同的值

这是我的C#代码:

 if (scriptNode.Attributes["for"] != null)
 {
                                {
    if (scriptNode.Attributes["for"] != null)
                                        ctrl = scriptNode.Attributes["for"].Value;

                                    if (scriptNode.Attributes["event"] != null)
                                        evt = scriptNode.Attributes["event"].Value;

                                    if (scriptNode.Attributes["type"] != null)
                                        typ = scriptNode.Attributes["type"].Value;

                                    if (scriptNode.Attributes["language"] != null)
                                        lang = scriptNode.Attributes["language"].Value;
                                    if (scriptNode.InnerHtml != null)
                                        code = scriptNode.InnerHtml;

                                    func_name = ctrl + "_" + evt;
                                    if (ctrl != "window")
                                        new_script = Environment.NewLine + "function " + RemoveBrackets(func_name) + "(el)" + Environment.NewLine;
                                    else
                                        new_script = Environment.NewLine + "function " + AddBrackets(RemoveBrackets(func_name)) + Environment.NewLine;
                                    new_script += "{" + Environment.NewLine;


                new_script += "\r\n" + ReplaceThis(sFile, ctrl, evt, code, "this", "el") + "\r\n" + "}" + "\r\n";


                                    //remove for and event attributes
                                    scriptNode.Attributes["for"].Remove();
                                    scriptNode.Attributes["event"].Remove();

                                    //remove depraciated "language" attribute 
                                    //and replace it with "type" attribute
                                    if (scriptNode.Attributes["language"] != null)
                                        scriptNode.Attributes["language"].Remove();
                                    if (scriptNode.Attributes["type"] == null)
                                        scriptNode.Attributes.Add("type", "text/" + lang);

                                    //replace old javascript with a function code
                //HERE new_script variable contains the correct value but when I check  scriptNode.InnerHtml after assignment, it shows the messed up code.

                                    scriptNode.InnerHtml = new_script;

这很奇怪,我似乎无法找到解决方案。

我尝试过使用HtmlEncode

scriptNode.InnerHtml = HtmlDocument.HtmlEncode(new_script);

这产生了正确的脚本,如上面第二个例子中所述,但用<>替换了所有&lt;&gt;等。

结果是:

<script type="text/JScript">
function thisForm_onsubmit(el)
{

var Checked = false
var Counter = 0

for (;Counter &lt; el.choice.length; Counter++)
{
    if (el.choice[Counter].checked)
    {
        Checked = true
        el.action = el.choice[Counter].value
    }
}

if (!Checked)
{
    alert (&quot;Please make a selection&quot;)
    return false
}

}
</script>

我想过使用InnerText而不是InnerHtml,这更有意义,因为我正在改变的不是HTML,而是InnerText属性是只读的。

任何人都可以了解为什么会发生这种情况以及是否有解决方法?

1 个答案:

答案 0 :(得分:1)

修改后的脚本包含特殊字符g:ctrlp_extensions,我真的怀疑它会导致问题。 <很容易被误解为开头HTML标记的第一个字符,尤其是当它通过<属性使用时。

这是一种可能的解决方法。假设InnerHtml是包含修改后的Javascript的字符串变量,包括开始和结束标记(new_script)。您可以尝试将<script type="text/JScript"></script>加载到新的new_script中。然后替换第1个中的旧脚本 HtmlDocument使用第二个HtmlDocument实例中的新脚本:

HtmlDocument

<强> dotnetfiddle demo