Replace static_casts with to* functions for document types.
[WebKit-https.git] / Source / WebCore / dom / CustomElementRegistry.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in the documentation and/or other materials provided with the
13  *    distribution.
14  * 3. Neither the name of Google Inc. nor the names of its contributors
15  *    may be used to endorse or promote products derived from this
16  *    software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #if ENABLE(CUSTOM_ELEMENTS)
34
35 #include "CustomElementRegistry.h"
36
37 #include "CustomElementConstructor.h"
38 #include "CustomElementHelpers.h"
39 #include "Dictionary.h"
40 #include "Document.h"
41 #include "Element.h"
42 #include "HTMLNames.h"
43 #include "RuntimeEnabledFeatures.h"
44 #include <wtf/ASCIICType.h>
45
46 #if ENABLE(SVG)
47 #include "SVGNames.h"
48 #endif
49
50 #if ENABLE(MATHML)
51 #include "MathMLNames.h"
52 #endif
53
54 namespace WebCore {
55
56 CustomElementRegistry::CustomElementRegistry(Document* document)
57     : ContextDestructionObserver(document)
58 {
59 }
60
61 CustomElementRegistry::~CustomElementRegistry()
62 {
63 }
64
65 PassRefPtr<CustomElementConstructor> CustomElementRegistry::constructorOf(Element* element)
66 {
67     RefPtr<CustomElementRegistry> self = element->document()->registry();
68     if (!self)
69         return 0;
70     return self->find(element->tagQName());
71 }
72
73 bool CustomElementRegistry::isValidName(const AtomicString& name)
74 {
75     size_t hyphenPosition = name.find('-');
76     if (hyphenPosition == notFound)
77         return false;
78
79     DEFINE_STATIC_LOCAL(Vector<AtomicString>, reservedNames, ());
80     if (reservedNames.isEmpty()) {
81 #if ENABLE(MATHML)
82         reservedNames.append(MathMLNames::annotation_xmlTag.localName());
83 #endif
84
85 #if ENABLE(SVG)
86         reservedNames.append(SVGNames::color_profileTag.localName());
87         reservedNames.append(SVGNames::font_faceTag.localName());
88         reservedNames.append(SVGNames::font_face_srcTag.localName());
89         reservedNames.append(SVGNames::font_face_uriTag.localName());
90         reservedNames.append(SVGNames::font_face_formatTag.localName());
91         reservedNames.append(SVGNames::font_face_nameTag.localName());
92         reservedNames.append(SVGNames::missing_glyphTag.localName());
93 #endif
94     }
95
96     if (notFound != reservedNames.find(name))
97         return false;
98
99     return Document::isValidName(name.string());
100 }
101
102 PassRefPtr<CustomElementConstructor> CustomElementRegistry::registerElement(ScriptState* state, const AtomicString& name, const Dictionary& options, ExceptionCode& ec)
103 {
104     RefPtr<CustomElementRegistry> protect(this);
105
106     if (!CustomElementHelpers::isFeatureAllowed(state))
107         return 0;
108
109     QualifiedName newName(nullAtom, name.lower(), HTMLNames::xhtmlNamespaceURI);
110     if (!isValidName(newName.localName())) {
111         ec = INVALID_CHARACTER_ERR;
112         return 0;
113     }
114         
115     if (find(newName)) {
116         ec = INVALID_STATE_ERR;
117         return 0;
118     }
119
120     ScriptValue prototypeValue;
121     if (!options.get("prototype", prototypeValue)) {
122         // FIXME: Implement the default value handling.
123         // Currently default value of the "prototype" parameter, which
124         // is HTMLSpanElement.prototype, has an ambiguity about its
125         // behavior. The spec should be fixed before WebKit implements
126         // it. https://www.w3.org/Bugs/Public/show_bug.cgi?id=20801
127         ec = INVALID_STATE_ERR;
128         return 0;
129     }
130
131     if (!CustomElementHelpers::isValidPrototypeParameter(prototypeValue, state)) {
132         ec = INVALID_STATE_ERR;
133         return 0;
134     }
135     
136     // An script execution could happen in isValidPrototypeParameter(), which kills the document.
137     if (!document()) {
138         ec = INVALID_STATE_ERR;
139         return 0;
140     }
141
142     RefPtr<CustomElementConstructor> constructor = CustomElementConstructor::create(state, document(), newName, prototypeValue);
143     if (!constructor) {
144         ec = INVALID_STATE_ERR;
145         return 0;
146     }
147         
148     m_constructors.add(constructor->name().impl(), constructor);
149     return constructor;
150 }
151
152 PassRefPtr<CustomElementConstructor> CustomElementRegistry::find(const QualifiedName& name) const
153 {
154     ConstructorMap::const_iterator found = m_constructors.find(name.impl());
155     return (found != m_constructors.end()) ? found->value : 0;
156 }
157
158 PassRefPtr<Element> CustomElementRegistry::createElement(const QualifiedName& name) const
159 {
160     if (RefPtr<CustomElementConstructor> found = find(name))
161         return found->createElement();
162     return 0;
163 }
164
165 inline Document* CustomElementRegistry::document() const
166 {
167     return toDocument(m_scriptExecutionContext);
168 }
169
170 }
171
172 #endif // ENABLE(CUSTOM_ELEMENTS)