LibWeb: Implement USVString scalar value handling

USVString is defined in the IDL spec as:

> The USVString type corresponds to scalar value strings. Depending on
> the context, these can be treated as sequences of either 16-bit
> unsigned integer code units or scalar values.

This means we need to account for surrogate code points by using the
replacement character.

This fixes the last test in https://wpt.live/url/url-constructor.any.html
This commit is contained in:
Shannon Booth 2024-08-07 18:42:14 +12:00 committed by Tim Ledbetter
parent 2e884ff140
commit aa32bfa448
Notes: github-actions[bot] 2024-08-08 09:50:59 +00:00
3 changed files with 32 additions and 4 deletions

View file

@ -332,6 +332,11 @@ static void emit_includes_for_all_imports(auto& interface, auto& generator, bool
template<typename ParameterType>
static void generate_to_string(SourceGenerator& scoped_generator, ParameterType const& parameter, bool variadic, bool optional, Optional<ByteString> const& optional_default_value)
{
if (parameter.type->name() == "USVString")
scoped_generator.set("to_string", "to_well_formed_string"sv);
else
scoped_generator.set("to_string", "to_string"sv);
if (variadic) {
scoped_generator.append(R"~~~(
Vector<String> @cpp_name@;
@ -340,7 +345,7 @@ static void generate_to_string(SourceGenerator& scoped_generator, ParameterType
@cpp_name@.ensure_capacity(vm.argument_count() - @js_suffix@);
for (size_t i = @js_suffix@; i < vm.argument_count(); ++i) {
auto to_string_result = TRY(vm.argument(i).to_string(vm));
auto to_string_result = TRY(vm.argument(i).@to_string@(vm));
@cpp_name@.unchecked_append(move(to_string_result));
}
}
@ -350,14 +355,14 @@ static void generate_to_string(SourceGenerator& scoped_generator, ParameterType
scoped_generator.append(R"~~~(
@string_type@ @cpp_name@;
if (!@legacy_null_to_empty_string@ || !@js_name@@js_suffix@.is_null()) {
@cpp_name@ = TRY(@js_name@@js_suffix@.to_string(vm));
@cpp_name@ = TRY(@js_name@@js_suffix@.@to_string@(vm));
}
)~~~");
} else {
scoped_generator.append(R"~~~(
Optional<@string_type@> @cpp_name@;
if (!@js_name@@js_suffix@.is_nullish())
@cpp_name@ = TRY(@js_name@@js_suffix@.to_string(vm));
@cpp_name@ = TRY(@js_name@@js_suffix@.@to_string@(vm));
)~~~");
}
} else {
@ -375,7 +380,7 @@ static void generate_to_string(SourceGenerator& scoped_generator, ParameterType
scoped_generator.append(R"~~~(
if (!@js_name@@js_suffix@.is_undefined()) {
if (!@legacy_null_to_empty_string@ || !@js_name@@js_suffix@.is_null())
@cpp_name@ = TRY(@js_name@@js_suffix@.to_string(vm));
@cpp_name@ = TRY(@js_name@@js_suffix@.@to_string@(vm));
})~~~");
if (!may_be_null) {
scoped_generator.append(R"~~~( else {

View file

@ -163,6 +163,17 @@ pathname => '/foo/bar'
search => '??a=b&c=d'
searchParams => '%3Fa=b&c=d'
hash => ''
new URL('http://example.com/<2F><><EFBFBD>𐟾<EFBFBD><F0909FBE><EFBFBD>﷐﷏﷯ﷰ￾￿?<3F><><EFBFBD>𐟾<EFBFBD><F0909FBE><EFBFBD>﷐﷏﷯ﷰ￾￿', undefined)
protocol => 'http:'
username => ''
password => ''
host => 'example.com'
hostname => 'example.com'
port => ''
pathname => '/%EF%BF%BD%F0%90%9F%BE%EF%BF%BD%EF%B7%90%EF%B7%8F%EF%B7%AF%EF%B7%B0%EF%BF%BE%EF%BF%BF'
search => '?%EF%BF%BD%F0%90%9F%BE%EF%BF%BD%EF%B7%90%EF%B7%8F%EF%B7%AF%EF%B7%B0%EF%BF%BE%EF%BF%BF'
searchParams => '%EF%BF%BD%F0%90%9F%BE%EF%BF%BD%EF%B7%90%EF%B7%8F%EF%B7%AF%EF%B7%B0%EF%BF%BE%EF%BF%BF='
hash => ''
=========================================
URL.parse('ftp://serenityos.org:21', undefined)
protocol => 'ftp:'
@ -329,3 +340,14 @@ pathname => '/foo/bar'
search => '??a=b&c=d'
searchParams => '%3Fa=b&c=d'
hash => ''
URL.parse('http://example.com/<2F><><EFBFBD>𐟾<EFBFBD><F0909FBE><EFBFBD>﷐﷏﷯ﷰ￾￿?<3F><><EFBFBD>𐟾<EFBFBD><F0909FBE><EFBFBD>﷐﷏﷯ﷰ￾￿', undefined)
protocol => 'http:'
username => ''
password => ''
host => 'example.com'
hostname => 'example.com'
port => ''
pathname => '/%EF%BF%BD%F0%90%9F%BE%EF%BF%BD%EF%B7%90%EF%B7%8F%EF%B7%AF%EF%B7%B0%EF%BF%BE%EF%BF%BF'
search => '?%EF%BF%BD%F0%90%9F%BE%EF%BF%BD%EF%B7%90%EF%B7%8F%EF%B7%AF%EF%B7%B0%EF%BF%BE%EF%BF%BF'
searchParams => '%EF%BF%BD%F0%90%9F%BE%EF%BF%BD%EF%B7%90%EF%B7%8F%EF%B7%AF%EF%B7%B0%EF%BF%BE%EF%BF%BF='
hash => ''

View file

@ -37,6 +37,7 @@
{ input: '/c:/foo/bar', base: 'file:///c:/baz/qux' },
{ input: '', base: 'file:///test?test#test' },
{ input: '??a=b&c=d', base: 'http://example.org/foo/bar' },
{ input: 'http://example.com/\uD800\uD801\uDFFE\uDFFF\uFDD0\uFDCF\uFDEF\uFDF0\uFFFE\uFFFF?\uD800\uD801\uDFFE\uDFFF\uFDD0\uFDCF\uFDEF\uFDF0\uFFFE\uFFFF' },
];
for (url of urls) {