mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-25 17:25:08 +02:00
Meta: Replace GenerateCSSPseudoClass with a python generator
This commit is contained in:
committed by
Shannon Booth
parent
f7898d2c8b
commit
91022151d6
Notes:
github-actions[bot]
2026-04-25 10:04:33 +00:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/LadybirdBrowser/ladybird/commit/91022151d65 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/9083
@@ -59,9 +59,9 @@ function (generate_css_implementation)
|
||||
dependencies "${LIBWEB_INPUT_FOLDER}/CSS/Enums.json" "${LIBWEB_INPUT_FOLDER}/CSS/LogicalPropertyGroups.json"
|
||||
)
|
||||
|
||||
invoke_cpp_generator(
|
||||
invoke_py_generator(
|
||||
"PseudoClass.cpp"
|
||||
Lagom::GenerateCSSPseudoClass
|
||||
"generate_libweb_css_pseudo_class.py"
|
||||
"${LIBWEB_INPUT_FOLDER}/CSS/PseudoClasses.json"
|
||||
"CSS/PseudoClass.h"
|
||||
"CSS/PseudoClass.cpp"
|
||||
|
||||
191
Meta/Generators/generate_libweb_css_pseudo_class.py
Normal file
191
Meta/Generators/generate_libweb_css_pseudo_class.py
Normal file
@@ -0,0 +1,191 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Copyright (c) 2022-2026, Sam Atkins <sam@ladybird.org>
|
||||
# Copyright (c) 2026-present, the Ladybird developers.
|
||||
#
|
||||
# SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
|
||||
from pathlib import Path
|
||||
from typing import TextIO
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
from Utils.utils import title_casify
|
||||
|
||||
PARAMETER_TYPES = {
|
||||
"<an+b>": "ANPlusB",
|
||||
"<an+b-of>": "ANPlusBOf",
|
||||
"<compound-selector>": "CompoundSelector",
|
||||
"<forgiving-selector-list>": "ForgivingSelectorList",
|
||||
"<forgiving-relative-selector-list>": "ForgivingRelativeSelectorList",
|
||||
"<ident>": "Ident",
|
||||
"<language-ranges>": "LanguageRanges",
|
||||
"<level>#": "LevelList",
|
||||
"<relative-selector-list>": "RelativeSelectorList",
|
||||
"<selector-list>": "SelectorList",
|
||||
}
|
||||
|
||||
|
||||
def write_header_file(out: TextIO, pseudo_classes_data: dict) -> None:
|
||||
out.write("""
|
||||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/StringView.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
enum class PseudoClass {
|
||||
""")
|
||||
|
||||
for name, value in pseudo_classes_data.items():
|
||||
if "legacy-alias-for" in value:
|
||||
continue
|
||||
out.write(f" {title_casify(name)},\n")
|
||||
|
||||
out.write("""
|
||||
__Count,
|
||||
};
|
||||
|
||||
Optional<PseudoClass> pseudo_class_from_string(StringView);
|
||||
StringView pseudo_class_name(PseudoClass);
|
||||
|
||||
struct PseudoClassMetadata {
|
||||
enum class ParameterType {
|
||||
None,
|
||||
ANPlusB,
|
||||
ANPlusBOf,
|
||||
CompoundSelector,
|
||||
ForgivingSelectorList,
|
||||
ForgivingRelativeSelectorList,
|
||||
Ident,
|
||||
LanguageRanges,
|
||||
LevelList,
|
||||
RelativeSelectorList,
|
||||
SelectorList,
|
||||
} parameter_type;
|
||||
bool is_valid_as_function;
|
||||
bool is_valid_as_identifier;
|
||||
};
|
||||
PseudoClassMetadata pseudo_class_metadata(PseudoClass);
|
||||
|
||||
}
|
||||
""")
|
||||
|
||||
|
||||
def write_implementation_file(out: TextIO, pseudo_classes_data: dict) -> None:
|
||||
out.write("""
|
||||
#include <LibWeb/CSS/PseudoClass.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
Optional<PseudoClass> pseudo_class_from_string(StringView string)
|
||||
{
|
||||
""")
|
||||
|
||||
for name, value in pseudo_classes_data.items():
|
||||
alias_for = value.get("legacy-alias-for")
|
||||
target_name = title_casify(alias_for) if alias_for is not None else title_casify(name)
|
||||
out.write(f"""
|
||||
if (string.equals_ignoring_ascii_case("{name}"sv))
|
||||
return PseudoClass::{target_name};
|
||||
""")
|
||||
|
||||
out.write("""
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
StringView pseudo_class_name(PseudoClass pseudo_class)
|
||||
{
|
||||
switch (pseudo_class) {
|
||||
case PseudoClass::__Count:
|
||||
VERIFY_NOT_REACHED();
|
||||
""")
|
||||
|
||||
for name, value in pseudo_classes_data.items():
|
||||
if "legacy-alias-for" in value:
|
||||
continue
|
||||
out.write(f"""
|
||||
case PseudoClass::{title_casify(name)}:
|
||||
return "{name}"sv;
|
||||
""")
|
||||
|
||||
out.write("""
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
PseudoClassMetadata pseudo_class_metadata(PseudoClass pseudo_class)
|
||||
{
|
||||
switch (pseudo_class) {
|
||||
case PseudoClass::__Count:
|
||||
VERIFY_NOT_REACHED();
|
||||
""")
|
||||
|
||||
for name, value in pseudo_classes_data.items():
|
||||
if "legacy-alias-for" in value:
|
||||
continue
|
||||
|
||||
argument_string = value["argument"]
|
||||
is_valid_as_identifier = argument_string == ""
|
||||
is_valid_as_function = argument_string != ""
|
||||
|
||||
if argument_string.endswith("?"):
|
||||
is_valid_as_identifier = True
|
||||
argument_string = argument_string[:-1]
|
||||
|
||||
parameter_type = "None"
|
||||
if is_valid_as_function:
|
||||
if argument_string not in PARAMETER_TYPES:
|
||||
print(f"Unrecognized pseudo-class argument type: `{argument_string}`", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
parameter_type = PARAMETER_TYPES[argument_string]
|
||||
|
||||
is_valid_as_function_str = "true" if is_valid_as_function else "false"
|
||||
is_valid_as_identifier_str = "true" if is_valid_as_identifier else "false"
|
||||
|
||||
out.write(f"""
|
||||
case PseudoClass::{title_casify(name)}:
|
||||
return {{
|
||||
.parameter_type = PseudoClassMetadata::ParameterType::{parameter_type},
|
||||
.is_valid_as_function = {is_valid_as_function_str},
|
||||
.is_valid_as_identifier = {is_valid_as_identifier_str},
|
||||
}};
|
||||
""")
|
||||
|
||||
out.write("""
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
}
|
||||
""")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Generate CSS PseudoClasses", add_help=False)
|
||||
parser.add_argument("--help", action="help", help="Show this help message and exit")
|
||||
parser.add_argument("-h", "--header", required=True, help="Path to the PseudoClasses header file to generate")
|
||||
parser.add_argument(
|
||||
"-c", "--implementation", required=True, help="Path to the PseudoClasses implementation file to generate"
|
||||
)
|
||||
parser.add_argument("-j", "--json", required=True, help="Path to the JSON file to read from")
|
||||
args = parser.parse_args()
|
||||
|
||||
with open(args.json, "r", encoding="utf-8") as input_file:
|
||||
pseudo_classes_data = json.load(input_file)
|
||||
|
||||
with open(args.header, "w", encoding="utf-8") as output_file:
|
||||
write_header_file(output_file, pseudo_classes_data)
|
||||
|
||||
with open(args.implementation, "w", encoding="utf-8") as output_file:
|
||||
write_implementation_file(output_file, pseudo_classes_data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -5,7 +5,6 @@ lagom_tool(GenerateCSSMathFunctions SOURCES GenerateCSSMathFunctions.cpp
|
||||
lagom_tool(GenerateCSSMediaFeatureID SOURCES GenerateCSSMediaFeatureID.cpp LIBS LibMain)
|
||||
lagom_tool(GenerateCSSNumericFactoryMethods SOURCES GenerateCSSNumericFactoryMethods.cpp LIBS LibMain)
|
||||
lagom_tool(GenerateCSSPropertyID SOURCES GenerateCSSPropertyID.cpp LIBS LibMain)
|
||||
lagom_tool(GenerateCSSPseudoClass SOURCES GenerateCSSPseudoClass.cpp LIBS LibMain)
|
||||
lagom_tool(GenerateCSSPseudoElement SOURCES GenerateCSSPseudoElement.cpp LIBS LibMain)
|
||||
lagom_tool(GenerateCSSStyleProperties SOURCES GenerateCSSStyleProperties.cpp LIBS LibMain)
|
||||
lagom_tool(GenerateCSSUnits SOURCES GenerateCSSUnits.cpp LIBS LibMain)
|
||||
|
||||
@@ -1,235 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "GeneratorUtil.h"
|
||||
#include <AK/SourceGenerator.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
ErrorOr<void> generate_header_file(JsonObject& pseudo_classes_data, Core::File& file);
|
||||
ErrorOr<void> generate_implementation_file(JsonObject& pseudo_classes_data, Core::File& file);
|
||||
|
||||
ErrorOr<int> ladybird_main(Main::Arguments arguments)
|
||||
{
|
||||
StringView generated_header_path;
|
||||
StringView generated_implementation_path;
|
||||
StringView json_path;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(generated_header_path, "Path to the PseudoClasses header file to generate", "generated-header-path", 'h', "generated-header-path");
|
||||
args_parser.add_option(generated_implementation_path, "Path to the PseudoClasses implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
|
||||
args_parser.add_option(json_path, "Path to the JSON file to read from", "json-path", 'j', "json-path");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
auto json = TRY(read_entire_file_as_json(json_path));
|
||||
VERIFY(json.is_object());
|
||||
auto data = json.as_object();
|
||||
|
||||
auto generated_header_file = TRY(Core::File::open(generated_header_path, Core::File::OpenMode::Write));
|
||||
auto generated_implementation_file = TRY(Core::File::open(generated_implementation_path, Core::File::OpenMode::Write));
|
||||
|
||||
TRY(generate_header_file(data, *generated_header_file));
|
||||
TRY(generate_implementation_file(data, *generated_implementation_file));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ErrorOr<void> generate_header_file(JsonObject& pseudo_classes_data, Core::File& file)
|
||||
{
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
||||
generator.append(R"~~~(
|
||||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/StringView.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
enum class PseudoClass {
|
||||
)~~~");
|
||||
|
||||
pseudo_classes_data.for_each_member([&](auto& name, JsonValue const& value) {
|
||||
if (value.as_object().has("legacy-alias-for"sv))
|
||||
return;
|
||||
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name:titlecase", title_casify(name));
|
||||
|
||||
member_generator.appendln(" @name:titlecase@,");
|
||||
});
|
||||
generator.append(R"~~~(
|
||||
__Count,
|
||||
};
|
||||
|
||||
Optional<PseudoClass> pseudo_class_from_string(StringView);
|
||||
StringView pseudo_class_name(PseudoClass);
|
||||
|
||||
struct PseudoClassMetadata {
|
||||
enum class ParameterType {
|
||||
None,
|
||||
ANPlusB,
|
||||
ANPlusBOf,
|
||||
CompoundSelector,
|
||||
ForgivingSelectorList,
|
||||
ForgivingRelativeSelectorList,
|
||||
Ident,
|
||||
LanguageRanges,
|
||||
LevelList,
|
||||
RelativeSelectorList,
|
||||
SelectorList,
|
||||
} parameter_type;
|
||||
bool is_valid_as_function;
|
||||
bool is_valid_as_identifier;
|
||||
};
|
||||
PseudoClassMetadata pseudo_class_metadata(PseudoClass);
|
||||
|
||||
}
|
||||
)~~~");
|
||||
|
||||
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<void> generate_implementation_file(JsonObject& pseudo_classes_data, Core::File& file)
|
||||
{
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
||||
generator.append(R"~~~(
|
||||
#include <LibWeb/CSS/PseudoClass.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
Optional<PseudoClass> pseudo_class_from_string(StringView string)
|
||||
{
|
||||
)~~~");
|
||||
|
||||
pseudo_classes_data.for_each_member([&](auto& name, JsonValue const& value) {
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name", name);
|
||||
|
||||
if (auto alias_for = value.as_object().get_string("legacy-alias-for"sv); alias_for.has_value()) {
|
||||
member_generator.set("name:titlecase", title_casify(alias_for.value()));
|
||||
} else {
|
||||
member_generator.set("name:titlecase", title_casify(name));
|
||||
}
|
||||
|
||||
member_generator.append(R"~~~(
|
||||
if (string.equals_ignoring_ascii_case("@name@"sv))
|
||||
return PseudoClass::@name:titlecase@;
|
||||
)~~~");
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
StringView pseudo_class_name(PseudoClass pseudo_class)
|
||||
{
|
||||
switch (pseudo_class) {
|
||||
case PseudoClass::__Count:
|
||||
VERIFY_NOT_REACHED();
|
||||
)~~~");
|
||||
|
||||
pseudo_classes_data.for_each_member([&](auto& name, JsonValue const& value) {
|
||||
if (value.as_object().has("legacy-alias-for"sv))
|
||||
return;
|
||||
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name", name);
|
||||
member_generator.set("name:titlecase", title_casify(name));
|
||||
|
||||
member_generator.append(R"~~~(
|
||||
case PseudoClass::@name:titlecase@:
|
||||
return "@name@"sv;
|
||||
)~~~");
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
PseudoClassMetadata pseudo_class_metadata(PseudoClass pseudo_class)
|
||||
{
|
||||
switch (pseudo_class) {
|
||||
case PseudoClass::__Count:
|
||||
VERIFY_NOT_REACHED();
|
||||
)~~~");
|
||||
|
||||
pseudo_classes_data.for_each_member([&](auto& name, JsonValue const& value) {
|
||||
auto& pseudo_class = value.as_object();
|
||||
if (pseudo_class.has("legacy-alias-for"sv))
|
||||
return;
|
||||
|
||||
auto member_generator = generator.fork();
|
||||
auto argument_string = pseudo_class.get_string("argument"sv).value();
|
||||
|
||||
bool is_valid_as_identifier = argument_string.is_empty();
|
||||
bool is_valid_as_function = !argument_string.is_empty();
|
||||
|
||||
if (argument_string.ends_with('?')) {
|
||||
is_valid_as_identifier = true;
|
||||
argument_string = MUST(argument_string.substring_from_byte_offset(0, argument_string.byte_count() - 1));
|
||||
}
|
||||
|
||||
String parameter_type = "None"_string;
|
||||
if (is_valid_as_function) {
|
||||
if (argument_string == "<an+b>"sv) {
|
||||
parameter_type = "ANPlusB"_string;
|
||||
} else if (argument_string == "<an+b-of>"sv) {
|
||||
parameter_type = "ANPlusBOf"_string;
|
||||
} else if (argument_string == "<compound-selector>"sv) {
|
||||
parameter_type = "CompoundSelector"_string;
|
||||
} else if (argument_string == "<forgiving-selector-list>"sv) {
|
||||
parameter_type = "ForgivingSelectorList"_string;
|
||||
} else if (argument_string == "<forgiving-relative-selector-list>"sv) {
|
||||
parameter_type = "ForgivingRelativeSelectorList"_string;
|
||||
} else if (argument_string == "<ident>"sv) {
|
||||
parameter_type = "Ident"_string;
|
||||
} else if (argument_string == "<language-ranges>"sv) {
|
||||
parameter_type = "LanguageRanges"_string;
|
||||
} else if (argument_string == "<level>#"sv) {
|
||||
parameter_type = "LevelList"_string;
|
||||
} else if (argument_string == "<relative-selector-list>"sv) {
|
||||
parameter_type = "RelativeSelectorList"_string;
|
||||
} else if (argument_string == "<selector-list>"sv) {
|
||||
parameter_type = "SelectorList"_string;
|
||||
} else {
|
||||
warnln("Unrecognized pseudo-class argument type: `{}`", argument_string);
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
member_generator.set("name:titlecase", title_casify(name));
|
||||
member_generator.set("parameter_type", parameter_type);
|
||||
member_generator.set("is_valid_as_function", is_valid_as_function ? "true"_string : "false"_string);
|
||||
member_generator.set("is_valid_as_identifier", is_valid_as_identifier ? "true"_string : "false"_string);
|
||||
|
||||
member_generator.append(R"~~~(
|
||||
case PseudoClass::@name:titlecase@:
|
||||
return {
|
||||
.parameter_type = PseudoClassMetadata::ParameterType::@parameter_type@,
|
||||
.is_valid_as_function = @is_valid_as_function@,
|
||||
.is_valid_as_identifier = @is_valid_as_identifier@,
|
||||
};
|
||||
)~~~");
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
}
|
||||
)~~~");
|
||||
|
||||
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
|
||||
return {};
|
||||
}
|
||||
Reference in New Issue
Block a user