getAllViewportMediaCssRule function

List<CssMediaRule> getAllViewportMediaCssRule(
  1. int viewportWidth,
  2. dynamic viewportHeight
)

Returns all CssMediaRule applied for viewportWidth viewportHeight.

Implementation

List<CssMediaRule> getAllViewportMediaCssRule(
    int viewportWidth, viewportHeight) {
  var rules = getAllMediaCssRule(r'(?:min|max)-(?:width|height):\s*.*?');

  var viewportRules = <CssMediaRule>[];

  for (var rule in rules) {
    var conditionText =
        rule.conditionText!.trim().replaceAll(RegExp(r'^\(|\)$'), '');

    var parts = split(conditionText, ':', 2);
    if (parts.length != 2) continue;

    var type = parts[0].trim().toLowerCase();
    var value = parts[1].trim().toLowerCase();

    value = value.replaceFirst(RegExp(r'px$'), '');
    if (isNum(value)) {
      var n = parseNum(value);

      if (type == 'min-width') {
        if (viewportWidth >= n!) {
          viewportRules.add(rule);
        }
      } else if (type == 'min-height') {
        if (viewportHeight >= n) {
          viewportRules.add(rule);
        }
      } else if (type == 'max-width') {
        if (viewportWidth <= n!) {
          viewportRules.add(rule);
        }
      } else if (type == 'max-height') {
        if (viewportHeight <= n) {
          viewportRules.add(rule);
        }
      }
    }
  }

  return viewportRules;
}