Skip to content

Commit

Permalink
Ensure that URLs passed to parser objects are canonicalized
Browse files Browse the repository at this point in the history
Closes #2297
  • Loading branch information
nex3 committed Aug 12, 2024
1 parent 613fb17 commit ad02a41
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/src/parse/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:charcode/charcode.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:source_span/source_span.dart';
import 'package:string_scanner/string_scanner.dart';

Expand Down Expand Up @@ -59,10 +60,27 @@ class Parser {
@protected
Parser(String contents,
{Object? url, Logger? logger, InterpolationMap? interpolationMap})
: scanner = SpanScanner(contents, sourceUrl: url),
: scanner =
SpanScanner(contents, sourceUrl: _canonicalizeIfRelative(url)),
logger = logger ?? const Logger.stderr(),
_interpolationMap = interpolationMap;

/// If [url] is a relative (possibly String-serialized) URL, canonicalizes it
/// as a path and returns it as a URL.
static Uri? _canonicalizeIfRelative(Object? urlOrString) {
var url = switch (urlOrString) {
null => null,
Uri() => urlOrString,
String() => Uri.parse(urlOrString),
_ => throw ArgumentError("url $urlOrString must be a Uri or a String")
};

return switch (url) {
Uri(scheme: '') => p.toUri(canonicalize(p.fromUri(url))),
_ => url
};
}

String _parseIdentifier() {
return wrapSpanFormatException(() {
var result = identifier();
Expand Down

0 comments on commit ad02a41

Please sign in to comment.