dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / parser / tests_css_parser_2.lucb
#==============================================================================================
#
# tests_css_parser_2 - focused tests of the CSS parser core (region r30b)
#
# DESCRIPTION:
# The ErrorReporter's table and css_syntax's reporting hook, the rule contexts, the
# ParsingParams constructors, the value parsing contexts, substitution-function
# presence, <general-enclosed>'s <any-value> check, and the comma-separated and
# unicode-range serializers. Expected values follow from the C++ (each test says
# where).
#
#==============================================================================================
test "the ErrorReporter counts repeated errors once, by value, and keeps its own copy":
with memory.heap:
var reporter = CssErrorReporter()
let first = CssParsingError.css_invalid_value_error(CssInvalidValueError(value_type = ak.fly_string_from_str("sizes attribute"),
value_string = ak.string_from_str("a long value string"), description = ak.string_from_str("Failed in step 3.1; all whitespace")))
let same = CssParsingError.css_invalid_value_error(CssInvalidValueError(value_type = ak.fly_string_from_str("sizes attribute"),
value_string = ak.string_from_str("a long value string"), description = ak.string_from_str("Failed in step 3.1; all whitespace")))
let other = CssParsingError.css_unknown_rule_error(CssUnknownRuleError(rule_name = ak.fly_string_from_str("foo")))
css_error_reporter_report(&reporter, first)
css_error_reporter_report(&reporter, same)
css_error_reporter_report(&reporter, other)
assert(ak.hash_map_size(&reporter.m_errors) == 2)
let counted = ak.hash_map_get_k(&reporter.m_errors, same) else trap("the error is in the table")
assert(counted.occurrences == 2)
let once = ak.hash_map_get_k(&reporter.m_errors, other) else trap("the other error is in the table")
assert(once.occurrences == 1)
# The same kind with another value is another error; another kind with the same names too.
assert(not css_parsing_error_eq(&first, &other))
let location = CssParsingError.css_invalid_rule_location_error(CssInvalidRuleLocationError(outer_rule_name = ak.fly_string_from_str("foo"), inner_rule_name = ak.fly_string_from_str("foo")))
let rule = CssParsingError.css_unknown_rule_error(CssUnknownRuleError(rule_name = ak.fly_string_from_str("foo")))
assert(css_parsing_error_eq(&rule, &other))
assert(not css_parsing_error_eq(&location, &other))
css_error_reporter_dump(&reporter)
test "a parser installs the hook through which css_syntax reports misplaced rules":
with memory.heap:
var parser = test_css_parser("a", false)
_ = css_parser_parse_as_list_of_component_values(&parser)
assert(css_syntax.invalid_rule_location_reporter != none)
# AtRule::for_each_as_declaration_list reports a qualified rule inside the at-rule.
var inner = css_syntax.QualifiedRule()
var children = ak.Vector[css_syntax.RuleOrListOfDeclarations]()
ak.vector_append_t(&children, css_syntax.RuleOrListOfDeclarations.rule(css_syntax.Rule.qualified_rule(inner)))
let at_rule = css_syntax.AtRule(name = ak.fly_string_from_str("font-face"), child_rules_and_lists_of_declarations = children, is_block_rule = true)
css_syntax.at_rule_for_each_as_declaration_list(&at_rule, ak.Function1[const css_syntax.Declaration*, unit](call = test_css_ignore_declaration, context = none))
let expected = CssParsingError.css_invalid_rule_location_error(CssInvalidRuleLocationError(
outer_rule_name = ak.fly_string_from_str("@font-face"), inner_rule_name = ak.fly_string_from_str("qualified-rule")))
assert(ak.hash_map_get_k(&css_error_reporter_the().m_errors, expected) != none)
_ = inner
## A declaration visitor that does nothing.
func test_css_ignore_declaration(context: void*?, declaration: const css_syntax.Declaration*):
_ = context
_ = declaration
test "rule_context_type_for_rule maps every CSSRule type that can hold rules":
assert(css_rule_context_type_for_rule(.container) == CssRuleContext.at_container)
assert(css_rule_context_type_for_rule(.counter_style) == CssRuleContext.at_counter_style)
assert(css_rule_context_type_for_rule(.style) == CssRuleContext.style)
assert(css_rule_context_type_for_rule(.media) == CssRuleContext.at_media)
assert(css_rule_context_type_for_rule(.font_face) == CssRuleContext.at_font_face)
assert(css_rule_context_type_for_rule(.font_feature_values) == CssRuleContext.at_font_feature_values)
assert(css_rule_context_type_for_rule(.function) == CssRuleContext.at_function)
assert(css_rule_context_type_for_rule(.keyframes) == CssRuleContext.at_keyframes)
assert(css_rule_context_type_for_rule(.keyframe) == CssRuleContext.keyframe)
assert(css_rule_context_type_for_rule(.supports) == CssRuleContext.at_supports)
assert(css_rule_context_type_for_rule(.layer_block) == CssRuleContext.at_layer)
assert(css_rule_context_type_for_rule(.margin) == CssRuleContext.margin)
assert(css_rule_context_type_for_rule(.nested_declarations) == CssRuleContext.style)
assert(css_rule_context_type_for_rule(.page) == CssRuleContext.at_page)
assert(css_rule_context_type_for_rule(.property) == CssRuleContext.at_property)
test "the ParsingParams constructors set what C++ sets, and the parser takes copies":
with memory.heap:
var params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(¶ms, CssParsingMode.svg_presentation_attribute)
assert(params.realm == none and params.document == none)
assert(params.mode == CssParsingMode.svg_presentation_attribute)
assert(params.is_ua_style_sheet == CssIsUaStyleSheet.no)
assert(ak.vector_is_empty(¶ms.value_context) and ak.vector_is_empty(¶ms.rule_context))
var with_context: CssParsingParams = ---
css_parsing_params_construct_css_value_parsing_context(&with_context, CssValueParsingContext.property_id(css_data.PropertyId.width))
assert(ak.vector_size(&with_context.value_context) == 1)
assert(with_context.mode == CssParsingMode.normal and with_context.is_ua_style_sheet == CssIsUaStyleSheet.no)
var parser = css_parser_create(with_context, ak.sv("a"))
assert(css_parser_is_parsing_svg_presentation_attribute(&parser) == false)
assert(css_parser_document(&parser) == none and css_parser_window(&parser) == none)
assert(not css_parser_in_quirks_mode(&parser))
# The parser's value context is its own: pushing to it leaves the params' context as it was.
css_parser_push_temporary_value_parsing_context(&parser, CssValueParsingContext.css_special_context(CssSpecialContext.media_condition))
assert(ak.vector_size(&parser.m_value_context) == 2)
assert(ak.vector_size(&with_context.value_context) == 1)
css_parser_pop_temporary_value_parsing_context(&parser)
var svg_parser = css_parser_create(params, ak.sv("a"))
assert(css_parser_is_parsing_svg_presentation_attribute(&svg_parser))
test "value parsing contexts: tree-counting and random functions, the auto sharing name":
with memory.heap:
var params: CssParsingParams = ---
css_parsing_params_construct_css_value_parsing_context(¶ms, CssValueParsingContext.property_id(css_data.PropertyId.width))
var parser = css_parser_create(params, ak.sv(""))
assert(css_parser_context_allows_tree_counting_functions(&parser))
assert(css_parser_context_allows_random_functions(&parser))
# Not in quirks mode (no document): never a quirky length.
assert(not css_parser_context_allows_quirky_length(&parser))
parser.m_random_function_index = 3
let name = css_parser_random_value_sharing_auto_name(&parser)
assert(ak.fly_string_eq_string_view(&name, ak.sv("width 3")))
# A function context inside the property keeps the index; leaving the property resets it.
css_parser_push_temporary_value_parsing_context(&parser, CssValueParsingContext.css_function_context(CssFunctionContext(name = ak.sv("calc"))))
css_parser_pop_temporary_value_parsing_context(&parser)
assert(parser.m_random_function_index == 3)
css_parser_push_temporary_value_parsing_context(&parser, CssValueParsingContext.property_id(css_data.PropertyId.height))
css_parser_pop_temporary_value_parsing_context(&parser)
assert(parser.m_random_function_index == 3)
_ = ak.vector_take_last(&parser.m_value_context)
css_parser_push_temporary_value_parsing_context(&parser, CssValueParsingContext.property_id(css_data.PropertyId.height))
css_parser_pop_temporary_value_parsing_context(&parser)
assert(parser.m_random_function_index == 0)
# Descriptor and special contexts.
css_parser_push_temporary_value_parsing_context(&parser, CssValueParsingContext.css_descriptor_context(CssDescriptorContext(at_rule = .font_face, descriptor = .font_family)))
assert(not css_parser_context_allows_tree_counting_functions(&parser))
css_parser_pop_temporary_value_parsing_context(&parser)
var canvas_params: CssParsingParams = ---
css_parsing_params_construct_css_value_parsing_context(&canvas_params, CssValueParsingContext.css_special_context(CssSpecialContext.canvas_context_generic_value))
var canvas_parser = css_parser_create(canvas_params, ak.sv(""))
assert(not css_parser_context_allows_random_functions(&canvas_parser))
assert(not css_parser_context_allows_tree_counting_functions(&canvas_parser))
var matrix_params: CssParsingParams = ---
css_parsing_params_construct_css_value_parsing_context(&matrix_params, CssValueParsingContext.css_special_context(CssSpecialContext.dom_matrix_init_string))
var matrix_parser = css_parser_create(matrix_params, ak.sv(""))
assert(not css_parser_context_allows_random_functions(&matrix_parser))
assert(not css_parser_context_allows_tree_counting_functions(&matrix_parser))
css_parser_push_temporary_value_parsing_context(&matrix_parser, CssValueParsingContext.property_id(css_data.PropertyId.width))
assert(css_parser_context_allows_random_functions(&matrix_parser))
test "substitution-function presence of values without functions; <any-value>":
with memory.heap:
var parser = test_css_parser("a [b {c}] 1px ) d", false)
let values = css_parser_parse_as_list_of_component_values(&parser)
var presence = css_syntax.SubstitutionFunctionsPresence()
css_parser_collect_arbitrary_substitution_function_presence_vector(&values, &presence) else trap("no syntax error")
assert(not presence.attr and not presence.env and not presence.if_ and not presence.inherit and not presence.var_)
# An unmatched ) is not an <any-value>; a block of tokens is.
assert(not css_parser_contains_only_any_value(&values))
var nested = test_css_parser("a [b {c}] f(1, (2))", false)
let nested_values = css_parser_parse_as_list_of_component_values(&nested)
assert(css_parser_contains_only_any_value(&nested_values))
var bad = test_css_parser("f(\"a\nb\")", false)
let bad_values = css_parser_parse_as_list_of_component_values(&bad)
assert(not css_parser_contains_only_any_value(&bad_values))
test "serialize_a_comma_separated_list and serialize_unicode_ranges":
with memory.heap:
var ranges = ak.Vector[web_fonts.UnicodeRange]()
ak.vector_append_t(&ranges, web_fonts.UnicodeRange(m_min_code_point = 0, m_max_code_point = 0x7f))
ak.vector_append_t(&ranges, web_fonts.UnicodeRange(m_min_code_point = 0x41, m_max_code_point = 0x41))
ak.vector_append_t(&ranges, web_fonts.UnicodeRange(m_min_code_point = 0x1f600, m_max_code_point = 0x10ffff))
var builder = ak.string_builder_new()
css_serialize_unicode_ranges(&builder, &ranges)
let text = ak.string_builder_to_string_without_validation(&builder)
assert(ak.string_eq_string_view(&text, ak.sv("\"U+0-7F\", \"U+41\", \"U+1F600-10FFFF\"")))
var empty_builder = ak.string_builder_new()
let no_ranges = ak.Vector[web_fonts.UnicodeRange]()
css_serialize_unicode_ranges(&empty_builder, &no_ranges)
assert(ak.string_builder_is_empty(&empty_builder))
test "an empty stylesheet's contents, block contents and declaration blocks":
with memory.heap:
var params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(¶ms)
let declarations = parse_css_property_declaration_block(params, ak.sv(""))
assert(ak.vector_is_empty(&declarations.properties) and ak.ordered_hash_map_is_empty(&declarations.custom_properties))
let descriptors = parse_css_descriptor_declaration_block(params, .font_face, ak.sv(""))
assert(ak.vector_is_empty(&descriptors))
assert(parse_css_value(params, ak.sv(""), css_data.PropertyId.width) == none)
assert(parse_css_supports(params, ak.sv("")) == none)
let values = parse_component_values_list(params, ak.sv("a b"))
assert(ak.vector_size(&values) == 3)
# A block of only whitespace and semicolons has no declarations to convert.
var parser = css_parser_create(params, ak.sv(" ; ;"))
let block = css_parser_parse_as_property_declaration_block(&parser)
assert(ak.vector_is_empty(&block.properties))