diff --git a/Libraries/LibURL/CMakeLists.txt b/Libraries/LibURL/CMakeLists.txt index f7f3982f291..2341cf265d2 100644 --- a/Libraries/LibURL/CMakeLists.txt +++ b/Libraries/LibURL/CMakeLists.txt @@ -10,6 +10,7 @@ set(SOURCES Pattern/Canonicalization.cpp Pattern/ConstructorStringParser.cpp Pattern/Init.cpp + Pattern/Part.cpp Pattern/Pattern.cpp Pattern/String.cpp Pattern/Tokenizer.cpp diff --git a/Libraries/LibURL/Pattern/Part.cpp b/Libraries/LibURL/Pattern/Part.cpp new file mode 100644 index 00000000000..9c295b02aad --- /dev/null +++ b/Libraries/LibURL/Pattern/Part.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace URL::Pattern { + +Part::Part(Type type, String value, Modifier modifier) + : type(type) + , value(move(value)) + , modifier(modifier) +{ +} + +Part::Part(Type type, String value, Modifier modifier, String name, String prefix, String suffix) + : type(type) + , value(move(value)) + , modifier(modifier) + , name(move(name)) + , prefix(move(prefix)) + , suffix(move(suffix)) +{ +} + +StringView Part::type_to_string(Part::Type type) +{ + switch (type) { + case Type::FixedText: + return "FixedText"sv; + case Type::Regexp: + return "Regexp"sv; + case Type::SegmentWildcard: + return "SegmentWildcard"sv; + case Type::FullWildcard: + return "FullWildcard"sv; + } + + VERIFY_NOT_REACHED(); +} + +// https://urlpattern.spec.whatwg.org/#convert-a-modifier-to-a-string +StringView Part::convert_modifier_to_string(Part::Modifier modifier) +{ + // 1. If modifier is "zero-or-more", then return "*". + if (modifier == Modifier::ZeroOrMore) + return "*"sv; + + // 2. If modifier is "optional", then return "?". + if (modifier == Modifier::Optional) + return "?"sv; + + // 3. If modifier is "one-or-more", then return "+". + if (modifier == Modifier::OneOrMore) + return "+"sv; + + // 4. Return the empty string. + return ""sv; +} + +} diff --git a/Libraries/LibURL/Pattern/Part.h b/Libraries/LibURL/Pattern/Part.h new file mode 100644 index 00000000000..0947bca9135 --- /dev/null +++ b/Libraries/LibURL/Pattern/Part.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2025, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace URL::Pattern { + +// https://urlpattern.spec.whatwg.org/#part +struct Part { + // https://urlpattern.spec.whatwg.org/#part-type + enum class Type { + // The part represents a simple fixed text string. + FixedText, + + // The part represents a matching group with a custom regular expression. + Regexp, + + // The part represents a matching group that matches code points up to the next separator code point. This is + // typically used for a named group like ":foo" that does not have a custom regular expression. + SegmentWildcard, + + // The part represents a matching group that greedily matches all code points. This is typically used for + // the "*" wildcard matching group. + FullWildcard, + }; + + // https://urlpattern.spec.whatwg.org/#part-modifier + enum class Modifier { + // The part does not have a modifier. + None, + + // The part has an optional modifier indicated by the U+003F (?) code point. + Optional, + + // The part has a "zero or more" modifier indicated by the U+002A (*) code point. + ZeroOrMore, + + // The part has a "one or more" modifier indicated by the U+002B (+) code point. + OneOrMore, + }; + + static StringView convert_modifier_to_string(Modifier); + static StringView type_to_string(Type); + + Part(Type, String value, Modifier); + Part(Type, String value, Modifier, String name, String prefix, String suffix); + + // https://urlpattern.spec.whatwg.org/#part-type + // A part has an associated type, a string, which must be set upon creation. + Type type {}; + + // https://urlpattern.spec.whatwg.org/#part-value + // A part has an associated value, a string, which must be set upon creation. + String value; + + // https://urlpattern.spec.whatwg.org/#part-modifier + // A part has an associated modifier a string, which must be set upon creation. + Modifier modifier; + + // https://urlpattern.spec.whatwg.org/#part-name + // A part has an associated name, a string, initially the empty string. + String name; + + // https://urlpattern.spec.whatwg.org/#part-prefix + // A part has an associated prefix, a string, initially the empty string. + String prefix; + + // https://urlpattern.spec.whatwg.org/#part-suffix + // A part has an associated suffix, a string, initially the empty string. + String suffix; +}; + +}