Skip to content

Commit 5b2bbd0

Browse files
committed
minimise UrlMatcher changes
1 parent bb34b26 commit 5b2bbd0

File tree

5 files changed

+50
-58
lines changed

5 files changed

+50
-58
lines changed

playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -480,17 +480,17 @@ public APIRequestContextImpl request() {
480480

481481
@Override
482482
public void route(String url, Consumer<Route> handler, RouteOptions options) {
483-
route(new UrlMatcher(this.connection.localUtils, baseUrl, url), handler, options);
483+
route(UrlMatcher.forGlob(baseUrl, url, this.connection.localUtils, false), handler, options);
484484
}
485485

486486
@Override
487487
public void route(Pattern url, Consumer<Route> handler, RouteOptions options) {
488-
route(new UrlMatcher(this.connection.localUtils, url), handler, options);
488+
route(new UrlMatcher(url), handler, options);
489489
}
490490

491491
@Override
492492
public void route(Predicate<String> url, Consumer<Route> handler, RouteOptions options) {
493-
route(new UrlMatcher(this.connection.localUtils, url), handler, options);
493+
route(new UrlMatcher(url), handler, options);
494494
}
495495

496496
@Override
@@ -502,7 +502,7 @@ public void routeFromHAR(Path har, RouteFromHAROptions options) {
502502
recordIntoHar(null, har, options);
503503
return;
504504
}
505-
UrlMatcher matcher = UrlMatcher.forOneOf(this.connection.localUtils, baseUrl, options.url);
505+
UrlMatcher matcher = UrlMatcher.forOneOf(baseUrl, options.url, this.connection.localUtils, false);
506506
HARRouter harRouter = new HARRouter(connection.localUtils, har, options.notFound);
507507
onClose(context -> harRouter.dispose());
508508
route(matcher, route -> harRouter.handle(route), null);
@@ -517,17 +517,17 @@ private void route(UrlMatcher matcher, Consumer<Route> handler, RouteOptions opt
517517

518518
@Override
519519
public void routeWebSocket(String url, Consumer<WebSocketRoute> handler) {
520-
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, baseUrl, url), handler);
520+
routeWebSocketImpl(UrlMatcher.forGlob(baseUrl, url, this.connection.localUtils, true), handler);
521521
}
522522

523523
@Override
524524
public void routeWebSocket(Pattern pattern, Consumer<WebSocketRoute> handler) {
525-
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, pattern), handler);
525+
routeWebSocketImpl(new UrlMatcher(pattern), handler);
526526
}
527527

528528
@Override
529529
public void routeWebSocket(Predicate<String> predicate, Consumer<WebSocketRoute> handler) {
530-
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, predicate), handler);
530+
routeWebSocketImpl(new UrlMatcher(predicate), handler);
531531
}
532532

