* Combine the `contextSelectors` with the `hostMarker` and the `otherSelectors` * to create a selector that matches the same as `:host-context()`. * * Given a single context selector `A` we need to output selectors that match on the host and as an * ancestor of the host: * * ``` * A <hostMark
( contextSelectors: string[], otherSelectors: string, pseudoPrefix = '', )
| 1278 | * @param otherSelectors the rest of the selectors that are not context selectors. |
| 1279 | */ |
| 1280 | function _combineHostContextSelectors( |
| 1281 | contextSelectors: string[], |
| 1282 | otherSelectors: string, |
| 1283 | pseudoPrefix = '', |
| 1284 | ): string { |
| 1285 | const hostMarker = _polyfillHostNoCombinator; |
| 1286 | _polyfillHostRe.lastIndex = 0; // reset the regex to ensure we get an accurate test |
| 1287 | const otherSelectorsHasHost = _polyfillHostRe.test(otherSelectors); |
| 1288 | |
| 1289 | // If there are no context selectors then just output a host marker |
| 1290 | if (contextSelectors.length === 0) { |
| 1291 | return hostMarker + otherSelectors; |
| 1292 | } |
| 1293 | |
| 1294 | const combined: string[] = [contextSelectors.pop() || '']; |
| 1295 | while (contextSelectors.length > 0) { |
| 1296 | const length = combined.length; |
| 1297 | const contextSelector = contextSelectors.pop(); |
| 1298 | for (let i = 0; i < length; i++) { |
| 1299 | const previousSelectors = combined[i]; |
| 1300 | // Add the new selector as a descendant of the previous selectors |
| 1301 | combined[length * 2 + i] = previousSelectors + ' ' + contextSelector; |
| 1302 | // Add the new selector as an ancestor of the previous selectors |
| 1303 | combined[length + i] = contextSelector + ' ' + previousSelectors; |
| 1304 | // Add the new selector to act on the same element as the previous selectors |
| 1305 | combined[i] = contextSelector + previousSelectors; |
| 1306 | } |
| 1307 | } |
| 1308 | // Finally connect the selector to the `hostMarker`s: either acting directly on the host |
| 1309 | // (A<hostMarker>) or as an ancestor (A <hostMarker>). |
| 1310 | return combined |
| 1311 | .map((s) => |
| 1312 | otherSelectorsHasHost |
| 1313 | ? `${pseudoPrefix}${s}${otherSelectors}` |
| 1314 | : `${pseudoPrefix}${s}${hostMarker}${otherSelectors}, ${pseudoPrefix}${s} ${hostMarker}${otherSelectors}`, |
| 1315 | ) |
| 1316 | .join(','); |
| 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * Mutate the given `groups` array so that there are `multiples` clones of the original array |
no test coverage detected