-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrule.js
77 lines (67 loc) · 1.65 KB
/
rule.js
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
'use strict';
import Node from './node';
import { last, valueOf } from './utils';
import { parseSelector } from './parse';
/**
* Creates CSS rule from given tokens
* @param {StreamReader} stream
* @param {Token[]} tokens
* @param {Token} [content]
* @return {Rule}
*/
export default function createRule(stream, tokens, contentStart, contentEnd) {
if (!tokens.length) {
return null;
}
const name = tokens[0];
name.end = last(tokens).end;
return new Rule(stream, name, contentStart, contentEnd);
}
/**
* Represents CSS rule
* @type {Node}
*/
export class Rule extends Node {
/**
* @param {StreamReader} stream
* @param {Token} name Rule’s name token
* @param {Token} contentStart Rule’s content start token
* @param {Token} [contentEnd] Rule’s content end token
*/
constructor(stream, name, contentStart, contentEnd) {
super('rule');
this.stream = stream;
this.selectorToken = name;
this.contentStartToken = contentStart;
this.contentEndToken = contentEnd || contentStart;
this._parsedSelector = null;
}
/**
* Returns rule selector
* @return {String}
*/
get selector() {
return valueOf(this.selectorToken);
}
get parsedSelector() {
if (!this._parsedSelector) {
this._parsedSelector = parseSelector(this.selectorToken.limit());
}
return this._parsedSelector;
}
/**
* Returns node’s start position in stream
* @return {*}
*/
get start() {
return this.selectorToken && this.selectorToken.start;
}
/**
* Returns node’s end position in stream
* @return {*}
*/
get end() {
const token = this.contentEndToken || this.contentStartToken || this.nameToken;
return token && token.end;
}
}