isStandardSyntaxUrl.cjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const hasLessInterpolation = require('./hasLessInterpolation.cjs');
  5. const hasPsvInterpolation = require('./hasPsvInterpolation.cjs');
  6. const hasScssInterpolation = require('./hasScssInterpolation.cjs');
  7. const hasTplInterpolation = require('./hasTplInterpolation.cjs');
  8. const IS_LESS_VARIABLE_IN_URL = /^@@?[\w-]+$/;
  9. const IS_SCSS_VARIABLE_IN_URL = /^[$\s\w+\-,./*'"@#?]+$/;
  10. /**
  11. * Check whether a URL is standard
  12. *
  13. * @param {string} url
  14. * @returns {boolean}
  15. */
  16. function isStandardSyntaxUrl(url) {
  17. if (url.length === 0) {
  18. return true;
  19. }
  20. // Sass interpolation works anywhere
  21. if (hasScssInterpolation(url) || hasTplInterpolation(url) || hasPsvInterpolation(url)) {
  22. return false;
  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. return true;
  30. }
  31. // Less variable works only at the beginning
  32. // Check is less variable, allow use '@url/some/path'
  33. // https://github.com/less/less.js/blob/3.x/lib/less/parser/parser.js#L547
  34. if (url.startsWith('@') && IS_LESS_VARIABLE_IN_URL.test(url)) {
  35. return false;
  36. }
  37. // In url without quotes scss variable can be everywhere
  38. // But in this case it is allowed to use only specific characters
  39. // Also forbidden "/" at the end of url
  40. if (url.includes('$') && IS_SCSS_VARIABLE_IN_URL.test(url) && !url.endsWith('/')) {
  41. return false;
  42. }
  43. return true;
  44. }
  45. module.exports = isStandardSyntaxUrl;