Skip to content

Commit

Permalink
Implement URL.parse()
Browse files Browse the repository at this point in the history
Adding static method to URL class which returns null when url string is
invalid.

Chrome Status: https://chromestatus.com/feature/6301071388704768

Issues: whatwg/url#825

Intent to Ship: https://groups.google.com/u/3/a/chromium.org/g/blink-dev/c/G070zUd0e4c

Bug: 331041242
Change-Id: I794e1d4624593de8ebfcfb9b227951e8c38e9a58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5414853
Commit-Queue: Tsuyoshi Horo <horo@chromium.org>
Reviewed-by: Hayato Ito <hayato@chromium.org>
Reviewed-by: Tsuyoshi Horo <horo@chromium.org>
Reviewed-by: Yoav Weiss (@Shopify) <yoavweiss@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1294046}
  • Loading branch information
Jxck authored and Chromium LUCI CQ committed Apr 30, 2024
1 parent e2e23f4 commit d1db8eb
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 54 deletions.
26 changes: 26 additions & 0 deletions third_party/blink/renderer/core/url/dom_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "third_party/blink/renderer/core/url/url_search_params.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/loader/fetch/memory_cache.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"

namespace blink {

Expand Down Expand Up @@ -63,13 +64,38 @@ DOMURL::DOMURL(PassKey,
exception_state.ThrowTypeError("Invalid URL");
}

DOMURL::DOMURL(PassKey, const KURL& url): url_(url) {
}

DOMURL::~DOMURL() = default;

void DOMURL::Trace(Visitor* visitor) const {
visitor->Trace(search_params_);
ScriptWrappable::Trace(visitor);
}

// static
DOMURL* DOMURL::parse(const String& str) {
KURL url(str);
if (!url.IsValid()) {
return nullptr;
}
return MakeGarbageCollected<DOMURL>(PassKey(), url);
}

// static
DOMURL* DOMURL::parse(const String& str, const String& base) {
KURL base_url(base);
if (!base_url.IsValid()) {
return nullptr;
}
KURL url(base_url, str);
if (!url.IsValid()) {
return nullptr;
}
return MakeGarbageCollected<DOMURL>(PassKey(), url);
}

// static
bool DOMURL::canParse(const String& url) {
return KURL(NullURL(), url).IsValid();
Expand Down
4 changes: 4 additions & 0 deletions third_party/blink/renderer/core/url/dom_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ class CORE_EXPORT DOMURL final : public ScriptWrappable, public DOMURLUtils {
ExceptionState& exception_state);

DOMURL(PassKey, const String& url, const KURL& base, ExceptionState&);
DOMURL(PassKey, const KURL& url);
~DOMURL() override;

static DOMURL* parse(const String& url);
static DOMURL* parse(const String& url, const String& base);

static bool canParse(const String& url);
static bool canParse(const String& url, const String& base);

Expand Down
1 change: 1 addition & 0 deletions third_party/blink/renderer/core/url/url.idl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
] interface URL {
[RaisesException] constructor(USVString url, optional USVString base);

[RuntimeEnabled=URLParse] static URL? parse(USVString url, optional USVString base);
static boolean canParse(USVString url, optional USVString base);

[RaisesException=Setter] stringifier attribute USVString href;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3963,6 +3963,10 @@
name: "URLAttributeFix",
status: "stable",
},
{
name: "URLParse",
status: "stable",
},
{
name: "URLPatternCompareComponent",
status: "experimental",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1934,6 +1934,7 @@ interface TrustedTypePolicyFactory : EventTarget
setter onbeforecreatepolicy
interface URL
static method canParse
static method parse
attribute @@toStringTag
getter hash
getter host
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,7 @@ interface TrustedTypePolicyFactory : EventTarget
method isScriptURL
interface URL
static method canParse
static method parse
attribute @@toStringTag
getter hash
getter host
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] interface URL
[Worker] static method canParse
[Worker] static method createObjectURL
[Worker] static method parse
[Worker] static method revokeObjectURL
[Worker] attribute @@toStringTag
[Worker] getter hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9064,6 +9064,7 @@ interface UIEvent : Event
interface URL
static method canParse
static method createObjectURL
static method parse
static method revokeObjectURL
attribute @@toStringTag
getter hash
Expand Down Expand Up @@ -11399,6 +11400,7 @@ interface webkitSpeechRecognitionEvent : Event
interface webkitURL
static method canParse
static method createObjectURL
static method parse
static method revokeObjectURL
attribute @@toStringTag
getter hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] interface URL
[Worker] static method canParse
[Worker] static method createObjectURL
[Worker] static method parse
[Worker] static method revokeObjectURL
[Worker] attribute @@toStringTag
[Worker] getter hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] interface URL
[Worker] static method canParse
[Worker] static method createObjectURL
[Worker] static method parse
[Worker] static method revokeObjectURL
[Worker] attribute @@toStringTag
[Worker] getter hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10178,6 +10178,7 @@ interface UIEvent : Event
interface URL
static method canParse
static method createObjectURL
static method parse
static method revokeObjectURL
attribute @@toStringTag
getter hash
Expand Down Expand Up @@ -12674,6 +12675,7 @@ interface webkitSpeechRecognitionEvent : Event
interface webkitURL
static method canParse
static method createObjectURL
static method parse
static method revokeObjectURL
attribute @@toStringTag
getter hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] interface URL
[Worker] static method canParse
[Worker] static method createObjectURL
[Worker] static method parse
[Worker] static method revokeObjectURL
[Worker] attribute @@toStringTag
[Worker] getter hash
Expand Down

0 comments on commit d1db8eb

Please sign in to comment.