UNPKG

1.37 kBJavaScriptView Raw
1'use strict';
2
3const hasLessInterpolation = require('../utils/hasLessInterpolation');
4const hasPsvInterpolation = require('../utils/hasPsvInterpolation');
5const hasScssInterpolation = require('../utils/hasScssInterpolation');
6const hasTplInterpolation = require('../utils/hasTplInterpolation');
7
8/**
9 * Check whether a URL is standard
10 *
11 * @param {string} url
12 * @returns {boolean}
13 */
14module.exports = function (url) {
15 if (url.length === 0) {
16 return true;
17 }
18
19 // Sass interpolation works anywhere
20 if (hasScssInterpolation(url) || hasTplInterpolation(url) || hasPsvInterpolation(url)) {
21 return false;
22 }
23
24 // Inside `'` and `"` work only LESS interpolation
25 if ((url.startsWith(`'`) && url.endsWith(`'`)) || (url.startsWith(`"`) && url.endsWith(`"`))) {
26 if (hasLessInterpolation(url)) {
27 return false;
28 }
29
30 return true;
31 }
32
33 // Less variable works only at the beginning
34 // Check is less variable, allow use '@url/some/path'
35 // https://github.com/less/less.js/blob/3.x/lib/less/parser/parser.js#L547
36 if (url.startsWith('@') && /^@@?[\w-]+$/.test(url)) {
37 return false;
38 }
39
40 // In url without quotes scss variable can be everywhere
41 // But in this case it is allowed to use only specific characters
42 // Also forbidden "/" at the end of url
43 if (url.includes('$') && /^[$\s\w+-/*'"/]+$/.test(url) && !url.endsWith('/')) {
44 return false;
45 }
46
47 return true;
48};