dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / counter_style_2.lucb
#==============================================================================================
#
# counter_style_2 - CounterStyle: counter representations
#
# DESCRIPTION:
# Port of LibWeb/CSS/CounterStyle.cpp lines 199-694:
# generate_an_initial_representation_for_the_counter_value (each system, the
# extended CJK tables), uses_a_negative_sign and generate a counter representation.
# Where the C++ mixes an i64 counter value with a size_t symbol count, the arithmetic
# is unsigned as in C++ (`(value - 1) % size()`).
#
# Copyright (c) 2026, Callum Law <callumlaw1709@outlook.com>.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Initial representations =================================================================
## Ported from Libraries/LibWeb/CSS/CounterStyle.cpp:199 Web::CSS::CounterStyle::generate_an_initial_representation_for_the_counter_value.
pub func css_counter_style_generate_an_initial_representation_for_the_counter_value(this: CssCounterStyle*, value: i64) -> ak.String?:
match this.m_algorithm:
.css_additive_counter_style_algorithm(additive_algorithm):
return css_counter_style_additive_representation(&additive_algorithm, value)
.css_fixed_counter_style_algorithm(fixed_algorithm):
# https://drafts.csswg.org/css-counter-styles-3/#fixed-system
# The first counter symbol is the representation for the first symbol value, and subsequent counter values
# are represented by subsequent counter symbols. Once the list of counter symbols is exhausted, further
# values cannot be represented by this counter style, and must instead be represented by the fallback
# counter style.
let index = value - (i64)fixed_algorithm.first_symbol
if index < 0 or index >= (i64)ak.vector_size(&fixed_algorithm.symbol_list):
return none
return ak.fly_string_to_string_const(ak.vector_at_const(&fixed_algorithm.symbol_list, (usize)index))
.css_generic_counter_style_algorithm(generic_algorithm):
return css_counter_style_generic_representation(&generic_algorithm, value)
.css_ethiopic_numeric_counter_style_algorithm(_):
return css_counter_style_ethiopic_numeric_representation(value)
.css_extended_cjk_counter_style_algorithm(extended_cjk_algorithm):
return css_counter_style_extended_cjk_representation(&extended_cjk_algorithm, value)
## The additive alternative of generate_an_initial_representation_for_the_counter_value.
# https://drafts.csswg.org/css-counter-styles-3/#additive-system
func css_counter_style_additive_representation(additive_algorithm: const CssAdditiveCounterStyleAlgorithm*, value_: i64) -> ak.String?:
# To construct the representation:
# 1. Let value initially be the counter value, S initially be the empty string, and symbol list initially
# be the list of additive tuples.
var value = value_
# 2. If value is zero:
if value == 0:
# 1. If symbol list contains a tuple with a weight of zero, append that tuple’s counter symbol to S and
# return S.
for tuple in ak.vector_span_const(&additive_algorithm.symbol_list):
if tuple.weight == 0:
return ak.fly_string_to_string_const(&tuple.symbol)
# 2. Otherwise, the given counter value cannot be represented by this counter style, and must instead
# be represented by the fallback counter style.
return none
var builder = ak.string_builder_new()
# 3. For each tuple in symbol list:
for tuple in ak.vector_span_const(&additive_algorithm.symbol_list):
# 1. Let symbol and weight be tuple’s counter symbol and weight, respectively.
# 2. If weight is zero, or weight is greater than value, continue.
if tuple.weight == 0 or (i64)tuple.weight > value:
continue
# 3. Let reps be floor( value / weight ).
let reps = value // (i64)tuple.weight
# 4. Append symbol to S reps times.
var i: i32 = 0
while (i64)i < reps:
ak.string_builder_append_string_view(&builder, ak.fly_string_bytes_as_string_view(&tuple.symbol))
i += 1
# 5. Decrement value by weight * reps.
value -= (i64)tuple.weight * reps
# 6. If value is zero, return S.
if value == 0:
return ak.string_builder_to_string(&builder) else trap("MUST: StringBuilder::to_string")
# Assertion: value is still non-zero.
assert(value != 0)
# The given counter value cannot be represented by this counter style, and must instead be represented by
# the fallback counter style.
return none
## `String::join(""sv, symbols)` for a Vector of FlyStrings.
func css_counter_style_join_symbols(symbols: const ak.Vector[ak.FlyString]*) -> ak.String:
var builder = ak.string_builder_new()
ak.string_builder_join_fly_string(&builder, ak.sv(""), ak.vector_span_const(symbols))
return ak.string_builder_to_string(&builder) else trap("MUST: String::join")
## The generic (cyclic, numeric, alphabetic, symbolic) alternative of
## generate_an_initial_representation_for_the_counter_value.
func css_counter_style_generic_representation(generic_algorithm: const CssGenericCounterStyleAlgorithm*, value_: i64) -> ak.String?:
var value = value_
let symbol_count = (u64)ak.vector_size(&generic_algorithm.symbol_list)
match generic_algorithm.type_:
.cyclic:
# https://drafts.csswg.org/css-counter-styles-3/#cyclic-system
# If there are N counter symbols and a representation is being constructed for the integer value, the
# representation is the counter symbol at index ( (value-1) mod N) of the list of counter symbols
# (0-indexed).
let index = (u64)(value -% 1) % symbol_count
return ak.fly_string_to_string_const(ak.vector_at_const(&generic_algorithm.symbol_list, (usize)index))
.numeric:
# https://drafts.csswg.org/css-counter-styles-3/#numeric-system
# If there are N counter symbols, the representation is a base N number using the counter symbols as
# digits. To construct the representation, run the following algorithm:
# Let N be the length of the list of counter symbols, value initially be the counter value, S
# initially be the empty string, and symbol(n) be the nth counter symbol in the list of counter
# symbols (0-indexed).
# 1. If value is 0, append symbol(0) to S and return S.
if value == 0:
return ak.fly_string_to_string_const(ak.vector_at_const(&generic_algorithm.symbol_list, 0))
# NB: Our string builder doesn't support prepending, so we use a vector and convert that to a string at
# the end.
var symbols = ak.Vector[ak.FlyString]()
# 2. While value is not equal to 0:
while value != 0:
# 1. Prepend symbol( value mod N ) to S.
ak.vector_prepend_u(&symbols, *ak.vector_at_const(&generic_algorithm.symbol_list, (usize)((u64)value % symbol_count)))
# 2. Set value to floor( value / N ).
value = (i64)((u64)value // symbol_count)
# 3. Return S.
return css_counter_style_join_symbols(&symbols)
.alphabetic:
# https://drafts.csswg.org/css-counter-styles-3/#alphabetic-system
# If there are N counter symbols, the representation is a base N alphabetic number using the counter
# symbols as digits. To construct the representation, run the following algorithm:
# Let N be the length of the list of counter symbols, value initially be the counter value, S initially
# be the empty string, and symbol(n) be the nth counter symbol in the list of counter symbols
# (0-indexed).
# NB: Our string builder doesn't support prepending, so we use a vector and convert that to a string at
# the end.
var symbols = ak.Vector[ak.FlyString]()
# While value is not equal to 0:
while value != 0:
# 1. Set value to value - 1.
value -= 1
# 2. Prepend symbol( value mod N ) to S.
ak.vector_prepend_u(&symbols, *ak.vector_at_const(&generic_algorithm.symbol_list, (usize)((u64)value % symbol_count)))
# 3. Set value to floor( value / N ).
value = (i64)((u64)value // symbol_count)
# Finally, return S.
return css_counter_style_join_symbols(&symbols)
.symbolic:
# https://drafts.csswg.org/css-counter-styles-3/#symbolic-system
# To construct the representation, run the following algorithm:
# Let N be the length of the list of counter symbols, value initially be the counter value, S initially
# be the empty string, and symbol(n) be the nth counter symbol in the list of counter symbols
# (0-indexed).
# 1. Let the chosen symbol be symbol( (value - 1) mod N).
let symbol = *ak.vector_at_const(&generic_algorithm.symbol_list, (usize)((u64)(value -% 1) % symbol_count))
# 2. Let the representation length be ceil( value / N ).
let representation_length = ((u64)value +% symbol_count -% 1) // symbol_count
# 3. Append the chosen symbol to S a number of times equal to the representation length.
# Finally, return S.
let symbol_string = ak.fly_string_to_string_const(&symbol)
return ak.string_repeated_string(&symbol_string, (usize)representation_length) else trap("MUST: String::repeated")
.additive:
# NB: This is handled by AdditiveCounterStyleAlgorithm.
trap("VERIFY_NOT_REACHED")
_: trap("VERIFY_NOT_REACHED")
## The Ethiopic numeric alternative of generate_an_initial_representation_for_the_counter_value.
# https://drafts.csswg.org/css-counter-styles-3/#ethiopic-numeric-counter-style
func css_counter_style_ethiopic_numeric_representation(value_: i64) -> ak.String?:
var value = value_
# The following algorithm converts decimal digits to ethiopic numbers:
# 1. If the number is 1, return "፩" (U+1369).
if value == 1:
return ak.string_from_str("\u{1369}")
# 2. Split the number into groups of two digits, starting with the least significant decimal digit.
var groups = ak.Vector[u8]()
while value != 0:
ak.vector_append_t(&groups, (u8)(value % 100))
value = value // 100
var builder = ak.string_builder_new()
# 3. Index each group sequentially, starting from the least significant as group number zero.
# NB: We iterate in descending order of significance, so we can append to the string builder in order
let group_count = (i32)ak.vector_size(&groups)
var i = group_count - 1
while i >= 0:
let group_value = *ak.vector_at_const(&groups, (usize)i)
# 4. If the group has the value zero, or if the group is the most significant one and has the value
# 1, or if the group has an odd index (as given in the previous step) and has the value 1, then
# remove the digits (but leave the group, so it still has a separator appended below).
if group_value != 0 and not (i == group_count - 1 and group_value == 1) and not (i % 2 == 1 and group_value == 1):
# 5. For each remaining digit, substitute the relevant ethiopic character from the list below.
# Tens
# Values Codepoints
# 10 ፲ U+1372
# 20 ፳ U+1373
# 30 ፴ U+1374
# 40 ፵ U+1375
# 50 ፶ U+1376
# 60 ፷ U+1377
# 70 ፸ U+1378
# 80 ፹ U+1379
# 90 ፺ U+137A
let tens = group_value // 10
if tens != 0:
ak.string_builder_append_code_point(&builder, 0x1372 + (u32)tens - 1)
# Units
# Values Codepoints
# 1 ፩ U+1369
# 2 ፪ U+136A
# 3 ፫ U+136B
# 4 ፬ U+136C
# 5 ፭ U+136D
# 6 ፮ U+136E
# 7 ፯ U+136F
# 8 ፰ U+1370
# 9 ፱ U+1371
let units = group_value % 10
if units != 0:
ak.string_builder_append_code_point(&builder, 0x1369 + (u32)units - 1)
# 6. For each group with an odd index (as given in the second step), except groups which originally
# had a value of zero, append ፻ U+137B.
if i % 2 == 1 and group_value != 0:
ak.string_builder_append_code_point(&builder, 0x137B)
# 7. For each group with an even index (as given in the second step), except the group with index
# 0, append ፼ U+137C.
elif i % 2 == 0 and i != 0:
ak.string_builder_append_code_point(&builder, 0x137C)
i -= 1
# 8. Concatenate the groups into one string, and return it.
return ak.string_builder_to_string(&builder) else trap("MUST: StringBuilder::to_string")
## The extended CJK alternative of generate_an_initial_representation_for_the_counter_value.
# https://drafts.csswg.org/css-counter-styles-3/#extended-range-optional
func css_counter_style_extended_cjk_representation(extended_cjk_algorithm: const CssExtendedCjkCounterStyleAlgorithm*, value: i64) -> ak.String?:
# All of the styles are defined by almost identical algorithms (specified as a single algorithm here, with
# the differences called out when relevant), but use different sets of characters.
# The following tables define the characters used in these styles:
match extended_cjk_algorithm.type_:
# | Values | Codepoints
# | | simp-chinese-informal | simp-chinese-formal | trad-chinese-informal | trad-chinese-formal
# | Digit 0 | 零 U+96F6 | 零 U+96F6 | 零 U+96F6 | 零 U+96F6
# | Digit 1 | 一 U+4E00 | 壹 U+58F9 | 一 U+4E00 | 壹 U+58F9
# | Digit 2 | 二 U+4E8C | 贰 U+8D30 | 二 U+4E8C | 貳 U+8CB3
# | Digit 3 | 三 U+4E09 | 叁 U+53C1 | 三 U+4E09 | 參 U+53C3
# | Digit 4 | 四 U+56DB | 肆 U+8086 | 四 U+56DB | 肆 U+8086
# | Digit 5 | 五 U+4E94 | 伍 U+4F0D | 五 U+4E94 | 伍 U+4F0D
# | Digit 6 | 六 U+516D | 陆 U+9646 | 六 U+516D | 陸 U+9678
# | Digit 7 | 七 U+4E03 | 柒 U+67D2 | 七 U+4E03 | 柒 U+67D2
# | Digit 8 | 八 U+516B | 捌 U+634C | 八 U+516B | 捌 U+634C
# | Digit 9 | 九 U+4E5D | 玖 U+7396 | 九 U+4E5D | 玖 U+7396
# | Second Digit Marker | 十 U+5341 | 拾 U+62FE | 十 U+5341 | 拾 U+62FE
# | Third Digit Marker | 百 U+767E | 佰 U+4F70 | 百 U+767E | 佰 U+4F70
# | Fourth Digit Marker | 千 U+5343 | 仟 U+4EDF | 千 U+5343 | 仟 U+4EDF
# | Second Group Marker | 万 U+4E07 | 万 U+4E07 | 萬 U+842C | 萬 U+842C
# | Third Group Marker | 亿 U+4EBF | 亿 U+4EBF | 億 U+5104 | 億 U+5104
# | Fourth Group Marker | 万亿 U+4E07 U+4EBF | 万亿 U+4E07 U+4EBF | 兆 U+5146 | 兆 U+5146
.simp_chinese_informal:
let digit_strings: ak.FlyString[10] = [
ak.fly_string_from_str("\u{96F6}"), ak.fly_string_from_str("\u{4E00}"), ak.fly_string_from_str("\u{4E8C}"), ak.fly_string_from_str("\u{4E09}"), ak.fly_string_from_str("\u{56DB}"),
ak.fly_string_from_str("\u{4E94}"), ak.fly_string_from_str("\u{516D}"), ak.fly_string_from_str("\u{4E03}"), ak.fly_string_from_str("\u{516B}"), ak.fly_string_from_str("\u{4E5D}"),
]
let digit_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{5341}"), ak.fly_string_from_str("\u{767E}"), ak.fly_string_from_str("\u{5343}")]
let group_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{4E07}"), ak.fly_string_from_str("\u{4EBF}"), ak.fly_string_from_str("\u{4E07}\u{4EBF}")]
return css_generate_an_initial_representation_for_extended_cjk_system(value, CssExtendedCjkCounterStyleAlgorithmType.simp_chinese_informal, digit_strings, digit_marker_strings, group_marker_strings)
.simp_chinese_formal:
let digit_strings: ak.FlyString[10] = [
ak.fly_string_from_str("\u{96F6}"), ak.fly_string_from_str("\u{58F9}"), ak.fly_string_from_str("\u{8D30}"), ak.fly_string_from_str("\u{53C1}"), ak.fly_string_from_str("\u{8086}"),
ak.fly_string_from_str("\u{4F0D}"), ak.fly_string_from_str("\u{9646}"), ak.fly_string_from_str("\u{67D2}"), ak.fly_string_from_str("\u{634C}"), ak.fly_string_from_str("\u{7396}"),
]
let digit_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{62FE}"), ak.fly_string_from_str("\u{4F70}"), ak.fly_string_from_str("\u{4EDF}")]
let group_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{4E07}"), ak.fly_string_from_str("\u{4EBF}"), ak.fly_string_from_str("\u{4E07}\u{4EBF}")]
return css_generate_an_initial_representation_for_extended_cjk_system(value, CssExtendedCjkCounterStyleAlgorithmType.simp_chinese_formal, digit_strings, digit_marker_strings, group_marker_strings)
.trad_chinese_informal:
let digit_strings: ak.FlyString[10] = [
ak.fly_string_from_str("\u{96F6}"), ak.fly_string_from_str("\u{4E00}"), ak.fly_string_from_str("\u{4E8C}"), ak.fly_string_from_str("\u{4E09}"), ak.fly_string_from_str("\u{56DB}"),
ak.fly_string_from_str("\u{4E94}"), ak.fly_string_from_str("\u{516D}"), ak.fly_string_from_str("\u{4E03}"), ak.fly_string_from_str("\u{516B}"), ak.fly_string_from_str("\u{4E5D}"),
]
let digit_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{5341}"), ak.fly_string_from_str("\u{767E}"), ak.fly_string_from_str("\u{5343}")]
let group_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{842C}"), ak.fly_string_from_str("\u{5104}"), ak.fly_string_from_str("\u{5146}")]
return css_generate_an_initial_representation_for_extended_cjk_system(value, CssExtendedCjkCounterStyleAlgorithmType.trad_chinese_informal, digit_strings, digit_marker_strings, group_marker_strings)
.trad_chinese_formal:
let digit_strings: ak.FlyString[10] = [
ak.fly_string_from_str("\u{96F6}"), ak.fly_string_from_str("\u{58F9}"), ak.fly_string_from_str("\u{8CB3}"), ak.fly_string_from_str("\u{53C3}"), ak.fly_string_from_str("\u{8086}"),
ak.fly_string_from_str("\u{4F0D}"), ak.fly_string_from_str("\u{9678}"), ak.fly_string_from_str("\u{67D2}"), ak.fly_string_from_str("\u{634C}"), ak.fly_string_from_str("\u{7396}"),
]
let digit_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{62FE}"), ak.fly_string_from_str("\u{4F70}"), ak.fly_string_from_str("\u{4EDF}")]
let group_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{842C}"), ak.fly_string_from_str("\u{5104}"), ak.fly_string_from_str("\u{5146}")]
return css_generate_an_initial_representation_for_extended_cjk_system(value, CssExtendedCjkCounterStyleAlgorithmType.trad_chinese_formal, digit_strings, digit_marker_strings, group_marker_strings)
# Values | Codepoints
# | japanese-informal | japanese-formal
# Digit 0 | 〇 U+3007 | 零 U+96F6
# Digit 1 | 一 U+4E00 | 壱 U+58F1
# Digit 2 | 二 U+4E8C | 弐 U+5F10
# Digit 3 | 三 U+4E09 | 参 U+53C2
# Digit 4 | 四 U+56DB | 四 U+56DB
# Digit 5 | 五 U+4E94 | 伍 U+4f0D
# Digit 6 | 六 U+516D | 六 U+516D
# Digit 7 | 七 U+4E03 | 七 U+4E03
# Digit 8 | 八 U+516B | 八 U+516B
# Digit 9 | 九 U+4E5D | 九 U+4E5D
# Second Digit Marker | 十 U+5341 | 拾 U+62FE
# Third Digit Marker | 百 U+767E | 百 U+767E
# Fourth Digit Marker | 千 U+5343 | 阡 U+9621
# Second Group Marker | 万 U+4E07 | 萬 U+842C
# Third Group Marker | 億 U+5104 | 億 U+5104
# Fourth Group Marker | 兆 U+5146 | 兆 U+5146
.japanese_informal:
let digit_strings: ak.FlyString[10] = [
ak.fly_string_from_str("\u{3007}"), ak.fly_string_from_str("\u{4E00}"), ak.fly_string_from_str("\u{4E8C}"), ak.fly_string_from_str("\u{4E09}"), ak.fly_string_from_str("\u{56DB}"),
ak.fly_string_from_str("\u{4E94}"), ak.fly_string_from_str("\u{516D}"), ak.fly_string_from_str("\u{4E03}"), ak.fly_string_from_str("\u{516B}"), ak.fly_string_from_str("\u{4E5D}"),
]
let digit_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{5341}"), ak.fly_string_from_str("\u{767E}"), ak.fly_string_from_str("\u{5343}")]
let group_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{4E07}"), ak.fly_string_from_str("\u{5104}"), ak.fly_string_from_str("\u{5146}")]
return css_generate_an_initial_representation_for_extended_cjk_system(value, CssExtendedCjkCounterStyleAlgorithmType.japanese_informal, digit_strings, digit_marker_strings, group_marker_strings)
.japanese_formal:
let digit_strings: ak.FlyString[10] = [
ak.fly_string_from_str("\u{96F6}"), ak.fly_string_from_str("\u{58F1}"), ak.fly_string_from_str("\u{5F10}"), ak.fly_string_from_str("\u{53C2}"), ak.fly_string_from_str("\u{56DB}"),
ak.fly_string_from_str("\u{4F0D}"), ak.fly_string_from_str("\u{516D}"), ak.fly_string_from_str("\u{4E03}"), ak.fly_string_from_str("\u{516B}"), ak.fly_string_from_str("\u{4E5D}"),
]
let digit_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{62FE}"), ak.fly_string_from_str("\u{767E}"), ak.fly_string_from_str("\u{9621}")]
let group_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{842C}"), ak.fly_string_from_str("\u{5104}"), ak.fly_string_from_str("\u{5146}")]
return css_generate_an_initial_representation_for_extended_cjk_system(value, CssExtendedCjkCounterStyleAlgorithmType.japanese_formal, digit_strings, digit_marker_strings, group_marker_strings)
# Values | Codepoints
# | korean-hangul-formal | korean-hanja-informal | korean-hanja-formal
# Digit 0 | 영 U+C601 | 零 U+96F6 | 零 U+96F6
# Digit 1 | 일 U+C77C | 一 U+4E00 | 壹 U+58F9
# Digit 2 | 이 U+C774 | 二 U+4E8C | 貳 U+8CB3
# Digit 3 | 삼 U+C0BC | 三 U+4E09 | 參 U+53C3
# Digit 4 | 사 U+C0AC | 四 U+56DB | 四 U+56DB
# Digit 5 | 오 U+C624 | 五 U+4E94 | 五 U+4E94
# Digit 6 | 육 U+C721 | 六 U+516D | 六 U+516D
# Digit 7 | 칠 U+CE60 | 七 U+4E03 | 七 U+4E03
# Digit 8 | 팔 U+D314 | 八 U+516B | 八 U+516B
# Digit 9 | 구 U+AD6C | 九 U+4E5D | 九 U+4E5D
# Second Digit Marker | 십 U+C2ED | 十 U+5341 | 拾 U+62FE
# Third Digit Marker | 백 U+BC31 | 百 U+767E | 百 U+767E
# Fourth Digit Marker | 천 U+CC9C | 千 U+5343 | 仟 U+4EDF
# Second Group Marker | 만 U+B9CC | 萬 U+842C | 萬 U+842C
# Third Group Marker | 억 U+C5B5 | 億 U+5104 | 億 U+5104
# Fourth Group Marker | 조 U+C870 | 兆 U+5146 | 兆 U+5146
.korean_hangul_formal:
let digit_strings: ak.FlyString[10] = [
ak.fly_string_from_str("\u{C601}"), ak.fly_string_from_str("\u{C77C}"), ak.fly_string_from_str("\u{C774}"), ak.fly_string_from_str("\u{C0BC}"), ak.fly_string_from_str("\u{C0AC}"),
ak.fly_string_from_str("\u{C624}"), ak.fly_string_from_str("\u{C721}"), ak.fly_string_from_str("\u{CE60}"), ak.fly_string_from_str("\u{D314}"), ak.fly_string_from_str("\u{AD6C}"),
]
let digit_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{C2ED}"), ak.fly_string_from_str("\u{BC31}"), ak.fly_string_from_str("\u{CC9C}")]
let group_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{B9CC}"), ak.fly_string_from_str("\u{C5B5}"), ak.fly_string_from_str("\u{C870}")]
return css_generate_an_initial_representation_for_extended_cjk_system(value, CssExtendedCjkCounterStyleAlgorithmType.korean_hangul_formal, digit_strings, digit_marker_strings, group_marker_strings)
.korean_hanja_informal:
let digit_strings: ak.FlyString[10] = [
ak.fly_string_from_str("\u{96F6}"), ak.fly_string_from_str("\u{4E00}"), ak.fly_string_from_str("\u{4E8C}"), ak.fly_string_from_str("\u{4E09}"), ak.fly_string_from_str("\u{56DB}"),
ak.fly_string_from_str("\u{4E94}"), ak.fly_string_from_str("\u{516D}"), ak.fly_string_from_str("\u{4E03}"), ak.fly_string_from_str("\u{516B}"), ak.fly_string_from_str("\u{4E5D}"),
]
let digit_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{5341}"), ak.fly_string_from_str("\u{767E}"), ak.fly_string_from_str("\u{5343}")]
let group_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{842C}"), ak.fly_string_from_str("\u{5104}"), ak.fly_string_from_str("\u{5146}")]
return css_generate_an_initial_representation_for_extended_cjk_system(value, CssExtendedCjkCounterStyleAlgorithmType.korean_hanja_informal, digit_strings, digit_marker_strings, group_marker_strings)
.korean_hanja_formal:
let digit_strings: ak.FlyString[10] = [
ak.fly_string_from_str("\u{96F6}"), ak.fly_string_from_str("\u{58F9}"), ak.fly_string_from_str("\u{8CB3}"), ak.fly_string_from_str("\u{53C3}"), ak.fly_string_from_str("\u{56DB}"),
ak.fly_string_from_str("\u{4E94}"), ak.fly_string_from_str("\u{516D}"), ak.fly_string_from_str("\u{4E03}"), ak.fly_string_from_str("\u{516B}"), ak.fly_string_from_str("\u{4E5D}"),
]
let digit_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{62FE}"), ak.fly_string_from_str("\u{767E}"), ak.fly_string_from_str("\u{4EDF}")]
let group_marker_strings: ak.FlyString[3] = [ak.fly_string_from_str("\u{842C}"), ak.fly_string_from_str("\u{5104}"), ak.fly_string_from_str("\u{5146}")]
return css_generate_an_initial_representation_for_extended_cjk_system(value, CssExtendedCjkCounterStyleAlgorithmType.korean_hanja_formal, digit_strings, digit_marker_strings, group_marker_strings)
_: trap("VERIFY_NOT_REACHED")
# mark: Negative signs and counter representations =============================================
## Ported from Libraries/LibWeb/CSS/CounterStyle.cpp:563 Web::CSS::CounterStyle::uses_a_negative_sign.
# https://drafts.csswg.org/css-counter-styles-3/#counter-style-negative
pub func css_counter_style_uses_a_negative_sign(this: CssCounterStyle*) -> bool:
# Not all system values use a negative sign. In particular, a counter style uses a negative sign if its system
# value is symbolic, alphabetic, numeric, additive, or extends if the extended counter style itself uses a negative
# sign.
# NB: We have resolved extends to the underlying algorithm before calling this
match this.m_algorithm:
.css_additive_counter_style_algorithm(_):
return true
.css_fixed_counter_style_algorithm(_):
return false
.css_generic_counter_style_algorithm(generic_system):
match generic_system.type_:
.cyclic: return false
.symbolic, .alphabetic, .numeric: return true
.additive:
# NB: This is handled by AdditiveCounterStyleAlgorithm.
trap("VERIFY_NOT_REACHED")
_: trap("VERIFY_NOT_REACHED")
.css_ethiopic_numeric_counter_style_algorithm(_):
# https://drafts.csswg.org/css-counter-styles-3/#complex-predefined-counters
# All of the counter styles defined in this section have a spoken form of numbers, and use a negative sign.
return true
.css_extended_cjk_counter_style_algorithm(_):
# https://drafts.csswg.org/css-counter-styles-3/#complex-predefined-counters
# All of the counter styles defined in this section have a spoken form of numbers, and use a negative sign.
return true
## Ported from Libraries/LibWeb/CSS/CounterStyle.cpp:604 Web::CSS::generate_a_counter_representation_impl.
# https://drafts.csswg.org/css-counter-styles-3/#generate-a-counter
func css_generate_a_counter_representation_impl(counter_style_: CssCounterStyle*?, style_scope: const CssStyleScope*, value: i32, fallback_history: ak.HashTable[ak.FlyString, ak.FlyStringTraits]*) -> ak.String:
# When asked to generate a counter representation using a particular counter style for a particular
# counter value, follow these steps:
# 1. If the counter style is unknown, exit this algorithm and instead generate a counter representation using the
# decimal style and the same counter value.
let counter_style = counter_style_ else return css_generate_a_counter_representation_impl(css_counter_style_decimal(), style_scope, value, fallback_history)
# 2. If the counter value is outside the range of the counter style, exit this algorithm and instead generate a
# counter representation using the counter style’s fallback style and the same counter value.
var in_range = false
for entry in ak.vector_span_const(&counter_style.m_range):
if value >= entry.start and value <= entry.end:
in_range = true
break
if not in_range:
return css_generate_a_counter_representation_using_fallback(counter_style, style_scope, value, fallback_history)
let value_is_negative_and_uses_negative_sign = value < 0 and css_counter_style_uses_a_negative_sign(counter_style)
# 3. Using the counter value and the counter algorithm for the counter style, generate an initial representation
# for the counter value. If the counter value is negative and the counter style uses a negative sign, instead
# generate an initial representation using the absolute value of the counter value.
let initial_value = -(i64)value if value_is_negative_and_uses_negative_sign else (i64)value
let maybe_representation = css_counter_style_generate_an_initial_representation_for_the_counter_value(counter_style, initial_value)
# AD-HOC: Algorithms are sometimes unable to produce a representation and require us to use the fallback - we
# represent this by returning an empty Optional.
var representation = maybe_representation else return css_generate_a_counter_representation_using_fallback(counter_style, style_scope, value, fallback_history)
# 4. Prepend symbols to the representation as specified in the pad descriptor.
# https://drafts.csswg.org/css-counter-styles-3/#counter-style-pad
# Let difference be the provided <integer> minus the number of grapheme clusters in the initial representation
# for the counter value.
# FIXME: We should be counting grapheme clusters here.
var difference = counter_style.m_pad.minimum_length - (i32)ak.string_base_length_in_code_units(&representation.string_base)
# If the counter value is negative and the counter style uses a negative sign, further reduce difference by
# the number of grapheme clusters in the counter style’s negative descriptor’s <symbol>(s).
# FIXME: We should be counting grapheme clusters here.
if value_is_negative_and_uses_negative_sign:
let prefix = ak.fly_string_to_string_const(&counter_style.m_negative_sign.prefix)
let suffix = ak.fly_string_to_string_const(&counter_style.m_negative_sign.suffix)
# `difference -= size_t`: computed in size_t and truncated back to int, as C++ does.
difference = (i32)((u64)(i64)difference -% (u64)(ak.string_base_length_in_code_units(&prefix.string_base) + ak.string_base_length_in_code_units(&suffix.string_base)))
# If difference is greater than zero, prepend difference copies of the specified <symbol> to the representation.
if difference > 0:
let pad_symbol = ak.fly_string_to_string_const(&counter_style.m_pad.symbol)
let padding = ak.string_repeated_string(&pad_symbol, (usize)difference) else trap("MUST: String::repeated")
var builder = ak.string_builder_new()
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&padding))
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&representation))
representation = ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")
# 5. If the counter value is negative and the counter style uses a negative sign, wrap the representation in the
# counter style’s negative sign as specified in the negative descriptor.
if value_is_negative_and_uses_negative_sign:
var builder = ak.string_builder_new()
ak.string_builder_append_string_view(&builder, ak.fly_string_bytes_as_string_view(&counter_style.m_negative_sign.prefix))
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&representation))
ak.string_builder_append_string_view(&builder, ak.fly_string_bytes_as_string_view(&counter_style.m_negative_sign.suffix))
representation = ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")
# 6. Return the representation.
return representation
## generate_a_counter_representation_impl's lambda `generate_a_counter_representation_using_fallback`.
func css_generate_a_counter_representation_using_fallback(counter_style: CssCounterStyle*, style_scope: const CssStyleScope*, value: i32, fallback_history: ak.HashTable[ak.FlyString, ak.FlyStringTraits]*) -> ak.String:
assert(not ak.fly_string_eq_string_view(&counter_style.m_name, ak.sv("decimal")))
let fallback_name = counter_style.m_fallback else trap("VERIFY: a fallback")
let fallback = css_style_scope_get_registered_counter_style(style_scope, fallback_name)
# https://drafts.csswg.org/css-counter-styles-3/#counter-style-fallback
# If the value of the fallback descriptor isn’t the name of any defined counter style, the used value of the
# fallback descriptor is decimal instead. Similarly, while following fallbacks to find a counter style that
# can render the given counter value, if a loop in the specified fallbacks is detected, the decimal style must
# be used instead.
if fallback == none or ak.hash_table_contains_t(fallback_history, fallback_name):
return css_generate_a_counter_representation_impl(css_counter_style_decimal(), style_scope, value, fallback_history)
_ = ak.hash_table_set(fallback_history, counter_style.m_name)
return css_generate_a_counter_representation_impl(fallback, style_scope, value, fallback_history)
## Ported from Libraries/LibWeb/CSS/CounterStyle.cpp:680 Web::CSS::generate_a_counter_representation.
pub func css_generate_a_counter_representation(counter_style: CssCounterStyle*?, style_scope: const CssStyleScope*, value: i32) -> ak.String:
var fallback_history: ak.HashTable[ak.FlyString, ak.FlyStringTraits]
return css_generate_a_counter_representation_impl(counter_style, style_scope, value, &fallback_history)