findMediaOperator.js 746 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. const styleSearch = require('style-search');
  3. const rangeOperators = ['>=', '<=', '>', '<', '='];
  4. /** @typedef {import('style-search').StyleSearchMatch} StyleSearchMatch */
  5. /**
  6. * @template {import('postcss').AtRule} T
  7. * @param {T} atRule
  8. * @param {(match: StyleSearchMatch, params: string, atRule: T) => void} cb
  9. */
  10. module.exports = function findMediaOperator(atRule, cb) {
  11. if (atRule.name.toLowerCase() !== 'media') {
  12. return;
  13. }
  14. const params = atRule.raws.params ? atRule.raws.params.raw : atRule.params;
  15. styleSearch({ source: params, target: rangeOperators }, (match) => {
  16. const before = params[match.startIndex - 1];
  17. if (before === '>' || before === '<') {
  18. return;
  19. }
  20. cb(match, params, atRule);
  21. });
  22. };