initial import
[vuplus_webkit] / Source / WebCore / dom / DOMImplementation.cpp
1 /*
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/)
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #include "config.h"
26 #include "DOMImplementation.h"
27
28 #include "ContentType.h"
29 #include "CSSStyleSheet.h"
30 #include "DocumentType.h"
31 #include "Element.h"
32 #include "ExceptionCode.h"
33 #include "Frame.h"
34 #include "FrameLoaderClient.h"
35 #include "FTPDirectoryDocument.h"
36 #include "HTMLDocument.h"
37 #include "HTMLNames.h"
38 #include "HTMLViewSourceDocument.h"
39 #include "Image.h"
40 #include "ImageDocument.h"
41 #include "MediaDocument.h"
42 #include "MediaList.h"
43 #include "MediaPlayer.h"
44 #include "MIMETypeRegistry.h"
45 #include "Page.h"
46 #include "PluginData.h"
47 #include "PluginDocument.h"
48 #include "RegularExpression.h"
49 #include "Settings.h"
50 #include "TextDocument.h"
51 #include "XMLNames.h"
52 #include <wtf/StdLibExtras.h>
53
54 #if ENABLE(SVG)
55 #include "SVGNames.h"
56 #include "SVGDocument.h"
57 #endif
58
59 namespace WebCore {
60
61 #if ENABLE(SVG)
62
63 typedef HashSet<String, CaseFoldingHash> FeatureSet;
64
65 static void addString(FeatureSet& set, const char* string)
66 {
67     set.add(string);
68 }
69
70 static bool isSVG10Feature(const String &feature)
71 {
72     static bool initialized = false;
73     DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
74     if (!initialized) {
75 #if ENABLE(FILTERS) && ENABLE(SVG_FONTS)
76         addString(svgFeatures, "svg");
77         addString(svgFeatures, "svg.static");
78 #endif
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");
87 #endif
88 //      addString(svgFeatures, "svg.all");
89 //      addString(svgFeatures, "dom.svg.all");
90         initialized = true;
91     }
92     return svgFeatures.contains(feature);
93 }
94
95 static bool isSVG11Feature(const String &feature)
96 {
97     static bool initialized = false;
98     DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
99     if (!initialized) {
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");
108 #endif
109 #if ENABLE(SVG_ANIMATION)
110         addString(svgFeatures, "SVG-animation");
111         addString(svgFeatures, "SVGDOM-animation");
112 #endif
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");
138 #if ENABLE(FILTERS)
139 //      addString(svgFeatures, "Filter");
140         addString(svgFeatures, "BasicFilter");
141 #endif
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"); 
153 #endif
154 #if ENABLE(SVG_FONTS)
155         addString(svgFeatures, "Font");
156         addString(svgFeatures, "BasicFont");
157 #endif
158         addString(svgFeatures, "Extensibility");
159         initialized = true;
160     }
161     return svgFeatures.contains(feature);
162 }
163 #endif
164
165 DOMImplementation::DOMImplementation(Document* document)
166     : m_document(document)
167 {
168 }
169
170 bool DOMImplementation::hasFeature(const String& feature, const String& version)
171 {
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";
175     if (lower == "css"
176             || lower == "css2"
177             || lower == "events"
178             || lower == "htmlevents"
179             || lower == "mouseevents"
180             || lower == "mutationevents"
181             || lower == "range"
182             || lower == "stylesheets"
183             || lower == "traversal"
184             || lower == "uievents"
185             || lower == "views")
186         return version.isEmpty() || version == "2.0";
187     if (lower == "xpath" || lower == "textevents")
188         return version.isEmpty() || version == "3.0";
189
190 #if ENABLE(SVG)
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)))
193             return true;
194     }
195
196     if ((version.isEmpty() || version == "1.0") && feature.startsWith("org.w3c.", false)) {
197         if (isSVG10Feature(feature.right(feature.length() - 8)))
198             return true;
199     }
200 #endif
201     
202     return false;
203 }
204
205 PassRefPtr<DocumentType> DOMImplementation::createDocumentType(const String& qualifiedName,
206     const String& publicId, const String& systemId, ExceptionCode& ec)
207 {
208     String prefix, localName;
209     if (!Document::parseQualifiedName(qualifiedName, prefix, localName, ec))
210         return 0;
211
212     return DocumentType::create(0, qualifiedName, publicId, systemId);
213 }
214
215 DOMImplementation* DOMImplementation::getInterface(const String& /*feature*/)
216 {
217     return 0;
218 }
219
220 PassRefPtr<Document> DOMImplementation::createDocument(const String& namespaceURI,
221     const String& qualifiedName, DocumentType* doctype, ExceptionCode& ec)
222 {
223     RefPtr<Document> doc;
224 #if ENABLE(SVG)
225     if (namespaceURI == SVGNames::svgNamespaceURI)
226         doc = SVGDocument::create(0, KURL());
227     else
228 #endif
229     if (namespaceURI == HTMLNames::xhtmlNamespaceURI)
230         doc = Document::createXHTML(0, KURL());
231     else
232         doc = Document::create(0, KURL());
233
234     doc->setSecurityOrigin(m_document->securityOrigin());
235
236     RefPtr<Node> documentElement;
237     if (!qualifiedName.isEmpty()) {
238         documentElement = doc->createElementNS(namespaceURI, qualifiedName, ec);
239         if (ec)
240             return 0;
241     }
242
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;
250         return 0;
251     }
252
253     // FIXME: Shouldn't this call appendChild instead?
254     if (doctype)
255         doc->parserAddChild(doctype);
256     if (documentElement)
257         doc->parserAddChild(documentElement.release());
258
259     return doc.release();
260 }
261
262 PassRefPtr<CSSStyleSheet> DOMImplementation::createCSSStyleSheet(const String&, const String& media, ExceptionCode&)
263 {
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();
269 }
270
271 bool DOMImplementation::isXMLMIMEType(const String& mimeType)
272 {
273     if (mimeType == "text/xml" || mimeType == "application/xml" || mimeType == "text/xsl")
274         return true;
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;
278 }
279
280 bool DOMImplementation::isTextMIMEType(const String& mimeType)
281 {
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"))
286         return true;
287
288     return false;
289 }
290
291 PassRefPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
292 {
293     RefPtr<HTMLDocument> d = HTMLDocument::create(0, KURL());
294     d->open();
295     d->write("<!doctype html><html><body></body></html>");
296     d->setTitle(title);
297     d->setSecurityOrigin(m_document->securityOrigin());
298     return d.release();
299 }
300
301 PassRefPtr<Document> DOMImplementation::createDocument(const String& type, Frame* frame, const KURL& url, bool inViewSourceMode)
302 {
303     if (inViewSourceMode)
304         return HTMLViewSourceDocument::create(frame, url, type);
305
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"
310 #if ENABLE(XHTMLMP)
311         || type == "application/vnd.wap.xhtml+xml"
312 #endif
313         )
314         return Document::createXHTML(frame, url);
315
316 #if ENABLE(FTPDIR)
317     // Plugins cannot take FTP from us either
318     if (type == "application/x-ftp-directory")
319         return FTPDirectoryDocument::create(frame, url);
320 #endif
321
322     PluginData* pluginData = 0;
323     if (frame && frame->page() && frame->loader()->subframeLoader()->allowPlugins(NotAboutToInstantiatePlugin))
324         pluginData = frame->page()->pluginData();
325
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);
332
333 #if ENABLE(VIDEO)
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);
337 #endif
338
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);
346
347 #if ENABLE(SVG)
348     if (type == "image/svg+xml") {
349 #if ENABLE(DASHBOARD_SUPPORT)    
350         Settings* settings = frame ? frame->settings() : 0;
351         if (!settings || !settings->usesDashboardBackwardCompatibilityMode())
352 #endif
353             return SVGDocument::create(frame, url);
354     }
355 #endif
356     if (isXMLMIMEType(type))
357         return Document::create(frame, url);
358
359     return HTMLDocument::create(frame, url);
360 }
361
362 }