Update To 11.40.268.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 "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         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");
91         initialized = true;
92     }
93     return feature.startsWith("org.w3c.", false)
94         && svgFeatures.contains(feature.right(feature.length() - 8));
95 }
96
97 static bool isSupportedSVG11Feature(const String& feature, const String& version)
98 {
99     if (!version.isEmpty() && version != "1.1")
100         return false;
101
102     static bool initialized = false;
103     DEFINE_STATIC_LOCAL(FeatureSet, svgFeatures, ());
104     if (!initialized) {
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");
151         initialized = true;
152     }
153     return feature.startsWith("http://www.w3.org/tr/svg11/feature#", false)
154         && svgFeatures.contains(feature.right(feature.length() - 35));
155 }
156
157 DOMImplementation::DOMImplementation(Document& document)
158     : m_document(document)
159 {
160 }
161
162 bool DOMImplementation::hasFeature(const String& feature, const String& version)
163 {
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);
169     }
170     return true;
171 }
172
173 bool DOMImplementation::hasFeatureForBindings(const String& feature, const String& version)
174 {
175     if (!hasFeature(feature, version)) {
176         UseCounter::count(m_document, UseCounter::DOMImplementationHasFeatureReturnFalse);
177         return false;
178     }
179     return true;
180 }
181
182 PassRefPtrWillBeRawPtr<DocumentType> DOMImplementation::createDocumentType(const AtomicString& qualifiedName,
183     const String& publicId, const String& systemId, ExceptionState& exceptionState)
184 {
185     AtomicString prefix, localName;
186     if (!Document::parseQualifiedName(qualifiedName, prefix, localName, exceptionState))
187         return nullptr;
188
189     return DocumentType::create(m_document, qualifiedName, publicId, systemId);
190 }
191
192 PassRefPtrWillBeRawPtr<XMLDocument> DOMImplementation::createDocument(const AtomicString& namespaceURI,
193     const AtomicString& qualifiedName, DocumentType* doctype, ExceptionState& exceptionState)
194 {
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()));
201     } else {
202         doc = XMLDocument::create(init);
203     }
204
205     doc->setSecurityOrigin(document().securityOrigin()->isolatedCopy());
206     doc->setContextFeatures(document().contextFeatures());
207
208     RefPtrWillBeRawPtr<Node> documentElement = nullptr;
209     if (!qualifiedName.isEmpty()) {
210         documentElement = doc->createElementNS(namespaceURI, qualifiedName, exceptionState);
211         if (exceptionState.hadException())
212             return nullptr;
213     }
214
215     if (doctype)
216         doc->appendChild(doctype);
217     if (documentElement)
218         doc->appendChild(documentElement.release());
219
220     return doc.release();
221 }
222
223 bool DOMImplementation::isXMLMIMEType(const String& mimeType)
224 {
225     if (equalIgnoringCase(mimeType, "text/xml")
226         || equalIgnoringCase(mimeType, "application/xml")
227         || equalIgnoringCase(mimeType, "text/xsl"))
228         return true;
229
230     // Per RFCs 3023 and 2045, an XML MIME type is of the form:
231     // ^[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+/[0-9a-zA-Z_\\-+~!$\\^{}|.%'`#&*]+\+xml$
232
233     int length = mimeType.length();
234     if (length < 7)
235         return false;
236
237     if (mimeType[0] == '/' || mimeType[length - 5] == '/' || !mimeType.endsWith("+xml", false))
238         return false;
239
240     bool hasSlash = false;
241     for (int i = 0; i < length - 4; ++i) {
242         UChar ch = mimeType[i];
243         if (ch >= '0' && ch <= '9')
244             continue;
245         if (ch >= 'a' && ch <= 'z')
246             continue;
247         if (ch >= 'A' && ch <= 'Z')
248             continue;
249         switch (ch) {
250         case '_':
251         case '-':
252         case '+':
253         case '~':
254         case '!':
255         case '$':
256         case '^':
257         case '{':
258         case '}':
259         case '|':
260         case '.':
261         case '%':
262         case '\'':
263         case '`':
264         case '#':
265         case '&':
266         case '*':
267             continue;
268         case '/':
269             if (hasSlash)
270                 return false;
271             hasSlash = true;
272             continue;
273         default:
274             return false;
275         }
276     }
277
278     return true;
279 }
280
281 bool DOMImplementation::isJSONMIMEType(const String& mimeType)
282 {
283     if (mimeType.startsWith("application/json", false))
284         return true;
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]);
293             }
294             return parameterMarker > subtype;
295         }
296     }
297     return false;
298 }
299
300 static bool isTextPlainType(const String& mimeType)
301 {
302     return mimeType.startsWith("text/", false)
303         && !(equalIgnoringCase(mimeType, "text/html")
304             || equalIgnoringCase(mimeType, "text/xml")
305             || equalIgnoringCase(mimeType, "text/xsl"));
306 }
307
308 bool DOMImplementation::isTextMIMEType(const String& mimeType)
309 {
310     return MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType) || isJSONMIMEType(mimeType) || isTextPlainType(mimeType);
311 }
312
313 PassRefPtrWillBeRawPtr<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
314 {
315     DocumentInit init = DocumentInit::fromContext(document().contextDocument())
316         .withRegistrationContext(document().registrationContext());
317     RefPtrWillBeRawPtr<HTMLDocument> d = HTMLDocument::create(init);
318     d->open();
319     d->write("<!doctype html><html><head></head><body></body></html>");
320     if (!title.isNull()) {
321         HTMLHeadElement* headElement = d->head();
322         ASSERT(headElement);
323         RefPtrWillBeRawPtr<HTMLTitleElement> titleElement = HTMLTitleElement::create(*d);
324         headElement->appendChild(titleElement);
325         titleElement->appendChild(d->createTextNode(title), ASSERT_NO_EXCEPTION);
326     }
327     d->setSecurityOrigin(document().securityOrigin()->isolatedCopy());
328     d->setContextFeatures(document().contextFeatures());
329     return d.release();
330 }
331
332 PassRefPtrWillBeRawPtr<Document> DOMImplementation::createDocument(const String& type, LocalFrame* frame, const KURL& url, bool inViewSourceMode)
333 {
334     return createDocument(type, DocumentInit(url, frame), inViewSourceMode);
335 }
336
337 PassRefPtrWillBeRawPtr<Document> DOMImplementation::createDocument(const String& type, const DocumentInit& init, bool inViewSourceMode)
338 {
339     if (inViewSourceMode)
340         return HTMLViewSourceDocument::create(init, type);
341
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);
347
348     PluginData* pluginData = 0;
349     if (init.frame() && init.frame()->page() && init.frame()->loader().allowPlugins(NotAboutToInstantiatePlugin))
350         pluginData = init.frame()->page()->pluginData();
351
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);
358
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);
362
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);
374
375     return HTMLDocument::create(init);
376 }
377
378 void DOMImplementation::trace(Visitor* visitor)
379 {
380     visitor->trace(m_document);
381 }
382
383 }