File: check_nesting.cpp

package info (click to toggle)
golang-github-wellington-go-libsass 0.9.2%2Bgit20181130.4ef5b9d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,128 kB
  • sloc: cpp: 28,607; ansic: 839; makefile: 44
file content (398 lines) | stat: -rw-r--r-- 10,428 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "sass.hpp"
#include <vector>

#include "check_nesting.hpp"

namespace Sass {

  CheckNesting::CheckNesting()
  : parents(std::vector<Statement_Ptr>()),
    traces(std::vector<Backtrace>()),
    parent(0), current_mixin_definition(0)
  { }

  void error(AST_Node_Ptr node, Backtraces traces, std::string msg) {
    traces.push_back(Backtrace(node->pstate()));
    throw Exception::InvalidSass(node->pstate(), traces, msg);
  }

  Statement_Ptr CheckNesting::visit_children(Statement_Ptr parent)
  {
    Statement_Ptr old_parent = this->parent;

    if (At_Root_Block_Ptr root = Cast<At_Root_Block>(parent)) {
      std::vector<Statement_Ptr> old_parents = this->parents;
      std::vector<Statement_Ptr> new_parents;

      for (size_t i = 0, L = this->parents.size(); i < L; i++) {
        Statement_Ptr p = this->parents.at(i);
        if (!root->exclude_node(p)) {
          new_parents.push_back(p);
        }
      }
      this->parents = new_parents;

      for (size_t i = this->parents.size(); i > 0; i--) {
        Statement_Ptr p = 0;
        Statement_Ptr gp = 0;
        if (i > 0) p = this->parents.at(i - 1);
        if (i > 1) gp = this->parents.at(i - 2);

        if (!this->is_transparent_parent(p, gp)) {
          this->parent = p;
          break;
        }
      }

      At_Root_Block_Ptr ar = Cast<At_Root_Block>(parent);
      Block_Ptr ret = ar->block();

      if (ret != NULL) {
        for (auto n : ret->elements()) {
          n->perform(this);
        }
      }

      this->parent = old_parent;
      this->parents = old_parents;

      return ret;
    }

    if (!this->is_transparent_parent(parent, old_parent)) {
      this->parent = parent;
    }

    this->parents.push_back(parent);

    Block_Ptr b = Cast<Block>(parent);

    if (Trace_Ptr trace = Cast<Trace>(parent)) {
      if (trace->type() == 'i') {
        this->traces.push_back(Backtrace(trace->pstate()));
      }
    }

    if (!b) {
      if (Has_Block_Ptr bb = Cast<Has_Block>(parent)) {
        b = bb->block();
      }
    }

    if (b) {
      for (auto n : b->elements()) {
        n->perform(this);
      }
    }

    this->parent = old_parent;
    this->parents.pop_back();

    if (Trace_Ptr trace = Cast<Trace>(parent)) {
      if (trace->type() == 'i') {
        this->traces.pop_back();
      }
    }

    return b;
  }


  Statement_Ptr CheckNesting::operator()(Block_Ptr b)
  {
    return this->visit_children(b);
  }

  Statement_Ptr CheckNesting::operator()(Definition_Ptr n)
  {
    if (!this->should_visit(n)) return NULL;
    if (!is_mixin(n)) {
      visit_children(n);
      return n;
    }

    Definition_Ptr old_mixin_definition = this->current_mixin_definition;
    this->current_mixin_definition = n;

    visit_children(n);

    this->current_mixin_definition = old_mixin_definition;

    return n;
  }

  Statement_Ptr CheckNesting::operator()(If_Ptr i)
  {
    this->visit_children(i);

    if (Block_Ptr b = Cast<Block>(i->alternative())) {
      for (auto n : b->elements()) n->perform(this);
    }

    return i;
  }

  Statement_Ptr CheckNesting::fallback_impl(Statement_Ptr s)
  {
    Block_Ptr b1 = Cast<Block>(s);
    Has_Block_Ptr b2 = Cast<Has_Block>(s);
    return b1 || b2 ? visit_children(s) : s;
  }

