diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2018-04-04 03:08:23 -0600 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2018-04-11 23:12:07 -0600 |
| commit | cd44e0a8d490bd43f7ead4f3c9c43cadd2471f77 (patch) | |
| tree | c29734e3ca5101f3504e2b0cf4c3f76ad9393674 | |
| parent | 061cc583b5b264a5de994f5bb2b436f9a6291840 (diff) | |
change PathResolver#expand_path to resolve parent references
| -rw-r--r-- | lib/asciidoctor/path_resolver.rb | 27 | ||||
| -rw-r--r-- | test/paths_test.rb | 4 |
2 files changed, 22 insertions, 9 deletions
diff --git a/lib/asciidoctor/path_resolver.rb b/lib/asciidoctor/path_resolver.rb index 553dea2e..42bd9e72 100644 --- a/lib/asciidoctor/path_resolver.rb +++ b/lib/asciidoctor/path_resolver.rb @@ -213,19 +213,28 @@ class PathResolver end alias posixfy posixify - # Public: Expand the path by resolving any parent references (..) - # and cleaning self references (.). - # - # The result will be relative if the path is relative and - # absolute if the path is absolute. The file separator used - # in the expanded path is the one specified when the class - # was constructed. + # Public: Expand the specified path by converting the path to a posix path, resolving parent + # references (..), and removing self references (.). # # path - the String path to expand # - # returns a String path with any parent or self references resolved. + # returns a String path as a posix path with parent references resolved and self references removed. + # The result will be relative if the path is relative and absolute if the path is absolute. def expand_path path - join_path *partition_path(path) + path_segments, path_root = partition_path path + if path_segments.include? DOT_DOT + resolved_segments = [] + path_segments.each do |segment| + if segment == DOT_DOT + resolved_segments.pop + else + resolved_segments << segment + end + end + join_path resolved_segments, path_root + else + join_path path_segments, path_root + end end # Public: Partition the path into path segments and remove any empty segments diff --git a/test/paths_test.rb b/test/paths_test.rb index fe984584..dc6d46cb 100644 --- a/test/paths_test.rb +++ b/test/paths_test.rb @@ -142,6 +142,10 @@ context 'Path Resolver' do assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path(nil, "#{JAIL}/assets/stylesheets", JAIL) end + test 'expands parent references in start path if target is empty' do + assert_equal "#{JAIL}/stylesheets", @resolver.system_path('', "#{JAIL}/assets/../stylesheets", JAIL) + end + test 'resolves start path if target is dot' do assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path('.', "#{JAIL}/assets/stylesheets", JAIL) assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path('./', "#{JAIL}/assets/stylesheets", JAIL) |
