Skip to content

Commit

Permalink
Merge pull request #1 from elgonzo/master
Browse files Browse the repository at this point in the history
XPathAttribute.NodeReturnType related tests and minor adjustments in HtmlNode.Encapsulator.cs
  • Loading branch information
rwecho authored Jul 27, 2023
2 parents 0b8507e + c432fda commit a124c39
Show file tree
Hide file tree
Showing 2 changed files with 420 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using System.Xml.XPath;

namespace HtmlAgilityPack
Expand Down Expand Up @@ -201,6 +200,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
}
else // It target attribute of HTMLTag
{
ThrowIfNodeReturnTypeIsExplicitlySetWhenAttributeNameIsGiven(xPathAttribute);
result = htmlNode.GetAttributeValue(xPathAttribute.AttributeName, null);
}

Expand Down Expand Up @@ -329,6 +329,8 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
}
else // It target attribute
{
ThrowIfNodeReturnTypeIsExplicitlySetWhenAttributeNameIsGiven(xPathAttribute);

foreach (HtmlNode node in nodeCollection)
{
string nodeAttributeValue = node.GetAttributeValue(xPathAttribute.AttributeName, null);
Expand Down Expand Up @@ -386,6 +388,15 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
}
#endregion targetObject_NOTDefined_XPath
}


private static void ThrowIfNodeReturnTypeIsExplicitlySetWhenAttributeNameIsGiven(XPathAttribute xPathAttr)
{
if (xPathAttr.IsNodeReturnTypeExplicitlySet && !string.IsNullOrEmpty(xPathAttr.AttributeName))
{
throw new InvalidNodeReturnTypeException("Specifying a ReturnType value not allowed for XPathAttribute annotations targeting element attributes");
}
}
}


Expand Down Expand Up @@ -749,7 +760,7 @@ internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnT
case ReturnType.OuterHtml:
return node.OuterHtml;
default:
throw new IndexOutOfRangeException("Unhandled ReturnType : " + returnType.ToString());
throw new InvalidNodeReturnTypeException(string.Format("Invalid ReturnType value {0}", returnType));
};
}
}
Expand Down Expand Up @@ -852,20 +863,6 @@ public XPathAttribute(string xpathString, string attributeName)
AttributeName = attributeName;
_nodeReturnType = ReturnType.InnerText;
}


/// <summary>
/// Specify Xpath and Attribute to find related Html Node and its attribute value.
/// </summary>
/// <param name="xpathString"></param>
/// <param name="attributeName"></param>
/// <param name="nodeReturnType">Specify you want the output include html text too.</param>
public XPathAttribute(string xpathString, string attributeName, ReturnType nodeReturnType)
{
XPath = xpathString;
AttributeName = attributeName;
NodeReturnType = nodeReturnType;
}
}


Expand Down Expand Up @@ -955,6 +952,26 @@ public MissingXPathException(string message) : base(message) { }
public MissingXPathException(string message, Exception inner) : base(message, inner) { }
}


/// <summary>
/// Exception that occurs when an XPathAttribute annotation has an invalid ReturnType specified.
/// </summary>
public class InvalidNodeReturnTypeException : Exception
{
/// <summary>
///
/// </summary>
/// <param name="message"></param>
public InvalidNodeReturnTypeException(string message)
: base(message) { }

/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public InvalidNodeReturnTypeException(string message, Exception inner) : base(message, inner) { }
}
}

#if FX20
Expand Down
Loading

0 comments on commit a124c39

Please sign in to comment.