index.js 1.8 KB

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