Skip to content

Commit dc62af4

Browse files
committed
Added screen and instances
1 parent f7d8d0a commit dc62af4

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

AngleSharp.Scripting.JavaScript/AngleSharp.Scripting.JavaScript.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@
5050
<Reference Include="System.Xml" />
5151
</ItemGroup>
5252
<ItemGroup>
53+
<Compile Include="Attributes\DomInstanceAttribute.cs" />
5354
<Compile Include="ConfigurationExtensions.cs" />
5455
<Compile Include="ConsoleInstance.cs" />
5556
<Compile Include="DomConstructorInstance.cs" />
5657
<Compile Include="DomConstructors.cs" />
5758
<Compile Include="DomEventInstance.cs" />
5859
<Compile Include="Dom\Navigator.cs" />
5960
<Compile Include="Dom\RequesterState.cs" />
61+
<Compile Include="Dom\Screen.cs" />
6062
<Compile Include="Dom\XmlHttpRequest.cs" />
6163
<Compile Include="Dom\XmlHttpRequestEventTarget.cs" />
6264
<Compile Include="Dom\XmlHttpRequestResponseType.cs" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace AngleSharp.Attributes
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Represents a single instance object.
7+
/// </summary>
8+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
9+
sealed class DomInstanceAttribute : Attribute
10+
{
11+
/// <summary>
12+
/// Creates a new instance.
13+
/// </summary>
14+
/// <param name="name">The name to use.</param>
15+
public DomInstanceAttribute(String name)
16+
{
17+
Name = name;
18+
}
19+
20+
/// <summary>
21+
/// Gets the name of the variable.
22+
/// </summary>
23+
public String Name
24+
{
25+
get;
26+
private set;
27+
}
28+
}
29+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace AngleSharp.Scripting.JavaScript.Dom
2+
{
3+
using AngleSharp.Attributes;
4+
using System;
5+
6+
/// <summary>
7+
/// Represents information about the screen of the output device.
8+
/// </summary>
9+
[DomName("Screen")]
10+
[DomInstance("screen")]
11+
[DomExposed("Window")]
12+
public sealed class Screen
13+
{
14+
/// <summary>
15+
/// Gets the available width of the rendering surface of the output
16+
/// device, in CSS pixels.
17+
/// </summary>
18+
[DomName("availWidth")]
19+
public Int32 AvailableWidth
20+
{
21+
get { return 1920; }
22+
}
23+
24+
/// <summary>
25+
/// Gets the available height of the rendering surface of the output
26+
/// device, in CSS pixels.
27+
/// </summary>
28+
[DomName("availHeight")]
29+
public Int32 AvailableHeight
30+
{
31+
get { return 1080; }
32+
}
33+
34+
/// <summary>
35+
/// Gets the width of the output device, in CSS pixels.
36+
/// </summary>
37+
[DomName("width")]
38+
public Int32 Width
39+
{
40+
get { return AvailableWidth; }
41+
}
42+
43+
/// <summary>
44+
/// Gets the height of the output device, in CSS pixels.
45+
/// </summary>
46+
[DomName("height")]
47+
public Int32 Height
48+
{
49+
get { return AvailableHeight; }
50+
}
51+
52+
/// <summary>
53+
/// Gets the value 24 - by specification.
54+
/// </summary>
55+
[DomName("colorDepth")]
56+
public UInt32 ColorDepth
57+
{
58+
get { return 24; }
59+
}
60+
61+
/// <summary>
62+
/// Gets the value 24 - by specification.
63+
/// </summary>
64+
[DomName("pixelDepth")]
65+
public UInt32 PixelDepth
66+
{
67+
get { return 24; }
68+
}
69+
}
70+
}

AngleSharp.Scripting.JavaScript/EngineInstance.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public EngineInstance(IWindow window, IDictionary<String, Object> assignments)
3232

3333
this.AddConstructors(_window, typeof(INode));
3434
this.AddConstructors(_window, this.GetType());
35+
this.AddInstances(_window, this.GetType());
3536
}
3637

3738
public DomNodeInstance Window

AngleSharp.Scripting.JavaScript/Extensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ public static void AddConstructors(this EngineInstance engine, ObjectInstance ob
173173
engine.AddConstructor(obj, exportedType);
174174
}
175175

176+
public static void AddInstances(this EngineInstance engine, ObjectInstance obj, Type type)
177+
{
178+
foreach (var exportedType in type.Assembly.ExportedTypes)
179+
engine.AddInstance(obj, exportedType);
180+
}
181+
176182
public static void AddConstructor(this EngineInstance engine, ObjectInstance obj, Type type)
177183
{
178184
var info = type.GetConstructors().FirstOrDefault(m =>
@@ -186,6 +192,26 @@ public static void AddConstructor(this EngineInstance engine, ObjectInstance obj
186192
}
187193
}
188194

195+
public static void AddInstance(this EngineInstance engine, ObjectInstance obj, Type type)
196+
{
197+
var attributes = type.GetCustomAttributes<DomInstanceAttribute>();
198+
var info = type.GetConstructor(Type.EmptyTypes);
199+
200+
if (info != null)
201+
{
202+
foreach (var attribute in attributes)
203+
{
204+
var instance = info.Invoke(null);
205+
206+
if (instance != null)
207+
{
208+
var node = engine.GetDomNode(instance);
209+
obj.FastSetProperty(attribute.Name, new PropertyDescriptor(node, false, true, false));
210+
}
211+
}
212+
}
213+
}
214+
189215
public static String GetOfficialName(this MemberInfo member)
190216
{
191217
var names = member.GetCustomAttributes<DomNameAttribute>();

0 commit comments

Comments
 (0)