Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / DOMImplementation.cpp
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 "core/dom/DOMImplementation.h"
27
28 #include "HTMLNames.h"
29 #include "SVGNames.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"
60
61 namespace WebCore {
62
63 typedef HashSet<String, CaseFoldingHash> FeatureSet;
64
65 static void addString(FeatureSet& set, const char* string)
66 {
67     set.add(string);
68 }
69
70 static bool isSupportedSVG10Feature(const String& feature, const String& version)
71 {
72     if (!version.isEmpty() && version != "1.0")
73         return false;
74
75     static bool initialized = false;
76     DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
77     if (!initialized) {
78 #if ENABLE(SVG_FONTS)
79         addString(svgFeatures, "svg");
80         addString(svgFeatures, "svg.static");
81 #endif
82 //      addString(svgFeatures, "svg.animation");
83 //      addString(svgFeatures, "svg.dynamic");
84 //      addString(svgFeatures, "svg.dom.animation");
85 //      addString(svgFeatures, "svg.dom.dynamic");
86 #if ENABLE(SVG_FONTS)
87         addString(svgFeatures, "dom");
88         addString(svgFeatures, "dom.svg");
89         addString(svgFeatures, "dom.svg.static");
90 #endif
91 //      addString(svgFeatures, "svg.all");
92 //      addString(svgFeatures, "dom.svg.all");
93         initialized = true;
94     }
95     return feature.startsWith("org.w3c.", false)
96         && svgFeatures.contains(feature.right(feature.length() - 8));
97 }
98
99 static bool isSupportedSVG11Feature(const String& feature, const String& version)
100 {
101     if (!version.isEmpty() && version != "1.1")
102         return false;
103
104     static bool initialized = false;
105     DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
106     if (!initialized) {
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");
115 #endif
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");
157 #endif
158         addString(svgFeatures, "Extensibility");
159         initialized = true;
160     }
161     return feature.startsWith("http://www.w3.org/tr/svg11/feature#", false)
162         && svgFeatures.contains(feature.right(feature.length() - 35));
163 }
164
165 DOMImplementation::DOMImplementation(Document& document)
166     : m_document(document)
167 {
168     ScriptWrappable::init(this);
169 }
170
171 bool DOMImplementation::hasFeature(const String& feature, const String& version)
172 {
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);
178     }
179     return true;
180 }
181
182 bool DOMImplementation::hasFeatureForBindings(const String& feature, const String& version)
183 {
184     if (!hasFeature(feature, version)) {
185         UseCounter::count(m_document, UseCounter::DOMImplementationHasFeatureReturnFalse);
186         return false;
187     }
188     return true;
189 }
190
191 PassRefPtr<DocumentType> DOMImplementation::createDocumentType(const AtomicString& qualifiedName,
192     const String& publicId, const String& systemId, ExceptionState& exceptionState)
193 {
194     AtomicString prefix, localName;
195     if (!Document::parseQualifiedName(qualifiedName, prefix, localName, exceptionState))
196         return 0;
197
198     return DocumentType::create(&m_document, qualifiedName, publicId, systemId);
199 }
200
201 DOMImplementation* DOMImplementation::getInterface(const String& /*feature*/)
202 {
203     return 0;
204 }
205
206 PassRefPtr<XMLDocument> DOMImplementation::createDocument(const AtomicString& namespaceURI,
207     const AtomicString& qualifiedName, DocumentType* doctype, ExceptionState& exceptionState)
208 {
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()));
217     } else {
218         doc = XMLDocument::create(init);
219     }
220
221     doc->setSecurityOrigin(m_document.securityOrigin()->isolatedCopy());
222     doc->setContextFeatures(m_document.contextFeatures());
223
224     RefPtr<Node> documentElement;
225     if (!qualifiedName.isEmpty()) {
226         documentElement = doc->createElementNS(namespaceURI, qualifiedName, exceptionState);
227         if (exceptionState.hadException())
228             return 0;
229     }
230
231     if (doctype)
232         doc->appendChild(doctype);
233     if (documentElement)
234         doc->appendChild(documentElement.release());
235
236     return doc.release();
237 }
238
239 PassRefPtr<CSSStyleSheet> DOMImplementation::createCSSStyleSheet(const String&, const String& media)
240 {
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));
245     return sheet;
246 }
247
248 bool DOMImplementation::isXMLMIMEType(const String& mimeType)
249 {
250     if (equalIgnoringCase(mimeType, "text/xml")
251         || equalIgnoringCase(mimeType, "application/xml")
252         || equalIgnoringCase(mimeType, "text/xsl"))
253         return true;
254
255     // Per RFCs 3023 and 2045, an XML MIME type is of the form:
256     // ^[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+/[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+\+xml$
257
258     int length = mimeType.length();
259     if (length < 7)
260         return false;
261
262     if (mimeType[0] == '/' || mimeType[length - 5] == '/' || !mimeType.endsWith("+xml", false))
263         return false;
264
265     bool hasSlash = false;
266     for (int i = 0; i < length - 4; ++i) {
267         UChar ch = mimeType[i];
268         if (ch >= '0' && ch <= '9')
269             continue;
270         if (ch >= 'a' && ch <= 'z')
271             continue;
272         if (ch >= 'A' && ch <= 'Z')
273             continue;
274         switch (ch) {
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         case '&':
291         case '*':
292             continue;
293         case '/':
294             if (hasSlash)
295                 return false;
296             hasSlash = true;
297             continue;
298         default:
299             return false;
300         }
301     }
302
303     return true;
304 }
305
306 bool DOMImplementation::isJSONMIMEType(const String& mimeType)
307 {
308     if (mimeType.startsWith("application/json", false))
309         return true;
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]);
318             }
319             return parameterMarker > subtype;
320         }
321     }
322     return false;
323 }
324
325 static bool isTextPlainType(const String& mimeType)
326 {
327     return mimeType.startsWith("text/", false)
328         && !(equalIgnoringCase(mimeType, "text/html")
329             || equalIgnoringCase(mimeType, "text/xml")
330             || equalIgnoringCase(mimeType, "text/xsl"));
331 }
332
333 bool DOMImplementation::isTextMIMEType(const String& mimeType)
334 {
335     return MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType) || isJSONMIMEType(mimeType) || isTextPlainType(mimeType);
336 }
337
338 PassRefPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
339 {
340     DocumentInit init = DocumentInit::fromContext(m_document.contextDocument())
341         .withRegistrationContext(m_document.registrationContext());
342     RefPtr<HTMLDocument> d = HTMLDocument::create(init);
343     d->open();
344     d->write("<!doctype html><html><body></body></html>");
345     if (!title.isNull())
346         d->setTitle(title);
347     d->setSecurityOrigin(m_document.securityOrigin()->isolatedCopy());
348     d->setContextFeatures(m_document.contextFeatures());
349     return d.release();
350 }
351
352 PassRefPtr<Document> DOMImplementation::createDocument(const String& type, Frame* frame, const KURL& url, bool inViewSourceMode)
353 {
354     return createDocument(type, DocumentInit(url, frame), inViewSourceMode);
355 }
356
357 PassRefPtr<Document> DOMImplementation::createDocument(const String& type, const DocumentInit& init, bool inViewSourceMode)
358 {
359     if (inViewSourceMode)
360         return HTMLViewSourceDocument::create(init, type);
361
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);
367
368     PluginData* pluginData = 0;
369     if (init.frame() && init.frame()->page() && init.frame()->loader().allowPlugins(NotAboutToInstantiatePlugin))
370         pluginData = init.frame()->page()->pluginData();
371
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);
378
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);
382
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);
394
395     return HTMLDocument::create(init);
396 }
397
398 }