@@ -200,50 +200,47 @@ namespace Sass {
200200 return result;
201201 }
202202
203+ // Resolution order for ambiguous imports:
204+ // (1) filename as given
205+ // (2) underscore + given
206+ // (3) underscore + given + extension
207+ // (4) given + extension
208+ string resolve_file_name (const string& base, const string& name)
209+ {
210+ // supported extensions
211+ const vector<string> exts = {
212+ " .scss" , " .sass" , " .css"
213+ };
214+ // create full path (maybe relative)
215+ string path (join_paths (base, name));
216+ if (file_exists (path)) return path;
217+ // next test variation with underscore
218+ path = join_paths (base, " _" + name);
219+ if (file_exists (path)) return path;
220+ // next test exts plus underscore
221+ for (auto ext : exts) {
222+ path = join_paths (base, " _" + name + ext);
223+ if (file_exists (path)) return path;
224+ }
225+ // next test plain name with exts
226+ for (auto ext : exts) {
227+ path = join_paths (base, name + ext);
228+ if (file_exists (path)) return path;
229+ }
230+ // nothing found
231+ return string (" " );
232+ }
233+
203234 char * resolve_and_load (string path, string& real_path)
204235 {
205- // Resolution order for ambiguous imports:
206- // (1) filename as given
207- // (2) underscore + given
208- // (3) underscore + given + extension
209- // (4) given + extension
210- char * contents = 0 ;
211236 real_path = path;
212- vector<string> exts (3 );
213- exts[0 ] = " .scss" ;
214- exts[1 ] = " .sass" ;
215- exts[2 ] = " .css" ;
216-
217- // if the file isn't found with the given filename (1)
218- if (!(contents = read_file (real_path))) {
219- string dir (dir_name (path));
220- string base (base_name (path));
221- real_path = dir + base;
222- // (2) underscore + given
223- string test_path (dir + " _" + base);
224- if ((contents = read_file (test_path))) {
225- real_path = test_path;
226- }
227- // (3) underscore + given + extension
228- if (!contents) {
229- for (auto ext : exts) {
230- test_path = dir + " _" + base + ext;
231- if ((contents = read_file (test_path))) {
232- real_path = test_path;
233- break ;
234- }
235- }
236- }
237- // (4) given + extension
238- if (!contents) {
239- for (auto ext : exts) {
240- test_path = dir + base + ext;
241- if ((contents = read_file (test_path))) {
242- real_path = test_path;
243- break ;
244- }
245- }
246- }
237+ char * contents = 0 ;
238+ string base (dir_name (path));
239+ string name (base_name (path));
240+ string resolved = resolve_file_name (base, name);
241+ if (resolved != " " ) {
242+ real_path = resolved;
243+ contents = read_file (resolved);
247244 }
248245#ifdef _WIN32
249246 // convert Windows backslashes to URL forward slashes
@@ -252,6 +249,19 @@ namespace Sass {
252249 return contents;
253250 }
254251
252+ bool file_exists (const string& path)
253+ {
254+ #ifdef _WIN32
255+ wstring wpath = UTF_8::convert_to_utf16 (path);
256+ DWORD dwAttrib = GetFileAttributesW (wpath.c_str ());
257+ return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
258+ (!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)));
259+ #else
260+ struct stat buffer;
261+ return (stat (path.c_str (), &buffer) == 0 );
262+ #endif
263+ }
264+
255265 char * read_file (string path)
256266 {
257267#ifdef _WIN32
0 commit comments