script/dom/filesystem/path.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Path algrithms from the File and Directory Entries API.
6//!
7//! <https://wicg.github.io/entries-api/#algorithms>
8
9use crate::dom::bindings::root::DomRoot;
10use crate::dom::filesystemdirectoryentry::FileSystemDirectoryEntry;
11use crate::dom::filesystementry::FileSystemEntry;
12
13/// <https://wicg.github.io/entries-api/#names-paths>
14/// A valid path is a sequence of '/' separated path segments,
15/// which does not contain contain NUL (U+0000) or '\' (U+005C REVERSE SOLIDUS).
16pub(crate) fn is_valid_path(path: &str) -> bool {
17 !path.contains('\0') && !path.contains('\\')
18}
19
20/// <https://wicg.github.io/entries-api/#resolve-a-relative-path>
21pub(crate) fn resolve_relative_path(abspath: &str, path: &str) -> String {
22 // Step 1. If path is an absolute path, return path.
23 if path.starts_with('/') {
24 return path.to_owned();
25 }
26
27 // Step 2. Let abspath segments be the result of strictly splitting abspath
28 // on '/' (U+002F SOLIDUS).
29 let mut abspath_segments: Vec<&str> = abspath.split('/').collect();
30
31 // Step 3. Let path segments be the result of strictly splitting path on
32 // '/' (U+002F SOLIDUS).
33 //
34 // Step 4. For each segment in path segments, switch on segment:
35 for segment in path.split('/') {
36 match segment {
37 // empty string: Continue.
38 "" => {},
39 // '.' (U+002E FULL STOP): Continue.
40 "." => {},
41 // '..' (U+002E FULL STOP, U+002E FULL STOP): Remove the last member
42 // of abspath segments unless it is the only member.
43 ".." => {
44 if abspath_segments.len() > 1 {
45 abspath_segments.pop();
46 }
47 },
48 // Otherwise: Append segment to abspath segments.
49 _ => abspath_segments.push(segment),
50 }
51 }
52
53 // Step 5. Return abspath segments joined by '/' (U+002F SOLIDUS).
54 //
55 // We do something different from spec which is broken:
56 // <https://github.com/WICG/entries-api/issues/50>
57 let mut result = String::new();
58 for segment in abspath_segments
59 .iter()
60 .filter(|segment| !segment.is_empty())
61 {
62 result.push('/');
63 result.push_str(segment);
64 }
65 if result.is_empty() {
66 result.push('/');
67 }
68 result
69}
70
71/// <https://wicg.github.io/entries-api/#evaluate-a-path>
72pub(crate) fn evaluate_path(
73 root: &FileSystemDirectoryEntry,
74 path: &str,
75) -> Option<DomRoot<FileSystemEntry>> {
76 // Step 1. Let segments be the result of strictly splitting path on '/'
77 // (U+002F SOLIDUS).
78
79 // Step 2. Remove the first entry from segments.
80 let mut segments = path.split('/').skip(1).peekable();
81
82 // The directory starts at the root; the stack tracks the directories
83 // traversed so far so that '..' can walk back up.
84 let mut directories = vec![DomRoot::from_ref(root)];
85
86 // Step 3. For each segment in segments, switch on segment:
87 while let Some(segment) = segments.next() {
88 match segment {
89 // empty string: Continue.
90 "" => {},
91 // '.' (U+002E FULL STOP): Continue.
92 "." => {},
93 // '..' (U+002E FULL STOP, U+002E FULL STOP): Let directory be
94 // directory's parent, or directory if none.
95 ".." => {
96 if directories.len() > 1 {
97 directories.pop();
98 }
99 },
100 // Otherwise: Run these steps:
101 name => {
102 let directory = directories.last().expect("directories is never empty");
103 // 1. Let item be the member of directory with name equal to
104 // segment, or return failure if none.
105 let item = directory.child(name)?;
106 // 2. If segment is the last item in segments, return item.
107 if segments.peek().is_none() {
108 return Some(item);
109 }
110 // 3. If item is a file, return failure.
111 let directory = DomRoot::downcast::<FileSystemDirectoryEntry>(item)?;
112 // 4. Let directory be item.
113 directories.push(directory);
114 },
115 }
116 }
117
118 // A path ending in '.' or '..' never returns from inside the loop; the
119 // directory reached so far is the result.
120 let directory = directories.pop().expect("directories is never empty");
121 Some(DomRoot::upcast::<FileSystemEntry>(directory))
122}