index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. const functionArgumentsSearch = require('../../utils/functionArgumentsSearch');
  3. const isStandardSyntaxUrl = require('../../utils/isStandardSyntaxUrl');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const ruleName = 'function-url-no-scheme-relative';
  8. const messages = ruleMessages(ruleName, {
  9. rejected: 'Unexpected scheme-relative url',
  10. });
  11. const meta = {
  12. url: 'https://stylelint.io/user-guide/rules/list/function-url-no-scheme-relative',
  13. };
  14. /** @type {import('stylelint').Rule} */
  15. const rule = (primary) => {
  16. return (root, result) => {
  17. const validOptions = validateOptions(result, ruleName, { actual: primary });
  18. if (!validOptions) {
  19. return;
  20. }
  21. root.walkDecls((decl) => {
  22. functionArgumentsSearch(decl.toString().toLowerCase(), 'url', (args, index) => {
  23. const url = args.trim().replace(/^['"]+|['"]+$/g, '');
  24. if (!isStandardSyntaxUrl(url) || !url.startsWith('//')) {
  25. return;
  26. }
  27. report({
  28. message: messages.rejected,
  29. node: decl,
  30. index,
  31. result,
  32. ruleName,
  33. });
  34. });
  35. });
  36. };
  37. };
  38. rule.ruleName = ruleName;
  39. rule.messages = messages;
  40. rule.meta = meta;
  41. module.exports = rule;