  bool CheckNesting::should_visit(Statement_Ptr node)
  {
    if (!this->parent) return true;

    if (Cast<Content>(node))
    { this->invalid_content_parent(this->parent, node); }

    if (is_charset(node))
    { this->invalid_charset_parent(this->parent, node); }

    if (Cast<Extension>(node))
    { this->invalid_extend_parent(this->parent, node); }

    // if (Cast<Import>(node))
    // { this->invalid_import_parent(this->parent); }

    if (this->is_mixin(node))
    { this->invalid_mixin_definition_parent(this->parent, node); }

    if (this->is_function(node))
    { this->invalid_function_parent(this->parent, node); }

    if (this->is_function(this->parent))
    { this->invalid_function_child(node); }

    if (Declaration_Ptr d = Cast<Declaration>(node))
    {
      this->invalid_prop_parent(this->parent, node);
      this->invalid_value_child(d->value());
    }

    if (Cast<Declaration>(this->parent))
    { this->invalid_prop_child(node); }

    if (Cast<Return>(node))
    { this->invalid_return_parent(this->parent, node); }

    return true;
  }

  void CheckNesting::invalid_content_parent(Statement_Ptr parent, AST_Node_Ptr node)
  {
    if (!this->current_mixin_definition) {
      error(node, traces, "@content may only be used within a mixin.");
    }
  }

  void CheckNesting::invalid_charset_parent(Statement_Ptr parent, AST_Node_Ptr node)
  {
    if (!(
        is_root_node(parent)
    )) {
      error(node, traces, "@charset may only be used at the root of a document.");
    }
  }

  void CheckNesting::invalid_extend_parent(Statement_Ptr parent, AST_Node_Ptr node)
  {
    if (!(
        Cast<Ruleset>(parent) ||
        Cast<Mixin_Call>(parent) ||
        is_mixin(parent)
    )) {
      error(node, traces, "Extend directives may only be used within rules.");
    }
  }

  // void CheckNesting::invalid_import_parent(Statement_Ptr parent, AST_Node_Ptr node)
  // {
  //   for (auto pp : this->parents) {
  //     if (
  //         Cast<Each>(pp) ||
  //         Cast<For>(pp) ||
  //         Cast<If>(pp) ||
  //         Cast<While>(pp) ||
  //         Cast<Trace>(pp) ||
  //         Cast<Mixin_Call>(pp) ||
  //         is_mixin(pp)
  //     ) {
  //       error(node, traces, "Import directives may not be defined within control directives or other mixins.");
  //     }
  //   }

  //   if (this->is_root_node(parent)) {
  //     return;
  //   }

  //   if (false/*n.css_import?*/) {
  //     error(node, traces, "CSS import directives may only be used at the root of a document.");
  //   }
  // }

  void CheckNesting::invalid_mixin_definition_parent(Statement_Ptr parent, AST_Node_Ptr node)
  {
    for (Statement_Ptr pp : this->parents) {
      if (
          Cast<Each>(pp) ||
          Cast<For>(pp) ||
          Cast<If>(pp) ||
          Cast<While>(pp) ||
          Cast<Trace>(pp) ||
          Cast<Mixin_Call>(pp) ||
          is_mixin(pp)
      ) {
        error(node, traces, "Mixins may not be defined within control directives or other mixins.");
      }
    }
  }

  void CheckNesting::invalid_function_parent(Statement_Ptr parent, AST_Node_Ptr node)
  {
    for (Statement_Ptr pp : this->parents) {
      if (
          Cast<Each>(pp) ||
          Cast<For>(pp) ||
          Cast<If>(pp) ||
          Cast<While>(pp) ||
          Cast<Trace>(pp) ||
          Cast<Mixin_Call>(pp) ||
          is_mixin(pp)
      ) {
        error(node, traces, "Functions may not be defined within control directives or other mixins.");
      }
    }
  }

