MCPcopy
hub / github.com/darkreader/darkreader / getBgImageModifier

Function getBgImageModifier

src/inject/dynamic-theme/modify-css.ts:415–687  ·  view source on GitHub ↗
(
    value: string,
    rule: CSSStyleRule,
    ignoreImageSelectors: string[],
    isCancelled: () => boolean,
    pushFilter: ((type: FilterType) => void) | null = null,
)

Source from the content-addressed store, hash-verified

413}
414
415export 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)})`;

Callers 2

getDeclarationsMethod · 0.90

Calls 8

parseGradientFunction · 0.90
getMatchesFunction · 0.90
logWarnFunction · 0.90
shouldIgnoreImageFunction · 0.85
getIndicesFunction · 0.85
getURLModifierFunction · 0.85
getGradientModifierFunction · 0.85
allMethod · 0.80

Tested by

no test coverage detected