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

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

{{source_files_for_generated_file(template_file, input_files)}}

#include "third_party/blink/renderer/core/{{namespace|lower}}_element_type_helpers.h"

#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
{% if namespace == "HTML" %}
#include "third_party/blink/renderer/core/html_names.h"
{% endif %}
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"

namespace blink {
{% if namespace == "HTML" %}
using HTMLTypeMap = HashMap<AtomicString, HTMLElementType>;

HTMLTypeMap CreateHTMLTypeMap() {
  HTMLTypeMap html_type_map;
  html_type_map.ReserveCapacityForSize({{tags|count}});
  static const struct {
    const QualifiedName* name;
    HTMLElementType type;
  } kTags[] = {
    {% for tag in tags|sort(attribute='name') %}
    { &html_names::{{tag|symbol}}Tag, HTMLElementType::k{{tag.js_interface}} },
    {% endfor %}
  };
  for (const auto& tag : kTags)
    html_type_map.insert(tag.name->LocalName(), tag.type);
  return html_type_map;
}

static const HTMLTypeMap& GetHTMLTypeMap() {
  DEFINE_STATIC_LOCAL(const HTMLTypeMap, html_type_map, (CreateHTMLTypeMap()));
  return html_type_map;
}

HTMLElementType HtmlElementTypeForTag(const AtomicString& tag_name, const Document* document) {
  const auto& html_type_map = GetHTMLTypeMap();
  auto it = html_type_map.find(tag_name);
  if (it == html_type_map.end())
    return HTMLElementType::kHTMLUnknownElement;

  {% for tag in tags|sort(attribute='name') %}
  {% if tag.runtimeEnabled %}
  if (tag_name == "{{tag.name}}") {
    if (!RuntimeEnabledFeatures::{{tag.runtimeEnabled}}Enabled(document->GetExecutionContext())) {
      return HTMLElementType::kHTMLUnknownElement;
    }
  }
  {% endif %}
  {% endfor %}
  return it->value;
}

bool IsKnownBuiltinTagName(const AtomicString& tag_name) {
  return GetHTMLTypeMap().Contains(tag_name);
}
{% endif %}
}  // namespace blink