Skip to content

Commit 457dad1

Browse files
committed
Use DomName in enum JsValues
1 parent ad44d9d commit 457dad1

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/AngleSharp.Js/Extensions/EngineExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public static JsValue ToJsValue(this Object obj, EngineInstance engine)
4545
}
4646
else if (obj is Enum)
4747
{
48+
string name = ((Enum)obj).GetOfficialName();
49+
if (name != null)
50+
{
51+
return new JsValue(name);
52+
}
53+
4854
return new JsValue(Convert.ToInt32(obj));
4955
}
5056

src/AngleSharp.Js/Extensions/ReflectionExtensions.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public static MethodInfo PrepareConvert(this Type fromType, Type toType)
5555
public static String GetOfficialName(this MemberInfo member)
5656
{
5757
var names = member.GetCustomAttributes<DomNameAttribute>();
58-
var officalNameAttribute = names.FirstOrDefault();
59-
return officalNameAttribute?.OfficialName ?? member.Name;
58+
var officialNameAttribute = names.FirstOrDefault();
59+
return officialNameAttribute?.OfficialName ?? member.Name;
6060
}
6161

6262
public static String GetOfficialName(this Type currentType, Type baseType)
@@ -87,6 +87,18 @@ public static String GetOfficialName(this Type currentType, Type baseType)
8787
return name;
8888
}
8989

90+
public static String GetOfficialName(this Enum value)
91+
{
92+
var enumType = value.GetType();
93+
var member = enumType.GetMember(value.ToString()).FirstOrDefault();
94+
95+
// if the enum value does not have a DomNameAttribute, calling member.GetOfficialName() would return the value name
96+
// to allow previous behaviour to be preserved, if the DomNameAttribute is not present then null will be returned
97+
IEnumerable<DomNameAttribute> names = member.GetCustomAttributes<DomNameAttribute>();
98+
var officialNameAttribute = names.FirstOrDefault();
99+
return officialNameAttribute?.OfficialName;
100+
}
101+
90102
public static PropertyInfo GetInheritedProperty(this Type type, String propertyName, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance)
91103
{
92104
if (type.GetTypeInfo().IsInterface)

0 commit comments

Comments
 (0)