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;
private readonly Document _owner;
static DomImplementation()
{
_features = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
AddFeature("XML", "1.0", "2.0");
AddFeature("HTML", "1.0", "2.0");
AddFeature("Core", "2.0");
AddFeature("Views", "2.0");
AddFeature("StyleSheets", "2.0");
AddFeature("CSS", "2.0");
AddFeature("CSS2", "2.0");
AddFeature("Traversal", "2.0");
AddFeature("Events", "2.0");
AddFeature("UIEvents", "2.0");
AddFeature("HTMLEvents", "2.0");
AddFeature("Range", "2.0");
AddFeature("MutationEvents", "2.0");
}
private static void AddFeature(string feature, params string[] versions)
{
_features.Add(feature, versions);
}
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);
DocumentType documentType = new DocumentType(_owner, qualifiedName);
documentType.PublicIdentifier = publicId;
documentType.SystemIdentifier = systemId;
return documentType;
}
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;
}
}
}