aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/css-tree/lib/syntax/node/MediaQuery.js
blob: f569c7559e8d6b3cc921a03774282201e68ab710 (plain) (blame)
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
import {
    Comma,
    EOF,
    Ident,
    LeftCurlyBracket,
    LeftParenthesis,
    Function as FunctionToken,
    Semicolon
} from '../../tokenizer/index.js';

export const name = 'MediaQuery';
export const structure = {
    modifier: [String, null],
    mediaType: [String, null],
    condition: ['Condition', null]
};

export function parse() {
    const start = this.tokenStart;
    let modifier = null;
    let mediaType = null;
    let condition = null;

    this.skipSC();

    if (this.tokenType === Ident && this.lookupTypeNonSC(1) !== LeftParenthesis) {
        // [ not | only ]? <media-type>
        const ident = this.consume(Ident);
        const identLowerCase = ident.toLowerCase();

        if (identLowerCase === 'not' || identLowerCase === 'only') {
            this.skipSC();
            modifier = identLowerCase;
            mediaType = this.consume(Ident);
        } else {
            mediaType = ident;
        }

        switch (this.lookupTypeNonSC(0)) {
            case Ident: {
                // and <media-condition-without-or>
                this.skipSC();
                this.eatIdent('and');
                condition = this.Condition('media');
                break;
            }

            case LeftCurlyBracket:
            case Semicolon:
            case Comma:
            case EOF:
                break;

            default:
                this.error('Identifier or parenthesis is expected');
        }
    } else {
        switch (this.tokenType) {
            case Ident:
            case LeftParenthesis:
            case FunctionToken: {
                // <media-condition>
                condition = this.Condition('media');
                break;
            }

            case LeftCurlyBracket:
            case Semicolon:
            case EOF:
                break;

            default:
                this.error('Identifier or parenthesis is expected');
        }
    }

    return {
        type: 'MediaQuery',
        loc: this.getLocation(start, this.tokenStart),
        modifier,
        mediaType,
        condition
    };
}

export function generate(node) {
    if (node.mediaType) {
        if (node.modifier) {
            this.token(Ident, node.modifier);
        }

        this.token(Ident, node.mediaType);

        if (node.condition) {
            this.token(Ident, 'and');
            this.node(node.condition);
        }
    } else if (node.condition) {
        this.node(node.condition);
    }
}