b1b11a694b0b48855f9dd76b38143fd184d973d8
[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 "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"
62
63 namespace blink {
64
65 typedef HashSet<String, CaseFoldingHash> FeatureSet;
66
67 static void addString(FeatureSet& set, const char* string)
68 {
69     set.add(string);
70 }
71
72 static bool isSupportedSVG10Feature(const String& feature, const String& version)
73 {
74     if (!version.isEmpty() && version != "1.0")
75         return false;
76
77     static bool initialized = false;
78     DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
79     if (!initialized) {
80 #if ENABLE(SVG_FONTS)
81         addString(svgFeatures, "svg");
82         addString(svgFeatures, "svg.static");
83 #endif
84 //      addString(svgFeatures, "svg.animation");
85 //      addString(svgFeatures, "svg.dynamic");
86 //      addString(svgFeatures, "svg.dom.animation");
87 //      addString(svgFeatures, "svg.dom.dynamic");
88 #if ENABLE(SVG_FONTS)
89         addString(svgFeatures, "dom");
90         addString(svgFeatures, "dom.svg");
91         addString(svgFeatures, "dom.svg.static");
92 #endif
93 //      addString(svgFeatures, "svg.all");
94 //      addString(svgFeatures, "dom.svg.all");
95         initialized = true;
96     }
97     return feature.startsWith("org.w3c.", false)
98         && svgFeatures.contains(feature.right(feature.length() - 8));
99 }
100
101 static bool isSupportedSVG11Feature(const String& feature, const String& version)
102 {
103     if (!version.isEmpty() && version != "1.1")
104         return false;
105
106     static bool initialized = false;
107     DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
108     if (!initialized) {
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");
117 #endif
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");
159 #endif
160         addString(svgFeatures, "Extensibility");
161         initialized = true;
162     }
163     return feature.startsWith("http://www.w3.org/tr/svg11/feature#", false)
164         && svgFeatures.contains(feature.right(feature.length() - 35));
165 }
166
167 DOMImplementation::DOMImplementation(Document& document)
168     : m_document(document)
169 {
170     ScriptWrappable::init(this);
171 }
172
173 bool DOMImplementation::hasFeature(const String& feature, const String& version)
174 {
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);
180     }
181     return true;
182 }
183
184 bool DOMImplementation::hasFeatureForBindings(const String& feature, const String& version)
185 {
186     if (!hasFeature(feature, version)) {
187         UseCounter::count(m_document, UseCounter::DOMImplementationHasFeatureReturnFalse);
188         return false;
189     }
190     return true;
191 }
192
193 PassRefPtrWillBeRawPtr<DocumentType> DOMImplementation::createDocumentType(const AtomicString& qualifiedName,
194     const String& publicId, const String& systemId, ExceptionState& exceptionState)
195 {
196     AtomicString prefix, localName;
197     if (!Document::parseQualifiedName(qualifiedName, prefix, localName, exceptionState))
198         return nullptr;
199
200     return DocumentType::create(m_document, qualifiedName, publicId, systemId);
201 }
202
203 PassRefPtrWillBeRawPtr<XMLDocument> DOMImplementation::createDocument(const AtomicString& namespaceURI,
204     const AtomicString& qualifiedName, DocumentType* doctype, ExceptionState& exceptionState)
205 {
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()));
212     } else {
213         doc = XMLDocument::create(init);
214     }
215
216     doc->setSecurityOrigin(document().securityOrigin()->isolatedCopy());
217     doc->setContextFeatures(document().contextFeatures());
218
219     RefPtrWillBeRawPtr<Node> documentElement = nullptr;
220     if (!qualifiedName.isEmpty()) {
221         documentElement = doc->createElementNS(namespaceURI, qualifiedName, exceptionState);
222         if (exceptionState.hadException())
223             return nullptr;
224     }
225
226     if (doctype)
227         doc->appendChild(doctype);
228     if (documentElement)
229         doc->appendChild(documentElement.release());
230
231     return doc.release();
232 }
233
234 bool DOMImplementation::isXMLMIMEType(const String& mimeType)
235 {
236     if (equalIgnoringCase(mimeType, "text/xml")
237         || equalIgnoringCase(mimeType, "application/xml")
238         || equalIgnoringCase(mimeType, "text/xsl"))
239         return true;
240
241     // Per RFCs 3023 and 2045, an XML MIME type is of the form:
242     // ^[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+/[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+\+xml$
243
244     int length = mimeType.length();
245     if (length < 7)
246         return false;
247
248     if (mimeType[0] == '/' || mimeType[length - 5] == '/' || !mimeType.endsWith("+xml", false))
249         return false;
250
251     bool hasSlash = false;
252     for (int i = 0; i < length - 4; ++i) {
253         UChar ch = mimeType[i];
254         if (ch >= '0' && ch <= '9')
255             continue;
256         if (ch >= 'a' && ch <= 'z')
257             continue;
258         if (ch >= 'A' && ch <= 'Z')
259             continue;
260         switch (ch) {
261         case '_':
262         case '-':
263         case '+':
264         case '~':
265         case '!':
266         case '$':
267         case '^':
268         case '{':
269         case '}':
270         case '|':
271         case '.':
272         case '%':
273         case '\'':
274         case '`':
275         case '#':
276         case '&':
277         case '*':
278             continue;
279         case '/':
280             if (hasSlash)
281                 return false;
282             hasSlash = true;
283             continue;
284         default:
285             return false;
286         }
287     }
288
289     return true;
290 }
291
292 bool DOMImplementation::isJSONMIMEType(const String& mimeType)
293 {
294     if (mimeType.startsWith("application/json", false))
295         return true;
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]);
304             }
305             return parameterMarker > subtype;
306         }
307     }
308     return false;
309 }
310
311 static bool isTextPlainType(const String& mimeType)
312 {
313     return mimeType.startsWith("text/", false)
314         && !(equalIgnoringCase(mimeType, "text/html")
315             || equalIgnoringCase(mimeType, "text/xml")
316             || equalIgnoringCase(mimeType, "text/xsl"));
317 }
318
319 bool DOMImplementation::isTextMIMEType(const String& mimeType)
320 {
321     return MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType) || isJSONMIMEType(mimeType) || isTextPlainType(mimeType);
322 }
323
324 PassRefPtrWillBeRawPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
325 {
326     DocumentInit init = DocumentInit::fromContext(document().contextDocument())
327         .withRegistrationContext(document().registrationContext());
328     RefPtrWillBeRawPtr<HTMLDocument> d = HTMLDocument::create(init);
329     d->open();
330     d->write("<!doctype html><html><head></head><body></body></html>");
331     if (!title.isNull()) {
332         HTMLHeadElement* headElement = d->head();
333         ASSERT(headElement);
334         RefPtrWillBeRawPtr<HTMLTitleElement> titleElement = HTMLTitleElement::create(*d);
335         headElement->appendChild(titleElement);
336         titleElement->appendChild(d->createTextNode(title), ASSERT_NO_EXCEPTION);
337     }
338     d->setSecurityOrigin(document().securityOrigin()->isolatedCopy());
339     d->setContextFeatures(document().contextFeatures());
340     return d.release();
341 }
342
343 PassRefPtrWillBeRawPtr<Document> DOMImplementation::createDocument(const String& type, LocalFrame* frame, const KURL& url, bool inViewSourceMode)
344 {
345     return createDocument(type, DocumentInit(url, frame), inViewSourceMode);
346 }
347
348 PassRefPtrWillBeRawPtr<Document> DOMImplementation::createDocument(const String& type, const DocumentInit& init, bool inViewSourceMode)
349 {
350     if (inViewSourceMode)
351         return HTMLViewSourceDocument::create(init, type);
352
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);
358
359     PluginData* pluginData = 0;
360     if (init.frame() && init.frame()->page() && init.frame()->loader().allowPlugins(NotAboutToInstantiatePlugin))
361         pluginData = init.frame()->page()->pluginData();
362
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);
369
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);
373
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);
385
386     return HTMLDocument::create(init);
387 }
388
389 void DOMImplementation::trace(Visitor* visitor)
390 {
391     visitor->trace(m_document);
392 }
393
394 }