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 "bindings/core/v8/ExceptionState.h"
29 #include "core/HTMLNames.h"
30 #include "core/SVGNames.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/Text.h"
40 #include "core/dom/XMLDocument.h"
41 #include "core/dom/custom/CustomElementRegistrationContext.h"
42 #include "core/frame/LocalFrame.h"
43 #include "core/frame/UseCounter.h"
44 #include "core/html/HTMLDocument.h"
45 #include "core/html/HTMLHeadElement.h"
46 #include "core/html/HTMLMediaElement.h"
47 #include "core/html/HTMLTitleElement.h"
48 #include "core/html/HTMLViewSourceDocument.h"
49 #include "core/html/ImageDocument.h"
50 #include "core/html/MediaDocument.h"
51 #include "core/html/PluginDocument.h"
52 #include "core/html/TextDocument.h"
53 #include "core/loader/FrameLoader.h"
54 #include "core/page/Page.h"
55 #include "platform/ContentType.h"
56 #include "platform/MIMETypeRegistry.h"
57 #include "platform/graphics/Image.h"
58 #include "platform/graphics/media/MediaPlayer.h"
59 #include "platform/plugins/PluginData.h"
60 #include "platform/weborigin/SecurityOrigin.h"
61 #include "wtf/StdLibExtras.h"
65 typedef HashSet<String, CaseFoldingHash> FeatureSet;
67 static void addString(FeatureSet& set, const char* string)
72 static bool isSupportedSVG10Feature(const String& feature, const String& version)
74 if (!version.isEmpty() && version != "1.0")
77 static bool initialized = false;
78 DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
81 addString(svgFeatures, "svg");
82 addString(svgFeatures, "svg.static");
84 // addString(svgFeatures, "svg.animation");
85 // addString(svgFeatures, "svg.dynamic");
86 // addString(svgFeatures, "svg.dom.animation");
87 // addString(svgFeatures, "svg.dom.dynamic");
89 addString(svgFeatures, "dom");
90 addString(svgFeatures, "dom.svg");
91 addString(svgFeatures, "dom.svg.static");
93 // addString(svgFeatures, "svg.all");
94 // addString(svgFeatures, "dom.svg.all");
97 return feature.startsWith("org.w3c.", false)
98 && svgFeatures.contains(feature.right(feature.length() - 8));
101 static bool isSupportedSVG11Feature(const String& feature, const String& version)
103 if (!version.isEmpty() && version != "1.1")
106 static bool initialized = false;
107 DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
109 // Sadly, we cannot claim to implement any of the SVG 1.1 generic feature sets
110 // lack of Font and Filter support.
111 // http://bugs.webkit.org/show_bug.cgi?id=15480
112 #if ENABLE(SVG_FONTS)
113 addString(svgFeatures, "SVG");
114 addString(svgFeatures, "SVGDOM");
115 addString(svgFeatures, "SVG-static");
116 addString(svgFeatures, "SVGDOM-static");
118 addString(svgFeatures, "SVG-animation");
119 addString(svgFeatures, "SVGDOM-animation");
120 // addString(svgFeatures, "SVG-dynamic);
121 // addString(svgFeatures, "SVGDOM-dynamic);
122 addString(svgFeatures, "CoreAttribute");
123 addString(svgFeatures, "Structure");
124 addString(svgFeatures, "BasicStructure");
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");
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");
140 addString(svgFeatures, "Gradient");
141 addString(svgFeatures, "Pattern");
142 addString(svgFeatures, "Clip");
143 addString(svgFeatures, "BasicClip");
144 addString(svgFeatures, "Mask");
145 addString(svgFeatures, "Filter");
146 addString(svgFeatures, "BasicFilter");
147 addString(svgFeatures, "DocumentEventsAttribute");
148 addString(svgFeatures, "GraphicalEventsAttribute");
149 // addString(svgFeatures, "AnimationEventsAttribute");
150 addString(svgFeatures, "Cursor");
151 addString(svgFeatures, "Hyperlinking");
152 addString(svgFeatures, "XlinkAttribute");
153 addString(svgFeatures, "View");
154 addString(svgFeatures, "Script");
155 addString(svgFeatures, "Animation");
156 #if ENABLE(SVG_FONTS)
157 addString(svgFeatures, "Font");
158 addString(svgFeatures, "BasicFont");
160 addString(svgFeatures, "Extensibility");
163 return feature.startsWith("http://www.w3.org/tr/svg11/feature#", false)
164 && svgFeatures.contains(feature.right(feature.length() - 35));
167 DOMImplementation::DOMImplementation(Document& document)
168 : m_document(document)
170 ScriptWrappable::init(this);
173 bool DOMImplementation::hasFeature(const String& feature, const String& version)
175 if (feature.startsWith("http://www.w3.org/TR/SVG", false)
176 || feature.startsWith("org.w3c.dom.svg", false)
177 || feature.startsWith("org.w3c.svg", false)) {
178 // FIXME: SVG 2.0 support?
179 return isSupportedSVG10Feature(feature, version) || isSupportedSVG11Feature(feature, version);
184 bool DOMImplementation::hasFeatureForBindings(const String& feature, const String& version)
186 if (!hasFeature(feature, version)) {
187 UseCounter::count(m_document, UseCounter::DOMImplementationHasFeatureReturnFalse);
193 PassRefPtrWillBeRawPtr<DocumentType> DOMImplementation::createDocumentType(const AtomicString& qualifiedName,
194 const String& publicId, const String& systemId, ExceptionState& exceptionState)
196 AtomicString prefix, localName;
197 if (!Document::parseQualifiedName(qualifiedName, prefix, localName, exceptionState))
200 return DocumentType::create(m_document, qualifiedName, publicId, systemId);
203 PassRefPtrWillBeRawPtr<XMLDocument> DOMImplementation::createDocument(const AtomicString& namespaceURI,
204 const AtomicString& qualifiedName, DocumentType* doctype, ExceptionState& exceptionState)
206 RefPtrWillBeRawPtr<XMLDocument> doc = nullptr;
207 DocumentInit init = DocumentInit::fromContext(document().contextDocument());
208 if (namespaceURI == SVGNames::svgNamespaceURI) {
209 doc = XMLDocument::createSVG(init);
210 } else if (namespaceURI == HTMLNames::xhtmlNamespaceURI) {
211 doc = XMLDocument::createXHTML(init.withRegistrationContext(document().registrationContext()));
213 doc = XMLDocument::create(init);
216 doc->setSecurityOrigin(document().securityOrigin()->isolatedCopy());
217 doc->setContextFeatures(document().contextFeatures());
219 RefPtrWillBeRawPtr<Node> documentElement = nullptr;
220 if (!qualifiedName.isEmpty()) {
221 documentElement = doc->createElementNS(namespaceURI, qualifiedName, exceptionState);
222 if (exceptionState.hadException())
227 doc->appendChild(doctype);
229 doc->appendChild(documentElement.release());
231 return doc.release();
234 bool DOMImplementation::isXMLMIMEType(const String& mimeType)
236 if (equalIgnoringCase(mimeType, "text/xml")
237 || equalIgnoringCase(mimeType, "application/xml")
238 || equalIgnoringCase(mimeType, "text/xsl"))
241 // Per RFCs 3023 and 2045, an XML MIME type is of the form:
242 // ^[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+/[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+\+xml$
244 int length = mimeType.length();
248 if (mimeType[0] == '/' || mimeType[length - 5] == '/' || !mimeType.endsWith("+xml", false))
251 bool hasSlash = false;
252 for (int i = 0; i < length - 4; ++i) {
253 UChar ch = mimeType[i];
254 if (ch >= '0' && ch <= '9')
256 if (ch >= 'a' && ch <= 'z')
258 if (ch >= 'A' && ch <= 'Z')
292 bool DOMImplementation::isJSONMIMEType(const String& mimeType)
294 if (mimeType.startsWith("application/json", false))
296 if (mimeType.startsWith("application/", false)) {
297 size_t subtype = mimeType.find("+json", 12, false);
298 if (subtype != kNotFound) {
299 // Just check that a parameter wasn't matched.
300 size_t parameterMarker = mimeType.find(";");
301 if (parameterMarker == kNotFound) {
302 unsigned endSubtype = static_cast<unsigned>(subtype) + 5;
303 return endSubtype == mimeType.length() || isASCIISpace(mimeType[endSubtype]);
305 return parameterMarker > subtype;
311 static bool isTextPlainType(const String& mimeType)
313 return mimeType.startsWith("text/", false)
314 && !(equalIgnoringCase(mimeType, "text/html")
315 || equalIgnoringCase(mimeType, "text/xml")
316 || equalIgnoringCase(mimeType, "text/xsl"));
319 bool DOMImplementation::isTextMIMEType(const String& mimeType)
321 return MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType) || isJSONMIMEType(mimeType) || isTextPlainType(mimeType);
324 PassRefPtrWillBeRawPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
326 DocumentInit init = DocumentInit::fromContext(document().contextDocument())
327 .withRegistrationContext(document().registrationContext());
328 RefPtrWillBeRawPtr<HTMLDocument> d = HTMLDocument::create(init);
330 d->write("<!doctype html><html><head></head><body></body></html>");
331 if (!title.isNull()) {
332 HTMLHeadElement* headElement = d->head();
334 RefPtrWillBeRawPtr<HTMLTitleElement> titleElement = HTMLTitleElement::create(*d);
335 headElement->appendChild(titleElement);
336 titleElement->appendChild(d->createTextNode(title), ASSERT_NO_EXCEPTION);
338 d->setSecurityOrigin(document().securityOrigin()->isolatedCopy());
339 d->setContextFeatures(document().contextFeatures());
343 PassRefPtrWillBeRawPtr<Document> DOMImplementation::createDocument(const String& type, LocalFrame* frame, const KURL& url, bool inViewSourceMode)
345 return createDocument(type, DocumentInit(url, frame), inViewSourceMode);
348 PassRefPtrWillBeRawPtr<Document> DOMImplementation::createDocument(const String& type, const DocumentInit& init, bool inViewSourceMode)
350 if (inViewSourceMode)
351 return HTMLViewSourceDocument::create(init, type);
353 // Plugins cannot take HTML and XHTML from us, and we don't even need to initialize the plugin database for those.
354 if (type == "text/html")
355 return HTMLDocument::create(init);
356 if (type == "application/xhtml+xml")
357 return XMLDocument::createXHTML(init);
359 PluginData* pluginData = 0;
360 if (init.frame() && init.frame()->page() && init.frame()->loader().allowPlugins(NotAboutToInstantiatePlugin))
361 pluginData = init.frame()->page()->pluginData();
363 // PDF is one image type for which a plugin can override built-in support.
364 // We do not want QuickTime to take over all image types, obviously.
365 if ((type == "application/pdf" || type == "text/pdf") && pluginData && pluginData->supportsMimeType(type))
366 return PluginDocument::create(init);
367 if (Image::supportsType(type))
368 return ImageDocument::create(init);
370 // Check to see if the type can be played by our MediaPlayer, if so create a MediaDocument
371 if (HTMLMediaElement::supportsType(ContentType(type)))
372 return MediaDocument::create(init);
374 // Everything else except text/plain can be overridden by plugins. In particular, Adobe SVG Viewer should be used for SVG, if installed.
375 // Disallowing plug-ins to use text/plain prevents plug-ins from hijacking a fundamental type that the browser is expected to handle,
376 // and also serves as an optimization to prevent loading the plug-in database in the common case.
377 if (type != "text/plain" && pluginData && pluginData->supportsMimeType(type))
378 return PluginDocument::create(init);
379 if (isTextMIMEType(type))
380 return TextDocument::create(init);
381 if (type == "image/svg+xml")
382 return XMLDocument::createSVG(init);
383 if (isXMLMIMEType(type))
384 return XMLDocument::create(init);
386 return HTMLDocument::create(init);
389 void DOMImplementation::trace(Visitor* visitor)
391 visitor->trace(m_document);