最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Html Agility Pack messing with my javascript - Stack Overflow

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论