Meta: Read dimensions from Units.json in CSS grammar parser

Avoids us having to maintain a separate hardcoded list.

This does mean we don't support parsing of `decibel` but it's not used
anywhere yet and will be supported automatically when added to
Units.json
This commit is contained in:
Callum Law 2026-05-01 12:20:15 +12:00 committed by Sam Atkins
parent c96e8f15e5
commit a9d2b1dde6
4 changed files with 12 additions and 10 deletions

View file

@ -93,6 +93,8 @@ function (generate_css_implementation)
"CSS/Parser/GeneratedValueTypesParsing.h"
"CSS/Parser/GeneratedValueTypesParsing.cpp"
arguments -j "${LIBWEB_INPUT_FOLDER}/CSS/ValueTypes.json"
-u "${LIBWEB_INPUT_FOLDER}/CSS/Units.json"
dependencies "${LIBWEB_INPUT_FOLDER}/CSS/Units.json"
)
invoke_py_generator(

View file

@ -14,6 +14,7 @@ from typing import TextIO
sys.path.append(str(Path(__file__).resolve().parent.parent))
from Utils.css_dimensions import load_css_dimensions
from Utils.CSSGrammar.generator import generate_css_parser_expression_for_grammar
from Utils.utils import snake_casify
@ -135,8 +136,11 @@ def main() -> int:
help="Path to the GeneratedValueTypesParsing implementation file to generate",
)
parser.add_argument("-j", "--json", required=True, help="Path to the JSON file to read from")
parser.add_argument("-u", "--units-json", required=True, help="Path to Units.json")
args = parser.parse_args()
load_css_dimensions(args.units_json)
with open(args.json, "r", encoding="utf-8") as json_file:
value_type_data = json.load(json_file)

View file

@ -3,6 +3,8 @@ from math import inf
from typing import Optional
from typing import Union
from Utils.css_dimensions import get_css_dimensions
@dataclass(frozen=True)
class NumericTypeRangeRestriction:
@ -22,16 +24,7 @@ def bound_value_to_string(value: float) -> str:
def is_dimension_type(type_name: str) -> bool:
# NB: Keep this up to date with the list of dimensions in Units.json
return type_name in (
"angle",
"decibel",
"flex",
"frequency",
"length",
"resolution",
"time",
)
return type_name in get_css_dimensions().keys()
def is_dimension_percentage_mix_type(type_name: str) -> bool:

View file

@ -6,8 +6,11 @@ from pathlib import Path
sys.path.insert(0, str(Path(os.environ["LADYBIRD_SOURCE_DIR"]) / "Meta"))
from Utils.css_dimensions import load_css_dimensions
from Utils.CSSGrammar.Parser.parser import parse_value_definition_grammar
load_css_dimensions(str(Path(os.environ["LADYBIRD_SOURCE_DIR"]).joinpath("Libraries/LibWeb/CSS/Units.json")))
class TestCSSGrammarParser(unittest.TestCase):
def test_parse_type_reference(self) -> None: