summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth Myhra <kennethmyhra@gmail.com>2023-02-03 21:49:54 +0100
committerLinus Groh <mail@linusgroh.de>2023-02-12 00:18:09 +0000
commitb74d5a423deeb0a9e504f6f3dece6b3472152bc4 (patch)
treec8c6f28dcf0e752fea9f13cfa2d16109a62274fe
parent2ad9c1fd6cc46183f0a57e30cd36a64afe5a42e8 (diff)
downloadserenity-b74d5a423deeb0a9e504f6f3dece6b3472152bc4.zip
LibWeb: Add convert string into scalar value from Infra spec
-rw-r--r--Userland/Libraries/LibWeb/Infra/Strings.cpp15
-rw-r--r--Userland/Libraries/LibWeb/Infra/Strings.h2
2 files changed, 17 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Infra/Strings.cpp b/Userland/Libraries/LibWeb/Infra/Strings.cpp
index 973982833b..9a95fb9750 100644
--- a/Userland/Libraries/LibWeb/Infra/Strings.cpp
+++ b/Userland/Libraries/LibWeb/Infra/Strings.cpp
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
+ * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -65,4 +66,18 @@ bool is_code_unit_prefix(StringView potential_prefix, StringView input)
}
}
+// https://infra.spec.whatwg.org/#scalar-value-string
+ErrorOr<String> convert_to_scalar_value_string(StringView string)
+{
+ // To convert a string into a scalar value string, replace any surrogates with U+FFFD.
+ StringBuilder scalar_value_builder;
+ auto utf8_view = Utf8View { string };
+ for (u32 code_point : utf8_view) {
+ if (is_unicode_surrogate(code_point))
+ code_point = 0xFFFD;
+ TRY(scalar_value_builder.try_append(code_point));
+ }
+ return scalar_value_builder.to_string();
+}
+
}
diff --git a/Userland/Libraries/LibWeb/Infra/Strings.h b/Userland/Libraries/LibWeb/Infra/Strings.h
index 17670b8fb6..a3f6fa3d8f 100644
--- a/Userland/Libraries/LibWeb/Infra/Strings.h
+++ b/Userland/Libraries/LibWeb/Infra/Strings.h
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
+ * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -13,5 +14,6 @@ namespace Web::Infra {
DeprecatedString strip_and_collapse_whitespace(StringView string);
bool is_code_unit_prefix(StringView potential_prefix, StringView input);
+ErrorOr<String> convert_to_scalar_value_string(StringView string);
}