DomImplementation
Provides a number of methods for performing operations that are independent of any particular instance of the DOM.
using AngleSharp.Dom.Xml;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Network;
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(ErrorCode.InvalidCharacter);
if (!qualifiedName.IsQualifiedName())
throw new DomException(ErrorCode.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)
{
Document document = new Document(null);
document.ContentType = MimeTypes.Html;
Document document2 = document;
document2.AppendChild(new DocumentType(document2, Tags.Html));
Document document3 = document;
document3.AppendChild(document3.CreateElement(Tags.Html));
document.DocumentElement.AppendChild(document.CreateElement(Tags.Head));
if (!string.IsNullOrEmpty(title)) {
IElement element = document.CreateElement(Tags.Title);
element.AppendChild(document.CreateTextNode(title));
document.Head.AppendChild(element);
}
document.DocumentElement.AppendChild(document.CreateElement(Tags.Body));
document.BaseUrl = _owner.BaseUrl;
return document;
}
public bool HasFeature(string feature, string version = null)
{
if (feature == null)
throw new ArgumentNullException("feature");
version = (version ?? string.Empty);
if (_features.TryGetValue(feature, out string[] value))
return value.Contains(version, StringComparison.OrdinalIgnoreCase);
return false;
}
}
}