summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/build/scripts/templates/element_type_helpers.h.tmpl
blob: d9beef375862e6df5c67a59352dbc752189121f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{% from "templates/macros.tmpl" import license, source_files_for_generated_file %}
{{ license() }}

{{source_files_for_generated_file(template_file, input_files)}}

#ifndef BLINK_CORE_{{namespace|upper}}_ELEMENT_TYPE_HELPERS_H_
#define BLINK_CORE_{{namespace|upper}}_ELEMENT_TYPE_HELPERS_H_

#include "{{base_element_header}}"
#include "core/{{namespace|lower}}_names.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"

namespace blink {
// Type checking.
{% for tag in tags|sort if not tag.multipleTagNames and not tag.noTypeHelpers %}
class {{tag.interface}};
// Catch unnecessary runtime check of type known at compile time.
void Is{{tag.interface}}(const {{tag.interface}}&);
void Is{{tag.interface}}(const {{tag.interface}}*);

inline bool Is{{tag.interface}}(const {{namespace}}Element& element) {
  {% if tag.runtimeEnabled %}
  if (!RuntimeEnabledFeatures::{{tag.runtimeEnabled}}Enabled())
    return false;
  {% endif %}
  return element.HasTagName({{namespace}}Names::{{tag|symbol}}Tag);
}
inline bool Is{{tag.interface}}(const {{namespace}}Element* element) {
  return element && Is{{tag.interface}}(*element);
}
inline bool Is{{tag.interface}}(const Node& node) {
  return node.Is{{namespace}}Element() && Is{{tag.interface}}(To{{namespace}}Element(node));
}
inline bool Is{{tag.interface}}(const Node* node) {
  return node && Is{{tag.interface}}(*node);
}
template <>
inline bool IsElementOfType<const {{tag.interface}}>(const Node& node) {
  return Is{{tag.interface}}(node);
}
template <>
inline bool IsElementOfType<const {{tag.interface}}>(const {{namespace}}Element& element) {
  return Is{{tag.interface}}(element);
}

{% endfor %}
// Using macros because the types are forward-declared and we don't want to use
// reinterpret_cast in the casting functions above. reinterpret_cast would be
// unsafe due to multiple inheritence.

{% for tag in tags|sort if not tag.multipleTagNames and not tag.noTypeHelpers %}
#define To{{tag.interface}}(x) blink::ToElement<blink::{{tag.interface}}>(x)
#define To{{tag.interface}}OrNull(x) blink::ToElementOrNull<blink::{{tag.interface}}>(x)
#define To{{tag.interface}}OrDie(x) blink::ToElementOrDie<blink::{{tag.interface}}>(x)
{% endfor %}

{% if namespace == "HTML" %}

enum class HTMLElementType {
  {% for element in elements|sort %}k{{element}},
  {% endfor %}
};

// Tag checking.
// tagName is the local name for an html element in lowercase
// The corresponding HTMLElement type for the tag name will be returned
// Do NOT use this function with SVG tag names and SVGElements
// If tagName is an undefined html tag name HTMLUnknownElement is returned
HTMLElementType htmlElementTypeForTag(const AtomicString& tagName);

{% endif %}
} // namespace blink

#endif