DOMImplementation.cpp [plain text]
#include "config.h"
#include "DOMImplementation.h"
#include "CSSStyleSheet.h"
#include "DocumentType.h"
#include "Element.h"
#include "ExceptionCode.h"
#include "Frame.h"
#include "FTPDirectoryDocument.h"
#include "HTMLDocument.h"
#include "HTMLNames.h"
#include "HTMLViewSourceDocument.h"
#include "Image.h"
#include "ImageDocument.h"
#include "MediaList.h"
#include "MIMETypeRegistry.h"
#include "Page.h"
#include "PluginDocument.h"
#include "PluginInfoStore.h"
#include "RegularExpression.h"
#include "Settings.h"
#include "TextDocument.h"
#include "XMLNames.h"
#if ENABLE(SVG)
#include "SVGNames.h"
#include "SVGDocument.h"
#endif
namespace WebCore {
static bool qualifiedNameIsMalformed(const String&)
{
return false;
}
#if ENABLE(SVG)
static void addString(HashSet<String, CaseFoldingHash>& set, const char* string)
{
set.add(string);
}
static bool isSVG10Feature(const String &feature)
{
static bool initialized = false;
static HashSet<String, CaseFoldingHash> svgFeatures;
if (!initialized) {
#if ENABLE(SVG_USE) && ENABLE(SVG_FOREIGN_OBJECT) && ENABLE(SVG_FILTER) && ENABLE(SVG_FONTS)
addString(svgFeatures, "svg");
addString(svgFeatures, "svg.static");
#endif
#if ENABLE(SVG_USE) && ENABLE(SVG_FOREIGN_OBJECT) && ENABLE(SVG_FILTER) && ENABLE(SVG_FONTS)
addString(svgFeatures, "dom");
addString(svgFeatures, "dom.svg");
addString(svgFeatures, "dom.svg.static");
#endif
initialized = true;
}
return svgFeatures.contains(feature);
}
static bool isSVG11Feature(const String &feature)
{
static bool initialized = false;
static HashSet<String, CaseFoldingHash> svgFeatures;
if (!initialized) {
#if ENABLE(SVG_USE) && ENABLE(SVG_FOREIGN_OBJECT) && ENABLE(SVG_FILTER) && ENABLE(SVG_FONTS)
addString(svgFeatures, "SVG");
addString(svgFeatures, "SVGDOM");
addString(svgFeatures, "SVG-static");
addString(svgFeatures, "SVGDOM-static");
#endif
addString(svgFeatures, "CoreAttribute");
#if ENABLE(SVG_USE)
addString(svgFeatures, "Structure");
addString(svgFeatures, "BasicStructure");
#endif
addString(svgFeatures, "ContainerAttribute");
addString(svgFeatures, "ConditionalProcessing");
addString(svgFeatures, "Image");
addString(svgFeatures, "Style");
addString(svgFeatures, "ViewportAttribute");
addString(svgFeatures, "Shape");
addString(svgFeatures, "BasicText");
addString(svgFeatures, "PaintAttribute");
addString(svgFeatures, "BasicPaintAttribute");
addString(svgFeatures, "OpacityAttribute");
addString(svgFeatures, "GraphicsAttribute");
addString(svgFeatures, "BaseGraphicsAttribute");
addString(svgFeatures, "Marker");
addString(svgFeatures, "Gradient");
addString(svgFeatures, "Pattern");
addString(svgFeatures, "Clip");
addString(svgFeatures, "BasicClip");
addString(svgFeatures, "Mask");
#if ENABLE(SVG_FILTER)
addString(svgFeatures, "BasicFilter");
#endif
addString(svgFeatures, "DocumentEventsAttribute");
addString(svgFeatures, "GraphicalEventsAttribute");
addString(svgFeatures, "Cursor");
addString(svgFeatures, "Hyperlinking");
addString(svgFeatures, "XlinkAttribute");
addString(svgFeatures, "ExternalResourcesRequired");
addString(svgFeatures, "Script");
#if ENABLE(SVG_FONTS)
addString(svgFeatures, "Font");
addString(svgFeatures, "BasicFont");
#endif
#if ENABLE(SVG_FOREIGN_OBJECT)
addString(svgFeatures, "Extensibility");
#endif
initialized = true;
}
return svgFeatures.contains(feature);
}
#endif
DOMImplementation::~DOMImplementation()
{
}
bool DOMImplementation::hasFeature (const String& feature, const String& version) const
{
String lower = feature.lower();
if (lower == "core" || lower == "html" || lower == "xml" || lower == "xhtml")
return version.isEmpty() || version == "1.0" || version == "2.0";
if (lower == "css"
|| lower == "css2"
|| lower == "events"
|| lower == "htmlevents"
|| lower == "mouseevents"
|| lower == "mutationevents"
|| lower == "range"
|| lower == "stylesheets"
|| lower == "traversal"
|| lower == "uievents"
|| lower == "views")
return version.isEmpty() || version == "2.0";
if (lower == "xpath" || lower == "textevents")
return version.isEmpty() || version == "3.0";
#if ENABLE(SVG)
if ((version.isEmpty() || version == "1.1") && feature.startsWith("http://www.w3.org/tr/svg11/feature#", false)) {
if (isSVG11Feature(feature.right(feature.length() - 35)))
return true;
}
if ((version.isEmpty() || version == "1.0") && feature.startsWith("org.w3c.", false)) {
if (isSVG10Feature(feature.right(feature.length() - 8)))
return true;
}
#endif
return false;
}
PassRefPtr<DocumentType> DOMImplementation::createDocumentType(const String& qualifiedName,
const String& publicId, const String& systemId, ExceptionCode& ec)
{
if (qualifiedName.isNull()) {
ec = NAMESPACE_ERR;
return 0;
}
String prefix, localName;
if (!Document::parseQualifiedName(qualifiedName, prefix, localName)) {
ec = INVALID_CHARACTER_ERR;
return 0;
}
if (qualifiedNameIsMalformed(qualifiedName)) {
ec = NAMESPACE_ERR;
return 0;
}
ec = 0;
return new DocumentType(this, 0, qualifiedName, publicId, systemId);
}
DOMImplementation* DOMImplementation::getInterface(const String& ) const
{
return 0;
}
PassRefPtr<Document> DOMImplementation::createDocument(const String& namespaceURI,
const String& qualifiedName, DocumentType* doctype, ExceptionCode& ec)
{
if (!qualifiedName.isEmpty()) {
String prefix, localName;
if (!Document::parseQualifiedName(qualifiedName, prefix, localName)) {
ec = INVALID_CHARACTER_ERR;
return 0;
}
int colonpos = qualifiedName.find(':');
if (qualifiedNameIsMalformed(qualifiedName) ||
(colonpos >= 0 && namespaceURI.isNull()) ||
(colonpos == 3 && qualifiedName[0] == 'x' && qualifiedName[1] == 'm' && qualifiedName[2] == 'l' &&
#if ENABLE(SVG)
namespaceURI != SVGNames::svgNamespaceURI &&
#endif
namespaceURI != XMLNames::xmlNamespaceURI)) {
ec = NAMESPACE_ERR;
return 0;
}
}
if (doctype && (doctype->document() || doctype->implementation() != this)) {
ec = WRONG_DOCUMENT_ERR;
return 0;
}
RefPtr<Document> doc;
#if ENABLE(SVG)
if (namespaceURI == SVGNames::svgNamespaceURI)
doc = new SVGDocument(this, 0);
else
#endif
if (namespaceURI == HTMLNames::xhtmlNamespaceURI)
doc = new Document(this, 0, true);
else
doc = new Document(this, 0);
if (doctype) {
doc->setDocType(doctype);
doctype->setDocument(doc.get());
}
if (!qualifiedName.isEmpty())
doc->addChild(doc->createElementNS(namespaceURI, qualifiedName, ec));
ec = 0;
return doc.release();
}
PassRefPtr<CSSStyleSheet> DOMImplementation::createCSSStyleSheet(const String&, const String& media, ExceptionCode& ec)
{
ec = 0;
CSSStyleSheet* const nullSheet = 0;
RefPtr<CSSStyleSheet> sheet = new CSSStyleSheet(nullSheet);
sheet->setMedia(new MediaList(sheet.get(), media, true));
return sheet.release();
}
PassRefPtr<Document> DOMImplementation::createDocument(Frame* frame)
{
return new Document(this, frame);
}
PassRefPtr<HTMLDocument> DOMImplementation::createHTMLDocument(Frame* frame)
{
return new HTMLDocument(this, frame);
}
DOMImplementation* DOMImplementation::instance()
{
static RefPtr<DOMImplementation> i = new DOMImplementation;
return i.get();
}
bool DOMImplementation::isXMLMIMEType(const String& mimeType)
{
if (mimeType == "text/xml" || mimeType == "application/xml" || mimeType == "text/xsl")
return true;
static const char* validChars = "[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]"; static RegularExpression xmlTypeRegExp(DeprecatedString("^") + validChars + "+/" + validChars + "+\\+xml$");
if (xmlTypeRegExp.match(mimeType.deprecatedString()) > -1)
return true;
return false;
}
bool DOMImplementation::isTextMIMEType(const String& mimeType)
{
if (MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType) ||
(mimeType.startsWith("text/") && mimeType != "text/html" &&
mimeType != "text/xml" && mimeType != "text/xsl"))
return true;
return false;
}
PassRefPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
{
RefPtr<HTMLDocument> d = new HTMLDocument(this, 0);
d->open();
d->write("<html><head><title>" + title + "</title></head><body></body></html>");
return d.release();
}
PassRefPtr<Document> DOMImplementation::createDocument(const String& type, Frame* frame, bool inViewSourceMode)
{
if (inViewSourceMode) {
if (type == "text/html" || type == "application/xhtml+xml" || type == "image/svg+xml" || isTextMIMEType(type) || isXMLMIMEType(type))
return new HTMLViewSourceDocument(this, frame, type);
}
if (type == "text/html")
return new HTMLDocument(this, frame);
if (type == "application/xhtml+xml")
return new Document(this, frame, true);
#if ENABLE(FTPDIR)
if (type == "application/x-ftp-directory")
return new FTPDirectoryDocument(this, frame);
#endif
if ((type == "application/pdf" || type == "text/pdf") && PluginInfoStore::supportsMIMEType(type))
return new PluginDocument(this, frame);
if (Image::supportsType(type))
return new ImageDocument(this, frame);
if (type != "text/plain" && PluginInfoStore::supportsMIMEType(type))
return new PluginDocument(this, frame);
if (isTextMIMEType(type))
return new TextDocument(this, frame);
#if ENABLE(SVG)
if (type == "image/svg+xml") {
Settings* settings = frame ? frame->settings() : 0;
if (!settings || !settings->usesDashboardBackwardCompatibilityMode())
return new SVGDocument(this, frame);
}
#endif
if (isXMLMIMEType(type))
return new Document(this, frame);
return new HTMLDocument(this, frame);
}
}