index.mjs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { isRegExp, isString } from '../../utils/validateTypes.mjs';
  2. import functionArgumentsSearch from '../../utils/functionArgumentsSearch.mjs';
  3. import getSchemeFromUrl from '../../utils/getSchemeFromUrl.mjs';
  4. import isStandardSyntaxUrl from '../../utils/isStandardSyntaxUrl.mjs';
  5. import matchesStringOrRegExp from '../../utils/matchesStringOrRegExp.mjs';
  6. import report from '../../utils/report.mjs';
  7. import ruleMessages from '../../utils/ruleMessages.mjs';
  8. import validateOptions from '../../utils/validateOptions.mjs';
  9. const ruleName = 'function-url-scheme-allowed-list';
  10. const messages = ruleMessages(ruleName, {
  11. rejected: (scheme) => `Unexpected URL scheme "${scheme}:"`,
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/function-url-scheme-allowed-list',
  15. };
  16. /** @type {import('stylelint').CoreRules[ruleName]} */
  17. const rule = (primary) => {
  18. return (root, result) => {
  19. const validOptions = validateOptions(result, ruleName, {
  20. actual: primary,
  21. possible: [isString, isRegExp],
  22. });
  23. if (!validOptions) {
  24. return;
  25. }
  26. root.walkDecls((decl) => {
  27. functionArgumentsSearch(decl.toString().toLowerCase(), 'url', (args, index) => {
  28. const unspacedUrlString = args.trim();
  29. if (!isStandardSyntaxUrl(unspacedUrlString)) {
  30. return;
  31. }
  32. const urlString = unspacedUrlString.replace(/^['"]+|['"]+$/g, '');
  33. const scheme = getSchemeFromUrl(urlString);
  34. if (scheme === null) {
  35. return;
  36. }
  37. if (matchesStringOrRegExp(scheme, primary)) {
  38. return;
  39. }
  40. report({
  41. message: messages.rejected,
  42. messageArgs: [scheme],
  43. node: decl,
  44. index,
  45. endIndex: index + args.length,
  46. result,
  47. ruleName,
  48. });
  49. });
  50. });
  51. };
  52. };
  53. rule.primaryOptionArray = true;
  54. rule.ruleName = ruleName;
  55. rule.messages = messages;
  56. rule.meta = meta;
  57. export default rule;