Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkgs/path/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 1.9.2-wip

- `isWithin` treats a parent that ends in `/.` as that directory, so
`isWithin('/dir/.', '/dir/.file')` is true.
- Make `Url` style better at recognizing schemes and authorities.
Only consider a path as being a schemed URL if it starts with a
valid scheme.
Expand Down
22 changes: 22 additions & 0 deletions pkgs/path/lib/src/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,28 @@ final class Context {
while (parentIndex < parent.length && childIndex < child.length) {
var parentCodeUnit = parent.codeUnitAt(parentIndex);
var childCodeUnit = child.codeUnitAt(childIndex);

// A lone "." component means "this directory". Skip it before comparing
// characters so a parent like "/dir/." is the same directory as "/dir"
// and still contains children such as "/dir/.file". Matching the "."
// against the child's next character would treat ".file" as a different
// component and reject a path that is actually inside the parent.
if (parentCodeUnit == chars.period && style.isSeparator(lastCodeUnit)) {
final nextParentIndex = parentIndex + 1;
final dotIsComponent = nextParentIndex == parent.length ||
style.isSeparator(parent.codeUnitAt(nextParentIndex));
if (dotIsComponent) {
parentIndex = nextParentIndex;
if (parentIndex < parent.length &&
style.isSeparator(parent.codeUnitAt(parentIndex))) {
lastParentSeparator = parentIndex;
lastCodeUnit = parent.codeUnitAt(parentIndex);
parentIndex++;
}
continue;
}
}

if (style.codeUnitsEqual(parentCodeUnit, childCodeUnit)) {
if (style.isSeparator(parentCodeUnit)) {
lastParentSeparator = parentIndex;
Expand Down
9 changes: 9 additions & 0 deletions pkgs/path/test/posix_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,15 @@ void main() {
expect(context.isWithin('foo/bar', 'foo/bar///'), isFalse);
expect(context.isWithin('foo/.bar', 'foo/.bar/baz'), isTrue);
expect(context.isWithin('foo/./bar', 'foo/.bar/baz'), isFalse);
// A trailing "." is the current directory, so it must not be glued onto
// a child whose next component merely starts with ".".
expect(context.isWithin('/dir/.', '/dir/.file'), isTrue);
expect(context.isWithin('/dir/.', '/dir/file'), isTrue);
expect(context.isWithin('/dir/.', '/dir'), isFalse);
expect(context.isWithin('/dir/.', '/dir/./.file'), isTrue);
expect(context.isWithin('foo/.', 'foo/.file'), isTrue);
expect(context.equals('/dir/.', '/dir'), isTrue);
expect(context.equals('/dir/.', '/dir/.'), isTrue);
expect(context.isWithin('foo/..bar', 'foo/..bar/baz'), isTrue);
expect(context.isWithin('foo/bar', 'foo/bar/baz/..'), isFalse);
expect(context.isWithin('foo/bar', 'foo/bar/baz/../qux'), isTrue);
Expand Down
Loading