1 | var TYPE = require('../../tokenizer').TYPE;
|
---|
2 | var rawMode = require('./Raw').mode;
|
---|
3 |
|
---|
4 | var ATKEYWORD = TYPE.AtKeyword;
|
---|
5 | var SEMICOLON = TYPE.Semicolon;
|
---|
6 | var LEFTCURLYBRACKET = TYPE.LeftCurlyBracket;
|
---|
7 | var RIGHTCURLYBRACKET = TYPE.RightCurlyBracket;
|
---|
8 |
|
---|
9 | function consumeRaw(startToken) {
|
---|
10 | return this.Raw(startToken, rawMode.leftCurlyBracketOrSemicolon, true);
|
---|
11 | }
|
---|
12 |
|
---|
13 | function isDeclarationBlockAtrule() {
|
---|
14 | for (var offset = 1, type; type = this.scanner.lookupType(offset); offset++) {
|
---|
15 | if (type === RIGHTCURLYBRACKET) {
|
---|
16 | return true;
|
---|
17 | }
|
---|
18 |
|
---|
19 | if (type === LEFTCURLYBRACKET ||
|
---|
20 | type === ATKEYWORD) {
|
---|
21 | return false;
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | return false;
|
---|
26 | }
|
---|
27 |
|
---|
28 | module.exports = {
|
---|
29 | name: 'Atrule',
|
---|
30 | structure: {
|
---|
31 | name: String,
|
---|
32 | prelude: ['AtrulePrelude', 'Raw', null],
|
---|
33 | block: ['Block', null]
|
---|
34 | },
|
---|
35 | parse: function() {
|
---|
36 | var start = this.scanner.tokenStart;
|
---|
37 | var name;
|
---|
38 | var nameLowerCase;
|
---|
39 | var prelude = null;
|
---|
40 | var block = null;
|
---|
41 |
|
---|
42 | this.eat(ATKEYWORD);
|
---|
43 |
|
---|
44 | name = this.scanner.substrToCursor(start + 1);
|
---|
45 | nameLowerCase = name.toLowerCase();
|
---|
46 | this.scanner.skipSC();
|
---|
47 |
|
---|
48 | // parse prelude
|
---|
49 | if (this.scanner.eof === false &&
|
---|
50 | this.scanner.tokenType !== LEFTCURLYBRACKET &&
|
---|
51 | this.scanner.tokenType !== SEMICOLON) {
|
---|
52 | if (this.parseAtrulePrelude) {
|
---|
53 | prelude = this.parseWithFallback(this.AtrulePrelude.bind(this, name), consumeRaw);
|
---|
54 |
|
---|
55 | // turn empty AtrulePrelude into null
|
---|
56 | if (prelude.type === 'AtrulePrelude' && prelude.children.head === null) {
|
---|
57 | prelude = null;
|
---|
58 | }
|
---|
59 | } else {
|
---|
60 | prelude = consumeRaw.call(this, this.scanner.tokenIndex);
|
---|
61 | }
|
---|
62 |
|
---|
63 | this.scanner.skipSC();
|
---|
64 | }
|
---|
65 |
|
---|
66 | switch (this.scanner.tokenType) {
|
---|
67 | case SEMICOLON:
|
---|
68 | this.scanner.next();
|
---|
69 | break;
|
---|
70 |
|
---|
71 | case LEFTCURLYBRACKET:
|
---|
72 | if (this.atrule.hasOwnProperty(nameLowerCase) &&
|
---|
73 | typeof this.atrule[nameLowerCase].block === 'function') {
|
---|
74 | block = this.atrule[nameLowerCase].block.call(this);
|
---|
75 | } else {
|
---|
76 | // TODO: should consume block content as Raw?
|
---|
77 | block = this.Block(isDeclarationBlockAtrule.call(this));
|
---|
78 | }
|
---|
79 |
|
---|
80 | break;
|
---|
81 | }
|
---|
82 |
|
---|
83 | return {
|
---|
84 | type: 'Atrule',
|
---|
85 | loc: this.getLocation(start, this.scanner.tokenStart),
|
---|
86 | name: name,
|
---|
87 | prelude: prelude,
|
---|
88 | block: block
|
---|
89 | };
|
---|
90 | },
|
---|
91 | generate: function(node) {
|
---|
92 | this.chunk('@');
|
---|
93 | this.chunk(node.name);
|
---|
94 |
|
---|
95 | if (node.prelude !== null) {
|
---|
96 | this.chunk(' ');
|
---|
97 | this.node(node.prelude);
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (node.block) {
|
---|
101 | this.node(node.block);
|
---|
102 | } else {
|
---|
103 | this.chunk(';');
|
---|
104 | }
|
---|
105 | },
|
---|
106 | walkContext: 'atrule'
|
---|
107 | };
|
---|