[css] Add a "did you mean" suggestion when an unknown CSS property is spotted

So using "bckgroundu: red" in a CSS file will report _"unknown declaration 'bckgroundu'; did you mean 'background'?"_
This commit is contained in:
Olivier Philippon
2022-04-29 10:28:30 +01:00
parent 6fba64face
commit 2c03f8cfe1
2 changed files with 69 additions and 13 deletions

View File

@@ -53,3 +53,40 @@ def test_color_property_parsing(css_value, expectation, expected_color):
if expected_color:
css_rule = stylesheet.rules[0]
assert css_rule.styles.background == expected_color
@pytest.mark.parametrize(
"css_property_name,expected_property_name_suggestion",
[
["backgroundu", "background"],
["bckgroundu", "background"],
["colr", "color"],
["colour", "color"],
["wdth", "width"],
["wth", "width"],
["wh", None],
["xkcd", None],
],
)
def test_did_you_mean_for_css_property_names(
css_property_name, expected_property_name_suggestion
):
stylesheet = Stylesheet()
css = """
* {
border: blue;
${PROPERTY}: red;
}
""".replace(
"${PROPERTY}", css_property_name
)
with pytest.raises(StylesheetParseError) as err:
stylesheet.parse(css)
error_token, error_message = err.value.errors.stylesheet.rules[0].errors[0]
if expected_property_name_suggestion is None:
assert "did you mean" not in error_message
else:
expected_did_you_mean_error_message = f"unknown declaration '{css_property_name}'; did you mean '{expected_property_name_suggestion}'?"
assert expected_did_you_mean_error_message == error_message