Html Agility Pack messing with my javascript - Stack Overflow

I'm using the Html Agility Pack to output some javascript in the head of my document. But after sa

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
Add a ment  | 

1 Answer 1

Reset to default 9

My 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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745574373a4633873.html

相关推荐

  • Html Agility Pack messing with my javascript - Stack Overflow

    I'm using the Html Agility Pack to output some javascript in the head of my document. But after sa

    12小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信