isStandardSyntaxUrl.js 1.3 KB

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