1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-15 07:22:28 +02:00

Simplify often-executed regex

This commit is contained in:
Raymond Hill 2021-12-10 07:47:21 -05:00
parent c71b7c7a42
commit 1490fd606b
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -31,8 +31,8 @@ import punycode from '../lib/punycode.js';
// Originally: // Originally:
// https://github.com/gorhill/uBlock/blob/8b5733a58d3acf9fb62815e14699c986bd1c2fdc/src/js/uritools.js // https://github.com/gorhill/uBlock/blob/8b5733a58d3acf9fb62815e14699c986bd1c2fdc/src/js/uritools.js
const reCommonHostnameFromURL = const reHostnameFromCommonURL =
/^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//; /^https:\/\/[0-9a-z._-]+[0-9a-z]\//;
const reAuthorityFromURI = const reAuthorityFromURI =
/^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/; /^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/;
const reHostFromNakedAuthority = const reHostFromNakedAuthority =
@ -75,20 +75,20 @@ function entityFromDomain(domain) {
} }
function hostnameFromURI(uri) { function hostnameFromURI(uri) {
let matches = reCommonHostnameFromURL.exec(uri); let match = reHostnameFromCommonURL.exec(uri);
if ( matches !== null ) { return matches[1]; } if ( match !== null ) { return match[0].slice(8, -1); }
matches = reAuthorityFromURI.exec(uri); match = reAuthorityFromURI.exec(uri);
if ( matches === null ) { return ''; } if ( match === null ) { return ''; }
const authority = matches[1].slice(2); const authority = match[1].slice(2);
if ( reHostFromNakedAuthority.test(authority) ) { if ( reHostFromNakedAuthority.test(authority) ) {
return authority.toLowerCase(); return authority.toLowerCase();
} }
matches = reHostFromAuthority.exec(authority); match = reHostFromAuthority.exec(authority);
if ( matches === null ) { if ( match === null ) {
matches = reIPv6FromAuthority.exec(authority); match = reIPv6FromAuthority.exec(authority);
if ( matches === null ) { return ''; } if ( match === null ) { return ''; }
} }
let hostname = matches[1]; let hostname = match[1];
while ( hostname.endsWith('.') ) { while ( hostname.endsWith('.') ) {
hostname = hostname.slice(0, -1); hostname = hostname.slice(0, -1);
} }
@ -104,8 +104,10 @@ function hostnameFromNetworkURL(url) {
} }
function originFromURI(uri) { function originFromURI(uri) {
const matches = reOriginFromURI.exec(uri); let match = reHostnameFromCommonURL.exec(uri);
return matches !== null ? matches[0].toLowerCase() : ''; if ( match !== null ) { return match[0].slice(0, -1); }
match = reOriginFromURI.exec(uri);
return match !== null ? match[0].toLowerCase() : '';
} }
function isNetworkURI(uri) { function isNetworkURI(uri) {