chromium/third_party/blink/renderer/build/scripts/templates/element_type_helpers.h.tmpl

{% from "templates/macros.tmpl" import license, source_files_for_generated_file %}
{{ license() }}

{{source_files_for_generated_file(template_file, input_files)}}

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

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

namespace blink {

class Document;

// Type checking.
{% for tag in tags|sort(attribute='name') if not tag.multipleTagNames and not tag.noTypeHelpers %}
class {{tag.interface}};
template <>
struct DowncastTraits<{{tag.interface}}> {
  static bool AllowFrom(const Element& element) {
    {% if tag.runtimeEnabled %}
    // If the following line doesn't compile, your feature may vary by context,
    // in which case you'll need to write your own type helpers that can
    // distinguish elements without relying solely on tag name.
    if (!RuntimeEnabledFeatures::{{tag.runtimeEnabled}}Enabled())
      return false;
    {% endif %}
    return element.HasTagName({{cpp_namespace}}::{{tag|symbol}}Tag);
  }
  // Force this helper to only be instantiated when used. Otherwise, the
  // `IsA<...>` calls will always trigger a build failure since
  // {{tag.interface}} is forward declared and not included.
  template <typename T = {{tag.interface}}>
  static bool AllowFrom(const Node& node) {
    // UnsafeTo<> is safe because Is*Element(), by definition, only returns
    // true if `node` is derived from `Element`.
    return node.Is{{namespace}}Element() && IsA<{{tag.interface}}>(UnsafeTo<{{namespace}}Element>(node));
  }
};

{% endfor %}

{% if namespace == "HTML" %}

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

// Tag checking.
// |tag_name| 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 |tag_name| is an undefined HTML tag name HTMLUnknownElement is returned.
HTMLElementType HtmlElementTypeForTag(const AtomicString& tag_name, const Document*);

bool IsKnownBuiltinTagName(const AtomicString& tag_name);

{% endif %}
}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_{{namespace|upper}}_ELEMENT_TYPE_HELPERS_H_