DOMImplementation.cpp [plain text]
#include "config.h"
#include "DOMImplementation.h"
#include "ContentType.h"
#include "CSSStyleSheet.h"
#include "DocumentType.h"
#include "Element.h"
#include "ExceptionCode.h"
#include "Frame.h"
#include "FrameLoaderClient.h"
#include "FTPDirectoryDocument.h"
#include "HTMLDocument.h"
#include "HTMLNames.h"
#include "HTMLViewSourceDocument.h"
#include "Image.h"
#include "ImageDocument.h"
#include "MediaDocument.h"
#include "MediaList.h"
#include "MediaPlayer.h"
#include "MIMETypeRegistry.h"
#include "Page.h"
#include "PluginData.h"
#include "PluginDocument.h"
#include "RegularExpression.h"
#include "Settings.h"
#include "TextDocument.h"
#include "ThreadGlobalData.h"
#include "XMLNames.h"
#include <wtf/StdLibExtras.h>
#if ENABLE(SVG)
#include "SVGNames.h"
#include "SVGDocument.h"
#endif
namespace WebCore {
#if ENABLE(SVG)
typedef HashSet<String, CaseFoldingHash> FeatureSet;
static void addString(FeatureSet& set, const char* string)
{
set.add(string);
}
static bool isSVG10Feature(const String &feature)
{
static bool initialized = false;
DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
if (!initialized) {
#if ENABLE(SVG_USE) && ENABLE(SVG_FOREIGN_OBJECT) && ENABLE(FILTERS) && ENABLE(SVG_FONTS)
addString(svgFeatures, "svg");
addString(svgFeatures, "svg.static");
#endif
#if ENABLE(SVG_USE) && ENABLE(SVG_FOREIGN_OBJECT) && ENABLE(FILTERS) && 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;
DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
if (!initialized) {
#if ENABLE(SVG_USE) && ENABLE(SVG_FOREIGN_OBJECT) && ENABLE(FILTERS) && ENABLE(SVG_FONTS)
addString(svgFeatures, "SVG");
addString(svgFeatures, "SVGDOM");
addString(svgFeatures, "SVG-static");
addString(svgFeatures, "SVGDOM-static");
#endif
#if ENABLE(SVG_ANIMATION)
addString(svgFeatures, "SVG-animation");
addString(svgFeatures, "SVGDOM-animation");
#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(FILTERS)
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_ANIMATION)
addString(svgFeatures, "Animation");
#endif
#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(Document* document)
: m_document(document)
{
}
bool DOMImplementation::hasFeature(const String& feature, const String& version)
{
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)
{
String prefix, localName;
if (!Document::parseQualifiedName(qualifiedName, prefix, localName, ec))
return 0;
return DocumentType::create(0, qualifiedName, publicId, systemId);
}
DOMImplementation* DOMImplementation::getInterface(const String& )
{
return 0;
}
PassRefPtr<Document> DOMImplementation::createDocument(const String& namespaceURI,
const String& qualifiedName, DocumentType* doctype, ExceptionCode& ec)
{
RefPtr<Document> doc;
#if ENABLE(SVG)
if (namespaceURI == SVGNames::svgNamespaceURI)
doc = SVGDocument::create(0, KURL());
else
#endif
if (namespaceURI == HTMLNames::xhtmlNamespaceURI)
doc = Document::createXHTML(0, KURL());
else
doc = Document::create(0, KURL());
doc->setSecurityOrigin(m_document->securityOrigin());
RefPtr<Node> documentElement;
if (!qualifiedName.isEmpty()) {
documentElement = doc->createElementNS(namespaceURI, qualifiedName, ec);
if (ec)
return 0;
}
if (doctype && doctype->document()) {
ec = WRONG_DOCUMENT_ERR;
return 0;
}
if (doctype)
doc->parserAddChild(doctype);
if (documentElement)
doc->parserAddChild(documentElement.release());
return doc.release();
}
PassRefPtr<CSSStyleSheet> DOMImplementation::createCSSStyleSheet(const String&, const String& media, ExceptionCode&)
{
RefPtr<CSSStyleSheet> sheet = CSSStyleSheet::create();
sheet->setMedia(MediaList::createAllowingDescriptionSyntax(sheet.get(), media));
return sheet.release();
}
static const char* const validXMLMIMETypeChars = "[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]";
XMLMIMETypeRegExp::XMLMIMETypeRegExp() :
m_regex(adoptPtr(new RegularExpression(WTF::makeString("^", validXMLMIMETypeChars, "+/", validXMLMIMETypeChars, "+\\+xml$"), TextCaseSensitive)))
{
}
XMLMIMETypeRegExp::~XMLMIMETypeRegExp()
{
}
bool XMLMIMETypeRegExp::isXMLMIMEType(const String& mimeType)
{
return m_regex->match(mimeType) > -1;
}
bool DOMImplementation::isXMLMIMEType(const String& mimeType)
{
if (mimeType == "text/xml" || mimeType == "application/xml" || mimeType == "text/xsl")
return true;
return threadGlobalData().xmlTypeRegExp().isXMLMIMEType(mimeType);
}
bool DOMImplementation::isTextMIMEType(const String& mimeType)
{
if (MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType)
|| mimeType == "application/json" || (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 = HTMLDocument::create(0, KURL());
d->open();
d->write("<!doctype html><html><body></body></html>");
d->setTitle(title);
d->setSecurityOrigin(m_document->securityOrigin());
return d.release();
}
PassRefPtr<Document> DOMImplementation::createDocument(const String& type, Frame* frame, const KURL& url, bool inViewSourceMode)
{
if (inViewSourceMode)
return HTMLViewSourceDocument::create(frame, url, type);
if (type == "text/html")
return HTMLDocument::create(frame, url);
if (type == "application/xhtml+xml"
#if ENABLE(XHTMLMP)
|| type == "application/vnd.wap.xhtml+xml"
#endif
)
return Document::createXHTML(frame, url);
#if ENABLE(FTPDIR)
if (type == "application/x-ftp-directory")
return FTPDirectoryDocument::create(frame, url);
#endif
PluginData* pluginData = 0;
if (frame && frame->page() && frame->loader()->subframeLoader()->allowPlugins(NotAboutToInstantiatePlugin))
pluginData = frame->page()->pluginData();
if ((type == "application/pdf" || type == "text/pdf") && pluginData && pluginData->supportsMimeType(type))
return PluginDocument::create(frame, url);
if (Image::supportsType(type))
return ImageDocument::create(frame, url);
if (type != "text/plain" && pluginData && pluginData->supportsMimeType(type))
return PluginDocument::create(frame, url);
if (isTextMIMEType(type))
return TextDocument::create(frame, url);
#if ENABLE(SVG)
if (type == "image/svg+xml") {
#if ENABLE(DASHBOARD_SUPPORT)
Settings* settings = frame ? frame->settings() : 0;
if (!settings || !settings->usesDashboardBackwardCompatibilityMode())
#endif
return SVGDocument::create(frame, url);
}
#endif
if (isXMLMIMEType(type))
return Document::create(frame, url);
return HTMLDocument::create(frame, url);
}
}