dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / parser / tests_css_rule_parsing.lucb
#==============================================================================================
#
# tests_css_rule_parsing - tests of rule, selector, media, descriptor and syntax parsing
# (region r31)
#
# DESCRIPTION:
# Ladybird has no unit tests for RuleParsing.cpp, SelectorParsing.cpp, MediaParsing.cpp,
# DescriptorParsing.cpp, Syntax.cpp or SyntaxParsing.cpp. The oracle cases
# (tests_css_rule_parsing_cases) run the parsers on 283 inputs and compare dumps made the
# way the local oracle makes them from the reference build, with the ErrorReporter's
# errors after each case: <syntax> definitions (their dump and serialization),
# <compound-selector>s and every kind of simple selector they hold, An+B patterns,
# combinators, <wq-name>s, <complex-selector> and @page selector errors, <media-type>
# failures, <layer-name>s, the error paths of the rule converters (on rules made
# directly: parsing at-rules from a stylesheet needs r37) and @function
# preludes. The focused tests below pin the rest to the C++ (values read from the donor's
# code): the <mf-value> token classes, <mf-name>, <mf-comparison>, the media query
# modifiers, and the syntax nodes made directly.
#
# Paths that reach other regions' stubs are not exercised yet: Selector::create and the
# PseudoElementSelector constructor (r38), MediaQuery and MediaFeature (r42), the CSSOM
# rule objects (r37), value parsing (r32, r33), DescriptorNameAndID and value_type_from_string
# (r36).
#
#==============================================================================================
# mark: The oracle dumps ========================================================================
## A parser over `input` whose declared namespaces are the oracle's: `svg`.
func test_css_rule_parsing_parser(input: str) -> CssParser:
var params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(¶ms)
_ = ak.hash_table_set(¶ms.declared_namespaces, ak.fly_string_from_str("svg"))
return css_parser_create(params, ak.sv(input))
## "SyntaxError" or "IncludesIgnoredVendorPrefix": the ParseError a failure carries.
func test_css_parse_error_name(code: ErrorCode) -> ak.String:
if code == css_parser_parse_error_syntax_error:
return ak.string_from_str("SyntaxError")
return ak.string_from_str("IncludesIgnoredVendorPrefix")
## The oracle's `combinator_name`.
func test_css_combinator_name(combinator: CssSelectorCombinator) -> str:
match combinator:
.none_: return "None"
.immediate_child: return "ImmediateChild"
.descendant: return "Descendant"
.next_sibling: return "NextSibling"
.subsequent_sibling: return "SubsequentSibling"
.column: return "Column"
_: trap("VERIFY_NOT_REACHED")
## The oracle's `qualified_name`: "ns=N ns_name='...' name='...' lower='...'".
func test_css_qualified_name(name: const CssSelectorSimpleSelectorQualifiedName*) -> ak.String:
return css_string_formatted("ns={} ns_name='{}' name='{}' lower='{}'", [ak.arg_i64((i64)name.namespace_type), css_arg_fly_string(&name.namespace_),
css_arg_fly_string(&name.name.name), css_arg_fly_string(&name.name.lowercase_name)])
## A simple selector's Name value.
func test_css_simple_selector_name(simple: const CssSelectorSimpleSelector*) -> CssSelectorSimpleSelectorName:
match simple.value:
.css_selector_simple_selector_name(name): return name
_: trap("not a name")
## A simple selector's QualifiedName value.
func test_css_simple_selector_qualified_name(simple: const CssSelectorSimpleSelector*) -> CssSelectorSimpleSelectorQualifiedName:
match simple.value:
.css_selector_simple_selector_qualified_name(name): return name
_: trap("not a qualified name")
## The languages of a pseudo-class, joined by ",".
func test_css_joined_fly_strings(strings: const ak.Vector[ak.FlyString]*) -> ak.String:
var builder = ak.string_builder_new()
for string in ak.vector_span_const(strings):
if not ak.string_builder_is_empty(&builder):
ak.string_builder_append(&builder, ",")
ak.string_builder_append_string_view(&builder, ak.fly_string_bytes_as_string_view(&string))
return ak.string_builder_to_string_without_validation(&builder)
## The oracle's `dump_simple`: one line per simple selector.
func test_css_dump_simple(out: ak.StringBuilder*, simple: const CssSelectorSimpleSelector*, indent: usize):
match simple.type_:
.universal:
let name = test_css_simple_selector_qualified_name(simple)
let text = test_css_qualified_name(&name)
test_css_line(out, indent, css_string_formatted("Universal {}", [css_arg_string(&text)]))
.tag_name:
let name = test_css_simple_selector_qualified_name(simple)
let text = test_css_qualified_name(&name)
test_css_line(out, indent, css_string_formatted("TagName {}", [css_arg_string(&text)]))
.id:
let name = test_css_simple_selector_name(simple)
test_css_line(out, indent, css_string_formatted("Id name='{}' lower='{}'", [css_arg_fly_string(&name.name), css_arg_fly_string(&name.lowercase_name)]))
.class_:
let name = test_css_simple_selector_name(simple)
test_css_line(out, indent, css_string_formatted("Class name='{}' lower='{}'", [css_arg_fly_string(&name.name), css_arg_fly_string(&name.lowercase_name)]))
.attribute:
match simple.value:
.css_selector_simple_selector_attribute(attribute):
let name = test_css_qualified_name(&attribute.qualified_name)
test_css_line(out, indent, css_string_formatted("Attribute match={} case={} {} value='{}'",
[ak.arg_i64((i64)attribute.match_type), ak.arg_i64((i64)attribute.case_type), css_arg_string(&name),
css_arg_string(&attribute.value)]))
_:
trap("not an attribute")
.pseudo_class:
match simple.value:
.css_selector_simple_selector_pseudo_class_selector(pseudo_class):
var levels = ak.string_builder_new()
for level in ak.vector_span_const(&pseudo_class.levels):
dump_appendff(&levels, "{},", [ak.arg_i64(level)])
var ident = ak.string_from_str("-")
if let pseudo_class_ident = pseudo_class.ident:
ident = css_string_formatted("{}/{}", [ak.arg_string_view(css_data.string_from_keyword(pseudo_class_ident.keyword)),
css_arg_fly_string(&pseudo_class_ident.string_value)])
let languages = test_css_joined_fly_strings(&pseudo_class.languages)
let levels_text = ak.string_builder_to_string_without_validation(&levels)
test_css_line(out, indent, css_string_formatted("PseudoClass {} anb={},{} forgiving={} args={} langs=[{}] ident={} levels=[{}]", [
ak.arg_string_view(css_data.pseudo_class_name(pseudo_class.type_)),
ak.arg_i64((i64)pseudo_class.an_plus_b_pattern.step_size),
ak.arg_i64((i64)pseudo_class.an_plus_b_pattern.offset),
ak.arg_i64(1 if pseudo_class.is_forgiving else 0),
ak.arg_u64((u64)ak.vector_size(&pseudo_class.argument_selector_list)),
css_arg_string(&languages),
css_arg_string(&ident),
css_arg_string(&levels_text),
]))
_:
trap("not a pseudo-class")
.pseudo_element:
test_css_line(out, indent, ak.string_from_str("PseudoElement"))
.nesting:
test_css_line(out, indent, ak.string_from_str("Nesting"))
.invalid:
test_css_line(out, indent, ak.string_from_str("Invalid"))
_:
trap("VERIFY_NOT_REACHED")
## "rest: N": the tokens the parser left in the stream.
func test_css_dump_rest(out: ak.StringBuilder*, tokens: const css_syntax.TokenStream[css_syntax.ComponentValue]*):
test_css_line(out, 1, css_string_formatted("rest: {}", [ak.arg_u64((u64)css_syntax.token_stream_remaining_token_count(tokens))]))
## The ErrorReporter's errors, "error: <message> (<occurrences>)", in its table's order.
func test_css_dump_errors(out: ak.StringBuilder*):
for entry in ak.hash_map_begin(&css_error_reporter_the().m_errors):
let message = css_serialize_parsing_error(&entry.key)
test_css_line(out, 1, css_string_formatted("error: {} ({})", [css_arg_string(&message), ak.arg_u64((u64)entry.value.occurrences)]))
## Runs one oracle case and returns its dump.
func test_css_rule_parsing_run_case(mode: str, input: str) -> ak.String:
var out = ak.string_builder_new()
ak.hash_map_clear(&css_error_reporter_the().m_errors)
var parser = test_css_rule_parsing_parser(input)
var values_parser = test_css_rule_parsing_parser(input)
let values = css_parser_parse_as_list_of_component_values(&values_parser)
var tokens = css_syntax.token_stream_create(values)
if mode == "syntax" or mode == "syntaxcustom":
let limit = CssLimitSingleComponentIdentToCustomIdent.no if mode == "syntax" else CssLimitSingleComponentIdentToCustomIdent.yes
if let node = css_parse_as_syntax(values, limit):
let dumped = css_syntax_node_dump(node)
let dumped_view = ak.string_bytes_as_string_view(&dumped)
let lines = ak.string_view_split_view_u8(&dumped_view, '\n')
for line in ak.vector_span_const(&lines):
test_css_line(&out, 1, ak.string_from_utf8(line) else trap("UTF-8"))
let text = css_syntax_node_to_string(node)
test_css_line(&out, 1, css_string_formatted("to_string: {}", [css_arg_string(&text)]))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
elif mode == "compound":
let compound: CssSelectorCompoundSelector? = css_parser_parse_compound_selector(&parser, &tokens) catch failure:
let name = test_css_parse_error_name(failure.code)
test_css_line(&out, 1, css_string_formatted("error {}", [css_arg_string(&name)]))
recover none
if let compound_selector = compound:
test_css_line(&out, 1, css_string_formatted("combinator {}", [ak.arg_str(test_css_combinator_name(compound_selector.combinator))]))
for simple in ak.vector_span_const(&compound_selector.simple_selectors):
test_css_dump_simple(&out, &simple, 2)
test_css_dump_rest(&out, &tokens)
elif mode == "anb":
if let pattern = css_parser_parse_a_n_plus_b_pattern(&parser, &tokens):
test_css_line(&out, 1, css_string_formatted("{} {}", [ak.arg_i64((i64)pattern.step_size), ak.arg_i64((i64)pattern.offset)]))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
test_css_dump_rest(&out, &tokens)
elif mode == "combinator":
if let combinator = css_parser_parse_selector_combinator(&parser, &tokens):
test_css_line(&out, 1, ak.string_from_str(test_css_combinator_name(combinator)))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
test_css_dump_rest(&out, &tokens)
elif mode == "qname" or mode == "qnamenowild":
let allow = CssParserAllowWildcardName.yes if mode == "qname" else CssParserAllowWildcardName.no
if let name = css_parser_parse_selector_qualified_name(&parser, &tokens, allow):
test_css_line(&out, 1, test_css_qualified_name(&name))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
test_css_dump_rest(&out, &tokens)
elif mode == "complexerr" or mode == "relativeerr":
let selector_type = CssParserSelectorType.standalone if mode == "complexerr" else CssParserSelectorType.relative
let selector: CssSelector*? = css_parser_parse_complex_selector(&parser, &tokens, selector_type) catch failure:
let name = test_css_parse_error_name(failure.code)
test_css_line(&out, 1, css_string_formatted("error {}", [css_arg_string(&name)]))
recover none
if selector != none:
test_css_line(&out, 1, ak.string_from_str("selector"))
elif mode == "pageerr":
let list: ak.Vector[CssPageSelector]? = css_parser_parse_a_page_selector_list(&parser, &tokens) catch failure:
let name = test_css_parse_error_name(failure.code)
test_css_line(&out, 1, css_string_formatted("error {}", [css_arg_string(&name)]))
recover none
if list != none:
test_css_line(&out, 1, ak.string_from_str("list"))
elif mode == "mediatype":
if let media_type = css_parser_parse_media_type(&parser, &tokens):
test_css_line(&out, 1, ak.fly_string_to_string_const_2(&media_type.name))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
test_css_dump_rest(&out, &tokens)
elif mode == "layername" or mode == "layernameblank":
let allow = CssParserAllowBlankLayerName.no if mode == "layername" else CssParserAllowBlankLayerName.yes
if let name = css_parser_parse_layer_name(&parser, &tokens, allow):
test_css_line(&out, 1, css_string_formatted("'{}'", [css_arg_fly_string(&name)]))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
test_css_dump_rest(&out, &tokens)
elif mode == "atrule" or mode == "atblock" or mode == "style":
var rule = css_syntax.Rule.qualified_rule(css_syntax.QualifiedRule(prelude = values))
if mode != "style":
let input_view = ak.sv(input)
let space = ak.string_view_find_u8(&input_view, ' ') else trap("a space after the at-rule's name")
let name = ak.string_view_substring_view_usize(&input_view, 0, space)
var prelude_parser = test_css_rule_parsing_parser(ak.string_view_text(ak.string_view_substring_view(&input_view, space + 1)))
rule = css_syntax.Rule.at_rule(css_syntax.AtRule(
name = ak.fly_string_from_utf8(name) else trap("UTF-8"),
prelude = css_parser_parse_as_list_of_component_values(&prelude_parser),
is_block_rule = mode == "atblock",
))
let converted = css_parser_convert_to_rule[CssNestedDeclarations](&parser, rule, CssParserNested.no)
test_css_line(&out, 1, ak.string_from_str("rule" if converted != none else "none"))
elif mode == "prelude":
if let prelude = css_parser_parse_function_prelude(&parser, &tokens):
let return_type = css_syntax_node_to_string(prelude.return_type)
test_css_line(&out, 1, css_string_formatted("name {} params {} returns {}", [css_arg_fly_string(&prelude.name), ak.arg_u64((u64)ak.vector_size(&prelude.parameters)),
css_arg_string(&return_type)]))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
test_css_dump_rest(&out, &tokens)
else:
trap("unknown oracle case mode")
test_css_dump_errors(&out)
return ak.string_builder_to_string_without_validation(&out)
test "rule, selector, media and syntax parsing match the reference build on the oracle cases":
with memory.heap:
var failures = 0
for case_ in test_css_rule_parsing_cases:
let actual = test_css_rule_parsing_run_case(case_.mode, case_.input)
let actual_view = ak.string_bytes_as_string_view(&actual)
if not ak.string_view_equals(&actual_view, case_.expected):
failures += 1
print(f"case {case_.mode} {case_.input}\nexpected:\n{case_.expected}actual:\n{ak.string_view_text(actual_view)}")
assert(failures == 0)
# mark: Focused tests ===========================================================================
## The component values of `input`.
func test_css_rule_parsing_values(input: str) -> ak.Vector[css_syntax.ComponentValue]:
var parser = test_css_rule_parsing_parser(input)
return css_parser_parse_as_list_of_component_values(&parser)
test "is_media_feature_value_token accepts the value tokens and rejects the punctuation (MediaParsing.cpp:450)":
with memory.heap:
# ident function at-keyword hash string number percentage dimension whitespace comma, then url, a
# delim other than < > =, a block and a function (component values that are not tokens).
let accepted = test_css_rule_parsing_values("a f( @k #h \"s\" 1 2% 3px , url(x) ! [b] g(c)")
for value in ak.vector_span_const(&accepted):
assert(css_is_media_feature_value_token(value))
# < > = : ; ) ] } and <!-- -->.
let rejected = test_css_rule_parsing_values("<>=:;)]}<!---->")
assert(ak.vector_size(&rejected) == 10)
for value in ak.vector_span_const(&rejected):
assert(not css_is_media_feature_value_token(value))
# An unclosed string is a bad-string token, an url with a space a bad-url token: both value tokens.
let bad = test_css_rule_parsing_values("url(a b)")
assert(css_is_media_feature_value_token(*ak.vector_first_const(&bad)))
## parse_mf_name on `input`: "normal|min|max <feature>" or "none", and the tokens left.
func test_css_mf_name(input: str, allow_min_max_prefix: bool) -> (str, usize):
let values = test_css_rule_parsing_values(input)
var tokens = css_syntax.token_stream_create(values)
let name = css_parse_mf_name(&tokens, allow_min_max_prefix)
let remaining = css_syntax.token_stream_remaining_token_count(&tokens)
if let parsed = name:
let prefix = match parsed.type_:
.normal => "normal"
.min => "min"
.max => "max"
_ => trap("VERIFY_NOT_REACHED")
let feature = css_data.string_from_media_feature_id(parsed.id)
assert(ak.string_view_equals(&feature, "width") or ak.string_view_equals(&feature, "color"))
return (prefix, remaining)
return ("none", remaining)
test "<mf-name> takes known features, and min-/max- prefixes of range features only when allowed (MediaParsing.cpp:186)":
with memory.heap:
assert(test_css_mf_name("width", false) == ("normal", 0))
assert(test_css_mf_name("WIDTH x", false) == ("normal", 2))
assert(test_css_mf_name("min-width", true) == ("min", 0))
assert(test_css_mf_name("MAX-width", true) == ("max", 0))
assert(test_css_mf_name("min-color", true) == ("min", 0))
# Not allowed, an unknown feature, a non-range feature with a prefix, a non-ident: rewound.
assert(test_css_mf_name("min-width", false) == ("none", 1))
assert(test_css_mf_name("bogus", true) == ("none", 1))
assert(test_css_mf_name("min-grid", true) == ("none", 1))
assert(test_css_mf_name("(width)", true) == ("none", 1))
## parse_comparison on `input`, and the tokens left.
func test_css_mf_comparison(input: str) -> (CssMediaFeatureComparison?, usize):
let values = test_css_rule_parsing_values(input)
var tokens = css_syntax.token_stream_create(values)
let comparison = css_parse_mf_comparison(&tokens)
return (comparison, css_syntax.token_stream_remaining_token_count(&tokens))
test "<mf-comparison> reads =, <, <=, > and >= after whitespace, and nothing else (MediaParsing.cpp:256)":
with memory.heap:
assert(test_css_mf_comparison("=") == (CssMediaFeatureComparison.equal, 0))
assert(test_css_mf_comparison(" < 1") == (CssMediaFeatureComparison.less_than, 2))
assert(test_css_mf_comparison("<=1") == (CssMediaFeatureComparison.less_than_or_equal, 1))
assert(test_css_mf_comparison("< =") == (CssMediaFeatureComparison.less_than, 2))
assert(test_css_mf_comparison(">") == (CssMediaFeatureComparison.greater_than, 0))
assert(test_css_mf_comparison(" >=") == (CssMediaFeatureComparison.greater_than_or_equal, 0))
assert(test_css_mf_comparison("=<") == (CssMediaFeatureComparison.equal, 1))
assert(test_css_mf_comparison("!") == (none, 1))
assert(test_css_mf_comparison("width") == (none, 1))
test "comparisons match when both are = or point the same way (MediaParsing.cpp:290)":
let comparisons: CssMediaFeatureComparison[5] = [.equal, .less_than, .less_than_or_equal, .greater_than, .greater_than_or_equal]
let groups: i32[5] = [0, 1, 1, 2, 2]
for i in 0..<5:
for j in 0..<5:
assert(css_mf_comparisons_match(comparisons[i], comparisons[j]) == (groups[i] == groups[j]))
## The `[ not | only ]?` of a media query on `input`, and the tokens left.
func test_css_media_query_modifier(input: str) -> (bool?, usize):
let values = test_css_rule_parsing_values(input)
var tokens = css_syntax.token_stream_create(values)
let modifier = css_parse_media_query_initial_modifier(&tokens)
return (modifier, css_syntax.token_stream_remaining_token_count(&tokens))
test "a media query's not / only modifier negates or not, and anything else is rewound (MediaParsing.cpp:69)":
with memory.heap:
assert(test_css_media_query_modifier("not screen") == (true, 2))
assert(test_css_media_query_modifier(" ONLY screen") == (false, 2))
assert(test_css_media_query_modifier("screen") == (none, 1))
assert(test_css_media_query_modifier("1") == (none, 1))
assert(test_css_media_query_modifier("") == (none, 0))
test "syntax nodes made directly: <type> nodes, multipliers and alternatives dump and serialize as the C++ (Syntax.cpp)":
with memory.heap:
# TypeSyntaxNode::create looks the type up with r36's value_type_from_string; the constructor does not.
let length = &(new CssTypeSyntaxNode[1] --- else trap("out of memory"))[0]
css_type_syntax_node_construct(length, ak.fly_string_from_str("length"), none)
let ident = css_ident_syntax_node_create(ak.fly_string_from_str("a b"), ak.CaseSensitivity.case_sensitive)
var children = ak.Vector[CssSyntaxNode*]()
ak.vector_append_t(&children, css_comma_separated_multiplier_syntax_node_create(length.css_syntax_node()).css_syntax_node())
ak.vector_append_t(&children, css_multiplier_syntax_node_create(ident.css_syntax_node()).css_syntax_node())
ak.vector_append_t(&children, css_universal_syntax_node_create().css_syntax_node())
let alternatives = css_alternatives_syntax_node_create(children)
let node = alternatives.css_syntax_node()
let text = css_syntax_node_to_string(node)
let text_view = ak.string_bytes_as_string_view(&text)
assert(ak.string_view_equals(&text_view, "<length># | a\\ b+ | *"))
let dump = css_syntax_node_dump(node)
let dump_view = ak.string_bytes_as_string_view(&dump)
assert(ak.string_view_equals(&dump_view, "Alternatives:\n CommaSeparatedMultiplier:\n Type: length\n Multiplier:\n Ident: a b\n Universal\n"))
assert(css_syntax_node_type(node) == CssSyntaxNodeNodeType.alternatives)
assert(is_css_alternatives_syntax_node(node) and not is_css_ident_syntax_node(node))
assert(css_type_syntax_node_value_type(length) == none)
let type_name = css_type_syntax_node_type_name(length)
assert(ak.fly_string_eq_string_view(&type_name, ak.sv("length")))
assert(css_alternatives_syntax_node_children_2(alternatives).length == 3)
assert(css_ident_syntax_node_case_sensitivity(as_css_ident_syntax_node(ident.css_syntax_node())) == ak.CaseSensitivity.case_sensitive)
test "join of component values concatenates their strings (String::join(\"\"sv, ...))":
with memory.heap:
let values = test_css_rule_parsing_values("a (b)f(1,2) \"x\"")
let joined = css_join_component_values(&values)
let joined_view = ak.string_bytes_as_string_view(&joined)
assert(ak.string_view_equals(&joined_view, "a (b)f(1,2) \"x\""))