2016-04-26 39 views
2

我使用HtmlAgilityPack和C#來轉換舊的IE標籤以及Javascript以與其他瀏覽器兼容。這裏有一個例子:使用HtmlAgilityPack設置InnerHtml屬性會產生意想不到的結果

舊代碼:

<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> 

我上面的,活動中移除的內容,和語言的腳本標籤屬性,添加了type = 「text/JScript」屬性並將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

不,沒有多線程 – ElenaDBA

+0

這是非常奇怪的,因爲到目前爲止我已經轉換爲許多文件的JavaScript沒有問題,只是這個特定的一個給我一個問題 – ElenaDBA

回答

1

修改後的腳本包含特殊字符<,我真的懷疑是造成這個問題。 <可能很容易被誤解爲開始HTML標記的第一個字符,特別是當它通過InnerHtml屬性使用時。

以下是一種可能的解決方法。假設new_script是包含修改的Javascript的字符串變量,包括開始和結束標記(<script type="text/JScript"></script>)。您可以嘗試將new_script加載到新的HtmlDocument中。然後用新的腳本從2 HtmlDocument實例來替換在第一 HtmlDocument舊腳本:

..... 
var newDoc = new HtmlDocument(); 
newDoc.LoadHtml(new_script); 
var newScript = newDoc.DocumentNode.SelectSingleNode("//script"); 
scriptNode.ParentNode.ReplaceChild(newScript, script); 

dotnetfiddle demo

+0

如何創建一個新的節點? var newScript = newDoc.DocumentNode.SelectSingleNode(「// script」);只抓取第一個

相關問題