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() 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) if (i >= 0)
{ {
@ -346,8 +354,21 @@ String URL::getSubPath (bool includeGetParameters) const
String URL::getQueryString() const String URL::getQueryString() const
{ {
String result;
if (parameterNames.size() > 0) 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 {}; return {};
} }
@ -861,6 +882,14 @@ URL URL::withParameters (const StringPairArray& parametersToAdd) const
return u; return u;
} }
URL URL::withAnchor (const String& anchorToAdd) const
{
auto u = *this;
u.anchor = anchorToAdd;
return u;
}
URL URL::withPOSTData (const String& newPostData) const URL URL::withPOSTData (const String& newPostData) const
{ {
return withPOSTData (MemoryBlock (newPostData.toRawUTF8(), newPostData.getNumBytesAsUTF8())); return withPOSTData (MemoryBlock (newPostData.toRawUTF8(), newPostData.getNumBytesAsUTF8()));

View file

@ -100,6 +100,11 @@ public:
*/ */
String getQueryString() const; String getQueryString() const;
/** If any anchor is set, returns URL-encoded anchor, including the "#"
prefix.
*/
String getAnchorString() const;
/** Returns the scheme of the URL. /** Returns the scheme of the URL.
e.g. for "http://www.xyz.com/foobar", this will return "http" (it won't e.g. for "http://www.xyz.com/foobar", this will return "http" (it won't
@ -187,7 +192,7 @@ public:
@see getParameterNames, getParameterValues @see getParameterNames, getParameterValues
*/ */
[[nodiscard]] URL withParameter (const String& parameterName, [[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. /** 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; [[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. /** 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 When performing a POST where one of your parameters is a binary file, this
@ -723,6 +732,7 @@ private:
String url; String url;
MemoryBlock postData; MemoryBlock postData;
StringArray parameterNames, parameterValues; StringArray parameterNames, parameterValues;
String anchor;
ReferenceCountedArray<Upload> filesToUpload; ReferenceCountedArray<Upload> filesToUpload;