Skip to content

Commit e0eefb3

Browse files
committed
fixes #187 FileSelector support S3 (hdfs et al) in IsValueValidPath - I actually left ValueIsValidPath false in this case, but I am hiding the error icon if a web address is used (e.g. starts with alphanumerics + :// )
1 parent 7adb533 commit e0eefb3

3 files changed

Lines changed: 78 additions & 10 deletions

File tree

test/+wt/+test/FileSelector.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,28 @@ function testFileEditField(testCase)
170170
%testCase.verifyTrue(logical(testCase.Widget.WarnImage.Visible));
171171

172172
end %function
173+
174+
175+
function testWebAddress(testCase)
176+
177+
% Get the edit field
178+
editControl = testCase.Widget.EditControl;
179+
180+
% Set the type
181+
testCase.verifySetProperty("SelectionType", "folder");
182+
183+
% Type a valid value
184+
newValue = "s3://abucket/afolder";
185+
testCase.verifyTypeAction(editControl, newValue, "Value");
186+
187+
% Verify the ValueIsValidPath value (false because not local
188+
% folder)
189+
testCase.verifyFalse(testCase.Widget.ValueIsValidPath)
190+
191+
% Verify the warn image does not show (we ignore web address)
192+
testCase.verifyFalse(logical(testCase.Widget.WarnImage.Visible));
193+
194+
end %function
173195

174196

175197
function testRootDirectoryAndHistory(testCase)

widgets/+wt/+utility/cleanPath.m

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function path = cleanPath(path)
1+
function out = cleanPath(in)
22
% cleanPath - Utility to clean and standardize a file/folder path
33
%
44
% This function will clean and standardize a file or folder path. It
@@ -24,15 +24,60 @@
2424
% "C:\Program Files\MATLAB"
2525
%
2626

27-
% Copyright 2020-2025 The MathWorks Inc.
27+
% Copyright 2020-2026 The MathWorks Inc.
2828
% ---------------------------------------------------------------------
2929

30-
% File separator - in case of regional variants
31-
fsep = regexptranslate("escape",filesep);
30+
arguments
31+
in string % accepts string arrays of any size
32+
end
3233

33-
% Pattern for regexp
34-
fsepOpts = join(["\\","/",fsep],"|");
35-
pattern = "^\s+|(?<=\S)(\s|" + fsepOpts + ")+$";
34+
if isempty(in)
35+
out = in;
36+
return
37+
end
3638

37-
% Perform replacement
38-
path = regexprep(path,pattern,"");
39+
fs = filesep;
40+
sz = size(in);
41+
out = strings(sz); % allocate same size
42+
43+
for idx = 1:numel(in)
44+
sIn = char(in(idx));
45+
if strlength(in(idx)) == 0
46+
out(idx) = in(idx);
47+
continue
48+
end
49+
50+
% If URI-like (scheme://) then leave unchanged
51+
if ~isempty(regexp(sIn, '^[A-Za-z][A-Za-z0-9+.\-]*://', 'once'))
52+
out(idx) = in(idx);
53+
continue
54+
end
55+
56+
% Detect UNC-style leading slashes
57+
isUNC = ~isempty(regexp(sIn, '^[\\/]{2,}', 'once'));
58+
59+
% Collapse any run of slashes/backslashes to single platform filesep
60+
s = regexprep(sIn, '[\\/]+', fs);
61+
62+
if isUNC
63+
% Remove any leading separators then ensure exactly two leading filesep
64+
s = regexprep(s, ['^' regexptranslate('escape', fs) '+'], '');
65+
s = [repmat(fs,1,2) s];
66+
else
67+
% Preserve drive-letter prefix like 'C:' and ensure at most one filesep after it
68+
m = regexp(sIn, '^([A-Za-z]:)[\\/]*', 'tokens', 'once');
69+
if ~isempty(m)
70+
drive = m{1};
71+
% Strip any leading drive+sep from s, then reapply drive and single filesep if needed
72+
s = regexprep(s, ['^' regexptranslate('escape', drive) regexptranslate('escape', fs) '?'], '');
73+
if isempty(s)
74+
s = drive;
75+
else
76+
s = [drive fs s];
77+
end
78+
end
79+
end
80+
81+
out(idx) = string(s);
82+
end
83+
end

widgets/+wt/FileSelector.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ function update(obj)
209209
end %if obj.ShowHistory
210210

211211
% Show the warning icon?
212-
showWarn = strlength(obj.Value) && ~obj.ValueIsValidPath;
212+
isWeb = startsWith(obj.Value, alphanumericsPattern + "://");
213+
showWarn = strlength(obj.Value) && ~obj.ValueIsValidPath && ~isWeb;
213214
obj.WarnImage.Visible = showWarn;
214215

215216
% Set warning icon tooltip

0 commit comments

Comments
 (0)