nav tabs on admin dashboard
This commit is contained in:
176
node_modules/normalize-url/index.js
generated
vendored
176
node_modules/normalize-url/index.js
generated
vendored
@@ -1,100 +1,92 @@
|
||||
'use strict';
|
||||
var url = require('url');
|
||||
var punycode = require('punycode');
|
||||
var queryString = require('query-string');
|
||||
var prependHttp = require('prepend-http');
|
||||
var sortKeys = require('sort-keys');
|
||||
var objectAssign = require('object-assign');
|
||||
// TODO: Use the `URL` global when targeting Node.js 10
|
||||
const URLParser = typeof URL === 'undefined' ? require('url').URL : URL;
|
||||
|
||||
var DEFAULT_PORTS = {
|
||||
'http:': 80,
|
||||
'https:': 443,
|
||||
'ftp:': 21
|
||||
const testParameter = (name, filters) => {
|
||||
return filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
|
||||
};
|
||||
|
||||
// protocols that always contain a `//`` bit
|
||||
var slashedProtocol = {
|
||||
'http': true,
|
||||
'https': true,
|
||||
'ftp': true,
|
||||
'gopher': true,
|
||||
'file': true,
|
||||
'http:': true,
|
||||
'https:': true,
|
||||
'ftp:': true,
|
||||
'gopher:': true,
|
||||
'file:': true
|
||||
};
|
||||
|
||||
function testParameter(name, filters) {
|
||||
return filters.some(function (filter) {
|
||||
return filter instanceof RegExp ? filter.test(name) : filter === name;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = function (str, opts) {
|
||||
opts = objectAssign({
|
||||
module.exports = (urlString, opts) => {
|
||||
opts = Object.assign({
|
||||
defaultProtocol: 'http:',
|
||||
normalizeProtocol: true,
|
||||
normalizeHttps: false,
|
||||
stripFragment: true,
|
||||
forceHttp: false,
|
||||
forceHttps: false,
|
||||
stripHash: true,
|
||||
stripWWW: true,
|
||||
removeQueryParameters: [/^utm_\w+/i],
|
||||
removeTrailingSlash: true,
|
||||
removeDirectoryIndex: false
|
||||
removeDirectoryIndex: false,
|
||||
sortQueryParameters: true
|
||||
}, opts);
|
||||
|
||||
if (typeof str !== 'string') {
|
||||
throw new TypeError('Expected a string');
|
||||
// Backwards compatibility
|
||||
if (Reflect.has(opts, 'normalizeHttps')) {
|
||||
opts.forceHttp = opts.normalizeHttps;
|
||||
}
|
||||
|
||||
var hasRelativeProtocol = str.indexOf('//') === 0;
|
||||
if (Reflect.has(opts, 'normalizeHttp')) {
|
||||
opts.forceHttps = opts.normalizeHttp;
|
||||
}
|
||||
|
||||
// prepend protocol
|
||||
str = prependHttp(str.trim()).replace(/^\/\//, 'http://');
|
||||
if (Reflect.has(opts, 'stripFragment')) {
|
||||
opts.stripHash = opts.stripFragment;
|
||||
}
|
||||
|
||||
var urlObj = url.parse(str);
|
||||
urlString = urlString.trim();
|
||||
|
||||
if (opts.normalizeHttps && urlObj.protocol === 'https:') {
|
||||
const hasRelativeProtocol = urlString.startsWith('//');
|
||||
const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
|
||||
|
||||
// Prepend protocol
|
||||
if (!isRelativeUrl) {
|
||||
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, opts.defaultProtocol);
|
||||
}
|
||||
|
||||
const urlObj = new URLParser(urlString);
|
||||
|
||||
if (opts.forceHttp && opts.forceHttps) {
|
||||
throw new Error('The `forceHttp` and `forceHttps` options cannot be used together');
|
||||
}
|
||||
|
||||
if (opts.forceHttp && urlObj.protocol === 'https:') {
|
||||
urlObj.protocol = 'http:';
|
||||
}
|
||||
|
||||
if (!urlObj.hostname && !urlObj.pathname) {
|
||||
throw new Error('Invalid URL');
|
||||
if (opts.forceHttps && urlObj.protocol === 'http:') {
|
||||
urlObj.protocol = 'https:';
|
||||
}
|
||||
|
||||
// prevent these from being used by `url.format`
|
||||
delete urlObj.host;
|
||||
delete urlObj.query;
|
||||
|
||||
// remove fragment
|
||||
if (opts.stripFragment) {
|
||||
delete urlObj.hash;
|
||||
// Remove hash
|
||||
if (opts.stripHash) {
|
||||
urlObj.hash = '';
|
||||
}
|
||||
|
||||
// remove default port
|
||||
var port = DEFAULT_PORTS[urlObj.protocol];
|
||||
if (Number(urlObj.port) === port) {
|
||||
delete urlObj.port;
|
||||
}
|
||||
|
||||
// remove duplicate slashes
|
||||
// Remove duplicate slashes if not preceded by a protocol
|
||||
if (urlObj.pathname) {
|
||||
urlObj.pathname = urlObj.pathname.replace(/\/{2,}/g, '/');
|
||||
// TODO: Use the following instead when targeting Node.js 10
|
||||
// `urlObj.pathname = urlObj.pathname.replace(/(?<!https?:)\/{2,}/g, '/');`
|
||||
urlObj.pathname = urlObj.pathname.replace(/((?![https?:]).)\/{2,}/g, (_, p1) => {
|
||||
if (/^(?!\/)/g.test(p1)) {
|
||||
return `${p1}/`;
|
||||
}
|
||||
return '/';
|
||||
});
|
||||
}
|
||||
|
||||
// decode URI octets
|
||||
// Decode URI octets
|
||||
if (urlObj.pathname) {
|
||||
urlObj.pathname = decodeURI(urlObj.pathname);
|
||||
}
|
||||
|
||||
// remove directory index
|
||||
// Remove directory index
|
||||
if (opts.removeDirectoryIndex === true) {
|
||||
opts.removeDirectoryIndex = [/^index\.[a-z]+$/];
|
||||
}
|
||||
|
||||
if (Array.isArray(opts.removeDirectoryIndex) && opts.removeDirectoryIndex.length) {
|
||||
var pathComponents = urlObj.pathname.split('/');
|
||||
var lastComponent = pathComponents[pathComponents.length - 1];
|
||||
if (Array.isArray(opts.removeDirectoryIndex) && opts.removeDirectoryIndex.length > 0) {
|
||||
let pathComponents = urlObj.pathname.split('/');
|
||||
const lastComponent = pathComponents[pathComponents.length - 1];
|
||||
|
||||
if (testParameter(lastComponent, opts.removeDirectoryIndex)) {
|
||||
pathComponents = pathComponents.slice(0, pathComponents.length - 1);
|
||||
@@ -102,60 +94,46 @@ module.exports = function (str, opts) {
|
||||
}
|
||||
}
|
||||
|
||||
// resolve relative paths, but only for slashed protocols
|
||||
if (slashedProtocol[urlObj.protocol]) {
|
||||
var domain = urlObj.protocol + '//' + urlObj.hostname;
|
||||
var relative = url.resolve(domain, urlObj.pathname);
|
||||
urlObj.pathname = relative.replace(domain, '');
|
||||
}
|
||||
|
||||
if (urlObj.hostname) {
|
||||
// IDN to Unicode
|
||||
urlObj.hostname = punycode.toUnicode(urlObj.hostname).toLowerCase();
|
||||
|
||||
// remove trailing dot
|
||||
// Remove trailing dot
|
||||
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
|
||||
|
||||
// remove `www.`
|
||||
if (opts.stripWWW) {
|
||||
// Remove `www.`
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
if (opts.stripWWW && /^www\.([a-z\-\d]{2,63})\.([a-z\.]{2,5})$/.test(urlObj.hostname)) {
|
||||
// Each label should be max 63 at length (min: 2).
|
||||
// The extension should be max 5 at length (min: 2).
|
||||
// Source: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
|
||||
urlObj.hostname = urlObj.hostname.replace(/^www\./, '');
|
||||
}
|
||||
}
|
||||
|
||||
// remove URL with empty query string
|
||||
if (urlObj.search === '?') {
|
||||
delete urlObj.search;
|
||||
}
|
||||
|
||||
var queryParameters = queryString.parse(urlObj.search);
|
||||
|
||||
// remove query unwanted parameters
|
||||
// Remove query unwanted parameters
|
||||
if (Array.isArray(opts.removeQueryParameters)) {
|
||||
for (var key in queryParameters) {
|
||||
for (const key of [...urlObj.searchParams.keys()]) {
|
||||
if (testParameter(key, opts.removeQueryParameters)) {
|
||||
delete queryParameters[key];
|
||||
urlObj.searchParams.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sort query parameters
|
||||
urlObj.search = queryString.stringify(sortKeys(queryParameters));
|
||||
// Sort query parameters
|
||||
if (opts.sortQueryParameters) {
|
||||
urlObj.searchParams.sort();
|
||||
}
|
||||
|
||||
// decode query parameters
|
||||
urlObj.search = decodeURIComponent(urlObj.search);
|
||||
// Take advantage of many of the Node `url` normalizations
|
||||
urlString = urlObj.toString();
|
||||
|
||||
// take advantage of many of the Node `url` normalizations
|
||||
str = url.format(urlObj);
|
||||
|
||||
// remove ending `/`
|
||||
// Remove ending `/`
|
||||
if (opts.removeTrailingSlash || urlObj.pathname === '/') {
|
||||
str = str.replace(/\/$/, '');
|
||||
urlString = urlString.replace(/\/$/, '');
|
||||
}
|
||||
|
||||
// restore relative protocol, if applicable
|
||||
// Restore relative protocol, if applicable
|
||||
if (hasRelativeProtocol && !opts.normalizeProtocol) {
|
||||
str = str.replace(/^http:\/\//, '//');
|
||||
urlString = urlString.replace(/^http:\/\//, '//');
|
||||
}
|
||||
|
||||
return str;
|
||||
return urlString;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user