From 7127c56bd7a81f85421a9b2ef56402ec4d176dde Mon Sep 17 00:00:00 2001 From: Davi Medeiros Date: Mon, 17 Aug 2020 19:36:19 +0200 Subject: [PATCH 1/2] feat: replace is-url-superb by valid-url -- REPLACE-IS --- lib/nodes/Word.js | 4 ++-- package-lock.json | 30 +++++++----------------------- package.json | 3 +-- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/lib/nodes/Word.js b/lib/nodes/Word.js index bdadbda..7a8322b 100644 --- a/lib/nodes/Word.js +++ b/lib/nodes/Word.js @@ -9,7 +9,7 @@ included in all copies or substantial portions of this Source Code Form. */ const colors = require('color-name'); -const isUrl = require('is-url-superb'); +const { isWebUri } = require('valid-url'); const { registerWalker } = require('../walker'); @@ -37,7 +37,7 @@ class Word extends Node { const { value } = lastNode; lastNode.isColor = colorRegex.test(value) || colorNames.includes(value.toLowerCase()); lastNode.isHex = hexRegex.test(value); - lastNode.isUrl = isUrl(value); + lastNode.isUrl = Boolean(isWebUri(value)); lastNode.isVariable = Word.testVariable(tokens[0], parser); } diff --git a/package-lock.json b/package-lock.json index 1122f56..102ed29 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2844,11 +2844,6 @@ } } }, - "ip-regex": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/ip-regex/-/ip-regex-4.1.0.tgz", - "integrity": "sha512-pKnZpbgCTfH/1NLIlOduP/V+WRXzC2MOz3Qo8xmxk8C5GudJLgK5QyLVXOSWy3ParAH7Eemurl3xjv/WXYFvMA==" - }, "irregular-plurals": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/irregular-plurals/-/irregular-plurals-3.2.0.tgz", @@ -3044,14 +3039,6 @@ "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=", "dev": true }, - "is-url-superb": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/is-url-superb/-/is-url-superb-3.0.0.tgz", - "integrity": "sha512-3faQP+wHCGDQT1qReM5zCPx2mxoal6DzbzquFlCYJLWyy4WPTved33ea2xFbX37z4NoriEwZGIYhFtx8RUB5wQ==", - "requires": { - "url-regex": "^5.0.0" - } - }, "is-windows": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz", @@ -5350,7 +5337,8 @@ "tlds": { "version": "1.203.1", "resolved": "https://registry.npmjs.org/tlds/-/tlds-1.203.1.tgz", - "integrity": "sha512-7MUlYyGJ6rSitEZ3r1Q1QNV8uSIzapS8SmmhSusBuIc7uIxPPwsKllEP0GRp1NS6Ik6F+fRZvnjDWm3ecv2hDw==" + "integrity": "sha512-7MUlYyGJ6rSitEZ3r1Q1QNV8uSIzapS8SmmhSusBuIc7uIxPPwsKllEP0GRp1NS6Ik6F+fRZvnjDWm3ecv2hDw==", + "dev": true }, "tmp": { "version": "0.0.33", @@ -5518,15 +5506,6 @@ "prepend-http": "^2.0.0" } }, - "url-regex": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/url-regex/-/url-regex-5.0.0.tgz", - "integrity": "sha512-O08GjTiAFNsSlrUWfqF1jH0H1W3m35ZyadHrGv5krdnmPPoxP27oDTqux/579PtaroiSGm5yma6KT1mHFH6Y/g==", - "requires": { - "ip-regex": "^4.1.0", - "tlds": "^1.203.0" - } - }, "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", @@ -5539,6 +5518,11 @@ "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==", "dev": true }, + "valid-url": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/valid-url/-/valid-url-1.0.9.tgz", + "integrity": "sha1-HBRHm0DxOXp1eC8RXkCGRHQzogA=" + }, "validate-npm-package-license": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz", diff --git a/package.json b/package.json index 1eb19cb..edd0113 100644 --- a/package.json +++ b/package.json @@ -31,9 +31,8 @@ ], "dependencies": { "color-name": "^1.1.4", - "is-url-superb": "^3.0.0", "postcss": "^7.0.5", - "url-regex": "^5.0.0" + "valid-url": "^1.0.9" }, "devDependencies": { "ava": "^3.5.1", From c1a23ffd95a408a3a77b1003e539fd1e5f23c053 Mon Sep 17 00:00:00 2001 From: Davi Medeiros Date: Mon, 17 Aug 2020 22:58:09 +0200 Subject: [PATCH 2/2] fix: patch valid-url to also validate protocol-relative urls --- lib/isUrl.js | 23 +++++++++++++++++++++++ lib/nodes/Word.js | 4 ++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 lib/isUrl.js diff --git a/lib/isUrl.js b/lib/isUrl.js new file mode 100644 index 0000000..2384520 --- /dev/null +++ b/lib/isUrl.js @@ -0,0 +1,23 @@ +/* + Copyright © 2018 Andrew Powell + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of this Source Code Form. +*/ +const { isWebUri } = require('valid-url'); + +const isUrl = (url) => { + let toValidate = url; + + if (url.startsWith('//')) { + toValidate = `http:${url}`; + } + + return Boolean(isWebUri(toValidate)); +}; + +module.exports = { isUrl }; diff --git a/lib/nodes/Word.js b/lib/nodes/Word.js index 7a8322b..bd7b239 100644 --- a/lib/nodes/Word.js +++ b/lib/nodes/Word.js @@ -9,8 +9,8 @@ included in all copies or substantial portions of this Source Code Form. */ const colors = require('color-name'); -const { isWebUri } = require('valid-url'); +const { isUrl } = require('../isUrl'); const { registerWalker } = require('../walker'); const Node = require('./Node'); @@ -37,7 +37,7 @@ class Word extends Node { const { value } = lastNode; lastNode.isColor = colorRegex.test(value) || colorNames.includes(value.toLowerCase()); lastNode.isHex = hexRegex.test(value); - lastNode.isUrl = Boolean(isWebUri(value)); + lastNode.isUrl = isUrl(value); lastNode.isVariable = Word.testVariable(tokens[0], parser); }