blob: 14c51fefa766c6eb8489aa2926b7b4a991575439 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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 "core/dom/DOMImplementation.h"
27
28#include "HTMLNames.h"
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010029#include "SVGNames.h"
Ben Murdochdf957042013-08-06 11:01:27 +010030#include "bindings/v8/ExceptionState.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010031#include "core/css/CSSStyleSheet.h"
32#include "core/css/MediaList.h"
33#include "core/css/StyleSheetContents.h"
34#include "core/dom/ContextFeatures.h"
Ben Murdoch02772c62013-07-26 10:21:05 +010035#include "core/dom/DocumentInit.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010036#include "core/dom/DocumentType.h"
37#include "core/dom/Element.h"
38#include "core/dom/ExceptionCode.h"
39#include "core/html/HTMLDocument.h"
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010040#include "core/html/HTMLMediaElement.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010041#include "core/html/HTMLViewSourceDocument.h"
42#include "core/html/ImageDocument.h"
43#include "core/html/MediaDocument.h"
44#include "core/html/PluginDocument.h"
45#include "core/html/TextDocument.h"
46#include "core/loader/FrameLoader.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010047#include "core/page/Frame.h"
48#include "core/page/Page.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010049#include "core/platform/ContentType.h"
50#include "core/platform/MIMETypeRegistry.h"
51#include "core/platform/graphics/Image.h"
52#include "core/platform/graphics/MediaPlayer.h"
53#include "core/plugins/PluginData.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010054#include "core/svg/SVGDocument.h"
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010055#include "weborigin/SecurityOrigin.h"
56#include "wtf/StdLibExtras.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010057
58namespace WebCore {
59
60typedef HashSet<String, CaseFoldingHash> FeatureSet;
61
62static void addString(FeatureSet& set, const char* string)
63{
64 set.add(string);
65}
66
Ben Murdochfff88842013-07-30 15:20:09 +010067static bool isSupportedSVG10Feature(const String& feature, const String& version)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010068{
69 if (!version.isEmpty() && version != "1.0")
70 return false;
71
72 static bool initialized = false;
73 DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
74 if (!initialized) {
75#if 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(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 feature.startsWith("org.w3c.", false)
93 && svgFeatures.contains(feature.right(feature.length() - 8));
94}
95
Ben Murdochfff88842013-07-30 15:20:09 +010096static bool isSupportedSVG11Feature(const String& feature, const String& version)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010097{
98 if (!version.isEmpty() && version != "1.1")
99 return false;
100
101 static bool initialized = false;
102 DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
103 if (!initialized) {
104 // Sadly, we cannot claim to implement any of the SVG 1.1 generic feature sets
105 // lack of Font and Filter support.
106 // http://bugs.webkit.org/show_bug.cgi?id=15480
107#if ENABLE(SVG_FONTS)
108 addString(svgFeatures, "SVG");
109 addString(svgFeatures, "SVGDOM");
110 addString(svgFeatures, "SVG-static");
111 addString(svgFeatures, "SVGDOM-static");
112#endif
113 addString(svgFeatures, "SVG-animation");
114 addString(svgFeatures, "SVGDOM-animation");
115// addString(svgFeatures, "SVG-dynamic);
116// addString(svgFeatures, "SVGDOM-dynamic);
117 addString(svgFeatures, "CoreAttribute");
118 addString(svgFeatures, "Structure");
119 addString(svgFeatures, "BasicStructure");
120 addString(svgFeatures, "ContainerAttribute");
121 addString(svgFeatures, "ConditionalProcessing");
122 addString(svgFeatures, "Image");
123 addString(svgFeatures, "Style");
124 addString(svgFeatures, "ViewportAttribute");
125 addString(svgFeatures, "Shape");
126 addString(svgFeatures, "Text");
127 addString(svgFeatures, "BasicText");
128 addString(svgFeatures, "PaintAttribute");
129 addString(svgFeatures, "BasicPaintAttribute");
130 addString(svgFeatures, "OpacityAttribute");
131 addString(svgFeatures, "GraphicsAttribute");
132 addString(svgFeatures, "BaseGraphicsAttribute");
133 addString(svgFeatures, "Marker");
134// addString(svgFeatures, "ColorProfile"); // requires color-profile, bug 6037
135 addString(svgFeatures, "Gradient");
136 addString(svgFeatures, "Pattern");
137 addString(svgFeatures, "Clip");
138 addString(svgFeatures, "BasicClip");
139 addString(svgFeatures, "Mask");
140 addString(svgFeatures, "Filter");
141 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");
150 addString(svgFeatures, "Script");
Ben Murdoch02772c62013-07-26 10:21:05 +0100151 addString(svgFeatures, "Animation");
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100152#if ENABLE(SVG_FONTS)
153 addString(svgFeatures, "Font");
154 addString(svgFeatures, "BasicFont");
155#endif
156 addString(svgFeatures, "Extensibility");
157 initialized = true;
158 }
159 return feature.startsWith("http://www.w3.org/tr/svg11/feature#", false)
160 && svgFeatures.contains(feature.right(feature.length() - 35));
161}
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100162
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100163DOMImplementation::DOMImplementation(Document* document)
164 : m_document(document)
165{
166 ScriptWrappable::init(this);
167}
168
169bool DOMImplementation::hasFeature(const String& feature, const String& version)
170{
Ben Murdochfff88842013-07-30 15:20:09 +0100171 if (feature.startsWith("http://www.w3.org/TR/SVG", false)
172 || feature.startsWith("org.w3c.dom.svg", false)
173 || feature.startsWith("org.w3c.svg", false)) {
174 // FIXME: SVG 2.0 support?
175 return isSupportedSVG10Feature(feature, version) || isSupportedSVG11Feature(feature, version);
176 }
177 return true;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100178}
179
180PassRefPtr<DocumentType> DOMImplementation::createDocumentType(const String& qualifiedName,
Ben Murdochdf957042013-08-06 11:01:27 +0100181 const String& publicId, const String& systemId, ExceptionState& es)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100182{
183 String prefix, localName;
Ben Murdochdf957042013-08-06 11:01:27 +0100184 if (!Document::parseQualifiedName(qualifiedName, prefix, localName, es))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100185 return 0;
186
187 return DocumentType::create(0, qualifiedName, publicId, systemId);
188}
189
190DOMImplementation* DOMImplementation::getInterface(const String& /*feature*/)
191{
192 return 0;
193}
194
195PassRefPtr<Document> DOMImplementation::createDocument(const String& namespaceURI,
Ben Murdochdf957042013-08-06 11:01:27 +0100196 const String& qualifiedName, DocumentType* doctype, ExceptionState& es)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100197{
198 RefPtr<Document> doc;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100199 if (namespaceURI == SVGNames::svgNamespaceURI)
Ben Murdoche69819b2013-07-17 14:56:49 +0100200 doc = SVGDocument::create();
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100201 else if (namespaceURI == HTMLNames::xhtmlNamespaceURI)
Ben Murdoch02772c62013-07-26 10:21:05 +0100202 doc = Document::createXHTML(DocumentInit().withRegistrationContext(m_document->registrationContext()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100203 else
Ben Murdoche69819b2013-07-17 14:56:49 +0100204 doc = Document::create();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100205
206 doc->setSecurityOrigin(m_document->securityOrigin());
207 doc->setContextFeatures(m_document->contextFeatures());
208
209 RefPtr<Node> documentElement;
210 if (!qualifiedName.isEmpty()) {
Ben Murdochdf957042013-08-06 11:01:27 +0100211 documentElement = doc->createElementNS(namespaceURI, qualifiedName, es);
212 if (es.hadException())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100213 return 0;
214 }
215
Ben Murdoche69819b2013-07-17 14:56:49 +0100216 // WrongDocumentError: Raised if doctype has already been used with a different document or was
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100217 // created from a different implementation.
218 // Hixie's interpretation of the DOM Core spec suggests we should prefer
Ben Murdoche69819b2013-07-17 14:56:49 +0100219 // other exceptions to WrongDocumentError (based on order mentioned in spec),
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100220 // but this matches the new DOM Core spec (http://www.w3.org/TR/domcore/).
221 if (doctype && doctype->document()) {
Ben Murdochdf957042013-08-06 11:01:27 +0100222 es.throwDOMException(WrongDocumentError);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100223 return 0;
224 }
225
226 if (doctype)
227 doc->appendChild(doctype);
228 if (documentElement)
229 doc->appendChild(documentElement.release());
230
231 return doc.release();
232}
233
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100234PassRefPtr<CSSStyleSheet> DOMImplementation::createCSSStyleSheet(const String&, const String& media)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100235{
236 // FIXME: Title should be set.
237 // FIXME: Media could have wrong syntax, in which case we should generate an exception.
238 RefPtr<CSSStyleSheet> sheet = CSSStyleSheet::create(StyleSheetContents::create());
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100239 sheet->setMediaQueries(MediaQuerySet::create(media));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100240 return sheet;
241}
242
243bool DOMImplementation::isXMLMIMEType(const String& mimeType)
244{
245 if (mimeType == "text/xml" || mimeType == "application/xml" || mimeType == "text/xsl")
246 return true;
247
248 // Per RFCs 3023 and 2045 a mime type is of the form:
249 // ^[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+/[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+\+xml$
250
251 int length = mimeType.length();
252 if (length < 7)
253 return false;
254
255 if (mimeType[0] == '/' ||
256 mimeType[length - 5] == '/' ||
257 mimeType[length - 4] != '+' ||
258 mimeType[length - 3] != 'x' ||
259 mimeType[length - 2] != 'm' ||
260 mimeType[length - 1] != 'l')
261 return false;
262
263 bool hasSlash = false;
264 for (int i = 0; i < length - 4; ++i) {
265 UChar ch = mimeType[i];
266 if (ch >= '0' && ch <= '9')
267 continue;
268 if (ch >= 'a' && ch <= 'z')
269 continue;
270 if (ch >= 'A' && ch <= 'Z')
271 continue;
272 switch (ch) {
273 case '_':
274 case '-':
275 case '+':
276 case '~':
277 case '!':
278 case '$':
279 case '^':
280 case '{':
281 case '}':
282 case '|':
283 case '.':
284 case '%':
285 case '\'':
286 case '`':
287 case '#':
288 case '&':
289 case '*':
290 continue;
291 case '/':
292 if (hasSlash)
293 return false;
294 hasSlash = true;
295 continue;
296 default:
297 return false;
298 }
299 }
300
301 return true;
302}
303
304bool DOMImplementation::isTextMIMEType(const String& mimeType)
305{
306 if (MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType)
307 || mimeType == "application/json" // Render JSON as text/plain.
308 || (mimeType.startsWith("text/") && mimeType != "text/html"
309 && mimeType != "text/xml" && mimeType != "text/xsl"))
310 return true;
311
312 return false;
313}
314
315PassRefPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
316{
Ben Murdoch02772c62013-07-26 10:21:05 +0100317 RefPtr<HTMLDocument> d = HTMLDocument::create(DocumentInit().withRegistrationContext(m_document->registrationContext()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100318 d->open();
319 d->write("<!doctype html><html><body></body></html>");
320 if (!title.isNull())
321 d->setTitle(title);
322 d->setSecurityOrigin(m_document->securityOrigin());
323 d->setContextFeatures(m_document->contextFeatures());
324 return d.release();
325}
326
327PassRefPtr<Document> DOMImplementation::createDocument(const String& type, Frame* frame, const KURL& url, bool inViewSourceMode)
328{
329 if (inViewSourceMode)
Ben Murdoche69819b2013-07-17 14:56:49 +0100330 return HTMLViewSourceDocument::create(DocumentInit(url, frame), type);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100331
332 // Plugins cannot take HTML and XHTML from us, and we don't even need to initialize the plugin database for those.
333 if (type == "text/html")
Ben Murdoche69819b2013-07-17 14:56:49 +0100334 return HTMLDocument::create(DocumentInit(url, frame));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100335 if (type == "application/xhtml+xml")
Ben Murdoche69819b2013-07-17 14:56:49 +0100336 return Document::createXHTML(DocumentInit(url, frame));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100337
338 PluginData* pluginData = 0;
Ben Murdochfff88842013-07-30 15:20:09 +0100339 if (frame && frame->page() && frame->loader()->allowPlugins(NotAboutToInstantiatePlugin))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100340 pluginData = frame->page()->pluginData();
341
342 // PDF is one image type for which a plugin can override built-in support.
343 // We do not want QuickTime to take over all image types, obviously.
344 if ((type == "application/pdf" || type == "text/pdf") && pluginData && pluginData->supportsMimeType(type))
Ben Murdoche69819b2013-07-17 14:56:49 +0100345 return PluginDocument::create(DocumentInit(url, frame));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100346 if (Image::supportsType(type))
Ben Murdoche69819b2013-07-17 14:56:49 +0100347 return ImageDocument::create(DocumentInit(url, frame));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100348
Torne (Richard Coles)81a51572013-05-13 16:52:28 +0100349 // Check to see if the type can be played by our MediaPlayer, if so create a MediaDocument
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100350 if (HTMLMediaElement::supportsType(ContentType(type)))
Ben Murdoche69819b2013-07-17 14:56:49 +0100351 return MediaDocument::create(DocumentInit(url, frame));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100352
353 // Everything else except text/plain can be overridden by plugins. In particular, Adobe SVG Viewer should be used for SVG, if installed.
354 // Disallowing plug-ins to use text/plain prevents plug-ins from hijacking a fundamental type that the browser is expected to handle,
355 // and also serves as an optimization to prevent loading the plug-in database in the common case.
Ben Murdoch02772c62013-07-26 10:21:05 +0100356 if (type != "text/plain" && pluginData && pluginData->supportsMimeType(type))
Ben Murdoche69819b2013-07-17 14:56:49 +0100357 return PluginDocument::create(DocumentInit(url, frame));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100358 if (isTextMIMEType(type))
Ben Murdoche69819b2013-07-17 14:56:49 +0100359 return TextDocument::create(DocumentInit(url, frame));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100360 if (type == "image/svg+xml")
Ben Murdoche69819b2013-07-17 14:56:49 +0100361 return SVGDocument::create(DocumentInit(url, frame));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100362 if (isXMLMIMEType(type))
Ben Murdoche69819b2013-07-17 14:56:49 +0100363 return Document::create(DocumentInit(url, frame));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100364
Ben Murdoche69819b2013-07-17 14:56:49 +0100365 return HTMLDocument::create(DocumentInit(url, frame));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100366}
367
368}