Skip to content

Commit

Permalink
html2: Reduce duplication in element scope checking
Browse files Browse the repository at this point in the history
robinlinden committed Jan 6, 2025
1 parent c2866ec commit 949edf6
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions html2/iparser_actions.h
Original file line number Diff line number Diff line change
@@ -55,18 +55,11 @@ class IActions {
return std::ranges::find(array, str) != std::cend(array);
}

// https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-scope
bool has_element_in_scope(std::string_view element_name) const {
static constexpr auto kScopeElements = std::to_array<std::string_view>({
"applet", "caption", "html", "table", "td", "th", "marquee", "object", "template",
// TODO(robinlinden): Add MathML and SVG elements.
// MathML mi, MathML mo, MathML mn, MathML ms, MathML mtext,
// MathML annotation-xml, SVG foreignObject, SVG desc, SVG
// title,
});

private:
template<auto const &scope_elements>
bool has_element_in_scope_impl(std::string_view element_name) const {
for (auto const element : names_of_open_elements()) {
if (is_in_array<kScopeElements>(element)) {
if (is_in_array<scope_elements>(element)) {
return false;
}

@@ -78,6 +71,20 @@ class IActions {
return false;
}

public:
// https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-scope
bool has_element_in_scope(std::string_view element_name) const {
static constexpr auto kScopeElements = std::to_array<std::string_view>({
"applet", "caption", "html", "table", "td", "th", "marquee", "object", "template",
// TODO(robinlinden): Add MathML and SVG elements.
// MathML mi, MathML mo, MathML mn, MathML ms, MathML mtext,
// MathML annotation-xml, SVG foreignObject, SVG desc, SVG
// title,
});

return has_element_in_scope_impl<kScopeElements>(element_name);
}

// https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-button-scope
bool has_element_in_button_scope(std::string_view element_name) const {
static constexpr auto kScopeElements = std::to_array<std::string_view>({
@@ -88,17 +95,7 @@ class IActions {
// title,
});

for (auto const element : names_of_open_elements()) {
if (is_in_array<kScopeElements>(element)) {
return false;
}

if (element == element_name) {
return true;
}
}

return false;
return has_element_in_scope_impl<kScopeElements>(element_name);
}
};

0 comments on commit 949edf6

Please sign in to comment.