From 224030fd90666fda8d66ccbc84a84f4735510026 Mon Sep 17 00:00:00 2001 From: Jonathan Magnan Date: Fri, 30 Aug 2024 13:59:05 -0400 Subject: [PATCH] Added options for: https://github.com/zzzprojects/html-agility-pack/issues/567 --- src/HtmlAgilityPack.Shared/HtmlDocument.cs | 15 ++++++++++----- src/HtmlAgilityPack.Shared/HtmlNode.cs | 11 ++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlDocument.cs b/src/HtmlAgilityPack.Shared/HtmlDocument.cs index 34cd7cad..7d863791 100644 --- a/src/HtmlAgilityPack.Shared/HtmlDocument.cs +++ b/src/HtmlAgilityPack.Shared/HtmlDocument.cs @@ -210,11 +210,16 @@ public static bool DisableBehaviorTagP /// public bool OptionWriteEmptyNodes; - /// - /// The max number of nested child nodes. - /// Added to prevent stackoverflow problem when a page has tens of thousands of opening html tags with no closing tags - /// - public int OptionMaxNestedChildNodes = 0; + /// + /// Defines if empty nodes must be written without the additional space. Default is false. + /// + public bool OptionWriteEmptyNodesWithoutSpace; + + /// + /// The max number of nested child nodes. + /// Added to prevent stackoverflow problem when a page has tens of thousands of opening html tags with no closing tags + /// + public int OptionMaxNestedChildNodes = 0; #endregion diff --git a/src/HtmlAgilityPack.Shared/HtmlNode.cs b/src/HtmlAgilityPack.Shared/HtmlNode.cs index 2cd331f3..fac3b4dd 100644 --- a/src/HtmlAgilityPack.Shared/HtmlNode.cs +++ b/src/HtmlAgilityPack.Shared/HtmlNode.cs @@ -2073,7 +2073,16 @@ public virtual void WriteTo(TextWriter outText, int level = 0) if (IsEmptyElement(Name)) { if ((_ownerdocument.OptionWriteEmptyNodes) || (_ownerdocument.OptionOutputAsXml)) - outText.Write(" />"); + { + if(_ownerdocument.OptionWriteEmptyNodesWithoutSpace) + { + outText.Write("/>"); + } + else + { + outText.Write(" />"); + } + } else { if (Name.Length > 0 && Name[0] == '?')