getAllViewportMediaCssRuleAsClassRule function

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

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

Implementation

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

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

  for (var mediaRule in rules) {
    var cssRules = mediaRule.cssRules;
    if (cssRules == null) continue;
    for (var rule in cssRules.whereType<CssStyleRule>()) {
      var block = rule.style.cssText;
      if (block == null || block.isEmpty) continue;

      var selectors = parseCssRuleSelectors(rule);
      var selectorsFixed = selectors.map((s) => '.$targetClass $s');
      var selectors2 = selectorsFixed.join(' , ');

      var blocks = rulesFixed.putIfAbsent(selectors2, () => <String>[]);
      blocks.add(block);
    }
  }

  var rules2 = rulesFixed
      .map((sel, blocks) {
        var css = blocks.join(' ; ');
        if (blocks.length > 1) {
          var css2 = CssStyleDeclaration.css(css).cssText;
          if (css2 != null) {
            css = css2;
          }
        }
        return MapEntry(sel, '$sel { $css }');
      })
      .values
      .toList();

  return rules2;
}