Commit 63170658 authored by yurak's avatar yurak Committed by Commit bot

Add function to check what HTML Element interface corresponds to a given tag name.

HTMLElementTypeHelpers.h (generated) now has an enum, HTMLElementType, for each HTML element interface in htmltagnames.in
(Not generated for SVG elements)

New function htmlElementTypeForTag returns HTMLElementType
	Tag names that are undefined return HTMLUnknownElement

New ElementTypeHelpers.cpp.tmpl for HTML namespace with definition of function
Generates HTMLElementTypeHelpers.cpp

This function is needed in custom elements algorithms.

BUG=

Review-Url: https://codereview.chromium.org/2419383002
Cr-Commit-Position: refs/heads/master@{#427045}
parent 8fdd7b1b
......@@ -56,18 +56,22 @@ class MakeElementTypeHelpersWriter(in_generator.Writer):
self._outputs = {
(self.namespace + "ElementTypeHelpers.h"): self.generate_helper_header,
(self.namespace + "ElementTypeHelpers.cpp"): self.generate_helper_implementation,
}
self._template_context = {
'namespace': self.namespace,
'tags': self.in_file.name_dictionaries,
'elements': set(),
}
tags = self._template_context['tags']
elements = self._template_context['elements']
interface_counts = defaultdict(int)
for tag in tags:
tag['interface'] = self._interface(tag)
interface_counts[tag['interface']] += 1
elements.add(tag['interface'])
for tag in tags:
tag['multipleTagNames'] = (interface_counts[tag['interface']] > 1 or tag['interface'] == self.fallbackInterface)
......@@ -76,6 +80,10 @@ class MakeElementTypeHelpersWriter(in_generator.Writer):
def generate_helper_header(self):
return self._template_context
@template_expander.use_jinja("ElementTypeHelpers.cpp.tmpl", filters=filters)
def generate_helper_implementation(self):
return self._template_context
def _interface(self, tag):
if tag['interfaceName']:
return tag['interfaceName']
......
......@@ -54,6 +54,7 @@ make_element_factory_files =
make_element_type_helpers_files =
make_qualified_names_files + [
"$_scripts_dir/make_element_type_helpers.py",
"$_scripts_dir/templates/ElementTypeHelpers.cpp.tmpl",
"$_scripts_dir/templates/ElementTypeHelpers.h.tmpl",
]
......
{% from "macros.tmpl" import license %}
{{ license() }}
#include "{{namespace}}ElementTypeHelpers.h"
#include "core/dom/Document.h"
#include "platform/RuntimeEnabledFeatures.h"
#include "wtf/HashMap.h"
namespace blink {
{% if namespace == "HTML" %}
using HTMLTypeMap = HashMap<AtomicString, HTMLElementType>;
static HTMLTypeMap* html_type_map = 0;
void createHTMLTypeMap() {
DCHECK(!html_type_map);
html_type_map = new HTMLTypeMap;
{% for tag in tags|sort %}
html_type_map->set(AtomicString("{{tag.name}}"), HTMLElementType::k{{tag.interface}});
{% endfor %}
}
HTMLElementType htmlElementTypeForTag(const AtomicString& tagName) {
if (!html_type_map) createHTMLTypeMap();
if (html_type_map->contains(tagName)) {
{% for tag in tags|sort %}
{% if tag.runtimeEnabled %}
if (tagName == "{{tag.name}}") {
if (!RuntimeEnabledFeatures::{{tag.runtimeEnabled}}Enabled()) {
return HTMLElementType::kHTMLUnknownElement;
}
}
{% endif %}
{% endfor %}
return html_type_map->get(tagName);
} else {
return HTMLElementType::kHTMLUnknownElement;
}
}
{% endif %}
} // namespace blink
......@@ -4,7 +4,7 @@
#ifndef {{namespace}}ElementTypeHelpers_h
#define {{namespace}}ElementTypeHelpers_h
#include "core/dom/Element.h"
#include "core/{{namespace|lower}}/{{namespace}}Element.h"
#include "{{namespace}}Names.h"
#include "platform/RuntimeEnabledFeatures.h"
......@@ -35,6 +35,22 @@ template <> inline bool isElementOfType<const {{tag.interface}}>(const {{namespa
{% for tag in tags|sort if not tag.multipleTagNames and not tag.noTypeHelpers %}
#define to{{tag.interface}}(x) blink::toElement<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
......@@ -495,6 +495,7 @@ process_in_files("make_core_generated_html_element_type_helpers") {
in_files = [ "html/HTMLTagNames.in" ]
other_inputs = make_element_type_helpers_files
outputs = [
"$blink_core_output_dir/HTMLElementTypeHelpers.cpp",
"$blink_core_output_dir/HTMLElementTypeHelpers.h",
]
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment