2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6 * Copyright (C) 2006 Samuel Weinig (sam@webkit.org)
7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 #include "DOMImplementation.h"
28 #include "ContentType.h"
29 #include "CSSStyleSheet.h"
30 #include "DocumentType.h"
32 #include "ExceptionCode.h"
34 #include "FrameLoaderClient.h"
35 #include "FTPDirectoryDocument.h"
36 #include "HTMLDocument.h"
37 #include "HTMLNames.h"
38 #include "HTMLViewSourceDocument.h"
40 #include "ImageDocument.h"
41 #include "MediaDocument.h"
42 #include "MediaList.h"
43 #include "MediaPlayer.h"
44 #include "MIMETypeRegistry.h"
46 #include "PluginData.h"
47 #include "PluginDocument.h"
48 #include "RegularExpression.h"
50 #include "TextDocument.h"
52 #include <wtf/StdLibExtras.h>
56 #include "SVGDocument.h"
63 typedef HashSet<String, CaseFoldingHash> FeatureSet;
65 static void addString(FeatureSet& set, const char* string)
70 static bool isSVG10Feature(const String &feature)
72 static bool initialized = false;
73 DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
75 #if ENABLE(FILTERS) && ENABLE(SVG_FONTS)
76 addString(svgFeatures, "svg");
77 addString(svgFeatures, "svg.static");
79 // addString(svgFeatures, "svg.animation");
80 // addString(svgFeatures, "svg.dynamic");
81 // addString(svgFeatures, "svg.dom.animation");
82 // addString(svgFeatures, "svg.dom.dynamic");
83 #if ENABLE(FILTERS) && ENABLE(SVG_FONTS)
84 addString(svgFeatures, "dom");
85 addString(svgFeatures, "dom.svg");
86 addString(svgFeatures, "dom.svg.static");
88 // addString(svgFeatures, "svg.all");
89 // addString(svgFeatures, "dom.svg.all");
92 return svgFeatures.contains(feature);
95 static bool isSVG11Feature(const String &feature)
97 static bool initialized = false;
98 DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
100 // Sadly, we cannot claim to implement any of the SVG 1.1 generic feature sets
101 // lack of Font and Filter support.
102 // http://bugs.webkit.org/show_bug.cgi?id=15480
103 #if ENABLE(FILTERS) && ENABLE(SVG_FONTS)
104 addString(svgFeatures, "SVG");
105 addString(svgFeatures, "SVGDOM");
106 addString(svgFeatures, "SVG-static");
107 addString(svgFeatures, "SVGDOM-static");
109 #if ENABLE(SVG_ANIMATION)
110 addString(svgFeatures, "SVG-animation");
111 addString(svgFeatures, "SVGDOM-animation");
113 // addString(svgFeatures, "SVG-dynamic);
114 // addString(svgFeatures, "SVGDOM-dynamic);
115 addString(svgFeatures, "CoreAttribute");
116 addString(svgFeatures, "Structure");
117 addString(svgFeatures, "BasicStructure");
118 addString(svgFeatures, "ContainerAttribute");
119 addString(svgFeatures, "ConditionalProcessing");
120 addString(svgFeatures, "Image");
121 addString(svgFeatures, "Style");
122 addString(svgFeatures, "ViewportAttribute");
123 addString(svgFeatures, "Shape");
124 // addString(svgFeatures, "Text"); // requires altGlyph, bug 6426
125 addString(svgFeatures, "BasicText");
126 addString(svgFeatures, "PaintAttribute");
127 addString(svgFeatures, "BasicPaintAttribute");
128 addString(svgFeatures, "OpacityAttribute");
129 addString(svgFeatures, "GraphicsAttribute");
130 addString(svgFeatures, "BaseGraphicsAttribute");
131 addString(svgFeatures, "Marker");
132 // addString(svgFeatures, "ColorProfile"); // requires color-profile, bug 6037
133 addString(svgFeatures, "Gradient");
134 addString(svgFeatures, "Pattern");
135 addString(svgFeatures, "Clip");
136 addString(svgFeatures, "BasicClip");
137 addString(svgFeatures, "Mask");
139 // addString(svgFeatures, "Filter");
140 addString(svgFeatures, "BasicFilter");
142 addString(svgFeatures, "DocumentEventsAttribute");
143 addString(svgFeatures, "GraphicalEventsAttribute");
144 // addString(svgFeatures, "AnimationEventsAttribute");
145 addString(svgFeatures, "Cursor");
146 addString(svgFeatures, "Hyperlinking");
147 addString(svgFeatures, "XlinkAttribute");
148 addString(svgFeatures, "ExternalResourcesRequired");
149 // addString(svgFeatures, "View"); // buggy <view> support, bug 16962
150 addString(svgFeatures, "Script");
151 #if ENABLE(SVG_ANIMATION)
152 addString(svgFeatures, "Animation");
154 #if ENABLE(SVG_FONTS)
155 addString(svgFeatures, "Font");
156 addString(svgFeatures, "BasicFont");
158 addString(svgFeatures, "Extensibility");
161 return svgFeatures.contains(feature);
165 DOMImplementation::DOMImplementation(Document* document)
166 : m_document(document)
170 bool DOMImplementation::hasFeature(const String& feature, const String& version)
172 String lower = feature.lower();
173 if (lower == "core" || lower == "html" || lower == "xml" || lower == "xhtml")
174 return version.isEmpty() || version == "1.0" || version == "2.0";
178 || lower == "htmlevents"
179 || lower == "mouseevents"
180 || lower == "mutationevents"
182 || lower == "stylesheets"
183 || lower == "traversal"
184 || lower == "uievents"
186 return version.isEmpty() || version == "2.0";
187 if (lower == "xpath" || lower == "textevents")
188 return version.isEmpty() || version == "3.0";
191 if ((version.isEmpty() || version == "1.1") && feature.startsWith("http://www.w3.org/tr/svg11/feature#", false)) {
192 if (isSVG11Feature(feature.right(feature.length() - 35)))
196 if ((version.isEmpty() || version == "1.0") && feature.startsWith("org.w3c.", false)) {
197 if (isSVG10Feature(feature.right(feature.length() - 8)))
205 PassRefPtr<DocumentType> DOMImplementation::createDocumentType(const String& qualifiedName,
206 const String& publicId, const String& systemId, ExceptionCode& ec)
208 String prefix, localName;
209 if (!Document::parseQualifiedName(qualifiedName, prefix, localName, ec))
212 return DocumentType::create(0, qualifiedName, publicId, systemId);
215 DOMImplementation* DOMImplementation::getInterface(const String& /*feature*/)
220 PassRefPtr<Document> DOMImplementation::createDocument(const String& namespaceURI,
221 const String& qualifiedName, DocumentType* doctype, ExceptionCode& ec)
223 RefPtr<Document> doc;
225 if (namespaceURI == SVGNames::svgNamespaceURI)
226 doc = SVGDocument::create(0, KURL());
229 if (namespaceURI == HTMLNames::xhtmlNamespaceURI)
230 doc = Document::createXHTML(0, KURL());
232 doc = Document::create(0, KURL());
234 doc->setSecurityOrigin(m_document->securityOrigin());
236 RefPtr<Node> documentElement;
237 if (!qualifiedName.isEmpty()) {
238 documentElement = doc->createElementNS(namespaceURI, qualifiedName, ec);
243 // WRONG_DOCUMENT_ERR: Raised if doctype has already been used with a different document or was
244 // created from a different implementation.
245 // Hixie's interpretation of the DOM Core spec suggests we should prefer
246 // other exceptions to WRONG_DOCUMENT_ERR (based on order mentioned in spec),
247 // but this matches the new DOM Core spec (http://www.w3.org/TR/domcore/).
248 if (doctype && doctype->document()) {
249 ec = WRONG_DOCUMENT_ERR;
253 // FIXME: Shouldn't this call appendChild instead?
255 doc->parserAddChild(doctype);
257 doc->parserAddChild(documentElement.release());
259 return doc.release();
262 PassRefPtr<CSSStyleSheet> DOMImplementation::createCSSStyleSheet(const String&, const String& media, ExceptionCode&)
264 // FIXME: Title should be set.
265 // FIXME: Media could have wrong syntax, in which case we should generate an exception.
266 RefPtr<CSSStyleSheet> sheet = CSSStyleSheet::create();
267 sheet->setMedia(MediaList::createAllowingDescriptionSyntax(sheet.get(), media));
268 return sheet.release();
271 bool DOMImplementation::isXMLMIMEType(const String& mimeType)
273 if (mimeType == "text/xml" || mimeType == "application/xml" || mimeType == "text/xsl")
275 static const char* const validChars = "[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]"; // per RFCs: 3023, 2045
276 DEFINE_STATIC_LOCAL(RegularExpression, xmlTypeRegExp, (String("^") + validChars + "+/" + validChars + "+\\+xml$", TextCaseSensitive));
277 return xmlTypeRegExp.match(mimeType) > -1;
280 bool DOMImplementation::isTextMIMEType(const String& mimeType)
282 if (MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType)
283 || mimeType == "application/json" // Render JSON as text/plain.
284 || (mimeType.startsWith("text/") && mimeType != "text/html"
285 && mimeType != "text/xml" && mimeType != "text/xsl"))
291 PassRefPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
293 RefPtr<HTMLDocument> d = HTMLDocument::create(0, KURL());
295 d->write("<!doctype html><html><body></body></html>");
297 d->setSecurityOrigin(m_document->securityOrigin());
301 PassRefPtr<Document> DOMImplementation::createDocument(const String& type, Frame* frame, const KURL& url, bool inViewSourceMode)
303 if (inViewSourceMode)
304 return HTMLViewSourceDocument::create(frame, url, type);
306 // Plugins cannot take HTML and XHTML from us, and we don't even need to initialize the plugin database for those.
307 if (type == "text/html")
308 return HTMLDocument::create(frame, url);
309 if (type == "application/xhtml+xml"
311 || type == "application/vnd.wap.xhtml+xml"
314 return Document::createXHTML(frame, url);
317 // Plugins cannot take FTP from us either
318 if (type == "application/x-ftp-directory")
319 return FTPDirectoryDocument::create(frame, url);
322 PluginData* pluginData = 0;
323 if (frame && frame->page() && frame->loader()->subframeLoader()->allowPlugins(NotAboutToInstantiatePlugin))
324 pluginData = frame->page()->pluginData();
326 // PDF is one image type for which a plugin can override built-in support.
327 // We do not want QuickTime to take over all image types, obviously.
328 if ((type == "application/pdf" || type == "text/pdf") && pluginData && pluginData->supportsMimeType(type))
329 return PluginDocument::create(frame, url);
330 if (Image::supportsType(type))
331 return ImageDocument::create(frame, url);
334 // Check to see if the type can be played by our MediaPlayer, if so create a MediaDocument
335 if (MediaPlayer::supportsType(ContentType(type)))
336 return MediaDocument::create(frame, url);
339 // Everything else except text/plain can be overridden by plugins. In particular, Adobe SVG Viewer should be used for SVG, if installed.
340 // Disallowing plug-ins to use text/plain prevents plug-ins from hijacking a fundamental type that the browser is expected to handle,
341 // and also serves as an optimization to prevent loading the plug-in database in the common case.
342 if (type != "text/plain" && pluginData && pluginData->supportsMimeType(type))
343 return PluginDocument::create(frame, url);
344 if (isTextMIMEType(type))
345 return TextDocument::create(frame, url);
348 if (type == "image/svg+xml") {
349 #if ENABLE(DASHBOARD_SUPPORT)
350 Settings* settings = frame ? frame->settings() : 0;
351 if (!settings || !settings->usesDashboardBackwardCompatibilityMode())
353 return SVGDocument::create(frame, url);
356 if (isXMLMIMEType(type))
357 return Document::create(frame, url);
359 return HTMLDocument::create(frame, url);