1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

URL: Add support for anchors in URLs

This commit is contained in:
hogliux 2022-11-01 16:56:18 +01:00
parent 4054e25052
commit 0e20a6fdc5
2 changed files with 42 additions and 3 deletions

View file

@ -178,7 +178,15 @@ URL::URL (File localFile)
void URL::init()
{
auto i = url.indexOfChar ('?');
auto i = url.indexOfChar ('#');
if (i >= 0)
{
anchor = removeEscapeChars (url.substring (i + 1));
url = url.upToFirstOccurrenceOf ("#", false, false);
}
i = url.indexOfChar ('?');
if (i >= 0)
{
@ -346,8 +354,21 @@ String URL::getSubPath (bool includeGetParameters) const
String URL::getQueryString() const
{
String result;
if (parameterNames.size() > 0)
return "?" + URLHelpers::getMangledParameters (*this);
result += "?" + URLHelpers::getMangledParameters (*this);
if (anchor.isNotEmpty())
result += getAnchorString();
return result;
}
String URL::getAnchorString() const
{
if (anchor.isNotEmpty())
return "#" + URL::addEscapeChars (anchor, true);
return {};
}
@ -861,6 +882,14 @@ URL URL::withParameters (const StringPairArray& parametersToAdd) const
return u;
}
URL URL::withAnchor (const String& anchorToAdd) const
{
auto u = *this;
u.anchor = anchorToAdd;
return u;
}
URL URL::withPOSTData (const String& newPostData) const
{
return withPOSTData (MemoryBlock (newPostData.toRawUTF8(), newPostData.getNumBytesAsUTF8()));

View file

@ -100,6 +100,11 @@ public:
*/
String getQueryString() const;
/** If any anchor is set, returns URL-encoded anchor, including the "#"
prefix.
*/
String getAnchorString() const;
/** Returns the scheme of the URL.
e.g. for "http://www.xyz.com/foobar", this will return "http" (it won't
@ -187,7 +192,7 @@ public:
@see getParameterNames, getParameterValues
*/
[[nodiscard]] URL withParameter (const String& parameterName,
const String& parameterValue) const;
const String& parameterValue) const;
/** Returns a copy of this URL, with a set of GET or POST parameters added.
@ -197,6 +202,10 @@ public:
*/
[[nodiscard]] URL withParameters (const StringPairArray& parametersToAdd) const;
/** Returns a copy of this URL, with an anchor added to the end of the URL.
*/
[[nodiscard]] URL withAnchor (const String& anchor) const;
/** Returns a copy of this URL, with a file-upload type parameter added to it.
When performing a POST where one of your parameters is a binary file, this
@ -723,6 +732,7 @@ private:
String url;
MemoryBlock postData;
StringArray parameterNames, parameterValues;
String anchor;
ReferenceCountedArray<Upload> filesToUpload;