Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit ab3a9ee

Browse files
author
Aaron Leung
committed
Merge pull request #451 from mgreter/source_maps_win_rel_paths
Problem with relative path on windows and source maps
2 parents 61edd14 + 1298156 commit ab3a9ee

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

file.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,10 @@ namespace Sass {
172172
}
173173
}
174174
}
175-
string extension;
176-
if (real_path.length() > 5) {
177-
extension = real_path.substr(real_path.length() - 5, 5);
178-
}
179-
for(size_t i=0; i<extension.size();++i)
180-
extension[i] = tolower(extension[i]);
175+
#ifdef _WIN32
176+
// convert Windows backslashes to URL forward slashes
177+
replace(real_path.begin(), real_path.end(), '\\', '/');
178+
#endif
181179
return contents;
182180
}
183181

source_map.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,43 @@
44
#include "context.hpp"
55
#endif
66

7+
#include <string>
78
#include <sstream>
89
#include <cstddef>
10+
#include <iomanip>
911

1012
namespace Sass {
1113
using std::ptrdiff_t;
1214
SourceMap::SourceMap(const string& file) : current_position(Position(1, 1)), file(file) { }
1315

16+
// taken from http://stackoverflow.com/a/7725289/1550314
17+
std::string encodeJsonString(const std::string& input) {
18+
std::ostringstream ss;
19+
for (std::string::const_iterator iter = input.begin(); iter != input.end(); iter++) {
20+
switch (*iter) {
21+
case '\\': ss << "\\\\"; break;
22+
case '"': ss << "\\\""; break;
23+
case '\b': ss << "\\b"; break;
24+
case '\f': ss << "\\f"; break;
25+
case '\n': ss << "\\n"; break;
26+
case '\r': ss << "\\r"; break;
27+
case '\t': ss << "\\t"; break;
28+
// is a legal escape in JSON
29+
case '/': ss << "\\/"; break;
30+
default: ss << *iter; break;
31+
}
32+
}
33+
34+
return ss.str();
35+
}
36+
1437
string SourceMap::generate_source_map() {
1538
string result = "{\n";
1639
result += " \"version\": 3,\n";
17-
result += " \"file\": \"" + file + "\",\n";
40+
result += " \"file\": \"" + encodeJsonString(file) + "\",\n";
1841
result += " \"sources\": [";
1942
for (size_t i = 0; i < files.size(); ++i) {
20-
result+="\"" + files[i] + "\",";
43+
result+="\"" + encodeJsonString(files[i]) + "\",";
2144
}
2245
if (!files.empty()) result.erase(result.length() - 1);
2346
result += "],\n";

0 commit comments

Comments
 (0)