admin管理员组文章数量:1022569
I'm using the Html Agility Pack to output some javascript in the head of my document. But after saving the document to the file system I recognized that the javascript source has been modified. I guess this happens because HAP is trying to validate my script. Is it possible to prevent this? As you can see below I already tried setting different options.
My code using HAP:
var htmlDoc = new HtmlDocument();
htmlDoc.OptionCheckSyntax = false;
htmlDoc.OptionAutoCloseOnEnd = false;
htmlDoc.OptionFixNestedTags = false;
htmlDoc.LoadHtml(htmlContent);
HtmlNode headNode = htmlDoc.DocumentNode.SelectSingleNode("//head");
headNode.AddScriptNode(htmlDoc, "../../Scripts/jquery-1.7.1.min.js");
Extension Method for adding the script tag
public static void AddScriptNode(this HtmlNode headNode, HtmlDocument htmlDoc, string filePath)
{
string content = "";
using (StreamReader rdr = File.OpenText(filePath))
{
content = rdr.ReadToEnd();
}
if(headNode != null)
{
HtmlNode scripts = htmlDoc.CreateElement("script");
scripts.Attributes.Add("type", "text/javascript");
scripts.InnerHtml = "\n" + content + "\n";
headNode.AppendChild(scripts);
}
}
I'm using the Html Agility Pack to output some javascript in the head of my document. But after saving the document to the file system I recognized that the javascript source has been modified. I guess this happens because HAP is trying to validate my script. Is it possible to prevent this? As you can see below I already tried setting different options.
My code using HAP:
var htmlDoc = new HtmlDocument();
htmlDoc.OptionCheckSyntax = false;
htmlDoc.OptionAutoCloseOnEnd = false;
htmlDoc.OptionFixNestedTags = false;
htmlDoc.LoadHtml(htmlContent);
HtmlNode headNode = htmlDoc.DocumentNode.SelectSingleNode("//head");
headNode.AddScriptNode(htmlDoc, "../../Scripts/jquery-1.7.1.min.js");
Extension Method for adding the script tag
public static void AddScriptNode(this HtmlNode headNode, HtmlDocument htmlDoc, string filePath)
{
string content = "";
using (StreamReader rdr = File.OpenText(filePath))
{
content = rdr.ReadToEnd();
}
if(headNode != null)
{
HtmlNode scripts = htmlDoc.CreateElement("script");
scripts.Attributes.Add("type", "text/javascript");
scripts.InnerHtml = "\n" + content + "\n";
headNode.AppendChild(scripts);
}
}
Share
Improve this question
asked Feb 27, 2012 at 13:42
KuepperKuepper
1,00415 silver badges41 bronze badges
1 Answer
Reset to default 9My assumption: when using scripts.InnerHtml
AgilityPack tries to parse the content as HTML. So if there are tags there they will be converted to HTML nodes.
To avoid this you should set the content of the <script>
as text. Unfortunately, HtmlNode.InnerText
property is a read-only but there is a workaround for this. You could just add a text(a ment node will be preferrable) node to your <script>
node:
if(headNode != null)
{
HtmlNode scripts = htmlDoc.CreateElement("script");
scripts.Attributes.Add("type", "text/javascript");
scripts.AppendChild(htmlDoc.CreateComment("\n" + content + "\n"));
headNode.AppendChild(scripts);
}
Here the body of your script will be added as a ment node (<!--
and -->
will be added).
I'm using the Html Agility Pack to output some javascript in the head of my document. But after saving the document to the file system I recognized that the javascript source has been modified. I guess this happens because HAP is trying to validate my script. Is it possible to prevent this? As you can see below I already tried setting different options.
My code using HAP:
var htmlDoc = new HtmlDocument();
htmlDoc.OptionCheckSyntax = false;
htmlDoc.OptionAutoCloseOnEnd = false;
htmlDoc.OptionFixNestedTags = false;
htmlDoc.LoadHtml(htmlContent);
HtmlNode headNode = htmlDoc.DocumentNode.SelectSingleNode("//head");
headNode.AddScriptNode(htmlDoc, "../../Scripts/jquery-1.7.1.min.js");
Extension Method for adding the script tag
public static void AddScriptNode(this HtmlNode headNode, HtmlDocument htmlDoc, string filePath)
{
string content = "";
using (StreamReader rdr = File.OpenText(filePath))
{
content = rdr.ReadToEnd();
}
if(headNode != null)
{
HtmlNode scripts = htmlDoc.CreateElement("script");
scripts.Attributes.Add("type", "text/javascript");
scripts.InnerHtml = "\n" + content + "\n";
headNode.AppendChild(scripts);
}
}
I'm using the Html Agility Pack to output some javascript in the head of my document. But after saving the document to the file system I recognized that the javascript source has been modified. I guess this happens because HAP is trying to validate my script. Is it possible to prevent this? As you can see below I already tried setting different options.
My code using HAP:
var htmlDoc = new HtmlDocument();
htmlDoc.OptionCheckSyntax = false;
htmlDoc.OptionAutoCloseOnEnd = false;
htmlDoc.OptionFixNestedTags = false;
htmlDoc.LoadHtml(htmlContent);
HtmlNode headNode = htmlDoc.DocumentNode.SelectSingleNode("//head");
headNode.AddScriptNode(htmlDoc, "../../Scripts/jquery-1.7.1.min.js");
Extension Method for adding the script tag
public static void AddScriptNode(this HtmlNode headNode, HtmlDocument htmlDoc, string filePath)
{
string content = "";
using (StreamReader rdr = File.OpenText(filePath))
{
content = rdr.ReadToEnd();
}
if(headNode != null)
{
HtmlNode scripts = htmlDoc.CreateElement("script");
scripts.Attributes.Add("type", "text/javascript");
scripts.InnerHtml = "\n" + content + "\n";
headNode.AppendChild(scripts);
}
}
Share
Improve this question
asked Feb 27, 2012 at 13:42
KuepperKuepper
1,00415 silver badges41 bronze badges
1 Answer
Reset to default 9My assumption: when using scripts.InnerHtml
AgilityPack tries to parse the content as HTML. So if there are tags there they will be converted to HTML nodes.
To avoid this you should set the content of the <script>
as text. Unfortunately, HtmlNode.InnerText
property is a read-only but there is a workaround for this. You could just add a text(a ment node will be preferrable) node to your <script>
node:
if(headNode != null)
{
HtmlNode scripts = htmlDoc.CreateElement("script");
scripts.Attributes.Add("type", "text/javascript");
scripts.AppendChild(htmlDoc.CreateComment("\n" + content + "\n"));
headNode.AppendChild(scripts);
}
Here the body of your script will be added as a ment node (<!--
and -->
will be added).
本文标签: Html Agility Pack messing with my javascriptStack Overflow
版权声明:本文标题:Html Agility Pack messing with my javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745574372a2156928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论