Skip to content

Commit b3915f1

Browse files
committed
Implemented the DOMParser #15
1 parent 26b8c2a commit b3915f1

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Compile Include="DomConstructorInstance.cs" />
5757
<Compile Include="DomConstructors.cs" />
5858
<Compile Include="DomEventInstance.cs" />
59+
<Compile Include="Dom\DomParser.cs" />
5960
<Compile Include="Dom\Navigator.cs" />
6061
<Compile Include="Dom\RequesterState.cs" />
6162
<Compile Include="Dom\Screen.cs" />
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
namespace AngleSharp.Scripting.JavaScript.Dom
2+
{
3+
using AngleSharp.Attributes;
4+
using AngleSharp.Dom;
5+
using AngleSharp.Parser.Html;
6+
using AngleSharp.Parser.Xml;
7+
using System;
8+
using System.Collections.Generic;
9+
10+
/// <summary>
11+
/// A way for JavaScript applications to access the parser.
12+
/// See: https://w3c.github.io/DOM-Parsing/#the-domparser-interface
13+
/// </summary>
14+
[DomName("DOMParser")]
15+
[DomExposed("Window")]
16+
public sealed class DomParser
17+
{
18+
static readonly Dictionary<String, Func<String, IDocument>> SupportedTypes = new Dictionary<String, Func<String, IDocument>>
19+
{
20+
{ "text/html", ParseHtml },
21+
{ "text/xml", ParseXml },
22+
{ "application/xml", ParseXml },
23+
{ "application/xhtml+xml", ParseXml },
24+
{ "image/svg+xml", ParseSvg }
25+
};
26+
27+
readonly IWindow _window;
28+
29+
[DomConstructor]
30+
public DomParser(IWindow window)
31+
{
32+
_window = window;
33+
}
34+
35+
[DomName("parseFromString")]
36+
public IDocument Parse(String str, String type)
37+
{
38+
var parser = default(Func<String, IDocument>);
39+
40+
if (!SupportedTypes.TryGetValue(type, out parser))
41+
{
42+
throw new DomException(DomError.NotSupported);
43+
}
44+
45+
//Potentially the resulting document should also have the following properties:
46+
//- the content type must be the type argument
47+
//- the URL value must be the URL of the active document
48+
//- the location value must be null
49+
return parser.Invoke(str);
50+
}
51+
52+
static IDocument ParseHtml(String content)
53+
{
54+
var options = new HtmlParserOptions
55+
{
56+
IsScripting = false,
57+
IsEmbedded = false
58+
};
59+
var html = new HtmlParser(options);
60+
return html.Parse(content);
61+
}
62+
63+
static IDocument ParseXml(String content)
64+
{
65+
var options = new XmlParserOptions
66+
{
67+
IsSuppressingErrors = true
68+
};
69+
var xml = new XmlParser();
70+
71+
try
72+
{
73+
return xml.Parse(content);
74+
}
75+
catch (XmlParseException ex)
76+
{
77+
content = GetXmlErrorContent(ex, content);
78+
return xml.Parse(content);
79+
}
80+
}
81+
82+
static IDocument ParseSvg(String content)
83+
{
84+
var svg = new XmlParser();
85+
return svg.Parse(content);
86+
}
87+
88+
static String GetXmlErrorContent(XmlParseException ex, String content)
89+
{
90+
return String.Format("<parsererror xmlns=\"http://www.mozilla.org/newlayout/xml/parsererror.xml\">{0}<sourcetext>{1}</sourcetext></parsererror>",
91+
ex.Message,
92+
String.Empty
93+
);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)