Skip to content

Commit c432fda

Browse files
author
elgonzo
committed
Remove XPathAttribute(string xpathString, string attributeName, ReturnType nodeReturnType) constructor.
Throw InvalidNodeReturnTypeException when a [XPath] annotation with attribute name also specifies a ReturnType. Change System.Exception to InvalidNodeReturnTypeException in Tools.GetHtmlForEncapsulation. Add tests regarding GetEncapsulatedData behaviour controlled by XPathAttribute.NodeReturnType.
1 parent 0b8507e commit c432fda

File tree

2 files changed

+420
-16
lines changed

2 files changed

+420
-16
lines changed

src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using System.Collections;
1212
using System.Collections.Generic;
1313
using System.Reflection;
14-
using System.Xml;
1514
using System.Xml.XPath;
1615

1716
namespace HtmlAgilityPack
@@ -201,6 +200,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
201200
}
202201
else // It target attribute of HTMLTag
203202
{
203+
ThrowIfNodeReturnTypeIsExplicitlySetWhenAttributeNameIsGiven(xPathAttribute);
204204
result = htmlNode.GetAttributeValue(xPathAttribute.AttributeName, null);
205205
}
206206

@@ -329,6 +329,8 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
329329
}
330330
else // It target attribute
331331
{
332+
ThrowIfNodeReturnTypeIsExplicitlySetWhenAttributeNameIsGiven(xPathAttribute);
333+
332334
foreach (HtmlNode node in nodeCollection)
333335
{
334336
string nodeAttributeValue = node.GetAttributeValue(xPathAttribute.AttributeName, null);
@@ -386,6 +388,15 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
386388
}
387389
#endregion targetObject_NOTDefined_XPath
388390
}
391+
392+
393+
private static void ThrowIfNodeReturnTypeIsExplicitlySetWhenAttributeNameIsGiven(XPathAttribute xPathAttr)
394+
{
395+
if (xPathAttr.IsNodeReturnTypeExplicitlySet && !string.IsNullOrEmpty(xPathAttr.AttributeName))
396+
{
397+
throw new InvalidNodeReturnTypeException("Specifying a ReturnType value not allowed for XPathAttribute annotations targeting element attributes");
398+
}
399+
}
389400
}
390401

391402

@@ -749,7 +760,7 @@ internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnT
749760
case ReturnType.OuterHtml:
750761
return node.OuterHtml;
751762
default:
752-
throw new IndexOutOfRangeException("Unhandled ReturnType : " + returnType.ToString());
763+
throw new InvalidNodeReturnTypeException(string.Format("Invalid ReturnType value {0}", returnType));
753764
};
754765
}
755766
}
@@ -852,20 +863,6 @@ public XPathAttribute(string xpathString, string attributeName)
852863
AttributeName = attributeName;
853864
_nodeReturnType = ReturnType.InnerText;
854865
}
855-
856-
857-
/// <summary>
858-
/// Specify Xpath and Attribute to find related Html Node and its attribute value.
859-
/// </summary>
860-
/// <param name="xpathString"></param>
861-
/// <param name="attributeName"></param>
862-
/// <param name="nodeReturnType">Specify you want the output include html text too.</param>
863-
public XPathAttribute(string xpathString, string attributeName, ReturnType nodeReturnType)
864-
{
865-
XPath = xpathString;
866-
AttributeName = attributeName;
867-
NodeReturnType = nodeReturnType;
868-
}
869866
}
870867

871868

@@ -955,6 +952,26 @@ public MissingXPathException(string message) : base(message) { }
955952
public MissingXPathException(string message, Exception inner) : base(message, inner) { }
956953
}
957954

955+
956+
/// <summary>
957+
/// Exception that occurs when an XPathAttribute annotation has an invalid ReturnType specified.
958+
/// </summary>
959+
public class InvalidNodeReturnTypeException : Exception
960+
{
961+
/// <summary>
962+
///
963+
/// </summary>
964+
/// <param name="message"></param>
965+
public InvalidNodeReturnTypeException(string message)
966+
: base(message) { }
967+
968+
/// <summary>
969+
///
970+
/// </summary>
971+
/// <param name="message"></param>
972+
/// <param name="inner"></param>
973+
public InvalidNodeReturnTypeException(string message, Exception inner) : base(message, inner) { }
974+
}
958975
}
959976

960977
#if FX20

0 commit comments

Comments
 (0)