MCPcopy
hub / github.com/darkreader/darkreader / matchPreparedURLPattern

Function matchPreparedURLPattern

src/utils/url.ts:227–278  ·  view source on GitHub ↗
(u: PreparedURL | null, p: PreparedPattern | null)

Source from the content-addressed store, hash-verified

225}
226
227function matchPreparedURLPattern(u: PreparedURL | null, p: PreparedPattern | null) {
228 if (
229 !(u && p)
230 || (p.hostParts.length > u.hostParts.length)
231 || (p.exactStart && p.hostParts.length !== u.hostParts.length)
232 || (p.exactEnd && p.pathParts.length !== u.pathParts.length)
233 || (p.port !== '*' && p.port !== u.port)
234 || (p.protocol && p.protocol !== u.protocol)
235 ) {
236 return false;
237 }
238
239 for (let i = 0; i < p.hostParts.length; i++) {
240 const pHostPart = p.hostParts[i];
241 const uHostPart = u.hostParts[i];
242 if (pHostPart !== '*' && pHostPart !== uHostPart) {
243 return false;
244 }
245 }
246
247 if (
248 p.hostParts.length >= 2
249 && p.hostParts.at(-1) !== '*'
250 && (
251 p.hostParts.length < u.hostParts.length - 1
252 || (
253 p.hostParts.length === u.hostParts.length - 1
254 && u.hostParts.at(-1) !== 'www'
255 )
256 )
257 ) {
258 return false;
259 }
260
261 if (p.pathParts.length === 0) {
262 return true;
263 }
264
265 if (p.pathParts.length > u.pathParts.length) {
266 return false;
267 }
268
269 for (let i = 0; i < p.pathParts.length; i++) {
270 const pPathPart = p.pathParts[i];
271 const uPathPart = u.pathParts[i];
272 if (pPathPart !== '*' && pPathPart !== uPathPart) {
273 return false;
274 }
275 }
276
277 return true;
278}
279
280function isRegExp(pattern: string) {
281 return pattern.startsWith('/') && pattern.endsWith('/') && pattern.length > 2;

Callers 2

matchURLPatternFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected