From a95287009da981f319c97c361b4767cb04b8c566 Mon Sep 17 00:00:00 2001 From: Agastya Chandrakant Date: Fri, 2 Oct 2020 00:09:51 +0530 Subject: [PATCH] Fixes #11 --- index.js | 4 ++-- test.js | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 25dca66..02ce125 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict'; -module.exports = url => { +module.exports = (url, {httpOnly = true} = {}) => { if (typeof url !== 'string') { throw new TypeError(`Expected a \`string\`, got \`${typeof url}\``); } @@ -12,5 +12,5 @@ module.exports = url => { // Scheme: https://tools.ietf.org/html/rfc3986#section-3.1 // Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3 - return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url); + return httpOnly ? /^https?:/.test(url) : /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url); }; diff --git a/test.js b/test.js index f68537b..f89bb56 100644 --- a/test.js +++ b/test.js @@ -4,10 +4,14 @@ import isAbsoluteUrl from '.'; test('main', t => { t.true(isAbsoluteUrl('http://sindresorhus.com')); t.true(isAbsoluteUrl('https://sindresorhus.com')); - t.true(isAbsoluteUrl('httpS://sindresorhus.com')); - t.true(isAbsoluteUrl('file://sindresorhus.com')); - t.true(isAbsoluteUrl('mailto:someone@example.com')); - t.true(isAbsoluteUrl('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D')); + t.false(isAbsoluteUrl('httpS://sindresorhus.com')); + t.true(isAbsoluteUrl('httpS://sindresorhus.com', {httpOnly: false})); + t.false(isAbsoluteUrl('file://sindresorhus.com')); + t.true(isAbsoluteUrl('file://sindresorhus.com', {httpOnly: false})); + t.false(isAbsoluteUrl('mailto:someone@example.com')); + t.true(isAbsoluteUrl('mailto:someone@example.com', {httpOnly: false})); + t.false(isAbsoluteUrl('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D')); + t.true(isAbsoluteUrl('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D', {httpOnly: false})); t.false(isAbsoluteUrl('//sindresorhus.com')); t.false(isAbsoluteUrl('/foo/bar')); t.false(isAbsoluteUrl('foo/bar'));