DomImplementation
Provides a number of methods for performing operations that are
independent of any particular instance of the DOM.
using AngleSharp.Dom.Html;
using AngleSharp.Dom.Xml;
using AngleSharp.Extensions;
using AngleSharp.Html;
using System;
using System.Collections.Generic;
namespace AngleSharp.Dom
{
internal sealed class DomImplementation : IImplementation
{
private static readonly Dictionary<string, string[]> features = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase) {
{
"XML",
new string[2] {
"1.0",
"2.0"
}
},
{
"HTML",
new string[2] {
"1.0",
"2.0"
}
},
{
"Core",
new string[1] {
"2.0"
}
},
{
"Views",
new string[1] {
"2.0"
}
},
{
"StyleSheets",
new string[1] {
"2.0"
}
},
{
"CSS",
new string[1] {
"2.0"
}
},
{
"CSS2",
new string[1] {
"2.0"
}
},
{
"Traversal",
new string[1] {
"2.0"
}
},
{
"Events",
new string[1] {
"2.0"
}
},
{
"UIEvents",
new string[1] {
"2.0"
}
},
{
"HTMLEvents",
new string[1] {
"2.0"
}
},
{
"Range",
new string[1] {
"2.0"
}
},
{
"MutationEvents",
new string[1] {
"2.0"
}
}
};
private readonly Document _owner;
public DomImplementation(Document owner)
{
_owner = owner;
}
public IDocumentType CreateDocumentType(string qualifiedName, string publicId, string systemId)
{
if (qualifiedName == null)
throw new ArgumentNullException("qualifiedName");
if (!qualifiedName.IsXmlName())
throw new DomException(DomError.InvalidCharacter);
if (!qualifiedName.IsQualifiedName())
throw new DomException(DomError.Namespace);
return new DocumentType(_owner, qualifiedName) {
PublicIdentifier = publicId,
SystemIdentifier = systemId
};
}
public IXmlDocument CreateDocument(string namespaceUri = null, string qualifiedName = null, IDocumentType doctype = null)
{
XmlDocument xmlDocument = new XmlDocument(null);
if (doctype != null)
xmlDocument.AppendChild(doctype);
if (!string.IsNullOrEmpty(qualifiedName)) {
IElement element = xmlDocument.CreateElement(namespaceUri, qualifiedName);
if (element != null)
xmlDocument.AppendChild(element);
}
xmlDocument.BaseUrl = _owner.BaseUrl;
return xmlDocument;
}
public IDocument CreateHtmlDocument(string title)
{
HtmlDocument htmlDocument = new HtmlDocument(null);
htmlDocument.AppendChild(new DocumentType(htmlDocument, TagNames.Html));
htmlDocument.AppendChild(htmlDocument.CreateElement(TagNames.Html));
htmlDocument.DocumentElement.AppendChild(htmlDocument.CreateElement(TagNames.Head));
if (!string.IsNullOrEmpty(title)) {
IElement element = htmlDocument.CreateElement(TagNames.Title);
element.AppendChild(htmlDocument.CreateTextNode(title));
htmlDocument.Head.AppendChild(element);
}
htmlDocument.DocumentElement.AppendChild(htmlDocument.CreateElement(TagNames.Body));
htmlDocument.BaseUrl = _owner.BaseUrl;
return htmlDocument;
}
public bool HasFeature(string feature, string version = null)
{
if (feature == null)
throw new ArgumentNullException("feature");
string[] value = null;
if (features.TryGetValue(feature, out value))
return value.Contains(version ?? string.Empty, StringComparison.OrdinalIgnoreCase);
return false;
}
}
}