1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:27:35 +00:00

LibWeb/Infra: Port strip_and_collapse_whitespace() to new String

This commit is contained in:
Linus Groh 2023-03-04 21:42:38 +00:00
parent f65cbeef5c
commit 93ed1b59c8
3 changed files with 8 additions and 8 deletions

View file

@ -108,7 +108,7 @@ DeprecatedString HTMLOptionElement::text() const
}); });
// Return the result of stripping and collapsing ASCII whitespace from the above concatenation. // Return the result of stripping and collapsing ASCII whitespace from the above concatenation.
return Infra::strip_and_collapse_whitespace(builder.string_view()); return Infra::strip_and_collapse_whitespace(builder.string_view()).release_value_but_fixme_should_propagate_errors().to_deprecated_string();
} }
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org> * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, networkException <networkexception@serenityos.org> * Copyright (c) 2022, networkException <networkexception@serenityos.org>
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org> * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
@ -8,7 +8,7 @@
*/ */
#include <AK/CharacterTypes.h> #include <AK/CharacterTypes.h>
#include <AK/DeprecatedString.h> #include <AK/String.h>
#include <AK/Utf16View.h> #include <AK/Utf16View.h>
#include <AK/Utf8View.h> #include <AK/Utf8View.h>
#include <LibWeb/Infra/CharacterTypes.h> #include <LibWeb/Infra/CharacterTypes.h>
@ -41,7 +41,7 @@ bool is_ascii_case_insensitive_match(StringView a, StringView b)
} }
// https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace // https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace
DeprecatedString strip_and_collapse_whitespace(StringView string) ErrorOr<String> strip_and_collapse_whitespace(StringView string)
{ {
// Replace any sequence of one or more consecutive code points that are ASCII whitespace in the string with a single U+0020 SPACE code point. // Replace any sequence of one or more consecutive code points that are ASCII whitespace in the string with a single U+0020 SPACE code point.
StringBuilder builder; StringBuilder builder;
@ -51,11 +51,11 @@ DeprecatedString strip_and_collapse_whitespace(StringView string)
builder.append(' '); builder.append(' ');
continue; continue;
} }
builder.append_code_point(code_point); TRY(builder.try_append_code_point(code_point));
} }
// ...and then remove any leading and trailing ASCII whitespace from that string. // ...and then remove any leading and trailing ASCII whitespace from that string.
return builder.string_view().trim(Infra::ASCII_WHITESPACE); return String::from_utf8(builder.string_view().trim(Infra::ASCII_WHITESPACE));
} }
// https://infra.spec.whatwg.org/#code-unit-prefix // https://infra.spec.whatwg.org/#code-unit-prefix

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org> * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, networkException <networkexception@serenityos.org> * Copyright (c) 2022, networkException <networkexception@serenityos.org>
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org> * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
@ -14,7 +14,7 @@
namespace Web::Infra { namespace Web::Infra {
bool is_ascii_case_insensitive_match(StringView a, StringView b); bool is_ascii_case_insensitive_match(StringView a, StringView b);
DeprecatedString strip_and_collapse_whitespace(StringView string); ErrorOr<String> strip_and_collapse_whitespace(StringView string);
bool is_code_unit_prefix(StringView potential_prefix, StringView input); bool is_code_unit_prefix(StringView potential_prefix, StringView input);
ErrorOr<String> convert_to_scalar_value_string(StringView string); ErrorOr<String> convert_to_scalar_value_string(StringView string);
ErrorOr<String> to_ascii_lowercase(StringView string); ErrorOr<String> to_ascii_lowercase(StringView string);