Skip to content

Commit 8bc5d07

Browse files
committed
Introduced HTML input helpers
1 parent df5c902 commit 8bc5d07

File tree

6 files changed

+247
-42
lines changed

6 files changed

+247
-42
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# 0.12.0
2+
3+
Released on Thursday, May 2 2019.
4+
5+
- Reference latest AngleSharp
6+
- Added `InputFile` class as a standard `IFile` implementation
7+
- Added `AppendFile` extensions for `IHtmlInputElement`
8+
19
# 0.10.1
210

311
Released on Monday, January 7 2019.

src/AngleSharp.Io.Tests/DisposableStream.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,14 @@
55

66
sealed class DisposableStream : Stream
77
{
8-
public override Boolean CanRead
9-
{
10-
get { return false; }
11-
}
12-
13-
public override Boolean CanSeek
14-
{
15-
get { return false; }
16-
}
17-
18-
public override Boolean CanWrite
19-
{
20-
get { return false; }
21-
}
22-
23-
public override Int64 Length
24-
{
25-
get { return 0; }
26-
}
27-
8+
public override Boolean CanRead => false;
9+
10+
public override Boolean CanSeek => false;
11+
12+
public override Boolean CanWrite => false;
13+
14+
public override Int64 Length => 0;
15+
2816
public override Int64 Position
2917
{
3018
get;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace AngleSharp.Io.Dom
2+
{
3+
using AngleSharp.Html;
4+
using AngleSharp.Html.Dom;
5+
using System;
6+
using System.IO;
7+
8+
/// <summary>
9+
/// Extensions for the HTML Input element.
10+
/// </summary>
11+
public static class HtmlInputElementExtensions
12+
{
13+
/// <summary>
14+
/// Appends a file to the input element.
15+
/// Requires the input element to be of type "file".
16+
/// </summary>
17+
/// <param name="input">The input to append to.</param>
18+
/// <param name="file">The file to append.</param>
19+
/// <returns>The input itself for chaining.</returns>
20+
public static IHtmlInputElement AppendFile(this IHtmlInputElement input, InputFile file)
21+
{
22+
input = input ?? throw new ArgumentNullException(nameof(input));
23+
24+
if (input.Type == InputTypeNames.File)
25+
{
26+
input.Files.Add(file ?? throw new ArgumentNullException(nameof(file)));
27+
}
28+
29+
return input;
30+
}
31+
32+
/// <summary>
33+
/// Appends a file to the input element.
34+
/// Requires the input element to be of type "file".
35+
/// </summary>
36+
/// <param name="input">The input to append to.</param>
37+
/// <param name="filePath">The path to the file, which should be appended.</param>
38+
/// <returns>The input itself for chaining.</returns>
39+
public static IHtmlInputElement AppendFile(this IHtmlInputElement input, String filePath)
40+
{
41+
filePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
42+
var name = Path.GetFileName(filePath);
43+
var ext = Path.GetExtension(filePath);
44+
var type = MimeTypeNames.FromExtension(ext);
45+
var stream = File.OpenRead(filePath);
46+
var modified = File.GetLastWriteTimeUtc(filePath);
47+
var file = new InputFile(name, type, stream, modified);
48+
return input.AppendFile(file);
49+
}
50+
}
51+
}

src/AngleSharp.Io/Dom/InputFile.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
namespace AngleSharp.Io.Dom
2+
{
3+
using System;
4+
using System.IO;
5+
6+
/// <summary>
7+
/// Represents an input file
8+
/// </summary>
9+
public class InputFile : IFile
10+
{
11+
private readonly String _fileName;
12+
private readonly Stream _content;
13+
private readonly String _type;
14+
private readonly DateTime _modified;
15+
16+
/// <summary>
17+
/// Creates a new input file.
18+
/// </summary>
19+
/// <param name="fileName">The name of the file.</param>
20+
/// <param name="type">The MIME type of the file.</param>
21+
/// <param name="content">The content stream.</param>
22+
/// <param name="modified">The last modified date.</param>
23+
public InputFile(String fileName, String type, Stream content, DateTime modified)
24+
{
25+
_fileName = fileName;
26+
_type = type;
27+
_content = content;
28+
_modified = modified;
29+
}
30+
31+
/// <summary>
32+
/// Creates a new input file.
33+
/// </summary>
34+
/// <param name="fileName">The name of the file.</param>
35+
/// <param name="type">The MIME type of the file.</param>
36+
/// <param name="content">The content stream.</param>
37+
public InputFile(String fileName, String type, Stream content)
38+
: this(fileName, type, content, DateTime.Now)
39+
{
40+
}
41+
42+
/// <summary>
43+
/// Creates a new input file.
44+
/// </summary>
45+
/// <param name="fileName">The name of the file.</param>
46+
/// <param name="type">The MIME type of the file.</param>
47+
/// <param name="content">The content stream.</param>
48+
public InputFile(String fileName, String type, Byte[] content)
49+
: this(fileName, type, new MemoryStream(content), DateTime.Now)
50+
{
51+
}
52+
53+
/// <summary>
54+
/// Gets the content stream.
55+
/// </summary>
56+
public Stream Body => _content;
57+
58+
/// <summary>
59+
/// Gets if the input stream is closed.
60+
/// </summary>
61+
public Boolean IsClosed => _content.CanRead == false;
62+
63+
/// <summary>
64+
/// Gets the last modified date.
65+
/// </summary>
66+
public DateTime LastModified => _modified;
67+
68+
/// <summary>
69+
/// Gets the length of the content stream.
70+
/// </summary>
71+
public Int32 Length => (Int32)_content.Length;
72+
73+
/// <summary>
74+
/// Gets the name of the file.
75+
/// </summary>
76+
public String Name => _fileName;
77+
78+
/// <summary>
79+
/// Gets the MIME type of the file.
80+
/// </summary>
81+
public String Type => _type;
82+
83+
/// <summary>
84+
/// Closes the content stream.
85+
/// </summary>
86+
public void Close() => _content.Close();
87+
88+
/// <summary>
89+
/// Disposes the content stream.
90+
/// </summary>
91+
void IDisposable.Dispose() => _content.Dispose();
92+
93+
/// <inheritdoc />
94+
public IBlob Slice(Int32 start = 0, Int32 end = Int32.MaxValue, String contentType = null)
95+
{
96+
var ms = new MemoryStream();
97+
_content.Position = start;
98+
var buffer = new Byte[Math.Max(0, Math.Min(end, _content.Length) - start)];
99+
_content.Read(buffer, 0, buffer.Length);
100+
ms.Write(buffer, 0, buffer.Length);
101+
_content.Position = 0;
102+
return new InputFile(_fileName, _type, ms);
103+
}
104+
}
105+
}

src/AngleSharp.Io/Dom/WebSocket.cs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,44 +137,32 @@ public WebSocket(IWindow window, String url, params String[] protocols)
137137
/// Gets the URL the connection is made to.
138138
/// </summary>
139139
[DomName("url")]
140-
public String Url
141-
{
142-
get { return _url.Href; }
143-
}
144-
140+
public String Url => _url.Href;
141+
145142
/// <summary>
146143
/// Gets the current state of the connection.
147144
/// </summary>
148145
[DomName("readyState")]
149-
public WebSocketReadyState ReadyState
150-
{
151-
get { return _state; }
152-
}
153-
146+
public WebSocketReadyState ReadyState => _state;
147+
154148
/// <summary>
155149
/// Gets the number of bytes of UTF-8 text that have been queued using
156150
/// Send() but that, as of the last time the event loop started
157151
/// executing a task, had not yet been transmitted to the network.
158152
/// </summary>
159153
[DomName("bufferedAmount")]
160-
public Int64 Buffered
161-
{
162-
get { return 0; }
163-
}
164-
154+
public Int64 Buffered => 0;
155+
165156
/// <summary>
166157
/// Gets the chosen protocol for the connection.
167158
/// </summary>
168159
[DomName("protocol")]
169-
public String Protocol
170-
{
171-
get { return _ws.SubProtocol ?? String.Empty; }
172-
}
173-
160+
public String Protocol => _ws.SubProtocol ?? String.Empty;
161+
174162
#endregion
175-
163+
176164
#region Methods
177-
165+
178166
/// <summary>
179167
/// Transmits data using the connection.
180168
/// </summary>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace AngleSharp.Io.Network
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// A collection of constants found in a cookie.
7+
/// </summary>
8+
public static class CookieFieldNames
9+
{
10+
/// <summary>
11+
/// The Comment attribute name.
12+
/// </summary>
13+
public static readonly String Comment = "Comment";
14+
15+
/// <summary>
16+
/// The CommentURL attribute name.
17+
/// </summary>
18+
public static readonly String CommentUrl = "CommentURL";
19+
20+
/// <summary>
21+
/// The Discard attribute name.
22+
/// </summary>
23+
public static readonly String Discard = "Discard";
24+
25+
/// <summary>
26+
/// The Domain attribute name.
27+
/// </summary>
28+
public static readonly String Domain = "Domain";
29+
30+
/// <summary>
31+
/// The Expires attribute name.
32+
/// </summary>
33+
public static readonly String Expires = "Expires";
34+
35+
/// <summary>
36+
/// The Max-Age attribute name.
37+
/// </summary>
38+
public static readonly String MaxAge = "Max-Age";
39+
40+
/// <summary>
41+
/// The Path attribute name.
42+
/// </summary>
43+
public static readonly String Path = "Path";
44+
45+
/// <summary>
46+
/// The Port attribute name.
47+
/// </summary>
48+
public static readonly String Port = "Port";
49+
50+
/// <summary>
51+
/// The Secure attribute name.
52+
/// </summary>
53+
public static readonly String Secure = "Secure";
54+
55+
/// <summary>
56+
/// The Version attribute name.
57+
/// </summary>
58+
public static readonly String Version = "Version";
59+
60+
/// <summary>
61+
/// The HttpOnly attribute name.
62+
/// </summary>
63+
public static readonly String HttpOnly = "HttpOnly";
64+
}
65+
}

0 commit comments

Comments
 (0)