dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / parser / html_parser_12.lucb
#==============================================================================================
#
# html_parser_12 - HTML::HTMLParser: dimension and legacy color values, the speculative
# parser, aborting, inserting an element, and HTMLParser.h's inline members
#
# DESCRIPTION:
# Port of LibWeb/HTML/Parser/HTMLParser.cpp (lines 5512-5892): the rules for parsing
# dimension values, non-zero dimension values and legacy color values, the table child
# element `align` keywords, realm(), starting and stopping the speculative HTML parser,
# aborting a parser and inserting an element at the adjusted insertion location; then the
# inline members of HTMLParser.h (lines 84-141).
#
#==============================================================================================
# mark: Dimension values ========================================================================
## Ported from HTMLParser.cpp:5513 Web::HTML::parse_current_dimension_value.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#current-dimension-value
func html_parse_current_dimension_value(value: f32, input: ak.Utf8View, position: ak.Utf8CodePointIterator) -> CssStyleValue*?:
# 1. If position is past the end of input, then return value as a length.
if ak.utf8_code_point_iterator_done(&position):
return css_length_style_value_create(css_length_make_px_css_pixels(gfx.css_pixels_nearest_value_for_f32(value))).css_style_value()
# 2. If the code point at position within input is U+0025 (%), then return value as a percentage.
if ak.utf8_code_point_iterator_deref(&position) == (u32)'%':
var percentage: CssPercentage = ---
css_percentage_construct(&percentage, f64(value))
return css_percentage_style_value_create(percentage).css_style_value()
# 3. Return value as a length.
return css_length_style_value_create(css_length_make_px_css_pixels(gfx.css_pixels_nearest_value_for_f32(value))).css_style_value()
## Ported from HTMLParser.cpp:5528 Web::HTML::parse_dimension_value.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-dimension-values
pub func html_parse_dimension_value(string: ak.StringView) -> CssStyleValue*?:
# 1. Let input be the string being parsed.
var input: ak.Utf8View = ---
ak.utf8_view_construct_string_view(&input, string)
if not ak.utf8_view_validate_allow_lonely_surrogates(&input):
return none
# 2. Let position be a position variable for input, initially pointing at the start of input.
var position = ak.utf8_view_begin(&input)
# 3. Skip ASCII whitespace within input given position.
while not ak.utf8_code_point_iterator_done(&position) and web_infra.is_ascii_whitespace(ak.utf8_code_point_iterator_deref(&position)):
_ = ak.utf8_code_point_iterator_inc(&position)
# 4. If position is past the end of input or the code point at position within input is not an ASCII digit,
# then return failure.
if ak.utf8_code_point_iterator_done(&position) or not ak.is_ascii_digit(ak.utf8_code_point_iterator_deref(&position)):
return none
# 5. Collect a sequence of code points that are ASCII digits from input given position,
# and interpret the resulting sequence as a base-ten integer. Let value be that number.
var number_string = ak.string_builder_new()
while not ak.utf8_code_point_iterator_done(&position) and ak.is_ascii_digit(ak.utf8_code_point_iterator_deref(&position)):
ak.string_builder_append_u8(&number_string, (u8)ak.utf8_code_point_iterator_deref(&position))
_ = ak.utf8_code_point_iterator_inc(&position)
let number_view = ak.string_builder_string_view(&number_string)
let integer_value = ak.string_view_to_number_f64(&number_view) else trap("VERIFY: digits parse")
var value = (f32)math.min(integer_value, f64(gfx.css_pixels_max_dimension_value))
# 6. If position is past the end of input, then return value as a length.
if ak.utf8_code_point_iterator_done(&position):
return css_length_style_value_create(css_length_make_px_css_pixels(gfx.css_pixels_from_f32(value))).css_style_value()
# 7. If the code point at position within input is U+002E (.), then:
if ak.utf8_code_point_iterator_deref(&position) == (u32)'.':
# 1. Advance position by 1.
_ = ak.utf8_code_point_iterator_inc(&position)
# 2. If position is past the end of input or the code point at position within input is not an ASCII digit,
# then return the current dimension value with value, input, and position.
if ak.utf8_code_point_iterator_done(&position) or not ak.is_ascii_digit(ak.utf8_code_point_iterator_deref(&position)):
return html_parse_current_dimension_value(value, input, position)
# 3. Let divisor have the value 1.
var divisor: f32 = 1.0
# 4. While true:
while true:
# 1. Multiply divisor by ten.
divisor *= 10.0
# 2. Add the value of the code point at position within input,
# interpreted as a base-ten digit (0..9) and divided by divisor, to value.
value += f32(ak.utf8_code_point_iterator_deref(&position) - (u32)'0') / divisor
# 3. Advance position by 1.
_ = ak.utf8_code_point_iterator_inc(&position)
# 4. If position is past the end of input, then return value as a length.
if ak.utf8_code_point_iterator_done(&position):
return css_length_style_value_create(css_length_make_px_css_pixels(gfx.css_pixels_nearest_value_for_f32(value))).css_style_value()
# 5. If the code point at position within input is not an ASCII digit, then break.
if not ak.is_ascii_digit(ak.utf8_code_point_iterator_deref(&position)):
break
# 8. Return the current dimension value with value, input, and position.
return html_parse_current_dimension_value(value, input, position)
## Ported from HTMLParser.cpp:5602 Web::HTML::parse_nonzero_dimension_value.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-non-zero-dimension-values
pub func html_parse_nonzero_dimension_value(string: ak.StringView) -> CssStyleValue*?:
# 1. Let input be the string being parsed.
# 2. Let value be the result of parsing input using the rules for parsing dimension values.
# 3. If value is an error, return an error.
let value = html_parse_dimension_value(string) else return none
# 4. If value is zero, return an error.
if css_style_value_is_length(value) and css_dimension_style_value_raw_value(css_style_value_as_length(value).css_dimension_style_value()) == 0.0:
return none
if css_style_value_is_percentage(value):
let percentage = css_percentage_style_value_percentage(css_style_value_as_percentage(value))
if css_percentage_value(&percentage) == 0.0:
return none
# 5. If value is a percentage, return value as a percentage.
# 6. Return value as a length.
return value
# mark: Legacy color values =====================================================================
## parse_legacy_color_value's `hex_nibble_to_u8` lambda.
func html_hex_nibble_to_u8(nibble: u8) -> u8:
if nibble >= '0' and nibble <= '9':
return nibble - '0'
if nibble >= 'a' and nibble <= 'f':
return nibble - 'a' + 10
return nibble -% 'A' +% 10
## parse_legacy_color_value's `replace_non_basic_multilingual_code_points` lambda.
func html_replace_non_basic_multilingual_code_points(string: ak.StringView) -> ak.ByteString:
var builder = ak.string_builder_new()
var view: ak.Utf8View = ---
ak.utf8_view_construct_string_view(&view, string)
var it = ak.utf8_view_begin(&view)
while not ak.utf8_code_point_iterator_done(&it):
let code_point = ak.utf8_code_point_iterator_deref(&it)
if code_point > 0xFFFF:
ak.string_builder_append(&builder, "00")
else:
ak.string_builder_append_code_point(&builder, code_point)
_ = ak.utf8_code_point_iterator_inc(&it)
return ak.string_builder_to_byte_string(&builder)
## parse_legacy_color_value's `replace_non_ascii_hex` lambda.
func html_replace_non_ascii_hex(string: ak.StringView) -> ak.ByteString:
var builder = ak.string_builder_new()
var view: ak.Utf8View = ---
ak.utf8_view_construct_string_view(&view, string)
var it = ak.utf8_view_begin(&view)
while not ak.utf8_code_point_iterator_done(&it):
let code_point = ak.utf8_code_point_iterator_deref(&it)
if ak.is_ascii_hex_digit(code_point):
ak.string_builder_append_code_point(&builder, code_point)
else:
ak.string_builder_append_code_point(&builder, (u32)'0')
_ = ak.utf8_code_point_iterator_inc(&it)
return ak.string_builder_to_byte_string(&builder)
## parse_legacy_color_value's `to_hex` lambda (it captures `length`).
func html_legacy_color_component_to_hex(string: ak.StringView, length: usize) -> u8:
if length == 1:
return html_hex_nibble_to_u8(ak.string_view_at(&string, 0))
let nib1 = html_hex_nibble_to_u8(ak.string_view_at(&string, 0))
let nib2 = html_hex_nibble_to_u8(ak.string_view_at(&string, 1))
return nib1 << 4 | nib2
## Ported from HTMLParser.cpp:5624 Web::HTML::parse_legacy_color_value.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-a-legacy-colour-value
pub func html_parse_legacy_color_value(string_view: ak.StringView) -> gfx.Color?:
# 1. If input is the empty string, then return failure.
if ak.string_view_is_empty(&string_view):
return none
var input = ak.byte_string_from_string_view(string_view)
# 2. Strip leading and trailing ASCII whitespace from input.
input = ak.byte_string_trim(&input, ak.sv(web_infra.ascii_whitespace))
# 3. If input is an ASCII case-insensitive match for "transparent", then return failure.
if ak.byte_string_equals_ignoring_ascii_case(&input, ak.sv("transparent")):
return none
# 4. If input is an ASCII case-insensitive match for one of the named colors, then return the CSS color corresponding to that keyword. [CSSCOLOR]
if let color = gfx.color_from_named_css_color_string(ak.byte_string_view(&input)):
return color
# 5. If input's code point length is four, and the first character in input is U+0023 (#), and the last three characters of input are all ASCII hex digits, then:
if ak.byte_string_length(&input) == 4 and ak.byte_string_at(&input, 0) == '#' and ak.is_ascii_hex_digit(ak.byte_string_at(&input, 1)) and ak.is_ascii_hex_digit(ak.byte_string_at(&input, 2)) and ak.is_ascii_hex_digit(ak.byte_string_at(&input, 3)):
# 1. Let result be a CSS color.
var result: gfx.Color
gfx.color_set_alpha(&result, 0xFF)
# 2. Interpret the second character of input as a hexadecimal digit; let the red component of result be the resulting number multiplied by 17.
gfx.color_set_red(&result, html_hex_nibble_to_u8(ak.byte_string_at(&input, 1)) *% 17)
# 3. Interpret the third character of input as a hexadecimal digit; let the green component of result be the resulting number multiplied by 17.
gfx.color_set_green(&result, html_hex_nibble_to_u8(ak.byte_string_at(&input, 2)) *% 17)
# 4. Interpret the fourth character of input as a hexadecimal digit; let the blue component of result be the resulting number multiplied by 17.
gfx.color_set_blue(&result, html_hex_nibble_to_u8(ak.byte_string_at(&input, 3)) *% 17)
# 5. Return result.
return result
# 6. Replace any code points greater than U+FFFF in input (i.e., any characters that are not in the basic multilingual plane) with "00".
input = html_replace_non_basic_multilingual_code_points(ak.byte_string_view(&input))
# 7. If input's code point length is greater than 128, truncate input, leaving only the first 128 characters.
if ak.byte_string_length(&input) > 128:
input = ak.byte_string_substring_usize(&input, 0, 128)
# 8. If the first character in input is U+0023 (#), then remove it.
if ak.byte_string_at(&input, 0) == '#':
input = ak.byte_string_substring(&input, 1)
# 9. Replace any character in input that is not an ASCII hex digit with U+0030 (0).
input = html_replace_non_ascii_hex(ak.byte_string_view(&input))
# 10. While input's code point length is zero or not a multiple of three, append U+0030 (0) to input.
var builder = ak.string_builder_new()
ak.string_builder_append_string_view(&builder, ak.byte_string_view(&input))
while ak.string_builder_length(&builder) == 0 or (ak.string_builder_length(&builder) % 3 != 0):
ak.string_builder_append_code_point(&builder, (u32)'0')
input = ak.string_builder_to_byte_string(&builder)
# 11. Split input into three strings of equal code point length, to obtain three components. Let length be the code point length that all of those components have (one third the code point length of input).
var length = ak.byte_string_length(&input) // 3
var first_component = ak.byte_string_substring_view_usize(&input, 0, length)
var second_component = ak.byte_string_substring_view_usize(&input, length, length)
var third_component = ak.byte_string_substring_view_usize(&input, length * 2, length)
# 12. If length is greater than 8, then remove the leading length-8 characters in each component, and let length be 8.
if length > 8:
first_component = ak.string_view_substring_view(&first_component, length - 8)
second_component = ak.string_view_substring_view(&second_component, length - 8)
third_component = ak.string_view_substring_view(&third_component, length - 8)
length = 8
# 13. While length is greater than two and the first character in each component is U+0030 (0), remove that character and reduce length by one.
while length > 2 and ak.string_view_at(&first_component, 0) == '0' and ak.string_view_at(&second_component, 0) == '0' and ak.string_view_at(&third_component, 0) == '0':
length -= 1
first_component = ak.string_view_substring_view(&first_component, 1)
second_component = ak.string_view_substring_view(&second_component, 1)
third_component = ak.string_view_substring_view(&third_component, 1)
# 14. If length is still greater than two, truncate each component, leaving only the first two characters in each.
if length > 2:
first_component = ak.string_view_substring_view_usize(&first_component, 0, 2)
second_component = ak.string_view_substring_view_usize(&second_component, 0, 2)
third_component = ak.string_view_substring_view_usize(&third_component, 0, 2)
# 15. Let result be a CSS color.
var result: gfx.Color
gfx.color_set_alpha(&result, 0xFF)
# 16. Interpret the first component as a hexadecimal number; let the red component of result be the resulting number.
gfx.color_set_red(&result, html_legacy_color_component_to_hex(first_component, length))
# 17. Interpret the second component as a hexadecimal number; let the green component of result be the resulting number.
gfx.color_set_green(&result, html_legacy_color_component_to_hex(second_component, length))
# 18. Interpret the third component as a hexadecimal number; let the blue component of result be the resulting number.
gfx.color_set_blue(&result, html_legacy_color_component_to_hex(third_component, length))
# 19. Return result.
return result
## Ported from HTMLParser.cpp:5767 Web::HTML::parse_table_child_element_align_value.
# https://html.spec.whatwg.org/multipage/rendering.html#tables-2
pub func html_parse_table_child_element_align_value(string_view: ak.StringView) -> CssStyleValue*?:
# The thead, tbody, tfoot, tr, td, and th elements, when they have an align attribute whose value is an ASCII
# case-insensitive match for either the string "center" or the string "middle", are expected to center text within
# themselves, as if they had their 'text-align' property set to 'center' in a presentational hint, and to align
# descendants to the center.
if ak.string_view_equals_ignoring_ascii_case(&string_view, ak.sv("center")) or ak.string_view_equals_ignoring_ascii_case(&string_view, ak.sv("middle")):
return css_keyword_style_value_create(css_data.Keyword.libweb_center).css_style_value()
# The thead, tbody, tfoot, tr, td, and th elements, when they have an align attribute whose value is an ASCII
# case-insensitive match for the string "left", are expected to left-align text within themselves, as if they had
# their 'text-align' property set to 'left' in a presentational hint, and to align descendants to the left.
if ak.string_view_equals_ignoring_ascii_case(&string_view, ak.sv("left")):
return css_keyword_style_value_create(css_data.Keyword.libweb_left).css_style_value()
# The thead, tbody, tfoot, tr, td, and th elements, when they have an align attribute whose value is an ASCII
# case-insensitive match for the string "right", are expected to right-align text within themselves, as if they
# had their 'text-align' property set to 'right' in a presentational hint, and to align descendants to the right.
if ak.string_view_equals_ignoring_ascii_case(&string_view, ak.sv("right")):
return css_keyword_style_value_create(css_data.Keyword.libweb_right).css_style_value()
# The thead, tbody, tfoot, tr, td, and th elements, when they have an align attribute whose value is an ASCII
# case-insensitive match for the string "justify", are expected to full-justify text within themselves, as if they
# had their 'text-align' property set to 'justify' in a presentational hint, and to align descendants to the left.
if ak.string_view_equals_ignoring_ascii_case(&string_view, ak.sv("justify")):
return css_keyword_style_value_create(css_data.Keyword.justify).css_style_value()
return none
# mark: The realm, the speculative parser, aborting and inserting ===============================
## Ported from HTMLParser.cpp:5797 Web::HTML::HTMLParser::realm.
pub func html_parser_realm(this: HtmlParser*) -> Realm*:
return bind_platform_object_realm(html_parser_document(this).bind_platform_object())
## Ported from HTMLParser.cpp:5803 Web::HTML::HTMLParser::start_the_speculative_html_parser.
# https://html.spec.whatwg.org/multipage/parsing.html#start-the-speculative-html-parser
pub func html_parser_start_the_speculative_html_parser(this: HtmlParser*):
# 1. Optionally, return.
# NOTE: We do not opt out.
# 2. If parser's active speculative HTML parser is not null, then stop the speculative HTML parser for parser.
if this.m_active_speculative_html_parser != none:
html_parser_stop_the_speculative_html_parser(this)
# 3. Let speculativeParser be a new speculative HTML parser, with the same state as parser.
# 4. Let speculativeDoc be a new isomorphic representation of parser's Document, where all elements are instead
# speculative mock elements. Let speculativeParser parse into speculativeDoc.
# NOTE: Speculative mock elements are produced on the fly during run(); we do not materialize a full speculativeDoc tree.
let document = html_parser_document(this)
let speculative_parser = html_speculative_html_parser_create(html_parser_realm(this), document, html_syntax.html_tokenizer_unparsed_input(&this.m_tokenizer), dom_document_base_url(document))
# 5. Set parser's active speculative HTML parser to speculativeParser.
this.m_active_speculative_html_parser = speculative_parser
# 6. In parallel, run speculativeParser until it is stopped or until it reaches the end of its input stream.
html_speculative_html_parser_run(speculative_parser)
## Ported from HTMLParser.cpp:5826 Web::HTML::HTMLParser::stop_the_speculative_html_parser.
# https://html.spec.whatwg.org/multipage/parsing.html#stop-the-speculative-html-parser
pub func html_parser_stop_the_speculative_html_parser(this: HtmlParser*):
# 1. Let speculativeParser be parser's active speculative HTML parser.
# 2. If speculativeParser is null, then return.
let speculative_parser = this.m_active_speculative_html_parser else return
# 3. Throw away any pending content in speculativeParser's input stream, and discard any future content that would
# have been added to it.
html_speculative_html_parser_stop(speculative_parser)
# 4. Set parser's active speculative HTML parser to null.
this.m_active_speculative_html_parser = none
## Ported from HTMLParser.cpp:5844 Web::HTML::HTMLParser::abort.
# https://html.spec.whatwg.org/multipage/parsing.html#abort-a-parser
pub func html_parser_abort(this: HtmlParser*):
# 1. Throw away any pending content in the input stream, and discard any future content that would have been added to it.
html_syntax.html_tokenizer_abort(&this.m_tokenizer)
# 2. Stop the speculative HTML parser for this HTML parser.
html_parser_stop_the_speculative_html_parser(this)
# 3. Update the current document readiness to "interactive".
dom_document_update_readiness(html_parser_document(this), HtmlDocumentReadyState.interactive)
# 4. Pop all the nodes off the stack of open elements.
while not html_stack_of_open_elements_is_empty(&this.m_stack_of_open_elements):
_ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
# 5. Update the current document readiness to "complete".
dom_document_update_readiness(html_parser_document(this), HtmlDocumentReadyState.complete)
this.m_aborted = true
## Ported from HTMLParser.cpp:5866 Web::HTML::HTMLParser::insert_an_element_at_the_adjusted_insertion_location.
# https://html.spec.whatwg.org/multipage/parsing.html#insert-an-element-at-the-adjusted-insertion-location
pub func html_parser_insert_an_element_at_the_adjusted_insertion_location(this: HtmlParser*, element: DomElement*):
# 1. Let the adjusted insertion location be the appropriate place for inserting a node.
let adjusted_insertion_location = html_parser_find_appropriate_place_for_inserting_node(this)
# 2. If it is not possible to insert element at the adjusted insertion location, abort these steps.
let parent = adjusted_insertion_location.parent else return
# 3. If the parser was not created as part of the HTML fragment parsing algorithm,
# then push a new element queue onto element's relevant agent's custom element reactions stack.
if not this.m_parsing_fragment:
ak.vector_append_t(&html_relevant_similar_origin_window_agent((JsObject*)element).custom_element_reactions_stack.element_queue_stack, ak.Vector[gc.Weak[DomElement]]())
# 4. Insert element at the adjusted insertion location.
dom_node_insert_before(parent, element.dom_node(), adjusted_insertion_location.insert_before_sibling)
# 5. If the parser was not created as part of the HTML fragment parsing algorithm,
# then pop the element queue from element's relevant agent's custom element reactions stack, and invoke custom element reactions in that queue.
if not this.m_parsing_fragment:
var queue = ak.vector_take_last(&html_relevant_similar_origin_window_agent((JsObject*)element).custom_element_reactions_stack.element_queue_stack)
bind_invoke_custom_element_reactions(&queue)
# mark: HTMLParser.h ============================================================================
## Ported from HTMLParser.h:84 Web::HTML::HTMLParser::insertion_mode.
pub func html_parser_insertion_mode(this: HtmlParser*) -> HtmlParserInsertionMode:
return this.m_insertion_mode
## Ported from HTMLParser.h:88 Web::HTML::HTMLParser::tokenizer.
pub func html_parser_tokenizer(this: HtmlParser*) -> html_syntax.HtmlTokenizer*:
return &this.m_tokenizer
## Ported from HTMLParser.h:93 Web::HTML::HTMLParser::aborted.
pub func html_parser_aborted(this: HtmlParser*) -> bool:
return this.m_aborted
## Ported from HTMLParser.h:94 Web::HTML::HTMLParser::stopped.
pub func html_parser_stopped(this: HtmlParser*) -> bool:
return this.m_stop_parsing
## Ported from HTMLParser.h:95 Web::HTML::HTMLParser::is_paused.
pub func html_parser_is_paused(this: HtmlParser*) -> bool:
return this.m_parser_pause_flag
## Ported from HTMLParser.h:96 Web::HTML::HTMLParser::is_script_created.
pub func html_parser_is_script_created(this: HtmlParser*) -> bool:
return this.m_script_created
## Ported from HTMLParser.h:98 Web::HTML::HTMLParser::script_nesting_level.
pub func html_parser_script_nesting_level(this: HtmlParser*) -> usize:
return this.m_script_nesting_level
## Ported from HTMLParser.h:101 Web::HTML::HTMLParser::set_post_parse_action.
pub func html_parser_set_post_parse_action(this: HtmlParser*, action: ak.Function0[unit]):
this.m_post_parse_action = action
## Ported from HTMLParser.h:141 Web::HTML::HTMLParser::stop_parsing.
pub func html_parser_stop_parsing(this: HtmlParser*):
this.m_stop_parsing = true