mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 12:19:54 +00:00
LibURL/Pattern: Implement helper for escaping a URL Pattern String
This commit is contained in:
parent
c7bba505ea
commit
6b85748f53
Notes:
github-actions[bot]
2025-04-06 12:28:29 +00:00
Author: https://github.com/shannonbooth
Commit: 6b85748f53
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3847
Reviewed-by: https://github.com/trflynn89
3 changed files with 65 additions and 0 deletions
|
@ -10,6 +10,7 @@ set(SOURCES
|
||||||
Pattern/Canonicalization.cpp
|
Pattern/Canonicalization.cpp
|
||||||
Pattern/ConstructorStringParser.cpp
|
Pattern/ConstructorStringParser.cpp
|
||||||
Pattern/Pattern.cpp
|
Pattern/Pattern.cpp
|
||||||
|
Pattern/String.cpp
|
||||||
Pattern/Tokenizer.cpp
|
Pattern/Tokenizer.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
49
Libraries/LibURL/Pattern/String.cpp
Normal file
49
Libraries/LibURL/Pattern/String.cpp
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <AK/StringBuilder.h>
|
||||||
|
#include <LibURL/Pattern/String.h>
|
||||||
|
|
||||||
|
namespace URL::Pattern {
|
||||||
|
|
||||||
|
// https://urlpattern.spec.whatwg.org/#escape-a-pattern-string
|
||||||
|
String escape_a_pattern_string(String const& input)
|
||||||
|
{
|
||||||
|
// 1. Assert: input is an ASCII string.
|
||||||
|
VERIFY(all_of(input.code_points(), is_ascii));
|
||||||
|
|
||||||
|
// 2. Let result be the empty string.
|
||||||
|
StringBuilder result;
|
||||||
|
|
||||||
|
// 3. Let index be 0.
|
||||||
|
// 4. While index is less than input’s length:
|
||||||
|
for (auto c : input.bytes_as_string_view()) {
|
||||||
|
// 1. Let c be input[index].
|
||||||
|
// 2. Increment index by 1.
|
||||||
|
|
||||||
|
// 3. If c is one of:
|
||||||
|
// * U+002B (+);
|
||||||
|
// * U+002A (*);
|
||||||
|
// * U+003F (?);
|
||||||
|
// * U+003A (:);
|
||||||
|
// * U+007B ({);
|
||||||
|
// * U+007D (});
|
||||||
|
// * U+0028 (();
|
||||||
|
// * U+0029 ()); or
|
||||||
|
// * U+005C (\),
|
||||||
|
// then append U+005C (\) to the end of result.
|
||||||
|
if ("+*?:{}()\\"sv.contains(c))
|
||||||
|
result.append('\\');
|
||||||
|
|
||||||
|
// 4. Append c to the end of result.
|
||||||
|
result.append(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Return result.
|
||||||
|
return result.to_string_without_validation();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
15
Libraries/LibURL/Pattern/String.h
Normal file
15
Libraries/LibURL/Pattern/String.h
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/String.h>
|
||||||
|
|
||||||
|
namespace URL::Pattern {
|
||||||
|
|
||||||
|
String escape_a_pattern_string(String const&);
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue