-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
180 lines (156 loc) · 5.2 KB
/
index.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
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
import { builders, types } from '../../utils/build-types.js'
import { TAG_CSS_PROPERTY } from '../../constants.js'
import cssEscape from 'cssesc'
import CSSParser from 'css-simple-parser'
import getPreprocessorTypeByAttribute from '../../utils/get-preprocessor-type-by-attribute.js'
import preprocess from '../../utils/preprocess-node.js'
import replaceInRange from '../../utils/replace-in-range.js'
const HOST = ':host'
const DISABLED_SELECTORS = ['from', 'to']
/**
* Matches valid, multiline JavaScript comments in almost all its forms.
* @const {RegExp}
* @static
*/
const R_MLCOMMS = /\/\*[^*]*\*+(?:[^*/][^*]*\*+)*\//g
/**
* Matches the list of css selectors excluding the pseudo selectors
* @const {RegExp}
* @static
*/
const R_CSS_SELECTOR_LIST =
/([^,]+)(?::(?!host)\w+(?:[\s|\S]*?\))?(?:[^,:]*)?)+|([^,]+)/g
/**
* Scope the css selectors prefixing them with the tag name
* @param {string} tag - Tag name of the root element
* @param {string} selectorList - list of selectors we need to scope
* @returns {string} scoped selectors
*/
export function addScopeToSelectorList(tag, selectorList) {
return selectorList.replace(R_CSS_SELECTOR_LIST, (match, selector) => {
const trimmedMatch = match.trim()
const trimmedSelector = selector ? selector.trim() : trimmedMatch
// skip selectors already using the tag name
if (trimmedSelector.indexOf(tag) === 0) {
return match
}
// skips the keywords and percents of css animations
if (
!trimmedSelector ||
DISABLED_SELECTORS.indexOf(trimmedSelector) > -1 ||
trimmedSelector.slice(-1) === '%'
) {
return match
}
// replace the `:host` pseudo-selector, where it is, with the root tag name;
// if `:host` was not included, add the tag name as prefix, and mirror all `[is]`
if (trimmedMatch.indexOf(HOST) < 0) {
return `${tag} ${trimmedMatch},[is="${tag}"] ${trimmedMatch}`
} else {
return `${trimmedMatch.replace(HOST, tag)},${trimmedMatch.replace(
HOST,
`[is="${tag}"]`,
)}`
}
})
}
/**
* Traverse the ast children
* @param {CSSParser.AST | CSSParser.NODE} ast - css parser node or ast
* @param {Function} fn - function that is needed to parse the single nodes
* @returns {CSSParser.AST | CSSParser.NODE} the original ast received
*/
const traverse = (ast, fn) => {
const { children } = ast
children.forEach((child) => {
// if fn returns false we stop the recursion
if (fn(child) !== false) traverse(child, fn)
})
return ast
}
/**
* Parses styles enclosed in a "scoped" tag
* The "css" string is received without comments or surrounding spaces.
*
* @param {string} tag - Tag name of the root element
* @param {string} css - The CSS code
* @returns {string} CSS with the styles scoped to the root element
*/
export function generateScopedCss(tag, css) {
const ast = CSSParser.parse(css)
const originalCssLength = css.length
traverse(ast, (node) => {
// calculate the selector offset from the original css length
const newSelectorOffset = css.length - originalCssLength
if (!node.selector.trim().startsWith('@')) {
// the css parser doesn't detect the comments so we manually remove them
const selector = node.selector.replace(R_MLCOMMS, '')
// replace the selector and override the original css
css = replaceInRange(
css,
node.selectorIndex + newSelectorOffset,
node.selectorIndexEnd + newSelectorOffset,
addScopeToSelectorList(tag, selector),
)
// stop the recursion
return false
}
})
return css
}
/**
* Remove comments, compact and trim whitespace
* @param { string } code - compiled css code
* @returns { string } css code normalized
*/
function compactCss(code) {
return code.replace(R_MLCOMMS, '').replace(/\s+/g, ' ').trim()
}
const escapeBackslashes = (s) => s.replace(/\\/g, '\\\\')
const escapeIdentifier = (identifier) =>
escapeBackslashes(
cssEscape(identifier, {
isIdentifier: true,
}),
)
/**
* Generate the component css
* @param { Object } sourceNode - node generated by the riot compiler
* @param { string } source - original component source code
* @param { Object } meta - compilation meta information
* @param { AST } ast - current AST output
* @returns { AST } the AST generated
*/
export default function css(sourceNode, source, meta, ast) {
const preprocessorName = getPreprocessorTypeByAttribute(sourceNode)
const { options } = meta
const preprocessorOutput = preprocess(
'css',
preprocessorName,
meta,
sourceNode.text,
)
const normalizedCssCode = compactCss(preprocessorOutput.code)
const escapedCssIdentifier = escapeIdentifier(meta.tagName)
const cssCode = (
options.scopedCss
? generateScopedCss(
escapedCssIdentifier,
escapeBackslashes(normalizedCssCode),
)
: escapeBackslashes(normalizedCssCode)
).trim()
types.visit(ast, {
visitProperty(path) {
if (path.value.key.name === TAG_CSS_PROPERTY) {
path.value.value = builders.templateLiteral(
[builders.templateElement({ raw: cssCode, cooked: '' }, false)],
[],
)
return false
}
this.traverse(path)
},
})
return ast
}