getAllOutOfViewportMediaCssRule function

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

Returns all CssMediaRule not applied for viewportWidth and viewportHeight.

Implementation

List<CssMediaRule> getAllOutOfViewportMediaCssRule(
    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;
}