Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / build / scripts / templates / ElementFactory.cpp.tmpl
1 {% from "macros.tmpl" import license %}
2 {{ license() }}
3
4 #include "config.h"
5 #include "{{namespace}}ElementFactory.h"
6
7 #include "{{namespace}}Names.h"
8 {% for tag in tags|sort %}
9 #include "core/{{namespace|lower}}/{{tag.interface}}.h"
10 {% endfor %}
11 {% if fallback_interface %}
12 #include "core/{{namespace|lower}}/{{fallback_interface}}.h"
13 {% endif %}
14 #include "core/dom/custom/CustomElement.h"
15 #include "core/dom/custom/CustomElementRegistrationContext.h"
16 #include "core/dom/Document.h"
17 #include "core/frame/Settings.h"
18 #include "platform/RuntimeEnabledFeatures.h"
19 #include "wtf/HashMap.h"
20
21 namespace blink {
22
23 using namespace {{namespace}}Names;
24
25 typedef PassRefPtrWillBeRawPtr<{{namespace}}Element> (*ConstructorFunction)(
26     Document&,
27     {% if namespace == 'HTML' %}
28     HTMLFormElement*,
29     {% endif %}
30     bool createdByParser);
31
32 typedef HashMap<AtomicString, ConstructorFunction> FunctionMap;
33
34 static FunctionMap* g_constructors = 0;
35
36 {% for tag in tags|sort if not tag.noConstructor %}
37 {% filter enable_conditional(tag.Conditional) %}
38 static PassRefPtrWillBeRawPtr<{{namespace}}Element> {{tag|symbol}}Constructor(
39     Document& document,
40     {% if namespace == 'HTML' %}
41     HTMLFormElement* formElement,
42     {% endif %}
43     bool createdByParser)
44 {
45     {% if tag.runtimeEnabled %}
46     if (!RuntimeEnabledFeatures::{{tag.runtimeEnabled}}Enabled())
47         return {{fallback_interface}}::create({{tag|symbol}}Tag, document);
48     {% endif %}
49     return {{tag.interface}}::create(
50         {%- if tag.multipleTagNames %}{{tag|symbol}}Tag, {% endif -%}
51         document
52         {%- if namespace == 'HTML' and tag.constructorNeedsFormElement %}, formElement{% endif -%}
53         {%- if tag.constructorNeedsCreatedByParser %}, createdByParser{% endif -%}
54     );
55 }
56 {% endfilter %}
57 {% endfor %}
58
59 struct Create{{namespace}}FunctionMapData {
60   const QualifiedName& tag;
61   ConstructorFunction func;
62 };
63
64 static void create{{namespace}}FunctionMap()
65 {
66     ASSERT(!g_constructors);
67     g_constructors = new FunctionMap;
68     // Empty array initializer lists are illegal [dcl.init.aggr] and will not
69     // compile in MSVC. If tags list is empty, add check to skip this.
70     static const Create{{namespace}}FunctionMapData data[] = {
71     {% for tag in tags|sort if not tag.noConstructor %}
72     {% filter enable_conditional(tag.Conditional) %}
73         { {{tag|symbol}}Tag, {{tag|symbol}}Constructor },
74     {% endfilter %}
75     {% endfor %}
76     };
77     for (size_t i = 0; i < WTF_ARRAY_LENGTH(data); i++)
78         g_constructors->set(data[i].tag.localName(), data[i].func);
79 }
80
81 PassRefPtrWillBeRawPtr<{{namespace}}Element> {{namespace}}ElementFactory::create{{namespace}}Element(
82     const AtomicString& localName,
83     Document& document,
84     {% if namespace == 'HTML' %}
85     HTMLFormElement* formElement,
86     {% endif %}
87     bool createdByParser)
88 {
89     if (!g_constructors)
90         create{{namespace}}FunctionMap();
91     if (ConstructorFunction function = g_constructors->get(localName))
92         return function(document, {% if namespace == 'HTML' %}formElement, {% endif %}createdByParser);
93
94     if (document.registrationContext() && CustomElement::isValidName(localName)) {
95         RefPtrWillBeRawPtr<Element> element = document.registrationContext()->createCustomTagElement(document, QualifiedName(nullAtom, localName, {{namespace_prefix}}NamespaceURI));
96         ASSERT_WITH_SECURITY_IMPLICATION(element->is{{namespace}}Element());
97         return static_pointer_cast<{{namespace}}Element>(element.release());
98     }
99
100     return {{fallback_interface}}::create(QualifiedName(nullAtom, localName, {{namespace_prefix}}NamespaceURI), document);
101 }
102
103 } // namespace blink