MCPcopy Index your code
hub / github.com/darkreader/darkreader / checkVariant

Function checkVariant

src/utils/colorscheme-parser.ts:128–186  ·  view source on GitHub ↗
(lineIndex: number, isSecondVariant: boolean)

Source from the content-addressed store, hash-verified

126 }
127
128 const checkVariant = (lineIndex: number, isSecondVariant: boolean): (ColorSchemeVariant & { variant?: string }) | undefined => {
129 // Get the possible variant name.
130 const variant = lines[lineIndex];
131 if (!variant) {
132 throwError(`The third line of the color scheme "${name}" is not defined.`);
133 return;
134 }
135
136 // Check if the variant is valid.
137 // if isSecondVariant is true, then we will check if the variant is 'Light', 'Dark' is not considered valid.
138 if (variant !== 'LIGHT' && variant !== 'DARK' && (isSecondVariant && variant === 'Light')) {
139 throwError(`The ${humanizeNumber(lineIndex)} line of the color scheme "${name}" is not a valid variant.`);
140 return;
141 }
142
143 // Get the possible background color.
144 const firstProperty = lines[lineIndex + 1];
145 if (!firstProperty) {
146 throwError(`The ${humanizeNumber(lineIndex + 1)} line of the color scheme "${name}" is not defined.`);
147 return;
148 }
149
150 // Check if the property is background color.
151 if (!firstProperty.startsWith('background: ')) {
152 throwError(`The ${humanizeNumber(lineIndex + 1)} line of the color scheme "${name}" is not background-color property.`);
153 return;
154 }
155
156 // Get the background color and check if it is a valid hex color.
157 const backgroundColor = firstProperty.slice(backgroundPropertyLength);
158 if (!isValidHexColor(backgroundColor)) {
159 throwError(`The ${humanizeNumber(lineIndex + 1)} line of the color scheme "${name}" is not a valid hex color.`);
160 return;
161 }
162
163 // Get the possible text color.
164 const secondProperty = lines[lineIndex + 2];
165 if (!secondProperty) {
166 throwError(`The ${humanizeNumber(lineIndex + 2)} line of the color scheme "${name}" is not defined.`);
167 return;
168 }
169 // Check if the property is text color.
170 if (!secondProperty.startsWith('text: ')) {
171 throwError(`The ${humanizeNumber(lineIndex + 2)} line of the color scheme "${name}" is not text-color property.`);
172 return;
173 }
174 // Get the text color and check if it is a valid hex color.
175 const textColor = secondProperty.slice(textPropertyLength);
176 if (!isValidHexColor(textColor)) {
177 throwError(`The ${humanizeNumber(lineIndex + 2)} line of the color scheme "${name}" is not a valid hex color.`);
178 return;
179 }
180 // If the variant is the second variant, then we will return the variant and the variant name.
181 return {
182 backgroundColor,
183 textColor,
184 variant,
185 };

Callers 1

parseColorSchemeConfigFunction · 0.85

Calls 3

throwErrorFunction · 0.85
humanizeNumberFunction · 0.85
isValidHexColorFunction · 0.85

Tested by

no test coverage detected