mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 05:07:35 +00:00
LibWeb: Move strip_and_collapse_whitespace() to Infra/
...and make it spec compliant by considering all ASCII whitespace, greatly simplifying it in the process :^)
This commit is contained in:
parent
b86c264975
commit
7760c00714
4 changed files with 50 additions and 28 deletions
|
@ -277,6 +277,7 @@ set(SOURCES
|
||||||
HighResolutionTime/Performance.cpp
|
HighResolutionTime/Performance.cpp
|
||||||
Infra/ByteSequences.cpp
|
Infra/ByteSequences.cpp
|
||||||
Infra/JSON.cpp
|
Infra/JSON.cpp
|
||||||
|
Infra/Strings.cpp
|
||||||
IntersectionObserver/IntersectionObserver.cpp
|
IntersectionObserver/IntersectionObserver.cpp
|
||||||
Layout/AvailableSpace.cpp
|
Layout/AvailableSpace.cpp
|
||||||
Layout/BlockContainer.cpp
|
Layout/BlockContainer.cpp
|
||||||
|
|
|
@ -13,8 +13,7 @@
|
||||||
#include <LibWeb/HTML/HTMLOptionElement.h>
|
#include <LibWeb/HTML/HTMLOptionElement.h>
|
||||||
#include <LibWeb/HTML/HTMLScriptElement.h>
|
#include <LibWeb/HTML/HTMLScriptElement.h>
|
||||||
#include <LibWeb/HTML/HTMLSelectElement.h>
|
#include <LibWeb/HTML/HTMLSelectElement.h>
|
||||||
#include <LibWeb/Infra/CharacterTypes.h>
|
#include <LibWeb/Infra/Strings.h>
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
namespace Web::HTML {
|
namespace Web::HTML {
|
||||||
|
|
||||||
|
@ -76,31 +75,6 @@ void HTMLOptionElement::set_value(String value)
|
||||||
set_attribute(HTML::AttributeNames::value, value);
|
set_attribute(HTML::AttributeNames::value, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace
|
|
||||||
static 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.
|
|
||||||
StringBuilder builder;
|
|
||||||
for (size_t i = 0; i < string.length(); ++i) {
|
|
||||||
if (isspace(string[i])) {
|
|
||||||
builder.append(' ');
|
|
||||||
while (i < string.length()) {
|
|
||||||
if (isspace(string[i])) {
|
|
||||||
++i;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
builder.append(string[i]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
builder.append(string[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ...and then remove any leading and trailing ASCII whitespace from that string.
|
|
||||||
return builder.to_string().trim(Infra::ASCII_WHITESPACE);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void concatenate_descendants_text_content(DOM::Node const* node, StringBuilder& builder)
|
static void concatenate_descendants_text_content(DOM::Node const* node, StringBuilder& builder)
|
||||||
{
|
{
|
||||||
// FIXME: SVGScriptElement should also be skipped, but it doesn't exist yet.
|
// FIXME: SVGScriptElement should also be skipped, but it doesn't exist yet.
|
||||||
|
@ -126,7 +100,7 @@ String 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 strip_and_collapse_whitespace(builder.to_string());
|
return Infra::strip_and_collapse_whitespace(builder.string_view());
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
|
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
|
||||||
|
|
32
Userland/Libraries/LibWeb/Infra/Strings.cpp
Normal file
32
Userland/Libraries/LibWeb/Infra/Strings.cpp
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <AK/Utf8View.h>
|
||||||
|
#include <LibWeb/Infra/CharacterTypes.h>
|
||||||
|
#include <LibWeb/Infra/Strings.h>
|
||||||
|
|
||||||
|
namespace Web::Infra {
|
||||||
|
|
||||||
|
// https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace
|
||||||
|
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.
|
||||||
|
StringBuilder builder;
|
||||||
|
for (auto code_point : Utf8View { string }) {
|
||||||
|
if (Infra::is_ascii_whitespace(code_point)) {
|
||||||
|
if (!builder.string_view().ends_with(' '))
|
||||||
|
builder.append(' ');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
builder.append_code_point(code_point);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...and then remove any leading and trailing ASCII whitespace from that string.
|
||||||
|
return builder.string_view().trim(Infra::ASCII_WHITESPACE);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
15
Userland/Libraries/LibWeb/Infra/Strings.h
Normal file
15
Userland/Libraries/LibWeb/Infra/Strings.h
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Forward.h>
|
||||||
|
|
||||||
|
namespace Web::Infra {
|
||||||
|
|
||||||
|
String strip_and_collapse_whitespace(StringView string);
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue