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 "core/dom/DOMImplementation.h"
28 #include "HTMLNames.h"
30 #include "bindings/v8/ExceptionState.h"
31 #include "core/css/CSSStyleSheet.h"
32 #include "core/css/MediaList.h"
33 #include "core/css/StyleSheetContents.h"
34 #include "core/dom/ContextFeatures.h"
35 #include "core/dom/DocumentInit.h"
36 #include "core/dom/DocumentType.h"
37 #include "core/dom/Element.h"
38 #include "core/dom/ExceptionCode.h"
39 #include "core/dom/XMLDocument.h"
40 #include "core/dom/custom/CustomElementRegistrationContext.h"
41 #include "core/html/HTMLDocument.h"
42 #include "core/html/HTMLMediaElement.h"
43 #include "core/html/HTMLViewSourceDocument.h"
44 #include "core/html/ImageDocument.h"
45 #include "core/html/MediaDocument.h"
46 #include "core/html/PluginDocument.h"
47 #include "core/html/TextDocument.h"
48 #include "core/loader/FrameLoader.h"
49 #include "core/frame/Frame.h"
50 #include "core/frame/UseCounter.h"
51 #include "core/page/Page.h"
52 #include "core/svg/SVGDocument.h"
53 #include "platform/ContentType.h"
54 #include "platform/MIMETypeRegistry.h"
55 #include "platform/graphics/Image.h"
56 #include "platform/graphics/media/MediaPlayer.h"
57 #include "platform/plugins/PluginData.h"
58 #include "platform/weborigin/SecurityOrigin.h"
59 #include "wtf/StdLibExtras.h"
63 typedef HashSet<String, CaseFoldingHash> FeatureSet;
65 static void addString(FeatureSet& set, const char* string)
70 static bool isSupportedSVG10Feature(const String& feature, const String& version)
72 if (!version.isEmpty() && version != "1.0")
75 static bool initialized = false;
76 DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
79 addString(svgFeatures, "svg");
80 addString(svgFeatures, "svg.static");
82 // addString(svgFeatures, "svg.animation");
83 // addString(svgFeatures, "svg.dynamic");
84 // addString(svgFeatures, "svg.dom.animation");
85 // addString(svgFeatures, "svg.dom.dynamic");
87 addString(svgFeatures, "dom");
88 addString(svgFeatures, "dom.svg");
89 addString(svgFeatures, "dom.svg.static");
91 // addString(svgFeatures, "svg.all");
92 // addString(svgFeatures, "dom.svg.all");
95 return feature.startsWith("org.w3c.", false)
96 && svgFeatures.contains(feature.right(feature.length() - 8));
99 static bool isSupportedSVG11Feature(const String& feature, const String& version)
101 if (!version.isEmpty() && version != "1.1")
104 static bool initialized = false;
105 DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
107 // Sadly, we cannot claim to implement any of the SVG 1.1 generic feature sets
108 // lack of Font and Filter support.
109 // http://bugs.webkit.org/show_bug.cgi?id=15480
110 #if ENABLE(SVG_FONTS)
111 addString(svgFeatures, "SVG");
112 addString(svgFeatures, "SVGDOM");
113 addString(svgFeatures, "SVG-static");
114 addString(svgFeatures, "SVGDOM-static");
116 addString(svgFeatures, "SVG-animation");
117 addString(svgFeatures, "SVGDOM-animation");
118 // addString(svgFeatures, "SVG-dynamic);
119 // addString(svgFeatures, "SVGDOM-dynamic);
120 addString(svgFeatures, "CoreAttribute");
121 addString(svgFeatures, "Structure");
122 addString(svgFeatures, "BasicStructure");
123 addString(svgFeatures, "ContainerAttribute");
124 addString(svgFeatures, "ConditionalProcessing");
125 addString(svgFeatures, "Image");
126 addString(svgFeatures, "Style");
127 addString(svgFeatures, "ViewportAttribute");
128 addString(svgFeatures, "Shape");
129 addString(svgFeatures, "Text");
130 addString(svgFeatures, "BasicText");
131 addString(svgFeatures, "PaintAttribute");
132 addString(svgFeatures, "BasicPaintAttribute");
133 addString(svgFeatures, "OpacityAttribute");
134 addString(svgFeatures, "GraphicsAttribute");
135 addString(svgFeatures, "BaseGraphicsAttribute");
136 addString(svgFeatures, "Marker");
137 // addString(svgFeatures, "ColorProfile"); // requires color-profile, bug 6037
138 addString(svgFeatures, "Gradient");
139 addString(svgFeatures, "Pattern");
140 addString(svgFeatures, "Clip");
141 addString(svgFeatures, "BasicClip");
142 addString(svgFeatures, "Mask");
143 addString(svgFeatures, "Filter");
144 addString(svgFeatures, "BasicFilter");
145 addString(svgFeatures, "DocumentEventsAttribute");
146 addString(svgFeatures, "GraphicalEventsAttribute");
147 // addString(svgFeatures, "AnimationEventsAttribute");
148 addString(svgFeatures, "Cursor");
149 addString(svgFeatures, "Hyperlinking");
150 addString(svgFeatures, "XlinkAttribute");
151 addString(svgFeatures, "View");
152 addString(svgFeatures, "Script");
153 addString(svgFeatures, "Animation");
154 #if ENABLE(SVG_FONTS)
155 addString(svgFeatures, "Font");
156 addString(svgFeatures, "BasicFont");
158 addString(svgFeatures, "Extensibility");
161 return feature.startsWith("http://www.w3.org/tr/svg11/feature#", false)
162 && svgFeatures.contains(feature.right(feature.length() - 35));
165 DOMImplementation::DOMImplementation(Document& document)
166 : m_document(document)
168 ScriptWrappable::init(this);
171 bool DOMImplementation::hasFeature(const String& feature, const String& version)
173 if (feature.startsWith("http://www.w3.org/TR/SVG", false)
174 || feature.startsWith("org.w3c.dom.svg", false)
175 || feature.startsWith("org.w3c.svg", false)) {
176 // FIXME: SVG 2.0 support?
177 return isSupportedSVG10Feature(feature, version) || isSupportedSVG11Feature(feature, version);
182 bool DOMImplementation::hasFeatureForBindings(const String& feature, const String& version)
184 if (!hasFeature(feature, version)) {
185 UseCounter::count(m_document, UseCounter::DOMImplementationHasFeatureReturnFalse);
191 PassRefPtr<DocumentType> DOMImplementation::createDocumentType(const AtomicString& qualifiedName,
192 const String& publicId, const String& systemId, ExceptionState& exceptionState)
194 AtomicString prefix, localName;
195 if (!Document::parseQualifiedName(qualifiedName, prefix, localName, exceptionState))
198 return DocumentType::create(&m_document, qualifiedName, publicId, systemId);
201 DOMImplementation* DOMImplementation::getInterface(const String& /*feature*/)
206 PassRefPtr<XMLDocument> DOMImplementation::createDocument(const AtomicString& namespaceURI,
207 const AtomicString& qualifiedName, DocumentType* doctype, ExceptionState& exceptionState)
209 RefPtr<XMLDocument> doc;
210 DocumentInit init = DocumentInit::fromContext(m_document.contextDocument());
211 if (namespaceURI == SVGNames::svgNamespaceURI) {
212 // FIXME: This should be an XMLDocument as per DOM4 but we need to get rid of SVGDocument first.
213 // SVGDocument no longer exists in SVG2.
214 doc = SVGDocument::create(init);
215 } else if (namespaceURI == HTMLNames::xhtmlNamespaceURI) {
216 doc = XMLDocument::createXHTML(init.withRegistrationContext(m_document.registrationContext()));
218 doc = XMLDocument::create(init);
221 doc->setSecurityOrigin(m_document.securityOrigin()->isolatedCopy());
222 doc->setContextFeatures(m_document.contextFeatures());
224 RefPtr<Node> documentElement;
225 if (!qualifiedName.isEmpty()) {
226 documentElement = doc->createElementNS(namespaceURI, qualifiedName, exceptionState);
227 if (exceptionState.hadException())
232 doc->appendChild(doctype);
234 doc->appendChild(documentElement.release());
236 return doc.release();
239 PassRefPtr<CSSStyleSheet> DOMImplementation::createCSSStyleSheet(const String&, const String& media)
241 // FIXME: Title should be set.
242 // FIXME: Media could have wrong syntax, in which case we should generate an exception.
243 RefPtr<CSSStyleSheet> sheet = CSSStyleSheet::create(StyleSheetContents::create(strictCSSParserContext()));
244 sheet->setMediaQueries(MediaQuerySet::create(media));
248 bool DOMImplementation::isXMLMIMEType(const String& mimeType)
250 if (equalIgnoringCase(mimeType, "text/xml")
251 || equalIgnoringCase(mimeType, "application/xml")
252 || equalIgnoringCase(mimeType, "text/xsl"))
255 // Per RFCs 3023 and 2045, an XML MIME type is of the form:
256 // ^[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+/[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+\+xml$
258 int length = mimeType.length();
262 if (mimeType[0] == '/' || mimeType[length - 5] == '/' || !mimeType.endsWith("+xml", false))
265 bool hasSlash = false;
266 for (int i = 0; i < length - 4; ++i) {
267 UChar ch = mimeType[i];
268 if (ch >= '0' && ch <= '9')
270 if (ch >= 'a' && ch <= 'z')
272 if (ch >= 'A' && ch <= 'Z')
306 bool DOMImplementation::isJSONMIMEType(const String& mimeType)
308 if (mimeType.startsWith("application/json", false))
310 if (mimeType.startsWith("application/", false)) {
311 size_t subtype = mimeType.find("+json", 12, false);
312 if (subtype != kNotFound) {
313 // Just check that a parameter wasn't matched.
314 size_t parameterMarker = mimeType.find(";");
315 if (parameterMarker == kNotFound) {
316 unsigned endSubtype = static_cast<unsigned>(subtype) + 5;
317 return endSubtype == mimeType.length() || isASCIISpace(mimeType[endSubtype]);
319 return parameterMarker > subtype;
325 static bool isTextPlainType(const String& mimeType)
327 return mimeType.startsWith("text/", false)
328 && !(equalIgnoringCase(mimeType, "text/html")
329 || equalIgnoringCase(mimeType, "text/xml")
330 || equalIgnoringCase(mimeType, "text/xsl"));
333 bool DOMImplementation::isTextMIMEType(const String& mimeType)
335 return MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType) || isJSONMIMEType(mimeType) || isTextPlainType(mimeType);
338 PassRefPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
340 DocumentInit init = DocumentInit::fromContext(m_document.contextDocument())
341 .withRegistrationContext(m_document.registrationContext());
342 RefPtr<HTMLDocument> d = HTMLDocument::create(init);
344 d->write("<!doctype html><html><body></body></html>");
347 d->setSecurityOrigin(m_document.securityOrigin()->isolatedCopy());
348 d->setContextFeatures(m_document.contextFeatures());
352 PassRefPtr<Document> DOMImplementation::createDocument(const String& type, Frame* frame, const KURL& url, bool inViewSourceMode)
354 return createDocument(type, DocumentInit(url, frame), inViewSourceMode);
357 PassRefPtr<Document> DOMImplementation::createDocument(const String& type, const DocumentInit& init, bool inViewSourceMode)
359 if (inViewSourceMode)
360 return HTMLViewSourceDocument::create(init, type);
362 // Plugins cannot take HTML and XHTML from us, and we don't even need to initialize the plugin database for those.
363 if (type == "text/html")
364 return HTMLDocument::create(init);
365 if (type == "application/xhtml+xml")
366 return XMLDocument::createXHTML(init);
368 PluginData* pluginData = 0;
369 if (init.frame() && init.frame()->page() && init.frame()->loader().allowPlugins(NotAboutToInstantiatePlugin))
370 pluginData = init.frame()->page()->pluginData();
372 // PDF is one image type for which a plugin can override built-in support.
373 // We do not want QuickTime to take over all image types, obviously.
374 if ((type == "application/pdf" || type == "text/pdf") && pluginData && pluginData->supportsMimeType(type))
375 return PluginDocument::create(init);
376 if (Image::supportsType(type))
377 return ImageDocument::create(init);
379 // Check to see if the type can be played by our MediaPlayer, if so create a MediaDocument
380 if (HTMLMediaElement::supportsType(ContentType(type)))
381 return MediaDocument::create(init);
383 // Everything else except text/plain can be overridden by plugins. In particular, Adobe SVG Viewer should be used for SVG, if installed.
384 // Disallowing plug-ins to use text/plain prevents plug-ins from hijacking a fundamental type that the browser is expected to handle,
385 // and also serves as an optimization to prevent loading the plug-in database in the common case.
386 if (type != "text/plain" && pluginData && pluginData->supportsMimeType(type))
387 return PluginDocument::create(init);
388 if (isTextMIMEType(type))
389 return TextDocument::create(init);
390 if (type == "image/svg+xml")
391 return SVGDocument::create(init);
392 if (isXMLMIMEType(type))
393 return XMLDocument::create(init);
395 return HTMLDocument::create(init);