Closed
Description
1. Description
It's not clear if HtmlDocument.CreateComment()
need to include <!--
and -->
. If it's included, OuterHtml
include it twice.
If it's not, WriteTo()
with OptionOutputAsXml = true
could raise an exception.
Note: the HTML Standard indicate HTML comments can't contains some sequences:
text [inside comments] must not start with the string ">", nor start with the string "->", nor contain the strings "", or "--!>", nor end with the string "<!-"
2. Exception
[System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length]
at System.String.Substring(Int32 startIndex, Int32 length)
at HtmlAgilityPack.HtmlNode.GetXmlComment(HtmlCommentNode comment)
at HtmlAgilityPack.HtmlNode.WriteTo(TextWriter outText, Int32 level)
at HtmlAgilityPack.HtmlNode.WriteTo()
at Program.Test(Boolean outputAsXml) :line 21
at Program.Main() :line 10
3. Fiddle or Project
A Fiddle that reproduce the issue: https://dotnetfiddle.net/vARPlD
// @nuget: HtmlAgilityPack -Version 1.11.54
using System;
using HtmlAgilityPack;
public class Program
{
public static void Main()
{
Test();
Test(true);
}
static void Test(bool outputAsXml = false)
{
Console.WriteLine(string.Format("---- Test with outputAsXml = {0} ----", outputAsXml));
var doc = new HtmlDocument();
doc.OptionOutputAsXml = outputAsXml;
doc.LoadHtml("<!-- abc -->");
var comment = doc.CreateComment(" cde ");
Console.WriteLine(string.Format("comment.OuterHtml = {0}", comment.OuterHtml));
Console.WriteLine(string.Format("comment.WriteTo() = {0}", comment.WriteTo()));
doc.DocumentNode.AppendChild(comment);
var comment2 = doc.CreateComment("<!-- fgh -->");
Console.WriteLine(string.Format("comment2.OuterHtml = {0}", comment2.OuterHtml));
Console.WriteLine(string.Format("commen2t.WriteTo() = {0}", comment2.WriteTo()));
doc.DocumentNode.AppendChild(comment2);
Console.WriteLine(string.Format("doc.DocumentNode.OuterHtml = {0}", doc.DocumentNode.OuterHtml));
Console.WriteLine(string.Format("doc.DocumentNode.WriteTo() = {0}", doc.DocumentNode.WriteTo()));
}
}
Will outuput:
---- Test with outputAsXml = False ----
comment.OuterHtml = <!-- cde -->
comment.WriteTo() = cde
comment2.OuterHtml = <!--<!-- fgh -->-->
commen2t.WriteTo() = <!-- fgh -->
doc.DocumentNode.OuterHtml = <!-- abc --> cde <!-- fgh -->
doc.DocumentNode.WriteTo() = <!-- abc --> cde <!-- fgh -->
---- Test with outputAsXml = True ----
comment.OuterHtml = <!-- cde -->
[Run-time exception]
Metadata
Assignees
Labels
No labels