• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if ENABLE(WML)
60 #include "WMLNames.h"
61 #include "WMLDocument.h"
62 #endif
63 
64 namespace WebCore {
65 
66 #if ENABLE(SVG)
67 
68 typedef HashSet<String, CaseFoldingHash> FeatureSet;
69 
addString(FeatureSet & set,const char * string)70 static void addString(FeatureSet& set, const char* string)
71 {
72     set.add(string);
73 }
74 
isSVG10Feature(const String & feature)75 static bool isSVG10Feature(const String &feature)
76 {
77     static bool initialized = false;
78     DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
79     if (!initialized) {
80 #if ENABLE(SVG_USE) && ENABLE(SVG_FOREIGN_OBJECT) && ENABLE(FILTERS) && ENABLE(SVG_FONTS)
81         addString(svgFeatures, "svg");
82         addString(svgFeatures, "svg.static");
83 #endif
84 //      addString(svgFeatures, "svg.animation");
85 //      addString(svgFeatures, "svg.dynamic");
86 //      addString(svgFeatures, "svg.dom.animation");
87 //      addString(svgFeatures, "svg.dom.dynamic");
88 #if ENABLE(SVG_USE) && ENABLE(SVG_FOREIGN_OBJECT) && ENABLE(FILTERS) && ENABLE(SVG_FONTS)
89         addString(svgFeatures, "dom");
90         addString(svgFeatures, "dom.svg");
91         addString(svgFeatures, "dom.svg.static");
92 #endif
93 //      addString(svgFeatures, "svg.all");
94 //      addString(svgFeatures, "dom.svg.all");
95         initialized = true;
96     }
97     return svgFeatures.contains(feature);
98 }
99 
isSVG11Feature(const String & feature)100 static bool isSVG11Feature(const String &feature)
101 {
102     static bool initialized = false;
103     DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
104     if (!initialized) {
105         // Sadly, we cannot claim to implement any of the SVG 1.1 generic feature sets
106         // lack of Font and Filter support.
107         // http://bugs.webkit.org/show_bug.cgi?id=15480
108 #if ENABLE(SVG_USE) && ENABLE(SVG_FOREIGN_OBJECT) && ENABLE(FILTERS) && ENABLE(SVG_FONTS)
109         addString(svgFeatures, "SVG");
110         addString(svgFeatures, "SVGDOM");
111         addString(svgFeatures, "SVG-static");
112         addString(svgFeatures, "SVGDOM-static");
113 #endif
114 #if ENABLE(SVG_ANIMATION)
115         addString(svgFeatures, "SVG-animation");
116         addString(svgFeatures, "SVGDOM-animation");
117 #endif
118 //      addString(svgFeatures, "SVG-dynamic);
119 //      addString(svgFeatures, "SVGDOM-dynamic);
120         addString(svgFeatures, "CoreAttribute");
121 #if ENABLE(SVG_USE)
122         addString(svgFeatures, "Structure");
123         addString(svgFeatures, "BasicStructure");
124 #endif
125         addString(svgFeatures, "ContainerAttribute");
126         addString(svgFeatures, "ConditionalProcessing");
127         addString(svgFeatures, "Image");
128         addString(svgFeatures, "Style");
129         addString(svgFeatures, "ViewportAttribute");
130         addString(svgFeatures, "Shape");
131 //      addString(svgFeatures, "Text"); // requires altGlyph, bug 6426
132         addString(svgFeatures, "BasicText");
133         addString(svgFeatures, "PaintAttribute");
134         addString(svgFeatures, "BasicPaintAttribute");
135         addString(svgFeatures, "OpacityAttribute");
136         addString(svgFeatures, "GraphicsAttribute");
137         addString(svgFeatures, "BaseGraphicsAttribute");
138         addString(svgFeatures, "Marker");
139 //      addString(svgFeatures, "ColorProfile"); // requires color-profile, bug 6037
140         addString(svgFeatures, "Gradient");
141         addString(svgFeatures, "Pattern");
142         addString(svgFeatures, "Clip");
143         addString(svgFeatures, "BasicClip");
144         addString(svgFeatures, "Mask");
145 #if ENABLE(FILTERS)
146 //      addString(svgFeatures, "Filter");
147         addString(svgFeatures, "BasicFilter");
148 #endif
149         addString(svgFeatures, "DocumentEventsAttribute");
150         addString(svgFeatures, "GraphicalEventsAttribute");
151 //      addString(svgFeatures, "AnimationEventsAttribute");
152         addString(svgFeatures, "Cursor");
153         addString(svgFeatures, "Hyperlinking");
154         addString(svgFeatures, "XlinkAttribute");
155         addString(svgFeatures, "ExternalResourcesRequired");
156 //      addString(svgFeatures, "View"); // buggy <view> support, bug 16962
157         addString(svgFeatures, "Script");
158 #if ENABLE(SVG_ANIMATION)
159         addString(svgFeatures, "Animation");
160 #endif
161 #if ENABLE(SVG_FONTS)
162         addString(svgFeatures, "Font");
163         addString(svgFeatures, "BasicFont");
164 #endif
165 #if ENABLE(SVG_FOREIGN_OBJECT)
166         addString(svgFeatures, "Extensibility");
167 #endif
168         initialized = true;
169     }
170     return svgFeatures.contains(feature);
171 }
172 #endif
173 
DOMImplementation(Document * ownerDocument)174 DOMImplementation::DOMImplementation(Document* ownerDocument)
175     : m_ownerDocument(ownerDocument)
176 {
177     ASSERT(m_ownerDocument);
178 }
179 
hasFeature(const String & feature,const String & version)180 bool DOMImplementation::hasFeature(const String& feature, const String& version)
181 {
182     String lower = feature.lower();
183     if (lower == "core" || lower == "html" || lower == "xml" || lower == "xhtml")
184         return version.isEmpty() || version == "1.0" || version == "2.0";
185     if (lower == "css"
186             || lower == "css2"
187             || lower == "events"
188             || lower == "htmlevents"
189             || lower == "mouseevents"
190             || lower == "mutationevents"
191             || lower == "range"
192             || lower == "stylesheets"
193             || lower == "traversal"
194             || lower == "uievents"
195             || lower == "views")
196         return version.isEmpty() || version == "2.0";
197     if (lower == "xpath" || lower == "textevents")
198         return version.isEmpty() || version == "3.0";
199 
200 #if ENABLE(SVG)
201     if ((version.isEmpty() || version == "1.1") && feature.startsWith("http://www.w3.org/tr/svg11/feature#", false)) {
202         if (isSVG11Feature(feature.right(feature.length() - 35)))
203             return true;
204     }
205 
206     if ((version.isEmpty() || version == "1.0") && feature.startsWith("org.w3c.", false)) {
207         if (isSVG10Feature(feature.right(feature.length() - 8)))
208             return true;
209     }
210 #endif
211 
212     return false;
213 }
214 
createDocumentType(const String & qualifiedName,const String & publicId,const String & systemId,ExceptionCode & ec)215 PassRefPtr<DocumentType> DOMImplementation::createDocumentType(const String& qualifiedName,
216     const String& publicId, const String& systemId, ExceptionCode& ec)
217 {
218     String prefix, localName;
219     if (!Document::parseQualifiedName(qualifiedName, prefix, localName, ec))
220         return 0;
221 
222     return DocumentType::create(0, qualifiedName, publicId, systemId);
223 }
224 
getInterface(const String &)225 DOMImplementation* DOMImplementation::getInterface(const String& /*feature*/)
226 {
227     return 0;
228 }
229 
createDocument(const String & namespaceURI,const String & qualifiedName,DocumentType * doctype,ExceptionCode & ec)230 PassRefPtr<Document> DOMImplementation::createDocument(const String& namespaceURI,
231     const String& qualifiedName, DocumentType* doctype, ExceptionCode& ec)
232 {
233     RefPtr<Document> doc;
234 #if ENABLE(SVG)
235     if (namespaceURI == SVGNames::svgNamespaceURI)
236         doc = SVGDocument::create(0, KURL());
237     else
238 #endif
239 #if ENABLE(WML)
240     if (namespaceURI == WMLNames::wmlNamespaceURI)
241         doc = WMLDocument::create(0, KURL());
242     else
243 #endif
244     if (namespaceURI == HTMLNames::xhtmlNamespaceURI)
245         doc = Document::createXHTML(0, KURL());
246     else
247         doc = Document::create(0, KURL());
248 
249     if (!m_ownerDocument) {
250         ec = INVALID_STATE_ERR;
251         return 0;
252     }
253     doc->setSecurityOrigin(m_ownerDocument->securityOrigin());
254 
255     RefPtr<Node> documentElement;
256     if (!qualifiedName.isEmpty()) {
257         documentElement = doc->createElementNS(namespaceURI, qualifiedName, ec);
258         if (ec)
259             return 0;
260     }
261 
262     // WRONG_DOCUMENT_ERR: Raised if doctype has already been used with a different document or was
263     // created from a different implementation.
264     // Hixie's interpretation of the DOM Core spec suggests we should prefer
265     // other exceptions to WRONG_DOCUMENT_ERR (based on order mentioned in spec),
266     // but this matches the new DOM Core spec (http://www.w3.org/TR/domcore/).
267     if (doctype && doctype->document()) {
268         ec = WRONG_DOCUMENT_ERR;
269         return 0;
270     }
271 
272     // FIXME: Shouldn't this call appendChild instead?
273     if (doctype)
274         doc->parserAddChild(doctype);
275     if (documentElement)
276         doc->parserAddChild(documentElement.release());
277 
278     return doc.release();
279 }
280 
createCSSStyleSheet(const String &,const String & media,ExceptionCode &)281 PassRefPtr<CSSStyleSheet> DOMImplementation::createCSSStyleSheet(const String&, const String& media, ExceptionCode&)
282 {
283     // FIXME: Title should be set.
284     // FIXME: Media could have wrong syntax, in which case we should generate an exception.
285     RefPtr<CSSStyleSheet> sheet = CSSStyleSheet::create();
286     sheet->setMedia(MediaList::createAllowingDescriptionSyntax(sheet.get(), media));
287     return sheet.release();
288 }
289 
isXMLMIMEType(const String & mimeType)290 bool DOMImplementation::isXMLMIMEType(const String& mimeType)
291 {
292     if (mimeType == "text/xml" || mimeType == "application/xml" || mimeType == "text/xsl")
293         return true;
294     static const char* const validChars = "[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]"; // per RFCs: 3023, 2045
295     DEFINE_STATIC_LOCAL(RegularExpression, xmlTypeRegExp, (String("^") + validChars + "+/" + validChars + "+\\+xml$", TextCaseSensitive));
296     return xmlTypeRegExp.match(mimeType) > -1;
297 }
298 
isTextMIMEType(const String & mimeType)299 bool DOMImplementation::isTextMIMEType(const String& mimeType)
300 {
301     if (MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType)
302         || mimeType == "application/json" // Render JSON as text/plain.
303         || (mimeType.startsWith("text/") && mimeType != "text/html"
304             && mimeType != "text/xml" && mimeType != "text/xsl"))
305         return true;
306 
307     return false;
308 }
309 
createHTMLDocument(const String & title)310 PassRefPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
311 {
312     RefPtr<HTMLDocument> d = HTMLDocument::create(0, KURL());
313     d->open();
314     d->write("<!doctype html><html><body></body></html>");
315     d->setTitle(title);
316     ASSERT(m_ownerDocument);
317     if (m_ownerDocument)
318         d->setSecurityOrigin(m_ownerDocument->securityOrigin());
319     return d.release();
320 }
321 
createDocument(const String & type,Frame * frame,const KURL & url,bool inViewSourceMode)322 PassRefPtr<Document> DOMImplementation::createDocument(const String& type, Frame* frame, const KURL& url, bool inViewSourceMode)
323 {
324     if (inViewSourceMode)
325         return HTMLViewSourceDocument::create(frame, url, type);
326 
327     // Plugins cannot take HTML and XHTML from us, and we don't even need to initialize the plugin database for those.
328     if (type == "text/html")
329         return HTMLDocument::create(frame, url);
330     if (type == "application/xhtml+xml"
331 #if ENABLE(XHTMLMP)
332         || type == "application/vnd.wap.xhtml+xml"
333 #endif
334         )
335         return Document::createXHTML(frame, url);
336 
337 #if ENABLE(WML)
338     if (type == "text/vnd.wap.wml" || type == "application/vnd.wap.wmlc")
339         return WMLDocument::create(frame, url);
340 #endif
341 
342 #if ENABLE(FTPDIR)
343     // Plugins cannot take FTP from us either
344     if (type == "application/x-ftp-directory")
345         return FTPDirectoryDocument::create(frame, url);
346 #endif
347 
348     PluginData* pluginData = 0;
349     if (frame && frame->page() && frame->loader()->subframeLoader()->allowPlugins(NotAboutToInstantiatePlugin))
350         pluginData = frame->page()->pluginData();
351 
352     // PDF is one image type for which a plugin can override built-in support.
353     // We do not want QuickTime to take over all image types, obviously.
354     if ((type == "application/pdf" || type == "text/pdf") && pluginData && pluginData->supportsMimeType(type))
355         return PluginDocument::create(frame, url);
356     if (Image::supportsType(type))
357         return ImageDocument::create(frame, url);
358 
359 #if ENABLE(VIDEO)
360      // Check to see if the type can be played by our MediaPlayer, if so create a MediaDocument
361      if (MediaPlayer::supportsType(ContentType(type)))
362          return MediaDocument::create(frame, url);
363 #endif
364 
365     // Everything else except text/plain can be overridden by plugins. In particular, Adobe SVG Viewer should be used for SVG, if installed.
366     // Disallowing plug-ins to use text/plain prevents plug-ins from hijacking a fundamental type that the browser is expected to handle,
367     // and also serves as an optimization to prevent loading the plug-in database in the common case.
368     if (type != "text/plain" && pluginData && pluginData->supportsMimeType(type))
369         return PluginDocument::create(frame, url);
370     if (isTextMIMEType(type))
371         return TextDocument::create(frame, url);
372 
373 #if ENABLE(SVG)
374     if (type == "image/svg+xml") {
375 #if ENABLE(DASHBOARD_SUPPORT)
376         Settings* settings = frame ? frame->settings() : 0;
377         if (!settings || !settings->usesDashboardBackwardCompatibilityMode())
378 #endif
379             return SVGDocument::create(frame, url);
380     }
381 #endif
382     if (isXMLMIMEType(type))
383         return Document::create(frame, url);
384 
385     return HTMLDocument::create(frame, url);
386 }
387 
388 }
389