MCPcopy
hub / github.com/Khan/aphrodite / generateCSS

Function generateCSS

src/generate.js:137–200  ·  view source on GitHub ↗
(
    selector /* : string */,
    styleTypes /* : SheetDefinition[] */,
    selectorHandlers /* : SelectorHandler[] */,
    stringHandlers /* : StringHandlers */,
    useImportant /* : boolean */
)

Source from the content-addressed store, hash-verified

135 * generateCSSRuleset(".foo:hover", { backgroundColor: "black" }, ...)
136 */
137export const generateCSS = (
138 selector /* : string */,
139 styleTypes /* : SheetDefinition[] */,
140 selectorHandlers /* : SelectorHandler[] */,
141 stringHandlers /* : StringHandlers */,
142 useImportant /* : boolean */
143) /* : string[] */ => {
144 const merged = new OrderedElements();
145
146 for (let i = 0; i < styleTypes.length; i++) {
147 merged.addStyleType(styleTypes[i]);
148 }
149
150 const plainDeclarations = new OrderedElements();
151 const generatedStyles = [];
152
153 // TODO(emily): benchmark this to see if a plain for loop would be faster.
154 merged.forEach((val, key) => {
155 // For each key, see if one of the selector handlers will handle these
156 // styles.
157 const foundHandler = selectorHandlers.some(handler => {
158 const result = handler(key, selector, (newSelector) => {
159 return generateCSS(
160 newSelector, [val], selectorHandlers,
161 stringHandlers, useImportant);
162 });
163 if (result != null) {
164 // If the handler returned something, add it to the generated
165 // CSS and stop looking for another handler.
166 if (Array.isArray(result)) {
167 generatedStyles.push(...result);
168 } else {
169 // eslint-disable-next-line
170 console.warn(
171 'WARNING: Selector handlers should return an array of rules.' +
172 'Returning a string containing multiple rules is deprecated.',
173 handler,
174 );
175 generatedStyles.push(`@media all {${result}}`);
176 }
177 return true;
178 }
179 });
180 // If none of the handlers handled it, add it to the list of plain
181 // style declarations.
182 if (!foundHandler) {
183 plainDeclarations.set(key, val, true);
184 }
185 });
186 const generatedRuleset = generateCSSRuleset(
187 selector,
188 plainDeclarations,
189 stringHandlers,
190 useImportant,
191 selectorHandlers,
192 );
193
194

Callers 4

inject.jsFile · 0.90
injectStyleOnceFunction · 0.90
assertCSSFunction · 0.90
generate_test.jsFile · 0.90

Calls 5

addStyleTypeMethod · 0.95
forEachMethod · 0.95
setMethod · 0.95
handlerFunction · 0.85
generateCSSRulesetFunction · 0.85

Tested by

no test coverage detected