(
selector /* : string */,
styleTypes /* : SheetDefinition[] */,
selectorHandlers /* : SelectorHandler[] */,
stringHandlers /* : StringHandlers */,
useImportant /* : boolean */
)
| 135 | * generateCSSRuleset(".foo:hover", { backgroundColor: "black" }, ...) |
| 136 | */ |
| 137 | export 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 |
no test coverage detected