2016-04-26 18 views
2

以前のIEタグやJavascriptを他のブラウザと互換性を持たせるために、HtmlAgilityPackとC#を使用しています。次に例を示します。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> 

は、タイプを追加しました= "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; 

私は非常に奇妙で、私は解決策を見つけることができません。

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

は私が考えますInnerHtmlの代わりにInnerTextを使用します。これは、私が変更しているものが実際にはHTMLではなく、InnerTextプロパティが読み取り専用であるために意味があります。

誰にこのようなことが起こっているのか、回避策があるのか​​どうかについては、誰にも分かりますか?

+1

は、あなたが複数のスレッドで作業している、と共有なっているものがあります。そして、第二HtmlDocumentインスタンスから新しいスクリプトを第一 HtmlDocumentで古いスクリプトを置き換えますそれらのスレッドの間?複数のスレッドが両方とも同じ値を更新しているときに何が起こるかのように見えます。出力の一部を他のスレッドの出力の一部で取得します。 –

+0

いいえ、複数のスレッドがありません – ElenaDBA

+0

これまでのところ私は問題なしで多くのファイルのjavascriptを変換しましたが、この特定のものは私に問題をもたらします。 – ElenaDBA

答えて

1

修正されたスクリプトには、問題の原因と思われる特殊文字<が含まれています。 <は、特に、InnerHtmlプロパティで使用されている場合、開始HTMLタグの最初の文字として誤って解釈される可能性があります。

これは考えられる回避策の1つです。 new_scriptは、開始タグと終了タグ(<script type="text/JScript"></script>)を含む、修正されたJavascriptを含む文字列変数であるとします。新しいHtmlDocumentnew_scriptをロードしようとすることができます。

..... 
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( "//スクリプト");最初の

関連する問題