Skip to content

Commit b29f50b

Browse files
author
Aaron Leung
committed
Getting CSS imports to output in the correct places. Addresses #289.
1 parent 07e9ead commit b29f50b

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

output_compressed.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Sass {
77
using namespace std;
88

9-
Output_Compressed::Output_Compressed(Context* ctx) : buffer(""), ctx(ctx) { }
9+
Output_Compressed::Output_Compressed(Context* ctx) : buffer(""), rendered_imports(""), ctx(ctx) { }
1010
Output_Compressed::~Output_Compressed() { }
1111

1212
inline void Output_Compressed::fallback_impl(AST_Node* n)
@@ -16,6 +16,13 @@ namespace Sass {
1616
buffer += i.get_buffer();
1717
}
1818

19+
void Output_Compressed::operator()(Import* imp)
20+
{
21+
Inspect insp(ctx);
22+
imp->perform(&insp);
23+
rendered_imports += insp.get_buffer();
24+
}
25+
1926
void Output_Compressed::operator()(Block* b)
2027
{
2128
if (!b->is_root()) return;

output_compressed.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Sass {
1414
using Operation_CRTP<void, Output_Compressed>::operator();
1515

1616
string buffer;
17+
string rendered_imports;
1718
Context* ctx;
1819

1920
void fallback_impl(AST_Node* n);
@@ -24,7 +25,7 @@ namespace Sass {
2425
Output_Compressed(Context* ctx = 0);
2526
virtual ~Output_Compressed();
2627

27-
string get_buffer() { return buffer; }
28+
string get_buffer() { return rendered_imports + buffer; }
2829

2930
// statements
3031
virtual void operator()(Block*);
@@ -34,7 +35,7 @@ namespace Sass {
3435
virtual void operator()(At_Rule*);
3536
virtual void operator()(Declaration*);
3637
// virtual void operator()(Assignment*);
37-
// virtual void operator()(Import*);
38+
virtual void operator()(Import*);
3839
// virtual void operator()(Import_Stub*);
3940
// virtual void operator()(Warning*);
4041
virtual void operator()(Comment*);

output_nested.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Sass {
1010
using namespace std;
1111

1212
Output_Nested::Output_Nested(bool source_comments, Context* ctx)
13-
: buffer(""), indentation(0), source_comments(source_comments), ctx(ctx)
13+
: buffer(""), rendered_imports(""), indentation(0), source_comments(source_comments), ctx(ctx)
1414
{ }
1515
Output_Nested::~Output_Nested() { }
1616

@@ -21,12 +21,23 @@ namespace Sass {
2121
buffer += i.get_buffer();
2222
}
2323

24+
void Output_Nested::operator()(Import* imp)
25+
{
26+
Inspect insp(ctx);
27+
imp->perform(&insp);
28+
if (!rendered_imports.empty()) {
29+
rendered_imports += "\n";
30+
}
31+
rendered_imports += insp.get_buffer();
32+
}
33+
2434
void Output_Nested::operator()(Block* b)
2535
{
2636
if (!b->is_root()) return;
2737
for (size_t i = 0, L = b->length(); i < L; ++i) {
38+
size_t old_len = buffer.length();
2839
(*b)[i]->perform(this);
29-
if (i < L-1) append_to_buffer("\n");
40+
if (i < L-1 && old_len < buffer.length()) append_to_buffer("\n");
3041
}
3142
}
3243

output_nested.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Sass {
1717
using Operation_CRTP<void, Output_Nested>::operator();
1818

1919
string buffer;
20+
string rendered_imports;
2021
size_t indentation;
2122
bool source_comments;
2223
Context* ctx;
@@ -31,7 +32,12 @@ namespace Sass {
3132
Output_Nested(bool source_comments = false, Context* ctx = 0);
3233
virtual ~Output_Nested();
3334

34-
string get_buffer() { return buffer; }
35+
string get_buffer() {
36+
if (!rendered_imports.empty() && !buffer.empty()) {
37+
rendered_imports += "\n";
38+
}
39+
return rendered_imports + buffer;
40+
}
3541

3642
// statements
3743
virtual void operator()(Block*);
@@ -41,7 +47,7 @@ namespace Sass {
4147
virtual void operator()(At_Rule*);
4248
// virtual void operator()(Declaration*);
4349
// virtual void operator()(Assignment*);
44-
// virtual void operator()(Import*);
50+
virtual void operator()(Import*);
4551
// virtual void operator()(Import_Stub*);
4652
// virtual void operator()(Warning*);
4753
// virtual void operator()(Comment*);

parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ namespace Sass {
128128
extension = import_path.substr(import_path.length() - 5, 4);
129129
}
130130
if (extension == ".css") {
131-
String_Constant* loc = new (ctx.mem) String_Constant(path, source_position, import_path);
131+
String_Constant* loc = new (ctx.mem) String_Constant(path, source_position, import_path, true);
132132
Argument* loc_arg = new (ctx.mem) Argument(path, source_position, loc);
133133
Arguments* loc_args = new (ctx.mem) Arguments(path, source_position);
134134
(*loc_args) << loc_arg;

0 commit comments

Comments
 (0)