Skip to content

Commit

Permalink
extract GetHtmlForEncapsulation into Tools class.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwecho committed Jul 13, 2023
1 parent 8995cd1 commit 2930b38
Showing 1 changed file with 26 additions and 81 deletions.
107 changes: 26 additions & 81 deletions src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
{
HtmlDocument innerHtmlDocument = new HtmlDocument();

innerHtmlDocument.LoadHtml(GetEncapsulatedHtml(xPathAttribute.NodeReturnType, htmlNode));
innerHtmlDocument.LoadHtml(Tools.GetHtmlForEncapsulation(htmlNode, xPathAttribute.NodeReturnType));

object o = GetEncapsulatedData(propertyInfo.PropertyType, innerHtmlDocument);

Expand Down Expand Up @@ -295,7 +295,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
foreach (HtmlNode node in nodeCollection)
{
HtmlDocument innerHtmlDocument = new HtmlDocument();
innerHtmlDocument.LoadHtml(GetEncapsulatedHtml(xPathAttribute.NodeReturnType, node));
innerHtmlDocument.LoadHtml(Tools.GetHtmlForEncapsulation(node, xPathAttribute.NodeReturnType));

object o = GetEncapsulatedData(T_Types[0], innerHtmlDocument);

Expand Down Expand Up @@ -381,23 +381,6 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
}
#endregion targetObject_NOTDefined_XPath
}



private static string GetEncapsulatedHtml(ReturnType returnType, HtmlNode node)
{
switch (returnType)
{
case ReturnType.InnerText:
return node.InnerText;
case ReturnType.InnerHtml:
return node.InnerHtml;
case ReturnType.OuterHtml:
return node.OuterHtml;
default:
throw new Exception("Unhandled ReturnType : " + returnType.ToString());
};
}
}


Expand Down Expand Up @@ -616,34 +599,7 @@ internal static T GetNodeValueBasedOnXPathReturnType<T>(HtmlNode htmlNode, XPath
throw new ArgumentNullException("parameter xpathAttribute is null");
}

object result;
Type TType = typeof(T);

switch (xPathAttribute.NodeReturnType)
{
case ReturnType.InnerHtml:
{
result = Convert.ChangeType(htmlNode.InnerHtml, TType);
}
break;


case ReturnType.InnerText:
{
result = Convert.ChangeType(htmlNode.InnerText, TType);
}
break;

case ReturnType.OuterHtml:
{
result = Convert.ChangeType(htmlNode.OuterHtml, TType);
}
break;

default: throw new Exception();
}

return (T)result;
return (T)Convert.ChangeType(GetHtmlForEncapsulation(htmlNode, xPathAttribute.NodeReturnType), typeof(T));
}


Expand All @@ -668,41 +624,10 @@ internal static IList GetNodesValuesBasedOnXPathReturnType(HtmlNodeCollection ht


IList result = listGenericType.CreateIListOfType();

switch (xPathAttribute.NodeReturnType)
foreach (HtmlNode node in htmlNodeCollection)
{

case ReturnType.InnerHtml:
{
foreach (HtmlNode node in htmlNodeCollection)
{
result.Add(Convert.ChangeType(node.InnerHtml, listGenericType));
}
}
break;


case ReturnType.InnerText:
{
foreach (HtmlNode node in htmlNodeCollection)
{
result.Add(Convert.ChangeType(node.InnerText, listGenericType));
}
}
break;


case ReturnType.OuterHtml:
{
foreach (HtmlNode node in htmlNodeCollection)
{
result.Add(Convert.ChangeType(node.OuterHtml, listGenericType));
}
}
break;

result.Add(Convert.ChangeType(GetHtmlForEncapsulation(node, xPathAttribute.NodeReturnType), listGenericType));
}

return result;
}

Expand Down Expand Up @@ -801,7 +726,27 @@ internal static int CountOfIEnumerable<T>(this IEnumerable<T> source)
return counter;
}


/// <summary>
/// Return html part of <see cref="HtmlNode"/> based on <see cref="ReturnType"/>
/// </summary>
/// <param name="node">A htmlNode instance.</param>
/// <param name="returnType"><see cref="ReturnType"/></param>
/// <returns>Html part</returns>
/// <exception cref="IndexOutOfRangeException">Out of range to the <see cref="ReturnType"/></exception>
internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnType)
{
switch (returnType)
{
case ReturnType.InnerText:
return node.InnerText;
case ReturnType.InnerHtml:
return node.InnerHtml;
case ReturnType.OuterHtml:
return node.OuterHtml;
default:
throw new IndexOutOfRangeException("Unhandled ReturnType : " + returnType.ToString());
};
}
}


Expand Down

0 comments on commit 2930b38

Please sign in to comment.