Truly independent web browser
#!/usr/bin/env python3
# Copyright (c) 2018-2020, Andreas Kling <[email protected]>
# Copyright (c) 2021-2026, Sam Atkins <[email protected]>
# Copyright (c) 2026-present, the Ladybird developers.
#
# SPDX-License-Identifier: BSD-2-Clause
import argparse
import copy
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 camel_casify
from Utils.utils import title_casify
from Utils.utils import underlying_type_for_enum
from Utils.utils import write_case_insensitive_ascii_string_to_enum_lookup
def is_legacy_alias(prop: dict) -> bool:
return "legacy-alias-for" in prop
def is_legacy_shorthand(prop: dict) -> bool:
return prop.get("legacy-shorthand", False)
def verify_alphabetical(data: dict, path: str) -> None:
most_recent_name = ""
for name in data:
if name < most_recent_name:
print(
f"`{name}` is in the wrong position in `{path}`. Please keep this list alphabetical!", file=sys.stderr
)
sys.exit(1)
most_recent_name = name
def replace_logical_aliases(properties: dict, logical_property_groups: dict) -> None:
# Grab the first physical property in each logical group, to use as the template
first_physical_property_in_logical_group = {}
for group_name, group in logical_property_groups.items():
physical = group["physical"]
if not physical:
raise ValueError(f"No physical properties in group {group_name}")
first_physical_property_in_logical_group[group_name] = next(iter(physical.values()))
logical_aliases = {}
for name, value in properties.items():
logical_alias_for = value.get("logical-alias-for")
if logical_alias_for is not None:
group_name = logical_alias_for.get("group")
if group_name is None:
print(f"Logical alias '{name}' is missing its group", file=sys.stderr)
sys.exit(1)
physical_property_name = first_physical_property_in_logical_group.get(group_name)
if physical_property_name is None:
print(f"Logical property group '{group_name}' not found! (Property: '{name}')", file=sys.stderr)
sys.exit(1)
logical_aliases[name] = physical_property_name
for name, alias in logical_aliases.items():
if alias not in properties:
print(f"No property '{alias}' found for logical alias '{name}'", file=sys.stderr)
sys.exit(1)
alias_object = copy.deepcopy(properties[alias])
# Copy over anything the logical property overrides
for key, val in properties[name].items():
alias_object[key] = val
# Quirks don't carry across to logical aliases
alias_object.pop("quirks", None)
properties[name] = alias_object
def populate_all_property_longhands(properties: dict) -> None:
all_entry = properties.get("all")
if all_entry is None:
raise ValueError("Missing 'all' property")
for name, value in properties.items():
if "longhands" in value or "legacy-alias-for" in value or name == "direction" or name == "unicode-bidi":
continue
all_entry["longhands"].append(name)
def is_animatable_property(properties: dict, property_name: str) -> bool:
prop = properties[property_name]
animation_type = prop.get("animation-type")
if animation_type is not None:
return animation_type != "none"
if "longhands" not in prop:
print(f"Property '{property_name}' must specify either 'animation-type' or 'longhands'", file=sys.stderr)
sys.exit(1)
for subproperty_name in prop["longhands"]:
if is_animatable_property(properties, subproperty_name):
return True
return False
def format_numeric_range(type_name: str, min_val: str, max_val: str) -> tuple[str, str]:
if type_name == "integer":
min_str = "AK::NumericLimits<i32>::min()" if min_val == "-∞" else min_val
max_str = "AK::NumericLimits<i32>::max()" if max_val == "∞" else max_val
else:
min_str = "AK::NumericLimits<float>::lowest()" if min_val == "-∞" else min_val
max_str = "AK::NumericLimits<float>::max()" if max_val == "∞" else max_val
return min_str, max_str
def property_ids_in_enum_order(properties: dict) -> tuple[list, list, list]:
shorthand_property_ids = []
inherited_longhand_property_ids = []
noninherited_longhand_property_ids = []
for name, value in properties.items():
# Legacy aliases don't get a PropertyID
if is_legacy_alias(value):
continue
inherited = value.get("inherited")
if "longhands" in value:
if inherited is not None:
print(f"Property '{name}' with longhands cannot specify 'inherited'", file=sys.stderr)
sys.exit(1)
shorthand_property_ids.append(name)
else:
if inherited is None:
print(f"Property '{name}' is missing 'inherited'", file=sys.stderr)
sys.exit(1)
if inherited:
inherited_longhand_property_ids.append(name)
else:
noninherited_longhand_property_ids.append(name)
return shorthand_property_ids, inherited_longhand_property_ids, noninherited_longhand_property_ids
# NB: The property list lives in LibWebCommon so that processes which do not link LibWeb can name properties.
def write_property_list_header_file(out: TextIO) -> None:
out.write("""
#pragma once
#include <AK/Span.h>
#include <AK/StringView.h>
#include <LibWebCommon/Export.h>
namespace Web::CSS {
struct PropertyListEntry {
StringView name;
bool is_inherited { false };
};
// Every property that has a PropertyID, in PropertyID order.
WEBCOMMON_API ReadonlySpan<PropertyListEntry> property_list();
}
""")
def write_property_list_implementation_file(out: TextIO, properties: dict) -> None:
shorthand_property_ids, inherited_longhand_property_ids, noninherited_longhand_property_ids = (
property_ids_in_enum_order(properties)
)
out.write("""
#include <LibWebCommon/CSS/PropertyList.h>
namespace Web::CSS {
static constexpr PropertyListEntry s_property_list[] = {
""")
for ids, is_inherited in (
(shorthand_property_ids, False),
(inherited_longhand_property_ids, True),
(noninherited_longhand_property_ids, False),
):
for name in ids:
out.write(f' {{ "{name}"sv, {"true" if is_inherited else "false"} }},\n')
out.write("""};
ReadonlySpan<PropertyListEntry> property_list()
{
return s_property_list;
}
}
""")
def write_header_file(out: TextIO, properties: dict, logical_property_groups: dict) -> None:
property_id_underlying_type = underlying_type_for_enum(len(properties))
logical_property_group_underlying_type = underlying_type_for_enum(len(logical_property_groups))
out.write(f"""
#pragma once
#include <AK/NonnullRefPtr.h>
#include <AK/Traits.h>
#include <AK/Utf16FlyString.h>
#include <AK/Utf16View.h>
#include <AK/Variant.h>
#include <LibJS/Forward.h>
#include <LibWeb/CSS/NumericRange.h>
#include <LibWeb/CSS/ValueType.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>
namespace Web::CSS {{
enum class PropertyID : {property_id_underlying_type} {{
Custom,
""")
shorthand_property_ids, inherited_longhand_property_ids, noninherited_longhand_property_ids = (
property_ids_in_enum_order(properties)
)
first_property_id = shorthand_property_ids[0]
last_property_id = noninherited_longhand_property_ids[-1]
first_longhand_property_id = inherited_longhand_property_ids[0]
last_longhand_property_id = noninherited_longhand_property_ids[-1]
first_inherited_property_id = inherited_longhand_property_ids[0]
last_inherited_property_id = inherited_longhand_property_ids[-1]
for ids in (shorthand_property_ids, inherited_longhand_property_ids, noninherited_longhand_property_ids):
for name in ids:
out.write(f"""
{title_casify(name)},
""")
out.write(f"""
}};
bool is_animatable_property(PropertyID);
Optional<PropertyID> property_id_from_camel_case_string(StringView);
Optional<PropertyID> property_id_from_camel_case_string(Utf16View);
WEB_API Optional<PropertyID> property_id_from_string(StringView);
WEB_API Optional<PropertyID> property_id_from_string(Utf16View);
[[nodiscard]] WEB_API Utf16FlyString const& string_from_property_id(PropertyID);
[[nodiscard]] Utf16FlyString const& camel_case_string_from_property_id(PropertyID);
WEB_API bool is_inherited_property(PropertyID);
WEB_API NonnullRefPtr<StyleValue const> property_initial_value(PropertyID);
bool property_is_single_valued(PropertyID);
bool property_is_list_valued(PropertyID);
bool property_accepts_type(PropertyID, ValueType);
WEB_API NumericRangesByValueType property_accepted_ranges_by_value_type(PropertyID);
Optional<ValueType> property_resolves_percentages_relative_to(PropertyID);
WEB_API bool property_is_shorthand(PropertyID);
WEB_API bool property_is_legacy_shorthand(PropertyID);
WEB_API Vector<PropertyID> const& longhands_for_shorthand(PropertyID);
Vector<PropertyID> const& expanded_longhands_for_shorthand(PropertyID);
bool property_maps_to_shorthand(PropertyID);
Vector<PropertyID> const& shorthands_for_longhand(PropertyID);
bool property_is_positional_value_list_shorthand(PropertyID);
bool property_needs_layout_for_getcomputedstyle(PropertyID);
bool property_needs_layout_node_for_resolved_value(PropertyID);
constexpr PropertyID first_property_id = PropertyID::{title_casify(first_property_id)};
constexpr PropertyID last_property_id = PropertyID::{title_casify(last_property_id)};
constexpr PropertyID first_inherited_property_id = PropertyID::{title_casify(first_inherited_property_id)};
constexpr PropertyID last_inherited_property_id = PropertyID::{title_casify(last_inherited_property_id)};
constexpr PropertyID first_longhand_property_id = PropertyID::{title_casify(first_longhand_property_id)};
constexpr PropertyID last_longhand_property_id = PropertyID::{title_casify(last_longhand_property_id)};
constexpr size_t number_of_longhand_properties = to_underlying(last_longhand_property_id) - to_underlying(first_longhand_property_id) + 1;
struct LogicalAliasMappingContext {{
WritingMode writing_mode;
Direction direction;
// TODO: text-orientation
}};
WEB_API bool property_is_logical_alias(PropertyID);
WEB_API PropertyID map_logical_alias_to_physical_property(PropertyID logical_property_id, LogicalAliasMappingContext const&);
enum class LogicalPropertyGroup : {logical_property_group_underlying_type} {{
""")
for name in logical_property_groups:
out.write(f"""
{title_casify(name)},
""")
out.write("""
};
Optional<LogicalPropertyGroup> logical_property_group_for_property(PropertyID);
} // namespace Web::CSS
namespace AK {
template<>
struct Formatter<Web::CSS::PropertyID> : Formatter<Utf16FlyString> {
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::PropertyID const& property_id)
{
return Formatter<Utf16FlyString>::format(builder, Web::CSS::string_from_property_id(property_id));
}
};
} // namespace AK
""")
def write_implementation_file(out: TextIO, properties: dict, logical_property_groups: dict, enum_names: list) -> None:
out.write("""
#include <AK/Assertions.h>
#include <AK/CharacterTypes.h>
#include <AK/NeverDestroyed.h>
#include <LibWeb/CSS/Enums.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/PropertyName.h>
#include <LibWeb/CSS/StyleValues/StyleValue.h>
#include <LibWeb/ComputedValuesRustFFI.h>
#include <LibWeb/StyleValueRustFFI.h>
#include <LibWebCommon/Infra/Strings.h>
namespace Web::CSS {
static auto generate_camel_case_property_table()
{
HashMap<StringView, PropertyID, CaseInsensitiveASCIIStringViewTraits> table;
""")
for name, value in properties.items():
legacy_alias_for = value.get("legacy-alias-for")
target = title_casify(legacy_alias_for) if legacy_alias_for is not None else title_casify(name)
out.write(f"""
table.set("{camel_casify(name)}"sv, PropertyID::{target});
""")
out.write("""
return table;
}
static auto const& camel_case_properties_table = *new HashMap<StringView, PropertyID, CaseInsensitiveASCIIStringViewTraits>(generate_camel_case_property_table());
Optional<PropertyID> property_id_from_camel_case_string(StringView string)
{
return camel_case_properties_table.get(string);
}
Optional<PropertyID> property_id_from_camel_case_string(Utf16View string)
{
for (auto const& entry : camel_case_properties_table) {
if (string.equals_ignoring_ascii_case(entry.key))
return entry.value;
}
return {};
}
""")
property_ids_by_name = {}
for name, value in properties.items():
legacy_alias_for = value.get("legacy-alias-for")
target = title_casify(legacy_alias_for) if legacy_alias_for is not None else title_casify(name)
property_ids_by_name[name] = f"PropertyID::{target}"
write_case_insensitive_ascii_string_to_enum_lookup(
out, "property_id_from_string_impl", "PropertyID", property_ids_by_name
)
out.write("""
Optional<PropertyID> property_id_from_string(StringView string)
{
if (is_a_custom_property_name_string(string))
return PropertyID::Custom;
return property_id_from_string_impl(string);
}
Optional<PropertyID> property_id_from_string(Utf16View string)
{
if (is_a_custom_property_name_string(string))
return PropertyID::Custom;
return property_id_from_string_impl(string);
}
Utf16FlyString const& string_from_property_id(PropertyID property_id) {
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
out.write(f"""
case PropertyID::{title_casify(name)}: {{
static Utf16FlyString const& name = *new Utf16FlyString("{name}"_utf16_fly_string);
return name;
}}
""")
out.write("""
default: {
static Utf16FlyString const& invalid_property_id_string = *new Utf16FlyString("(invalid CSS::PropertyID)"_utf16_fly_string);
return invalid_property_id_string;
}
}
}
Utf16FlyString const& camel_case_string_from_property_id(PropertyID property_id) {
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
out.write(f"""
case PropertyID::{title_casify(name)}: {{
static Utf16FlyString const& name = *new Utf16FlyString("{camel_casify(name)}"_utf16_fly_string);
return name;
}}
""")
out.write("""
default: {
static Utf16FlyString const& invalid_property_id_string = *new Utf16FlyString("(invalid CSS::PropertyID)"_utf16_fly_string);
return invalid_property_id_string;
}
}
}
bool is_animatable_property(PropertyID property_id)
{
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
if is_animatable_property(properties, name):
out.write(f" case PropertyID::{title_casify(name)}:\n")
out.write("""
return true;
default:
return false;
}
}
bool is_inherited_property(PropertyID property_id)
{
if (property_id >= first_inherited_property_id && property_id <= last_inherited_property_id)
return true;
return false;
}
bool property_needs_layout_for_getcomputedstyle(PropertyID property_id)
{
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
if value.get("needs-layout-for-getcomputedstyle", False):
out.write(f" case PropertyID::{title_casify(name)}:\n")
out.write("""
return true;
default:
return false;
}
}
bool property_needs_layout_node_for_resolved_value(PropertyID property_id)
{
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
if value.get("needs-layout-node-for-resolved-value", False):
out.write(f" case PropertyID::{title_casify(name)}:\n")
out.write("""
return true;
default:
return false;
}
}
NonnullRefPtr<StyleValue const> property_initial_value(PropertyID property_id)
{
static Array<Atomic<StyleValue const*>, to_underlying(last_property_id) + 1> initial_values;
auto& slot = initial_values[to_underlying(property_id)];
if (auto const* initial_value = slot.load(AK::MemoryOrder::memory_order_acquire))
return *initial_value;
// A longhand's initial value is the one style computation selects, which Rust parses.
if (property_id >= first_longhand_property_id && property_id <= last_longhand_property_id) {
auto const* data = static_cast<StyleValueFFI::StyleValueData const*>(ComputedValuesFFI::rust_style_metadata_initial_value(to_underlying(property_id)));
return StyleValue::intern(slot, StyleValue::adopt_rust_style_value_data(StyleValueFFI::rust_style_value_retain(data)));
}
// Lazily parse shorthand initial values as needed.
Parser::ParsingParams parsing_params;
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
if "initial" not in value:
print(f"No initial value specified for property '{name}'", file=sys.stderr)
sys.exit(1)
if "longhands" not in value:
continue
initial_value_string = value["initial"]
title = title_casify(name)
out.write(f""" case PropertyID::{title}:
{{
auto parsed_value = parse_css_value(parsing_params, "{initial_value_string}"sv, PropertyID::{title});
VERIFY(!parsed_value.is_null());
return StyleValue::intern(slot, parsed_value.release_nonnull());
}}
""")
out.write(""" default: VERIFY_NOT_REACHED();
}
VERIFY_NOT_REACHED();
}
bool property_is_single_valued(PropertyID property_id)
{
return !property_is_list_valued(property_id);
}
bool property_is_list_valued(PropertyID property_id)
{
switch (property_id) {
""")
for name, value in properties.items():
multiplicity = value.get("typed-om-multiplicity", value.get("multiplicity"))
if multiplicity is not None and multiplicity != "single":
if multiplicity not in ("list", "coordinating-list"):
print(
f"'{multiplicity}' is not a valid value for 'multiplicity'. "
"Accepted values are: 'single', 'list', 'coordinating-list'",
file=sys.stderr,
)
sys.exit(1)
out.write(f" case PropertyID::{title_casify(name)}:\n")
out.write("""
return true;
default:
return false;
}
}
bool property_accepts_type(PropertyID property_id, ValueType value_type)
{
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
valid_types = value.get("valid-types")
if not valid_types:
continue
out.write(f"""
case PropertyID::{title_casify(name)}: {{
switch (value_type) {{
""")
did_output_accepted_type = False
for type_entry in valid_types:
type_name = type_entry.split(" ")[0]
if type_name in enum_names:
continue
out.write(f" case ValueType::{title_casify(type_name)}:\n")
did_output_accepted_type = True
if did_output_accepted_type:
out.write(" return true;\n")
out.write("""
default:
return false;
}
}
""")
out.write("""
default:
return false;
}
}
NumericRangesByValueType property_accepted_ranges_by_value_type(PropertyID property_id)
{
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
valid_types = value.get("valid-types")
if not valid_types:
continue
ranges_parts = []
for type_entry in valid_types:
type_parts = type_entry.split(" ")
if len(type_parts) < 2:
continue
type_name = type_parts[0]
if type_name == "custom-ident":
continue
# Drop brackets: "[-∞,∞]" -> "-∞,∞"
type_range = type_parts[1][1:-1]
limits = type_range.split(",")
if len(limits) != 2:
raise ValueError(f"Bad range: {type_parts[1]}")
min_str, max_str = format_numeric_range(type_name, limits[0], limits[1])
ranges_parts.append(f"{{ ValueType::{title_casify(type_name)}, {{ {min_str}, {max_str} }} }}")
ranges = ", ".join(ranges_parts)
out.write(f"""
case PropertyID::{title_casify(name)}: {{
return {{ {ranges} }};
}}""")
out.write("""
default: {
return { };
}
}
}
Optional<ValueType> property_resolves_percentages_relative_to(PropertyID property_id)
{
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
resolved_type = value.get("percentages-resolve-to")
if resolved_type is None:
continue
out.write(f"""
case PropertyID::{title_casify(name)}:
return ValueType::{title_casify(resolved_type)};
""")
out.write("""
default:
return {};
}
}
""")
out.write("""
bool property_is_shorthand(PropertyID property_id)
{
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
if "longhands" in value:
out.write(f" case PropertyID::{title_casify(name)}:\n")
out.write("""
return true;
default:
return false;
}
}
bool property_is_legacy_shorthand(PropertyID property_id)
{
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_shorthand(value):
out.write(f" case PropertyID::{title_casify(name)}:\n")
out.write("""
return true;
default:
return false;
}
}
""")
def get_longhands(property_id: str) -> list:
return list(properties[property_id]["longhands"])
out.write("""
Vector<PropertyID> const& longhands_for_shorthand(PropertyID property_id)
{
switch (property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
if "longhands" in value:
longhands = ", ".join(f"PropertyID::{title_casify(lh)}" for lh in get_longhands(name))
out.write(f"""
case PropertyID::{title_casify(name)}: {{
static auto const& longhands = *new Vector<PropertyID> {{ {longhands} }};
return longhands;
}}""")
out.write("""
default:
static auto const& empty_longhands = *new Vector<PropertyID>;
return empty_longhands;
}
}
Vector<PropertyID> const& expanded_longhands_for_shorthand(PropertyID property_id)
{
switch (property_id) {
""")
def get_expanded_longhands(property_id: str) -> list:
expanded = []
for longhand_id in get_longhands(property_id):
prop = properties[longhand_id]
if "longhands" in prop:
expanded.extend(get_expanded_longhands(longhand_id))
else:
expanded.append(longhand_id)
return expanded
for name, value in properties.items():
if is_legacy_alias(value):
continue
if "longhands" in value:
longhands = ", ".join(f"PropertyID::{title_casify(lh)}" for lh in get_expanded_longhands(name))
out.write(f"""
case PropertyID::{title_casify(name)}: {{
static auto const& longhands = *new Vector<PropertyID> {{ {longhands} }};
return longhands;
}}""")
out.write("""
default: {
static auto const& empty_longhands = *new Vector<PropertyID>;
return empty_longhands;
}
}
}
""")
# shorthands_for_longhand_map: for each longhand, which shorthand(s) contain it.
shorthands_for_longhand_map = {}
for name, value in properties.items():
if is_legacy_alias(value) or is_legacy_shorthand(value):
continue
if "longhands" in value:
for longhand_name in value["longhands"]:
shorthands_for_longhand_map.setdefault(longhand_name, []).append(name)
out.write("""
bool property_maps_to_shorthand(PropertyID property_id)
{
switch (property_id) {
""")
out.writelines(f" case PropertyID::{title_casify(longhand)}:\n" for longhand in shorthands_for_longhand_map)
out.write("""
return true;
default:
return false;
}
}
Vector<PropertyID> const& shorthands_for_longhand(PropertyID property_id)
{
switch (property_id) {
""")
def get_shorthands_for_longhand(longhand: str) -> list:
shorthands = []
for immediate in shorthands_for_longhand_map.get(longhand, []):
shorthands.append(immediate)
if immediate in shorthands_for_longhand_map:
shorthands.extend(get_shorthands_for_longhand(immediate))
# https://www.w3.org/TR/cssom/#concept-shorthands-preferred-order
# Combined sort key:
# 4. Number of longhand properties (greatest first)
# 2. Items starting with "-" last
# 3. Items starting with "-webkit-" before other "-" items
# 1. Lexicographic
def sort_key(shorthand):
num_longhands = len(get_expanded_longhands(shorthand))
starts_with_dash = shorthand.startswith("-")
starts_with_webkit = shorthand.startswith("-webkit-")
# We want descending on num_longhands, so negate.
# For dash ordering: starts_with_dash True should sort LATER (tuple sort is ascending).
# Among dashed: -webkit- should be first (True < False? no, False < True).
# Actually, if both start with dash, the one starting with -webkit- should come first.
# So (starts_with_dash=True, starts_with_webkit=True) < (starts_with_dash=True, starts_with_webkit=False)
# means starts_with_webkit=True must be smaller: use NOT starts_with_webkit.
return (
-num_longhands,
starts_with_dash,
not starts_with_webkit,
shorthand,
)
shorthands.sort(key=sort_key)
return shorthands
for longhand in shorthands_for_longhand_map:
shorthands = ", ".join(f"PropertyID::{title_casify(s)}" for s in get_shorthands_for_longhand(longhand))
out.write(f"""
case PropertyID::{title_casify(longhand)}: {{
static auto const& shorthands = *new Vector<PropertyID> {{ {shorthands} }};
return shorthands;
}}""")
out.write("""
default: {
static auto const& empty_shorthands = *new Vector<PropertyID>;
return empty_shorthands;
}
}
}
""")
out.write("""
bool property_is_positional_value_list_shorthand(PropertyID property_id)
{
switch (property_id)
{
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
if "positional-value-list-shorthand" in value:
out.write(f" case PropertyID::{title_casify(name)}:\n")
out.write("""
return true;
default:
return false;
}
}
""")
out.write("""
bool property_is_logical_alias(PropertyID property_id)
{
switch(property_id) {
""")
for name, value in properties.items():
if is_legacy_alias(value):
continue
if "logical-alias-for" in value:
out.write(f" case PropertyID::{title_casify(name)}:\n")
out.write("""
return true;
default:
return false;
}
}
PropertyID map_logical_alias_to_physical_property(PropertyID property_id, LogicalAliasMappingContext const& mapping_context)
{
// https://drafts.csswg.org/css-writing-modes-4/#logical-to-physical
// FIXME: Note: The used direction depends on the computed writing-mode and text-orientation: in vertical writing
// modes, a text-orientation value of upright forces the used direction to ltr.
auto used_direction = mapping_context.direction;
switch(property_id) {
""")
for property_name, value in properties.items():
if is_legacy_alias(value):
continue
logical_alias_for = value.get("logical-alias-for")
if logical_alias_for is None:
continue
group_name = logical_alias_for.get("group")
mapping = logical_alias_for.get("mapping")
if group_name is None or mapping is None:
print(f"Logical alias '{property_name}' is missing either its group or its mapping!", file=sys.stderr)
sys.exit(1)
group = logical_property_groups.get(group_name)
if group is None:
print(f"Logical alias '{property_name}' has unrecognized group '{group_name}'", file=sys.stderr)
sys.exit(1)
def mapped_property(entry_name: str, group=group, group_name=group_name, property_name=property_name) -> str:
physical = group["physical"]
if entry_name not in physical:
print(
f"Logical property group '{group_name}' is missing entry for '{entry_name}', "
f"requested by property '{property_name}'.",
file=sys.stderr,
)
sys.exit(1)
return title_casify(physical[entry_name])
name_titlecase = title_casify(property_name)
out.write(f" case PropertyID::{name_titlecase}:\n")
if mapping == "block-end":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb)
return PropertyID::{mapped_property("bottom")};
if (first_is_one_of(mapping_context.writing_mode, WritingMode::VerticalRl, WritingMode::SidewaysRl))
return PropertyID::{mapped_property("left")};
return PropertyID::{mapped_property("right")};
""")
elif mapping == "block-size":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb)
return PropertyID::{mapped_property("height")};
return PropertyID::{mapped_property("width")};
""")
elif mapping == "block-start":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb)
return PropertyID::{mapped_property("top")};
if (first_is_one_of(mapping_context.writing_mode, WritingMode::VerticalRl, WritingMode::SidewaysRl))
return PropertyID::{mapped_property("right")};
return PropertyID::{mapped_property("left")};
""")
elif mapping == "end-end":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("bottom-right")};
return PropertyID::{mapped_property("bottom-left")};
}}
if (first_is_one_of(mapping_context.writing_mode, WritingMode::VerticalRl, WritingMode::SidewaysRl)) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("bottom-left")};
return PropertyID::{mapped_property("top-left")};
}}
if (mapping_context.writing_mode == WritingMode::VerticalLr) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("bottom-right")};
return PropertyID::{mapped_property("top-right")};
}}
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("top-right")};
return PropertyID::{mapped_property("bottom-right")};
""")
elif mapping == "end-start":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("bottom-left")};
return PropertyID::{mapped_property("bottom-right")};
}}
if (first_is_one_of(mapping_context.writing_mode, WritingMode::VerticalRl, WritingMode::SidewaysRl)) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("top-left")};
return PropertyID::{mapped_property("bottom-left")};
}}
if (mapping_context.writing_mode == WritingMode::VerticalLr) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("top-right")};
return PropertyID::{mapped_property("bottom-right")};
}}
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("bottom-right")};
return PropertyID::{mapped_property("top-right")};
""")
elif mapping == "inline-end":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("right")};
return PropertyID::{mapped_property("left")};
}}
if (first_is_one_of(mapping_context.writing_mode, WritingMode::VerticalRl, WritingMode::SidewaysRl, WritingMode::VerticalLr)) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("bottom")};
return PropertyID::{mapped_property("top")};
}}
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("top")};
return PropertyID::{mapped_property("bottom")};
""")
elif mapping == "inline-size":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb)
return PropertyID::{mapped_property("width")};
return PropertyID::{mapped_property("height")};
""")
elif mapping == "inline-start":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("left")};
return PropertyID::{mapped_property("right")};
}}
if (first_is_one_of(mapping_context.writing_mode, WritingMode::VerticalRl, WritingMode::SidewaysRl, WritingMode::VerticalLr)) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("top")};
return PropertyID::{mapped_property("bottom")};
}}
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("bottom")};
return PropertyID::{mapped_property("top")};
""")
elif mapping == "start-end":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("top-right")};
return PropertyID::{mapped_property("top-left")};
}}
if (first_is_one_of(mapping_context.writing_mode, WritingMode::VerticalRl, WritingMode::SidewaysRl)) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("bottom-right")};
return PropertyID::{mapped_property("top-right")};
}}
if (mapping_context.writing_mode == WritingMode::VerticalLr) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("bottom-left")};
return PropertyID::{mapped_property("top-left")};
}}
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("top-left")};
return PropertyID::{mapped_property("bottom-left")};
""")
elif mapping == "start-start":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("top-left")};
return PropertyID::{mapped_property("top-right")};
}}
if (first_is_one_of(mapping_context.writing_mode, WritingMode::VerticalRl, WritingMode::SidewaysRl)) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("top-right")};
return PropertyID::{mapped_property("bottom-right")};
}}
if (mapping_context.writing_mode == WritingMode::VerticalLr) {{
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("top-left")};
return PropertyID::{mapped_property("bottom-left")};
}}
if (used_direction == Direction::Ltr)
return PropertyID::{mapped_property("bottom-left")};
return PropertyID::{mapped_property("top-left")};
""")
elif mapping == "block-xy":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb)
return PropertyID::{mapped_property("y")};
return PropertyID::{mapped_property("x")};
""")
elif mapping == "inline-xy":
out.write(f"""
if (mapping_context.writing_mode == WritingMode::HorizontalTb)
return PropertyID::{mapped_property("x")};
return PropertyID::{mapped_property("y")};
""")
else:
print(f"Logical alias '{property_name}' has unrecognized mapping '{mapping}'", file=sys.stderr)
sys.exit(1)
out.write("""
default:
VERIFY(!property_is_logical_alias(property_id));
return property_id;
}
}
Optional<LogicalPropertyGroup> logical_property_group_for_property(PropertyID property_id)
{
switch(property_id) {
""")
# Build HashMap<String, Vector<String>> of all properties (physical + logical) per group
logical_property_group_members = {}
for group_name, mapping in logical_property_groups.items():
members = logical_property_group_members.setdefault(group_name, [])
for physical_property in mapping["physical"].values():
members.append(physical_property)
for logical_property in mapping["logical"].values():
members.append(logical_property)
for group_name, group_properties in logical_property_group_members.items():
out.writelines(f" case PropertyID::{title_casify(prop_name)}:\n" for prop_name in group_properties)
out.write(f" return LogicalPropertyGroup::{title_casify(group_name)};\n")
out.write("""
default:
return {};
}
}
} // namespace Web::CSS
""")
def main():
parser = argparse.ArgumentParser(description="Generate CSS PropertyID", 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 PropertyID header file to generate")
parser.add_argument(
"-c", "--implementation", required=True, help="Path to the PropertyID implementation file to generate"
)
parser.add_argument("-j", "--properties-json", required=True, help="Path to the properties JSON file to read from")
parser.add_argument("-e", "--enums-json", required=True, help="Path to the enums JSON file to read from")
parser.add_argument(
"-g", "--groups-json", required=True, help="Path to the logical property groups JSON file to read from"
)
parser.add_argument(
"--property-list", action="store_true", help="Generate the LibWebCommon property list instead of PropertyID"
)
args = parser.parse_args()
with open(args.properties_json, "r", encoding="utf-8") as f:
properties = json.load(f)
with open(args.enums_json, "r", encoding="utf-8") as f:
enums = json.load(f)
with open(args.groups_json, "r", encoding="utf-8") as f:
logical_property_groups = json.load(f)
verify_alphabetical(properties, args.properties_json)
verify_alphabetical(logical_property_groups, args.groups_json)
verify_alphabetical(enums, args.enums_json)
enum_names = list(enums.keys())
replace_logical_aliases(properties, logical_property_groups)
populate_all_property_longhands(properties)
if args.property_list:
with open(args.header, "w", encoding="utf-8") as f:
write_property_list_header_file(f)
with open(args.implementation, "w", encoding="utf-8") as f:
write_property_list_implementation_file(f, properties)
return
with open(args.header, "w", encoding="utf-8") as f:
write_header_file(f, properties, logical_property_groups)
with open(args.implementation, "w", encoding="utf-8") as f:
write_implementation_file(f, properties, logical_property_groups, enum_names)
if __name__ == "__main__":
main()