-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: refactor UrlMatcher #1720
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
@@ -30,20 +32,14 @@ | |
import static com.microsoft.playwright.impl.Utils.toJsRegexFlags; | ||
|
||
class UrlMatcher { | ||
final Object rawSource; | ||
private final Predicate<String> predicate; | ||
|
||
private static Predicate<String> toPredicate(Pattern pattern) { | ||
return s -> pattern.matcher(s).find(); | ||
} | ||
|
||
static UrlMatcher any() { | ||
return new UrlMatcher((Object) null, null); | ||
} | ||
private final URL baseURL; | ||
public final String glob; | ||
public final Pattern pattern; | ||
public final Predicate<String> predicate; | ||
|
||
static UrlMatcher forOneOf(URL baseUrl, Object object) { | ||
if (object == null) { | ||
return UrlMatcher.any(); | ||
return new UrlMatcher(null, null, null, null); | ||
} | ||
if (object instanceof String) { | ||
return new UrlMatcher(baseUrl, (String) object); | ||
|
@@ -68,50 +64,76 @@ static String resolveUrl(URL baseUrl, String spec) { | |
} | ||
} | ||
|
||
UrlMatcher(URL base, String url) { | ||
this(url, toPredicate(Pattern.compile(globToRegex(resolveUrl(base, url)))).or(s -> url == null || url.equals(s))); | ||
UrlMatcher(URL baseURL, String glob) { | ||
this(baseURL, glob, null, null); | ||
} | ||
|
||
UrlMatcher(Pattern pattern) { | ||
this(pattern, toPredicate(pattern)); | ||
this(null, null, pattern, null); | ||
} | ||
|
||
UrlMatcher(Predicate<String> predicate) { | ||
this(predicate, predicate); | ||
this(null, null, null, predicate); | ||
} | ||
|
||
private UrlMatcher(Object rawSource, Predicate<String> predicate) { | ||
this.rawSource = rawSource; | ||
private UrlMatcher(URL baseURL, String glob, Pattern pattern, Predicate<String> predicate) { | ||
this.baseURL = baseURL; | ||
this.glob = glob; | ||
this.pattern = pattern; | ||
this.predicate = predicate; | ||
} | ||
|
||
boolean test(String value) { | ||
return predicate == null || predicate.test(value); | ||
return testImpl(baseURL, pattern, predicate, glob, value); | ||
} | ||
|
||
private static boolean testImpl(URL baseURL, Pattern pattern, Predicate<String> predicate, String glob, String value) { | ||
yury-s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (pattern != null) { | ||
return pattern.matcher(value).find(); | ||
} | ||
if (predicate != null) { | ||
return predicate.test(value); | ||
} | ||
if (glob != null) { | ||
return Pattern.compile(globToRegex(resolveUrl(baseURL, glob))).matcher(value).find(); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
UrlMatcher that = (UrlMatcher) o; | ||
if (rawSource instanceof Pattern && that.rawSource instanceof Pattern) { | ||
Pattern a = (Pattern) rawSource; | ||
Pattern b = (Pattern) that.rawSource; | ||
return a.pattern().equals(b.pattern()) && a.flags() == b.flags(); | ||
if (pattern != null && !pattern.pattern().equals(that.pattern.pattern()) && pattern.flags() == that.pattern.flags()) { | ||
mxschmitt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false; | ||
} | ||
if (predicate != null && !predicate.equals(that.predicate)) { | ||
return false; | ||
} | ||
return Objects.equals(rawSource, that.rawSource); | ||
if (glob != null && !glob.equals(that.glob)) { | ||
return false; | ||
} | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I already have it like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree, with the current code e.g. |
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(rawSource); | ||
if (pattern != null) { | ||
return pattern.hashCode(); | ||
} | ||
if (predicate != null) { | ||
return predicate.hashCode(); | ||
} | ||
return glob.hashCode(); | ||
mxschmitt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (rawSource == null) | ||
return "<any>"; | ||
if (rawSource instanceof Predicate) | ||
return "matching predicate"; | ||
return rawSource.toString(); | ||
if (pattern != null) | ||
return String.format("<regex pattern=\"%s\" flags=\"%s\">", pattern.pattern(), toJsRegexFlags(pattern)); | ||
if (this.predicate != null) | ||
return "<predicate>"; | ||
return String.format("<glob pattern=\"%s\">", glob); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not resolve the pattern in the constructor and avoid plumbing base url all the way through?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this later on when I'll add this check:
https://github.com/microsoft/playwright/blob/edd789780ac473083f6fb0b8740faddc43b439ab/packages/playwright-core/src/utils/isomorphic/urlMatch.ts#L101-L103