getAllOutOfViewportMediaCssRuleAsClassRule function

List<String> getAllOutOfViewportMediaCssRuleAsClassRule(
  1. int viewportWidth,
  2. dynamic viewportHeight,
  3. String targetClass
)

Transforms all CssMediaRule to targetClass rule not applied for viewportWidth and viewportHeight.

Implementation

List<String> getAllOutOfViewportMediaCssRuleAsClassRule(
    int viewportWidth, viewportHeight, String targetClass) {
  var rules = getAllOutOfViewportMediaCssRule(viewportWidth, viewportHeight);

  var rulesFixed = <String, List<String>>{};

  for (var mediaRule in rules) {
    for (var rule in mediaRule.cssRules!.whereType<CssStyleRule>()) {
      var selectors = parseCssRuleSelectors(rule);
      var selectorsFixed = selectors.map((s) => '.$targetClass $s');

      var block = rule.style.cssText!;
      var blockUnset =
          block.replaceAll(RegExp(r':.*?;'), ': initial !important;');

      var selectors2 = selectorsFixed.join(' , ');
      var blocks = rulesFixed.putIfAbsent(selectors2, () => <String>[]);
      blocks.add(blockUnset);
    }
  }

  var rules2 = rulesFixed
      .map((key, value) {
        String? css = value.join(' ; ');
        if (value.length > 1) {
          css = CssStyleDeclaration.css(css).cssText;
        }
        return MapEntry(key, '$key { $css }');
      })
      .values
      .toList();

  return rules2;
}