output_nested.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "output_nested.hpp"
  2. #include "inspect.hpp"
  3. #include "ast.hpp"
  4. #include "context.hpp"
  5. #include "to_string.hpp"
  6. #include "util.hpp"
  7. #include <iostream>
  8. #include <sstream>
  9. #include <typeinfo>
  10. namespace Sass {
  11. using namespace std;
  12. Output_Nested::Output_Nested(bool source_comments, Context* ctx)
  13. : buffer(""), rendered_imports(""), indentation(0), source_comments(source_comments), ctx(ctx), seen_utf8(false)
  14. { }
  15. Output_Nested::~Output_Nested() { }
  16. inline void Output_Nested::fallback_impl(AST_Node* n)
  17. {
  18. Inspect i(ctx);
  19. n->perform(&i);
  20. const string& text = i.get_buffer();
  21. for(const char& chr : text) {
  22. // abort clause
  23. if (seen_utf8) break;
  24. // skip all normal ascii chars
  25. if (Util::isAscii(chr)) continue;
  26. // singleton
  27. seen_utf8 = true;
  28. }
  29. buffer += text;
  30. if (ctx && !ctx->_skip_source_map_update)
  31. ctx->source_map.update_column(text);
  32. }
  33. void Output_Nested::operator()(Import* imp)
  34. {
  35. Inspect insp(ctx);
  36. imp->perform(&insp);
  37. if (!rendered_imports.empty()) {
  38. rendered_imports += "\n";
  39. }
  40. rendered_imports += insp.get_buffer();
  41. }
  42. void Output_Nested::operator()(Block* b)
  43. {
  44. if (!b->is_root()) return;
  45. for (size_t i = 0, L = b->length(); i < L; ++i) {
  46. size_t old_len = buffer.length();
  47. (*b)[i]->perform(this);
  48. if (i < L-1 && old_len < buffer.length()) append_to_buffer("\n");
  49. }
  50. }
  51. void Output_Nested::operator()(Ruleset* r)
  52. {
  53. Selector* s = r->selector();
  54. Block* b = r->block();
  55. bool decls = false;
  56. // disabled to avoid clang warning [-Wunused-function]
  57. // Selector_List* sl = static_cast<Selector_List*>(s);
  58. // Filter out rulesets that aren't printable (process its children though)
  59. if (!Util::isPrintable(r)) {
  60. for (size_t i = 0, L = b->length(); i < L; ++i) {
  61. Statement* stm = (*b)[i];
  62. if (dynamic_cast<Has_Block*>(stm)) {
  63. stm->perform(this);
  64. }
  65. }
  66. return;
  67. }
  68. if (b->has_non_hoistable()) {
  69. decls = true;
  70. indent();
  71. if (source_comments) {
  72. stringstream ss;
  73. ss << "/* line " << r->position().line << ", " << r->path() << " */" << endl;
  74. append_to_buffer(ss.str());
  75. indent();
  76. }
  77. s->perform(this);
  78. append_to_buffer(" {\n");
  79. ++indentation;
  80. for (size_t i = 0, L = b->length(); i < L; ++i) {
  81. Statement* stm = (*b)[i];
  82. bool bPrintExpression = true;
  83. // Check print conditions
  84. if (typeid(*stm) == typeid(Declaration)) {
  85. Declaration* dec = static_cast<Declaration*>(stm);
  86. if (dec->value()->concrete_type() == Expression::STRING) {
  87. String_Constant* valConst = static_cast<String_Constant*>(dec->value());
  88. string val(valConst->value());
  89. if (val.empty()) {
  90. bPrintExpression = false;
  91. }
  92. }
  93. else if (dec->value()->concrete_type() == Expression::LIST) {
  94. List* list = static_cast<List*>(dec->value());
  95. bool all_invisible = true;
  96. for (size_t list_i = 0, list_L = list->length(); list_i < list_L; ++list_i) {
  97. Expression* item = (*list)[list_i];
  98. if (!item->is_invisible()) all_invisible = false;
  99. }
  100. if (all_invisible) bPrintExpression = false;
  101. }
  102. }
  103. // Print if OK
  104. if (!stm->is_hoistable() && bPrintExpression) {
  105. if (!stm->block()) indent();
  106. stm->perform(this);
  107. append_to_buffer("\n");
  108. }
  109. }
  110. --indentation;
  111. buffer.erase(buffer.length()-1);
  112. if (ctx) ctx->source_map.remove_line();
  113. append_to_buffer(" }\n");
  114. }
  115. if (b->has_hoistable()) {
  116. if (decls) ++indentation;
  117. // indent();
  118. for (size_t i = 0, L = b->length(); i < L; ++i) {
  119. Statement* stm = (*b)[i];
  120. if (stm->is_hoistable()) {
  121. stm->perform(this);
  122. }
  123. }
  124. if (decls) --indentation;
  125. }
  126. }
  127. void Output_Nested::operator()(Feature_Block* f)
  128. {
  129. Feature_Query* q = f->feature_queries();
  130. Block* b = f->block();
  131. // Filter out feature blocks that aren't printable (process its children though)
  132. if (!Util::isPrintable(f)) {
  133. for (size_t i = 0, L = b->length(); i < L; ++i) {
  134. Statement* stm = (*b)[i];
  135. if (dynamic_cast<Has_Block*>(stm)) {
  136. stm->perform(this);
  137. }
  138. }
  139. return;
  140. }
  141. indent();
  142. ctx->source_map.add_mapping(f);
  143. append_to_buffer("@supports ");
  144. q->perform(this);
  145. append_to_buffer(" {\n");
  146. Selector* e = f->selector();
  147. if (e && b->has_non_hoistable()) {
  148. // JMA - hoisted, output the non-hoistable in a nested block, followed by the hoistable
  149. ++indentation;
  150. indent();
  151. e->perform(this);
  152. append_to_buffer(" {\n");
  153. ++indentation;
  154. for (size_t i = 0, L = b->length(); i < L; ++i) {
  155. Statement* stm = (*b)[i];
  156. if (!stm->is_hoistable()) {
  157. if (!stm->block()) indent();
  158. stm->perform(this);
  159. append_to_buffer("\n");
  160. }
  161. }
  162. --indentation;
  163. buffer.erase(buffer.length()-1);
  164. if (ctx) ctx->source_map.remove_line();
  165. append_to_buffer(" }\n");
  166. --indentation;
  167. ++indentation;
  168. ++indentation;
  169. for (size_t i = 0, L = b->length(); i < L; ++i) {
  170. Statement* stm = (*b)[i];
  171. if (stm->is_hoistable()) {
  172. stm->perform(this);
  173. }
  174. }
  175. --indentation;
  176. --indentation;
  177. }
  178. else {
  179. // JMA - not hoisted, just output in order
  180. ++indentation;
  181. for (size_t i = 0, L = b->length(); i < L; ++i) {
  182. Statement* stm = (*b)[i];
  183. if (!stm->is_hoistable()) {
  184. if (!stm->block()) indent();
  185. }
  186. stm->perform(this);
  187. if (!stm->is_hoistable()) append_to_buffer("\n");
  188. }
  189. --indentation;
  190. }
  191. buffer.erase(buffer.length()-1);
  192. if (ctx) ctx->source_map.remove_line();
  193. append_to_buffer(" }\n");
  194. }
  195. void Output_Nested::operator()(Media_Block* m)
  196. {
  197. List* q = m->media_queries();
  198. Block* b = m->block();
  199. // Filter out media blocks that aren't printable (process its children though)
  200. if (!Util::isPrintable(m)) {
  201. for (size_t i = 0, L = b->length(); i < L; ++i) {
  202. Statement* stm = (*b)[i];
  203. if (dynamic_cast<Has_Block*>(stm)) {
  204. stm->perform(this);
  205. }
  206. }
  207. return;
  208. }
  209. indent();
  210. ctx->source_map.add_mapping(m);
  211. append_to_buffer("@media ");
  212. q->perform(this);
  213. append_to_buffer(" {\n");
  214. Selector* e = m->selector();
  215. if (e && b->has_non_hoistable()) {
  216. // JMA - hoisted, output the non-hoistable in a nested block, followed by the hoistable
  217. ++indentation;
  218. indent();
  219. e->perform(this);
  220. append_to_buffer(" {\n");
  221. ++indentation;
  222. for (size_t i = 0, L = b->length(); i < L; ++i) {
  223. Statement* stm = (*b)[i];
  224. if (!stm->is_hoistable()) {
  225. if (!stm->block()) indent();
  226. stm->perform(this);
  227. append_to_buffer("\n");
  228. }
  229. }
  230. --indentation;
  231. buffer.erase(buffer.length()-1);
  232. if (ctx) ctx->source_map.remove_line();
  233. append_to_buffer(" }\n");
  234. --indentation;
  235. ++indentation;
  236. ++indentation;
  237. for (size_t i = 0, L = b->length(); i < L; ++i) {
  238. Statement* stm = (*b)[i];
  239. if (stm->is_hoistable()) {
  240. stm->perform(this);
  241. }
  242. }
  243. --indentation;
  244. --indentation;
  245. }
  246. else {
  247. // JMA - not hoisted, just output in order
  248. ++indentation;
  249. for (size_t i = 0, L = b->length(); i < L; ++i) {
  250. Statement* stm = (*b)[i];
  251. if (!stm->is_hoistable()) {
  252. if (!stm->block()) indent();
  253. }
  254. stm->perform(this);
  255. if (!stm->is_hoistable()) append_to_buffer("\n");
  256. }
  257. --indentation;
  258. }
  259. buffer.erase(buffer.length()-1);
  260. if (ctx) ctx->source_map.remove_line();
  261. append_to_buffer(" }\n");
  262. }
  263. void Output_Nested::operator()(At_Rule* a)
  264. {
  265. string kwd = a->keyword();
  266. Selector* s = a->selector();
  267. Expression* v = a->value();
  268. Block* b = a->block();
  269. bool decls = false;
  270. // indent();
  271. append_to_buffer(kwd);
  272. if (s) {
  273. append_to_buffer(" ");
  274. s->perform(this);
  275. }
  276. else if (v) {
  277. append_to_buffer(" ");
  278. v->perform(this);
  279. }
  280. if (!b) {
  281. append_to_buffer(";");
  282. return;
  283. }
  284. append_to_buffer(" {\n");
  285. ++indentation;
  286. decls = true;
  287. for (size_t i = 0, L = b->length(); i < L; ++i) {
  288. Statement* stm = (*b)[i];
  289. if (!stm->is_hoistable()) {
  290. if (!stm->block()) indent();
  291. stm->perform(this);
  292. append_to_buffer("\n");
  293. }
  294. }
  295. --indentation;
  296. if (decls) ++indentation;
  297. for (size_t i = 0, L = b->length(); i < L; ++i) {
  298. Statement* stm = (*b)[i];
  299. if (stm->is_hoistable()) {
  300. stm->perform(this);
  301. append_to_buffer("\n");
  302. }
  303. }
  304. if (decls) --indentation;
  305. buffer.erase(buffer.length()-1);
  306. if (ctx) ctx->source_map.remove_line();
  307. if (b->has_hoistable()) {
  308. buffer.erase(buffer.length()-1);
  309. if (ctx) ctx->source_map.remove_line();
  310. }
  311. append_to_buffer(" }\n");
  312. }
  313. void Output_Nested::indent()
  314. { append_to_buffer(string(2*indentation, ' ')); }
  315. void Output_Nested::append_to_buffer(const string& text)
  316. {
  317. buffer += text;
  318. if (ctx && !ctx->_skip_source_map_update)
  319. ctx->source_map.update_column(text);
  320. for(const char& chr : text) {
  321. // abort clause
  322. if (seen_utf8) break;
  323. // skip all normal ascii chars
  324. if (Util::isAscii(chr)) continue;
  325. // singleton
  326. seen_utf8 = true;
  327. }
  328. }
  329. }