Skip to content

Commit 8995cd1

Browse files
committed
fix encapsulated item with corresponding ReturnType
1 parent c12580b commit 8995cd1

File tree

2 files changed

+82
-19
lines changed

2 files changed

+82
-19
lines changed

src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// Description: Html Agility Pack - HTML Parsers, selectors, traversors, manupulators.
1+
// Description: Html Agility Pack - HTML Parsers, selectors, traversors, manupulators.
22
// Website & Documentation: http://html-agility-pack.net
33
// Forum & Issues: https://github.com/zzzprojects/html-agility-pack
44
// License: https://github.com/zzzprojects/html-agility-pack/blob/master/LICENSE
55
// More projects: http://www.zzzprojects.com/
6-
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.
6+
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.
77

88
#if !METRO && !NETSTANDARD1_3
99

@@ -29,7 +29,7 @@ public partial class HtmlNode
2929
/// <exception cref="XPathException">Why it's thrown.</exception>
3030
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
3131
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
32-
/// <exception cref="FormatException">Why it's thrown.</exception>
32+
/// <exception cref="FormatException">Why it's thrown.</exception>
3333
/// <exception cref="Exception">Why it's thrown.</exception>
3434
public T GetEncapsulatedData<T>()
3535
{
@@ -50,7 +50,7 @@ public T GetEncapsulatedData<T>()
5050
/// <exception cref="XPathException">Why it's thrown.</exception>
5151
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
5252
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
53-
/// <exception cref="FormatException">Why it's thrown.</exception>
53+
/// <exception cref="FormatException">Why it's thrown.</exception>
5454
/// <exception cref="Exception">Why it's thrown.</exception>
5555
public T GetEncapsulatedData<T>(HtmlDocument htmlDocument)
5656
{
@@ -72,7 +72,7 @@ public T GetEncapsulatedData<T>(HtmlDocument htmlDocument)
7272
/// <exception cref="XPathException">Why it's thrown.</exception>
7373
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
7474
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
75-
/// <exception cref="FormatException">Why it's thrown.</exception>
75+
/// <exception cref="FormatException">Why it's thrown.</exception>
7676
/// <exception cref="Exception">Why it's thrown.</exception>
7777
public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = null)
7878
{
@@ -177,7 +177,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
177177
{
178178
HtmlDocument innerHtmlDocument = new HtmlDocument();
179179

180-
innerHtmlDocument.LoadHtml(htmlNode.InnerHtml);
180+
innerHtmlDocument.LoadHtml(GetEncapsulatedHtml(xPathAttribute.NodeReturnType, htmlNode));
181181

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

@@ -192,7 +192,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
192192
{
193193
string result = string.Empty;
194194

195-
if (xPathAttribute.AttributeName == null) // It target value of HTMLTag
195+
if (xPathAttribute.AttributeName == null) // It target value of HTMLTag
196196
{
197197
result = Tools.GetNodeValueBasedOnXPathReturnType<string>(htmlNode, xPathAttribute);
198198
}
@@ -295,7 +295,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
295295
foreach (HtmlNode node in nodeCollection)
296296
{
297297
HtmlDocument innerHtmlDocument = new HtmlDocument();
298-
innerHtmlDocument.LoadHtml(node.InnerHtml);
298+
innerHtmlDocument.LoadHtml(GetEncapsulatedHtml(xPathAttribute.NodeReturnType, node));
299299

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

@@ -384,6 +384,20 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
384384

385385

386386

387+
private static string GetEncapsulatedHtml(ReturnType returnType, HtmlNode node)
388+
{
389+
switch (returnType)
390+
{
391+
case ReturnType.InnerText:
392+
return node.InnerText;
393+
case ReturnType.InnerHtml:
394+
return node.InnerHtml;
395+
case ReturnType.OuterHtml:
396+
return node.OuterHtml;
397+
default:
398+
throw new Exception("Unhandled ReturnType : " + returnType.ToString());
399+
};
400+
}
387401
}
388402

389403

@@ -797,7 +811,7 @@ internal static int CountOfIEnumerable<T>(this IEnumerable<T> source)
797811
public enum ReturnType
798812
{
799813
/// <summary>
800-
/// The text between the start and end tags of the object.
814+
/// The text between the start and end tags of the object.
801815
/// </summary>
802816
InnerText,
803817

@@ -896,18 +910,18 @@ public sealed class SkipNodeNotFoundAttribute : Attribute
896910
public class NodeNotFoundException : Exception
897911
{
898912
/// <summary>
899-
///
913+
///
900914
/// </summary>
901915
public NodeNotFoundException() { }
902916

903917
/// <summary>
904-
///
918+
///
905919
/// </summary>
906920
/// <param name="message"></param>
907921
public NodeNotFoundException(string message) : base(message) { }
908922

909923
/// <summary>
910-
///
924+
///
911925
/// </summary>
912926
/// <param name="message"></param>
913927
/// <param name="inner"></param>
@@ -921,18 +935,18 @@ public NodeNotFoundException(string message, Exception inner) : base(message, in
921935
public class NodeAttributeNotFoundException : Exception
922936
{
923937
/// <summary>
924-
///
938+
///
925939
/// </summary>
926940
public NodeAttributeNotFoundException() { }
927941

928942
/// <summary>
929-
///
943+
///
930944
/// </summary>
931945
/// <param name="message"></param>
932946
public NodeAttributeNotFoundException(string message) : base(message) { }
933947

934948
/// <summary>
935-
///
949+
///
936950
/// </summary>
937951
/// <param name="message"></param>
938952
/// <param name="inner"></param>
@@ -947,18 +961,18 @@ public class MissingXPathException : Exception
947961
{
948962

949963
/// <summary>
950-
///
964+
///
951965
/// </summary>
952966
public MissingXPathException() { }
953967

954968
/// <summary>
955-
///
969+
///
956970
/// </summary>
957971
/// <param name="message"></param>
958972
public MissingXPathException(string message) : base(message) { }
959973

960974
/// <summary>
961-
///
975+
///
962976
/// </summary>
963977
/// <param name="message"></param>
964978
/// <param name="inner"></param>
@@ -967,7 +981,7 @@ public MissingXPathException(string message, Exception inner) : base(message, in
967981

968982
}
969983

970-
#if FX20
984+
#if FX20
971985
namespace System.Runtime.CompilerServices
972986
{
973987
[AttributeUsage(AttributeTargets.Method |

src/Tests/HtmlAgilityPack.Tests.NetStandard2_0/EncapsulatorTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
using System.Reflection.Metadata;
56
using Xunit;
67

78
namespace HtmlAgilityPack.Tests.NetStandard2_0
@@ -29,8 +30,32 @@ public void Dictionary_Test()
2930

3031
Assert.NotNull(wort);
3132
}
33+
34+
[Fact]
35+
public void EncapsulatedOuterHtml_Test()
36+
{
37+
var html = @"
38+
<div>
39+
<a href='3.html' class='single'>3</a>
40+
<div>hello
41+
<a href='1.html'>1</a>
42+
<a href='2.html'>2</a>
43+
</div>
44+
<div>world</div>
45+
</div>
46+
";
47+
var document = new HtmlDocument();
48+
document.LoadHtml(html);
49+
var outerHtml = document.DocumentNode.GetEncapsulatedData<OuterHtml>();
50+
Assert.True(outerHtml.Item3.Href == "3.html");
51+
Assert.True(outerHtml.Item3.Name == "3");
52+
53+
Assert.True(outerHtml.Items.Count == 3);
54+
Assert.True(outerHtml.Items.All(o => o.Href != null));
55+
}
3256
}
3357

58+
3459
#region StackOverFlow_TestClasses
3560

3661
[HasXPath]
@@ -204,5 +229,29 @@ public class Example
204229

205230
#endregion Dictionary_TestClasses
206231

232+
#region Encapsulated outer html test classes
233+
234+
[HasXPath]
235+
public class OuterHtml
236+
{
237+
[XPath("//a", ReturnType.OuterHtml)]
238+
public List<OuterHtmlItem> Items { get; set; }
239+
240+
[XPath("//a[@class='single']", ReturnType.OuterHtml)]
241+
public OuterHtmlItem Item3 { get; set; }
207242

243+
244+
[HasXPath]
245+
public class OuterHtmlItem
246+
{
247+
[XPath("a", "href")]
248+
[SkipNodeNotFound]
249+
public string Href { get; set; }
250+
251+
[XPath("a")]
252+
[SkipNodeNotFound]
253+
public string Name { get; set; }
254+
}
255+
}
256+
#endregion
208257
}

0 commit comments

Comments
 (0)