Skip to content

Commit 8a9c39e

Browse files
committed
internal_link [nfc]: Factor apart muddled "is internal link" condition
The logic here is fine; but "is internal link" isn't an accurate label for the condition that one gets from combining together this particular subset of the logic.
1 parent 5599f64 commit 8a9c39e

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

lib/model/internal_link.dart

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,27 +148,31 @@ class NarrowLink extends InternalLink {
148148
/// #narrow/stream/1-announce/stream/1-announce (duplicated operator)
149149
// TODO(#1661): handle all valid narrow links, returning a search narrow
150150
InternalLink? parseInternalLink(Uri url, PerAccountStore store) {
151-
if (!_isInternalLink(url, store.realmUrl)) return null;
152-
153-
final (category, segments) = _getCategoryAndSegmentsFromFragment(url.fragment);
154-
switch (category) {
155-
case 'narrow':
156-
if (segments.isEmpty || !segments.length.isEven) return null;
157-
return _interpretNarrowSegments(segments, store);
151+
if (!_sameOrigin(url, store.realmUrl)) return null;
152+
153+
if ((url.hasEmptyPath || url.path == '/')) {
154+
if (url.hasQuery) return null;
155+
if (!url.hasFragment) return null;
156+
// The URL is of the form `/#…` relative to the realm URL,
157+
// the shape used for representing a state within the web app.
158+
final (category, segments) = _getCategoryAndSegmentsFromFragment(url.fragment);
159+
switch (category) {
160+
case 'narrow':
161+
if (segments.isEmpty || !segments.length.isEven) return null;
162+
return _interpretNarrowSegments(segments, store);
163+
}
158164
}
165+
159166
return null;
160167
}
161168

162-
/// Check if `url` is an internal link on the given `realmUrl`.
163-
bool _isInternalLink(Uri url, Uri realmUrl) {
169+
/// Check if `url` has the same origin as `realmUrl`.
170+
bool _sameOrigin(Uri url, Uri realmUrl) {
164171
try {
165-
if (url.origin != realmUrl.origin) return false;
172+
return url.origin == realmUrl.origin;
166173
} on StateError {
167174
return false;
168175
}
169-
return (url.hasEmptyPath || url.path == '/')
170-
&& !url.hasQuery
171-
&& url.hasFragment;
172176
}
173177

174178
/// Split `fragment` of arbitrary segments and handle trailing slashes

0 commit comments

Comments
 (0)