  void CheckNesting::invalid_function_child(Statement_Ptr child)
  {
    if (!(
        Cast<Each>(child) ||
        Cast<For>(child) ||
        Cast<If>(child) ||
        Cast<While>(child) ||
        Cast<Trace>(child) ||
        Cast<Comment>(child) ||
        Cast<Debug>(child) ||
        Cast<Return>(child) ||
        Cast<Variable>(child) ||
        // Ruby Sass doesn't distinguish variables and assignments
        Cast<Assignment>(child) ||
        Cast<Warning>(child) ||
        Cast<Error>(child)
    )) {
      error(child, traces, "Functions can only contain variable declarations and control directives.");
    }
  }

  void CheckNesting::invalid_prop_child(Statement_Ptr child)
  {
    if (!(
        Cast<Each>(child) ||
        Cast<For>(child) ||
        Cast<If>(child) ||
        Cast<While>(child) ||
        Cast<Trace>(child) ||
        Cast<Comment>(child) ||
        Cast<Declaration>(child) ||
        Cast<Mixin_Call>(child)
    )) {
      error(child, traces, "Illegal nesting: Only properties may be nested beneath properties.");
    }
  }

  void CheckNesting::invalid_prop_parent(Statement_Ptr parent, AST_Node_Ptr node)
  {
    if (!(
        is_mixin(parent) ||
        is_directive_node(parent) ||
        Cast<Ruleset>(parent) ||
        Cast<Keyframe_Rule>(parent) ||
        Cast<Declaration>(parent) ||
        Cast<Mixin_Call>(parent)
    )) {
      error(node, traces, "Properties are only allowed within rules, directives, mixin includes, or other properties.");
    }
  }

  void CheckNesting::invalid_value_child(AST_Node_Ptr d)
  {
    if (Map_Ptr m = Cast<Map>(d)) {
      traces.push_back(Backtrace(m->pstate()));
      throw Exception::InvalidValue(traces, *m);
    }
    if (Number_Ptr n = Cast<Number>(d)) {
      if (!n->is_valid_css_unit()) {
        traces.push_back(Backtrace(n->pstate()));
        throw Exception::InvalidValue(traces, *n);
      }
    }

    // error(dbg + " isn't a valid CSS value.", m->pstate(),);

  }

  void CheckNesting::invalid_return_parent(Statement_Ptr parent, AST_Node_Ptr node)
  {
    if (!this->is_function(parent)) {
      error(node, traces, "@return may only be used within a function.");
    }
  }

  bool CheckNesting::is_transparent_parent(Statement_Ptr parent, Statement_Ptr grandparent)
  {
    bool parent_bubbles = parent && parent->bubbles();

    bool valid_bubble_node = parent_bubbles &&
                             !is_root_node(grandparent) &&
                             !is_at_root_node(grandparent);

    return Cast<Import>(parent) ||
           Cast<Each>(parent) ||
           Cast<For>(parent) ||
           Cast<If>(parent) ||
           Cast<While>(parent) ||
           Cast<Trace>(parent) ||
           valid_bubble_node;
  }

  bool CheckNesting::is_charset(Statement_Ptr n)
  {
    Directive_Ptr d = Cast<Directive>(n);
    return d && d->keyword() == "charset";
  }

  bool CheckNesting::is_mixin(Statement_Ptr n)
  {
    Definition_Ptr def = Cast<Definition>(n);
    return def && def->type() == Definition::MIXIN;
  }

  bool CheckNesting::is_function(Statement_Ptr n)
  {
    Definition_Ptr def = Cast<Definition>(n);
    return def && def->type() == Definition::FUNCTION;
  }

  bool CheckNesting::is_root_node(Statement_Ptr n)
  {
    if (Cast<Ruleset>(n)) return false;

    Block_Ptr b = Cast<Block>(n);
    return b && b->is_root();
  }

  bool CheckNesting::is_at_root_node(Statement_Ptr n)
  {
    return Cast<At_Root_Block>(n) != NULL;
  }

  bool CheckNesting::is_directive_node(Statement_Ptr n)
  {
    return Cast<Directive>(n) ||
           Cast<Import>(n) ||
           Cast<Media_Block>(n) ||
           Cast<Supports_Block>(n);
  }
}