533533
private void routeWebSocketImpl(UrlMatcher matcher, Consumer<WebSocketRoute> handler) {
@@ -656,17 +656,17 @@ public void unrouteAll() {
656656

657657
@Override
658658
public void unroute(String url, Consumer<Route> handler) {
659-
unroute(new UrlMatcher(this.connection.localUtils, this.baseUrl, url), handler);
659+
unroute(UrlMatcher.forGlob(this.baseUrl, url, this.connection.localUtils, false), handler);
660660
}
661661

662662
@Override
663663
public void unroute(Pattern url, Consumer<Route> handler) {
664-
unroute(new UrlMatcher(this.connection.localUtils, url), handler);
664+
unroute(new UrlMatcher(url), handler);
665665
}
666666

667667
@Override
668668
public void unroute(Predicate<String> url, Consumer<Route> handler) {
669-
unroute(new UrlMatcher(this.connection.localUtils, url), handler);
669+
unroute(new UrlMatcher(url), handler);
670670
}
671671

672672
@Override

playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ private Response waitForNavigationImpl(Logger logger, Runnable code, WaitForNavi
10311031

10321032
List<Waitable<Response>> waitables = new ArrayList<>();
10331033
if (matcher == null) {
1034-
matcher = UrlMatcher.forOneOf(this.connection.localUtils, page.context().baseUrl, options.url);
1034+
matcher = UrlMatcher.forOneOf(page.context().baseUrl, options.url, this.connection.localUtils, false);
10351035
}
10361036
logger.log("waiting for navigation " + matcher);
10371037
waitables.add(new WaitForNavigationHelper(matcher, options.waitUntil, logger));
@@ -1078,17 +1078,17 @@ void waitForTimeoutImpl(double timeout) {
10781078

10791079
@Override
10801080
public void waitForURL(String url, WaitForURLOptions options) {
1081-
waitForURL(new UrlMatcher(this.connection.localUtils, page.context().baseUrl, url), options);
1081+
waitForURL(UrlMatcher.forGlob(page.context().baseUrl, url, this.connection.localUtils, false), options);
10821082
}
10831083

10841084
@Override
10851085
public void waitForURL(Pattern url, WaitForURLOptions options) {
1086-
waitForURL(new UrlMatcher(this.connection.localUtils, url), options);
1086+
waitForURL(new UrlMatcher(url), options);
10871087
}
10881088

10891089
@Override
10901090
public void waitForURL(Predicate<String> url, WaitForURLOptions options) {
1091-
waitForURL(new UrlMatcher(this.connection.localUtils, url), options);
1091+
waitForURL(new UrlMatcher(url), options);
10921092
}
10931093

10941094
private void waitForURL(UrlMatcher matcher, WaitForURLOptions options) {

playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -785,17 +785,17 @@ public Frame frame(String name) {
785785

786786
@Override
787787
public Frame frameByUrl(String glob) {
788-
return frameFor(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, glob));
788+
return frameFor(UrlMatcher.forGlob(browserContext.baseUrl, glob, this.connection.localUtils, false));
789789
}
790790

791791
@Override
792792
public Frame frameByUrl(Pattern pattern) {
793-
return frameFor(new UrlMatcher(this.connection.localUtils, pattern));
793+
return frameFor(new UrlMatcher(pattern));
794794
}
795795

796796
@Override
797797
public Frame frameByUrl(Predicate<String> predicate) {
798-
return frameFor(new UrlMatcher(this.connection.localUtils, predicate));
798+
return frameFor(new UrlMatcher(predicate));
799799
}
800800

801801
@Override
@@ -1105,17 +1105,17 @@ private Response reloadImpl(ReloadOptions options) {
11051105

11061106
@Override
11071107
public void route(String url, Consumer<Route> handler, RouteOptions options) {
1108-
route(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, url), handler, options);
1108+
route(UrlMatcher.forGlob(browserContext.baseUrl, url, this.connection.localUtils, false), handler, options);
11091109
}
11101110

11111111
@Override
11121112
public void route(Pattern url, Consumer<Route> handler, RouteOptions options) {
1113-
route(new UrlMatcher(this.connection.localUtils, url), handler, options);
1113+
route(new UrlMatcher(url), handler, options);
11141114
}
11151115

11161116
@Override
11171117
public void route(Predicate<String> url, Consumer<Route> handler, RouteOptions options) {
1118-
route(new UrlMatcher(this.connection.localUtils, url), handler, options);
1118+
route(new UrlMatcher(url), handler, options);
11191119
}
11201120

11211121
@Override
@@ -1127,7 +1127,7 @@ public void routeFromHAR(Path har, RouteFromHAROptions options) {
11271127
browserContext.recordIntoHar(this, har, convertType(options, BrowserContext.RouteFromHAROptions.class));
11281128
return;
11291129
}
1130-
UrlMatcher matcher = UrlMatcher.forOneOf(this.connection.localUtils, browserContext.baseUrl, options.url);
1130+
UrlMatcher matcher = UrlMatcher.forOneOf(browserContext.baseUrl, options.url, this.connection.localUtils, false);
11311131
HARRouter harRouter = new HARRouter(connection.localUtils, har, options.notFound);
11321132
onClose(context -> harRouter.dispose());
11331133
route(matcher, route -> harRouter.handle(route), null);
@@ -1142,17 +1142,17 @@ private void route(UrlMatcher matcher, Consumer<Route> handler, RouteOptions opt
11421142

11431143
@Override
11441144
public void routeWebSocket(String url, Consumer<WebSocketRoute> handler) {
1145-
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, url), handler);
1145+
routeWebSocketImpl(UrlMatcher.forGlob(browserContext.baseUrl, url, this.connection.localUtils, true), handler);
11461146
}
11471147

11481148
@Override
11491149
public void routeWebSocket(Pattern pattern, Consumer<WebSocketRoute> handler) {
1150-
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, pattern), handler);
1150+
routeWebSocketImpl(new UrlMatcher(pattern), handler);
11511151
}
11521152

11531153
@Override
11541154
public void routeWebSocket(Predicate<String> predicate, Consumer<WebSocketRoute> handler) {
1155-
routeWebSocketImpl(new UrlMatcher(this.connection.localUtils, predicate), handler);
1155+
routeWebSocketImpl(new UrlMatcher(predicate), handler);
11561156
}
11571157

11581158
private void routeWebSocketImpl(UrlMatcher matcher, Consumer<WebSocketRoute> handler) {
@@ -1365,17 +1365,17 @@ public void unrouteAll() {
13651365

13661366
@Override
13671367
public void unroute(String url, Consumer<Route> handler) {
1368-
unroute(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, url), handler);
1368+
unroute(UrlMatcher.forGlob(browserContext.baseUrl, url, this.connection.localUtils, false), handler);
13691369
}
13701370

13711371
@Override
13721372
public void unroute(Pattern url, Consumer<Route> handler) {
1373-
unroute(new UrlMatcher(this.connection.localUtils, url), handler);
1373+
unroute(new UrlMatcher(url), handler);
13741374
}
13751375

13761376
@Override
13771377
public void unroute(Predicate<String> url, Consumer<Route> handler) {
1378-
unroute(new UrlMatcher(this.connection.localUtils, url), handler);
1378+
unroute(new UrlMatcher(url), handler);
13791379
}
13801380

13811381
private void unroute(UrlMatcher matcher, Consumer<Route> handler) {
@@ -1508,12 +1508,12 @@ public T get() {
15081508

15091509
@Override
15101510
public Request waitForRequest(String urlGlob, WaitForRequestOptions options, Runnable code) {
1511-
return waitForRequest(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, urlGlob), null, options, code);
1511+
return waitForRequest(UrlMatcher.forGlob(browserContext.baseUrl, urlGlob, this.connection.localUtils, false), null, options, code);
15121512
}
15131513

15141514
@Override
15151515
public Request waitForRequest(Pattern urlPattern, WaitForRequestOptions options, Runnable code) {
1516-
return waitForRequest(new UrlMatcher(this.connection.localUtils, urlPattern), null, options, code);
1516+
return waitForRequest(new UrlMatcher(urlPattern), null, options, code);
15171517
}
15181518

15191519
@Override
@@ -1553,12 +1553,12 @@ private Request waitForRequestFinishedImpl(WaitForRequestFinishedOptions options
15531553

15541554
@Override
15551555
public Response waitForResponse(String urlGlob, WaitForResponseOptions options, Runnable code) {
1556-
return waitForResponse(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, urlGlob), null, options, code);
1556+
return waitForResponse(UrlMatcher.forGlob(browserContext.baseUrl, urlGlob, this.connection.localUtils, false), null, options, code);
15571557
}
15581558

15591559
@Override
15601560
public Response waitForResponse(Pattern urlPattern, WaitForResponseOptions options, Runnable code) {
1561-
return waitForResponse(new UrlMatcher(this.connection.localUtils, urlPattern), null, options, code);
1561+
return waitForResponse(new UrlMatcher(urlPattern), null, options, code);
15621562
}
15631563

15641564
@Override
@@ -1606,17 +1606,17 @@ public void waitForTimeout(double timeout) {
16061606

16071607
@Override
16081608
public void waitForURL(String url, WaitForURLOptions options) {
1609-
waitForURL(new UrlMatcher(this.connection.localUtils, browserContext.baseUrl, url), options);
1609+
waitForURL(UrlMatcher.forGlob(browserContext.baseUrl, url, this.connection.localUtils, false), options);
16101610
}
16111611

16121612
@Override
16131613
public void waitForURL(Pattern url, WaitForURLOptions options) {
1614-
waitForURL(new UrlMatcher(this.connection.localUtils, url), options);
1614+
waitForURL(new UrlMatcher(url), options);
16151615
}
16161616

16171617
@Override
16181618
public void waitForURL(Predicate<String> url, WaitForURLOptions options) {
1619-
waitForURL(new UrlMatcher(this.connection.localUtils, url), options);
1619+
waitForURL(new UrlMatcher(url), options);
16201620
}
16211621

16221622
private void waitForURL(UrlMatcher matcher, WaitForURLOptions options) {

playwright/src/main/java/com/microsoft/playwright/impl/UrlMatcher.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,23 @@
2727
import static com.microsoft.playwright.impl.Utils.toJsRegexFlags;
2828

2929
class UrlMatcher {
30-
private final LocalUtils localUtils;
3130
private final String baseURL;
3231
public final String glob;
3332
public final Pattern pattern;
3433
public final Predicate<String> predicate;
3534

36-
static UrlMatcher forOneOf(LocalUtils localUtils, URL baseUrl, Object object) {
35+
static UrlMatcher forOneOf(URL baseUrl, Object object, LocalUtils localUtils, boolean isWebSocketUrl) {
3736
if (object == null) {
38-
return new UrlMatcher(localUtils, null, null, null, null);
37+
return new UrlMatcher(null, null, null, null);
3938
}
4039
if (object instanceof String) {
41-
return new UrlMatcher(localUtils, baseUrl, (String) object);
40+
return UrlMatcher.forGlob(baseUrl, (String) object, localUtils, isWebSocketUrl);
4241
}
4342
if (object instanceof Pattern) {
44-
return new UrlMatcher(localUtils, (Pattern) object);
43+
return new UrlMatcher((Pattern) object);
4544
}
4645
if (object instanceof Predicate) {
47-
return new UrlMatcher(localUtils, (Predicate<String>) object);
46+
return new UrlMatcher((Predicate<String>) object);
4847
}
4948
throw new PlaywrightException("Url must be String, Pattern or Predicate<String>, found: " + object.getClass().getTypeName());
5049
}
@@ -65,44 +64,37 @@ private static String resolveUrl(String baseUrl, String spec) {
6564
}
6665
}
6766

68-
UrlMatcher(LocalUtils localUtils, URL baseURL, String glob) {
69-
this(localUtils, baseURL, glob, null, null);
67+
static UrlMatcher forGlob(URL baseURL, String glob, LocalUtils localUtils, boolean isWebSocketUrl) {
68+
Pattern pattern = localUtils.globToRegex(glob, baseURL != null ? baseURL.toString() : null, isWebSocketUrl);
69+
return new UrlMatcher(baseURL, glob, pattern, null);
7070
}
7171

72-
UrlMatcher(LocalUtils localUtils, Pattern pattern) {
73-
this(localUtils, null, null, pattern, null);
72+
UrlMatcher(Pattern pattern) {
73+
this(null, null, pattern, null);
7474
}
7575

76-
UrlMatcher(LocalUtils localUtils, Predicate<String> predicate) {
77-
this(localUtils, null, null, null, predicate);
76+
UrlMatcher(Predicate<String> predicate) {
77+
this(null, null, null, predicate);
7878
}
7979

80-
private UrlMatcher(LocalUtils localUtils, URL baseURL, String glob, Pattern pattern, Predicate<String> predicate) {
80+
private UrlMatcher(URL baseURL, String glob, Pattern pattern, Predicate<String> predicate) {
8181
this.baseURL = baseURL != null ? baseURL.toString() : null;
8282
this.glob = glob;
8383
this.pattern = pattern;
8484
this.predicate = predicate;
85-
this.localUtils = localUtils;
8685
}
8786

8887
boolean test(String value) {
89-
return testImpl(localUtils, baseURL, pattern, predicate, glob, value, false);
88+
return testImpl(baseURL, pattern, predicate, glob, value);
9089
}
9190

92-
boolean testWebsocket(String value) {
93-
return testImpl(localUtils, baseURL, pattern, predicate, glob, value, true);
94-
}
95-
96-
private static boolean testImpl(LocalUtils localUtils, String baseURL, Pattern pattern, Predicate<String> predicate, String glob, String value, boolean isWebSocket) {
91+
private static boolean testImpl(String baseURL, Pattern pattern, Predicate<String> predicate, String glob, String value) {
9792
if (pattern != null) {
9893
return pattern.matcher(value).find();
9994
}
10095
if (predicate != null) {
10196
return predicate.test(value);
10297
}
103-
if (glob != null) {
104-
return localUtils.globToRegex(glob, baseURL, isWebSocket).matcher(value).find();
105-
}
10698
return true;
10799
}
108100

playwright/src/main/java/com/microsoft/playwright/impl/WebSocketRouter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void add(UrlMatcher matcher, Consumer<WebSocketRoute> handler) {
3232

3333
boolean handle(WebSocketRouteImpl route) {
3434
for (RouteInfo routeInfo: routes) {
35-
if (routeInfo.matcher.testWebsocket(route.url())) {
35+
if (routeInfo.matcher.test(route.url())) {
3636
routeInfo.handle(route);
3737
return true;
3838
}

0 commit comments

Comments
 (0)