|
| 1 | +namespace AngleSharp.Js.Cache |
| 2 | +{ |
| 3 | + using AngleSharp.Attributes; |
| 4 | + using System; |
| 5 | + using System.Collections.Concurrent; |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.Linq; |
| 8 | + using System.Reflection; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// The WebIDL constants of the interfaces (e.g. Node.ELEMENT_NODE), which the libraries |
| 12 | + /// declare as the fields of an enum named like the interface owning them. |
| 13 | + /// </summary> |
| 14 | + static class ConstantCache |
| 15 | + { |
| 16 | + private static readonly ConcurrentDictionary<Assembly, IReadOnlyDictionary<String, KeyValuePair<String, Double>[]>> _constants = new(); |
| 17 | + |
| 18 | + // Enums whose name doesn't say the interface owning their constants (AngleSharp 1.10 names |
| 19 | + // the document for those of a node, which all nodes have). |
| 20 | + private static readonly Dictionary<String, String> _owners = new(StringComparer.Ordinal) |
| 21 | + { |
| 22 | + { "AngleSharp.Dom.NodeType", "Node" }, |
| 23 | + { "AngleSharp.Dom.DocumentPositions", "Node" }, |
| 24 | + { "AngleSharp.Dom.DomError", "DOMException" }, |
| 25 | + }; |
| 26 | + |
| 27 | + public static IEnumerable<KeyValuePair<String, Double>> GetConstants(this IEnumerable<Assembly> libs, String name) |
| 28 | + { |
| 29 | + if (name is null) |
| 30 | + { |
| 31 | + return []; |
| 32 | + } |
| 33 | + |
| 34 | + return libs |
| 35 | + .Select(m => _constants.GetOrAdd(m, Collect)) |
| 36 | + .SelectMany(m => m.TryGetValue(name, out var constants) ? constants : []); |
| 37 | + } |
| 38 | + |
| 39 | + private static IReadOnlyDictionary<String, KeyValuePair<String, Double>[]> Collect(Assembly assembly) |
| 40 | + { |
| 41 | + var result = new Dictionary<String, List<KeyValuePair<String, Double>>>(StringComparer.Ordinal); |
| 42 | + |
| 43 | + foreach (var type in GetTypes(assembly)) |
| 44 | + { |
| 45 | + var info = type.GetTypeInfo(); |
| 46 | + |
| 47 | + if (!info.IsEnum || info.GetCustomAttribute<DomLiteralsAttribute>() != null) |
| 48 | + { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + var owner = _owners.TryGetValue(type.FullName ?? String.Empty, out var known) ? known : |
| 53 | + info.GetCustomAttributes<DomNameAttribute>().FirstOrDefault()?.OfficialName; |
| 54 | + |
| 55 | + if (owner is null) |
| 56 | + { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + foreach (var field in info.DeclaredFields.Where(m => m.IsLiteral)) |
| 61 | + { |
| 62 | + var constant = field.GetCustomAttributes<DomNameAttribute>().FirstOrDefault()?.OfficialName; |
| 63 | + |
| 64 | + if (constant is not null && IsConstantName(constant)) |
| 65 | + { |
| 66 | + if (!result.TryGetValue(owner, out var list)) |
| 67 | + { |
| 68 | + result[owner] = list = []; |
| 69 | + } |
| 70 | + |
| 71 | + if (!list.Any(m => m.Key == constant)) |
| 72 | + { |
| 73 | + list.Add(new KeyValuePair<String, Double>(constant, Convert.ToDouble(field.GetRawConstantValue()))); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return result.ToDictionary(m => m.Key, m => m.Value.ToArray(), StringComparer.Ordinal); |
| 80 | + } |
| 81 | + |
| 82 | + // constants are all upper case (the members of a string enumeration are not) |
| 83 | + private static Boolean IsConstantName(String name) => |
| 84 | + name.Length > 0 && Char.IsUpper(name[0]) && name.All(c => Char.IsUpper(c) || Char.IsDigit(c) || c == '_'); |
| 85 | + |
| 86 | + private static IEnumerable<Type> GetTypes(Assembly assembly) |
| 87 | + { |
| 88 | + try |
| 89 | + { |
| 90 | + return assembly.ExportedTypes; |
| 91 | + } |
| 92 | + catch (Exception) |
| 93 | + { |
| 94 | + return []; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments