dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / tokenized_features.lucb
#==============================================================================================
#
# tokenized_features - window.open()'s features: tokenizing them, parsing a boolean feature
# and checking if a popup window is requested
#
# DESCRIPTION:
# Port of Libraries/LibWeb/HTML/TokenizedFeatures.cpp (lines 1-200). parse_boolean_feature
# is a template over the TokenizedFeature enums (each has Yes and No); the port takes the
# enum's two values as arguments.
#
# Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Tokenizing ==============================================================================
## Ported from TokenizedFeatures.cpp:14 Web::HTML::normalize_feature_name.
# https://html.spec.whatwg.org/multipage/nav-history-apis.html#normalizing-the-feature-name
func html_normalize_feature_name(name: ak.String) -> ak.String:
# For legacy reasons, there are some aliases of some feature names. To normalize a feature name name, switch on name:
# "screenx"
if ak.string_eq_string_view(&name, ak.sv("screenx")):
# Return "left".
return ak.string_from_str("left")
# "screeny"
elif ak.string_eq_string_view(&name, ak.sv("screeny")):
# Return "top".
return ak.string_from_str("top")
# "innerwidth"
elif ak.string_eq_string_view(&name, ak.sv("innerwidth")):
# Return "width".
return ak.string_from_str("width")
# "innerheight"
elif ak.string_eq_string_view(&name, ak.sv("innerheight")):
# Return "height".
return ak.string_from_str("height")
# Anything else
else:
# Return name.
return name
## tokenize_open_features' is_feature_separator lambda.
# https://html.spec.whatwg.org/multipage/nav-history-apis.html#feature-separator
struct HtmlIsFeatureSeparator: ak.CharPredicate:
var unused: u8
func matches(ch: u8) -> bool:
return web_infra.is_ascii_whitespace((u32)ch) or ch == '=' or ch == ','
## tokenize_open_features' step 7 predicate (`is_ascii_whitespace(character) || character == '='`).
struct HtmlIsAsciiWhitespaceOrEquals: ak.CharPredicate:
var unused: u8
func matches(ch: u8) -> bool:
return web_infra.is_ascii_whitespace((u32)ch) or ch == '='
## Ported from TokenizedFeatures.cpp:46 Web::HTML::tokenize_open_features.
# https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-window-open-features-tokenize
pub func html_tokenize_open_features(features: ak.StringView) -> ak.OrderedHashMap[ak.String, ak.String, ak.StringTraits]:
# 1. Let tokenizedFeatures be a new ordered map.
var tokenized_features = ak.OrderedHashMap[ak.String, ak.String, ak.StringTraits]()
# 2. Let position point at the first code point of features.
var lexer = ak.generic_lexer_new(features)
let is_feature_separator = HtmlIsFeatureSeparator(unused = 0)
let is_ascii_whitespace = ak.CharPredicateFunction(function = web_infra.is_ascii_whitespace)
let is_ascii_whitespace_or_equals = HtmlIsAsciiWhitespaceOrEquals(unused = 0)
# 3. While position is not past the end of features:
while not ak.generic_lexer_is_eof(&lexer):
# 1. Let name be the empty string.
# 2. Let value be the empty string.
# 3. Collect a sequence of code points that are feature separators from features given position. This skips past leading separators before the name.
ak.generic_lexer_ignore_while(&lexer, &is_feature_separator)
# 4. Collect a sequence of code points that are not feature separators from features given position. Set name to the collected characters, converted to ASCII lowercase.
var name_view = ak.generic_lexer_consume_until_tt_predicate(&lexer, &is_feature_separator)
var name = ak.string_view_to_ascii_lowercase_string(&name_view)
# 5. Set name to the result of normalizing the feature name name.
name = html_normalize_feature_name(name)
# 6. While position is not past the end of features and the code point at position in features is not U+003D (=):
# 1. If the code point at position in features is U+002C (,), or if it is not a feature separator, then break.
# 2. Advance position by 1.
ak.generic_lexer_ignore_while(&lexer, &is_ascii_whitespace)
# 7. If the code point at position in features is a feature separator:
# 1. While position is not past the end of features and the code point at position in features is a feature separator:
# 1. If the code point at position in features is U+002C (,), then break.
# 2. Advance position by 1.
ak.generic_lexer_ignore_while(&lexer, &is_ascii_whitespace_or_equals)
# 2. Collect a sequence of code points that are not feature separators code points from features given position. Set value to the collected code points, converted to ASCII lowercase.
var value_view = ak.generic_lexer_consume_until_tt_predicate(&lexer, &is_feature_separator)
let value = ak.string_view_to_ascii_lowercase_string(&value_view)
# 8. If name is not the empty string, then set tokenizedFeatures[name] to value.
if not ak.string_is_empty(&name):
_ = ak.ordered_hash_map_set(&tokenized_features, name, value)
# 4. Return tokenizedFeatures.
return tokenized_features
# mark: Boolean features ========================================================================
## Ported from TokenizedFeatures.cpp:101 Web::HTML::parse_boolean_feature (`template<Enum T>`, with
## T::Yes and T::No passed in).
# https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-window-open-features-parse-boolean
pub func html_parse_boolean_feature[T](value: ak.StringView, yes: T, no: T) -> T:
# 1. If value is the empty string, then return true.
if ak.string_view_is_empty(&value):
return yes
# 2. If value is "yes", then return true.
if ak.string_view_equals(&value, "yes"):
return yes
# 3. If value is "true", then return true.
if ak.string_view_equals(&value, "true"):
return yes
# 4. Let parsed be the result of parsing value as an integer.
var parsed = ak.string_view_to_number_i64(&value)
# 5. If parsed is an error, then set it to 0.
if parsed == none:
parsed = 0
# 6. Return false if parsed is 0, and true otherwise.
return no if parsed == 0 else yes
## check_if_a_popup_window_is_requested's tokenizedFeatures[name], or none.
func html_tokenized_feature(tokenized_features: const ak.OrderedHashMap[ak.String, ak.String, ak.StringTraits]*, name: str) -> ak.StringView?:
let feature = ak.ordered_hash_map_get_k_const(tokenized_features, ak.string_from_str(name)) else return none
return ak.string_bytes_as_string_view(feature)
## check_if_a_popup_window_is_requested's check_if_a_window_feature_is_set lambda.
# https://html.spec.whatwg.org/multipage/window-object.html#window-feature-is-set
func html_check_if_a_window_feature_is_set[T](tokenized_features: const ak.OrderedHashMap[ak.String, ak.String, ak.StringTraits]*, feature_name: str, default_value: T, yes: T, no: T) -> T:
# 1. If tokenizedFeatures[featureName] exists, then return the result of parsing tokenizedFeatures[featureName] as a boolean feature.
if let feature = html_tokenized_feature(tokenized_features, feature_name):
return html_parse_boolean_feature(feature, yes, no)
# 2. Return defaultValue.
return default_value
## Ported from TokenizedFeatures.cpp:137 Web::HTML::check_if_a_popup_window_is_requested.
# https://html.spec.whatwg.org/multipage/window-object.html#popup-window-is-requested
pub func html_check_if_a_popup_window_is_requested(tokenized_features: const ak.OrderedHashMap[ak.String, ak.String, ak.StringTraits]*) -> HtmlPopup:
# 1. If tokenizedFeatures is empty, then return false.
if ak.ordered_hash_map_is_empty(tokenized_features):
return HtmlPopup.no
# 2. If tokenizedFeatures["popup"] exists, then return the result of parsing tokenizedFeatures["popup"] as a boolean feature.
if let popup_feature = html_tokenized_feature(tokenized_features, "popup"):
return html_parse_boolean_feature(popup_feature, HtmlPopup.yes, HtmlPopup.no)
# 3. Let location be the result of checking if a window feature is set, given tokenizedFeatures, "location", and false.
let location = html_check_if_a_window_feature_is_set(tokenized_features, "location", HtmlLocation.no, HtmlLocation.yes, HtmlLocation.no)
# 4. Let toolbar be the result of checking if a window feature is set, given tokenizedFeatures, "toolbar", and false.
let toolbar = html_check_if_a_window_feature_is_set(tokenized_features, "toolbar", HtmlToolbar.no, HtmlToolbar.yes, HtmlToolbar.no)
# 5. If location and toolbar are both false, then return true.
if location == HtmlLocation.no and toolbar == HtmlToolbar.no:
return HtmlPopup.yes
# 6. Let menubar be the result of checking if a window feature is set, given tokenizedFeatures, menubar", and false.
let menubar = html_check_if_a_window_feature_is_set(tokenized_features, "menubar", HtmlMenubar.no, HtmlMenubar.yes, HtmlMenubar.no)
# 7. If menubar is false, then return true.
if menubar == HtmlMenubar.no:
return HtmlPopup.yes
# 8. Let resizable be the result of checking if a window feature is set, given tokenizedFeatures, "resizable", and true.
let resizable = html_check_if_a_window_feature_is_set(tokenized_features, "resizable", HtmlResizable.yes, HtmlResizable.yes, HtmlResizable.no)
# 9. If resizable is false, then return true.
if resizable == HtmlResizable.no:
return HtmlPopup.yes
# 10. Let scrollbars be the result of checking if a window feature is set, given tokenizedFeatures, "scrollbars", and false.
let scrollbars = html_check_if_a_window_feature_is_set(tokenized_features, "scrollbars", HtmlScrollbars.no, HtmlScrollbars.yes, HtmlScrollbars.no)
# 11. If scrollbars is false, then return true.
if scrollbars == HtmlScrollbars.no:
return HtmlPopup.yes
# 12. Let status be the result of checking if a window feature is set, given tokenizedFeatures, "status", and false.
let status = html_check_if_a_window_feature_is_set(tokenized_features, "status", HtmlStatus.no, HtmlStatus.yes, HtmlStatus.no)
# 13. If status is false, then return true.
if status == HtmlStatus.no:
return HtmlPopup.yes
# 14. Return false.
return HtmlPopup.no