(
value: string,
rule: CSSStyleRule,
ignoreImageSelectors: string[],
isCancelled: () => boolean,
pushFilter: ((type: FilterType) => void) | null = null,
)
| 413 | } |
| 414 | |
| 415 | export function getBgImageModifier( |
| 416 | value: string, |
| 417 | rule: CSSStyleRule, |
| 418 | ignoreImageSelectors: string[], |
| 419 | isCancelled: () => boolean, |
| 420 | pushFilter: ((type: FilterType) => void) | null = null, |
| 421 | ): string | CSSValueModifier | null { |
| 422 | try { |
| 423 | if (shouldIgnoreImage(rule.selectorText, ignoreImageSelectors)) { |
| 424 | return value; |
| 425 | } |
| 426 | |
| 427 | const gradients = parseGradient(value); |
| 428 | const urls = getMatches(cssURLRegex, value); |
| 429 | |
| 430 | if (urls.length === 0 && gradients.length === 0) { |
| 431 | return value; |
| 432 | } |
| 433 | |
| 434 | const getIndices = (matches: string[]) => { |
| 435 | let index = 0; |
| 436 | return matches.map((match) => { |
| 437 | const valueIndex = value.indexOf(match, index); |
| 438 | index = valueIndex + match.length; |
| 439 | return {match, index: valueIndex}; |
| 440 | }); |
| 441 | }; |
| 442 | |
| 443 | const matches: BgImageMatches[] = |
| 444 | (gradients.map((i) => ({type: 'gradient', ...i})) as BgImageMatches[]) |
| 445 | .concat(getIndices(urls).map((i) => ({type: 'url', offset: 0, ...i}))) |
| 446 | .sort((a, b) => a.index > b.index ? 1 : -1); |
| 447 | |
| 448 | const getGradientModifier = (gradient: ParsedGradient) => { |
| 449 | const {typeGradient, match, hasComma} = gradient; |
| 450 | |
| 451 | const partsRegex = /([^\(\),]+(\([^\(\)]*(\([^\(\)]*\)*[^\(\)]*)?\))?([^\(\), ]|( (?!calc)))*),?/g; |
| 452 | const colorStopRegex = /^(from|color-stop|to)\(([^\(\)]*?,\s*)?(.*?)\)$/; |
| 453 | |
| 454 | const parts = getMatches(partsRegex, match, 1).map((part) => { |
| 455 | part = part.trim(); |
| 456 | |
| 457 | let rgb = parseColorWithCache(part); |
| 458 | if (rgb) { |
| 459 | return (theme: Theme) => modifyGradientColor(rgb!, theme); |
| 460 | } |
| 461 | |
| 462 | const space = part.lastIndexOf(' '); |
| 463 | rgb = parseColorWithCache(part.substring(0, space)); |
| 464 | if (rgb) { |
| 465 | return (theme: Theme) => `${modifyGradientColor(rgb!, theme)} ${part.substring(space + 1)}`; |
| 466 | } |
| 467 | |
| 468 | const colorStopMatch = part.match(colorStopRegex); |
| 469 | if (colorStopMatch) { |
| 470 | rgb = parseColorWithCache(colorStopMatch[3]); |
| 471 | if (rgb) { |
| 472 | return (theme: Theme) => `${colorStopMatch[1]}(${colorStopMatch[2] ? `${colorStopMatch[2]}, ` : ''}${modifyGradientColor(rgb!, theme)})`; |
no test coverage detected