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, ());
80 addString(svgFeatures, "svg");
81 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");
86 addString(svgFeatures, "dom");
87 addString(svgFeatures, "dom.svg");
88 addString(svgFeatures, "dom.svg.static");
89 // addString(svgFeatures, "svg.all");
90 // addString(svgFeatures, "dom.svg.all");
93 return feature.startsWith("org.w3c.", false)
94 && svgFeatures.contains(feature.right(feature.length() - 8));
97 static bool isSupportedSVG11Feature(const String& feature, const String& version)
99 if (!version.isEmpty() && version != "1.1")
102 static bool initialized = false;
103 DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
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 addString(svgFeatures, "SVG");
109 addString(svgFeatures, "SVGDOM");
110 addString(svgFeatures, "SVG-static");
111 addString(svgFeatures, "SVGDOM-static");
112 addString(svgFeatures, "SVG-animation");
113 addString(svgFeatures, "SVGDOM-animation");
114 // addString(svgFeatures, "SVG-dynamic);
115 // addString(svgFeatures, "SVGDOM-dynamic);
116 addString(svgFeatures, "CoreAttribute");
117 addString(svgFeatures, "Structure");
118 addString(svgFeatures, "BasicStructure");
119 addString(svgFeatures, "ContainerAttribute");
120 addString(svgFeatures, "ConditionalProcessing");
121 addString(svgFeatures, "Image");
122 addString(svgFeatures, "Style");
123 addString(svgFeatures, "ViewportAttribute");
124 addString(svgFeatures, "Shape");
125 addString(svgFeatures, "Text");
126 addString(svgFeatures, "BasicText");
127 addString(svgFeatures, "PaintAttribute");
128 addString(svgFeatures, "BasicPaintAttribute");
129 addString(svgFeatures, "OpacityAttribute");
130 addString(svgFeatures, "GraphicsAttribute");
131 addString(svgFeatures, "BaseGraphicsAttribute");
132 addString(svgFeatures, "Marker");
133 // addString(svgFeatures, "ColorProfile");
134 addString(svgFeatures, "Gradient");
135 addString(svgFeatures, "Pattern");
136 addString(svgFeatures, "Clip");
137 addString(svgFeatures, "BasicClip");
138 addString(svgFeatures, "Mask");
139 addString(svgFeatures, "Filter");
140 addString(svgFeatures, "BasicFilter");
141 addString(svgFeatures, "DocumentEventsAttribute");
142 addString(svgFeatures, "GraphicalEventsAttribute");
143 // addString(svgFeatures, "AnimationEventsAttribute");
144 addString(svgFeatures, "Cursor");
145 addString(svgFeatures, "Hyperlinking");
146 addString(svgFeatures, "XlinkAttribute");
147 addString(svgFeatures, "View");
148 addString(svgFeatures, "Script");
149 addString(svgFeatures, "Animation");
150 addString(svgFeatures, "Extensibility");
153 return feature.startsWith("http://www.w3.org/tr/svg11/feature#", false)
154 && svgFeatures.contains(feature.right(feature.length() - 35));
157 DOMImplementation::DOMImplementation(Document& document)
158 : m_document(document)
162 bool DOMImplementation::hasFeature(const String& feature, const String& version)
164 if (feature.startsWith("http://www.w3.org/TR/SVG", false)
165 || feature.startsWith("org.w3c.dom.svg", false)
166 || feature.startsWith("org.w3c.svg", false)) {
167 // FIXME: SVG 2.0 support?
168 return isSupportedSVG10Feature(feature, version) || isSupportedSVG11Feature(feature, version);
173 bool DOMImplementation::hasFeatureForBindings(const String& feature, const String& version)
175 if (!hasFeature(feature, version)) {
176 UseCounter::count(m_document, UseCounter::DOMImplementationHasFeatureReturnFalse);
182 PassRefPtrWillBeRawPtr<DocumentType> DOMImplementation::createDocumentType(const AtomicString& qualifiedName,
183 const String& publicId, const String& systemId, ExceptionState& exceptionState)
185 AtomicString prefix, localName;
186 if (!Document::parseQualifiedName(qualifiedName, prefix, localName, exceptionState))
189 return DocumentType::create(m_document, qualifiedName, publicId, systemId);
192 PassRefPtrWillBeRawPtr<XMLDocument> DOMImplementation::createDocument(const AtomicString& namespaceURI,
193 const AtomicString& qualifiedName, DocumentType* doctype, ExceptionState& exceptionState)
195 RefPtrWillBeRawPtr<XMLDocument> doc = nullptr;
196 DocumentInit init = DocumentInit::fromContext(document().contextDocument());
197 if (namespaceURI == SVGNames::svgNamespaceURI) {
198 doc = XMLDocument::createSVG(init);
199 } else if (namespaceURI == HTMLNames::xhtmlNamespaceURI) {
200 doc = XMLDocument::createXHTML(init.withRegistrationContext(document().registrationContext()));
202 doc = XMLDocument::create(init);
205 doc->setSecurityOrigin(document().securityOrigin()->isolatedCopy());
206 doc->setContextFeatures(document().contextFeatures());
208 RefPtrWillBeRawPtr<Node> documentElement = nullptr;
209 if (!qualifiedName.isEmpty()) {
210 documentElement = doc->createElementNS(namespaceURI, qualifiedName, exceptionState);
211 if (exceptionState.hadException())
216 doc->appendChild(doctype);
218 doc->appendChild(documentElement.release());
220 return doc.release();
223 bool DOMImplementation::isXMLMIMEType(const String& mimeType)
225 if (equalIgnoringCase(mimeType, "text/xml")
226 || equalIgnoringCase(mimeType, "application/xml")
227 || equalIgnoringCase(mimeType, "text/xsl"))
230 // Per RFCs 3023 and 2045, an XML MIME type is of the form:
231 // ^[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+/[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+\+xml$
233 int length = mimeType.length();
237 if (mimeType[0] == '/' || mimeType[length - 5] == '/' || !mimeType.endsWith("+xml", false))
240 bool hasSlash = false;
241 for (int i = 0; i < length - 4; ++i) {
242 UChar ch = mimeType[i];
243 if (ch >= '0' && ch <= '9')
245 if (ch >= 'a' && ch <= 'z')
247 if (ch >= 'A' && ch <= 'Z')
281 bool DOMImplementation::isJSONMIMEType(const String& mimeType)
283 if (mimeType.startsWith("application/json", false))
285 if (mimeType.startsWith("application/", false)) {
286 size_t subtype = mimeType.find("+json", 12, false);
287 if (subtype != kNotFound) {
288 // Just check that a parameter wasn't matched.
289 size_t parameterMarker = mimeType.find(";");
290 if (parameterMarker == kNotFound) {
291 unsigned endSubtype = static_cast<unsigned>(subtype) + 5;
292 return endSubtype == mimeType.length() || isASCIISpace(mimeType[endSubtype]);
294 return parameterMarker > subtype;
300 static bool isTextPlainType(const String& mimeType)
302 return mimeType.startsWith("text/", false)
303 && !(equalIgnoringCase(mimeType, "text/html")
304 || equalIgnoringCase(mimeType, "text/xml")
305 || equalIgnoringCase(mimeType, "text/xsl"));
308 bool DOMImplementation::isTextMIMEType(const String& mimeType)
310 return MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType) || isJSONMIMEType(mimeType) || isTextPlainType(mimeType);
313 PassRefPtrWillBeRawPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
315 DocumentInit init = DocumentInit::fromContext(document().contextDocument())
316 .withRegistrationContext(document().registrationContext());
317 RefPtrWillBeRawPtr<HTMLDocument> d = HTMLDocument::create(init);
319 d->write("<!doctype html><html><head></head><body></body></html>");
320 if (!title.isNull()) {
321 HTMLHeadElement* headElement = d->head();
323 RefPtrWillBeRawPtr<HTMLTitleElement> titleElement = HTMLTitleElement::create(*d);
324 headElement->appendChild(titleElement);
325 titleElement->appendChild(d->createTextNode(title), ASSERT_NO_EXCEPTION);
327 d->setSecurityOrigin(document().securityOrigin()->isolatedCopy());
328 d->setContextFeatures(document().contextFeatures());
332 PassRefPtrWillBeRawPtr<Document> DOMImplementation::createDocument(const String& type, LocalFrame* frame, const KURL& url, bool inViewSourceMode)
334 return createDocument(type, DocumentInit(url, frame), inViewSourceMode);
337 PassRefPtrWillBeRawPtr<Document> DOMImplementation::createDocument(const String& type, const DocumentInit& init, bool inViewSourceMode)
339 if (inViewSourceMode)
340 return HTMLViewSourceDocument::create(init, type);
342 // Plugins cannot take HTML and XHTML from us, and we don't even need to initialize the plugin database for those.
343 if (type == "text/html")
344 return HTMLDocument::create(init);
345 if (type == "application/xhtml+xml")
346 return XMLDocument::createXHTML(init);
348 PluginData* pluginData = 0;
349 if (init.frame() && init.frame()->page() && init.frame()->loader().allowPlugins(NotAboutToInstantiatePlugin))
350 pluginData = init.frame()->page()->pluginData();
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(init);
356 if (Image::supportsType(type))
357 return ImageDocument::create(init);
359 // Check to see if the type can be played by our MediaPlayer, if so create a MediaDocument
360 if (HTMLMediaElement::supportsType(ContentType(type)))
361 return MediaDocument::create(init);
363 // Everything else except text/plain can be overridden by plugins. In particular, Adobe SVG Viewer should be used for SVG, if installed.
364 // Disallowing plug-ins to use text/plain prevents plug-ins from hijacking a fundamental type that the browser is expected to handle,
365 // and also serves as an optimization to prevent loading the plug-in database in the common case.
366 if (type != "text/plain" && pluginData && pluginData->supportsMimeType(type))
367 return PluginDocument::create(init);
368 if (isTextMIMEType(type))
369 return TextDocument::create(init);
370 if (type == "image/svg+xml")
371 return XMLDocument::createSVG(init);
372 if (isXMLMIMEType(type))
373 return XMLDocument::create(init);
375 return HTMLDocument::create(init);
378 void DOMImplementation::trace(Visitor* visitor)
380 visitor->trace(m_document);