<commit_before>// Copyright (c) 2014, the vcss project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library vcss.browser;

import 'dart:collection';
import 'dart:svg' as svg;
import 'dart:html' as html;
import 'svg_icon.dart';
import 'stylesheet.dart';
import 'builder.dart';

class StyleSheetElement {
  final StyleSheet styleSheet;
  final html.StyleElement element = new html.StyleElement();
  final HashSet<int> required = new HashSet<int>();
  bool disabled = false;
  int _index = 0;

  StyleSheetElement(this.styleSheet, List<String> rules) {
    _collectDependencies(styleSheet);
    element.append(new html.Text(rules.join('\n')));
  }

  void _collectDependencies(StyleSheet s) {
    for (final d in s.require) {
      _collectDependencies(d);
    }
    required.add(s.id);
  }

  void enable() {
    if (disabled) {
      disabled = false;
      assert(() {
        element.setAttribute('disabled', '');
        return true;
      });
      element.disabled = false;
    }
  }

  void disable() {
    if (!disabled) {
      disabled = true;
      assert(() {
        element.setAttribute('disabled', 'true');
        return true;
      });
      element.disabled = true;
    }
  }
}

class IconSetElement {
  final element = new svg.SvgSvgElement();
  final defs = new svg.DefsElement();
  final HashMap<String, SvgIcon> icons = new HashMap<String, SvgIcon>();

  IconSetElement() {
    element.append(defs);
  }

  void add(SvgIcon icon) {
    icons.putIfAbsent(icon.id, () {
      defs.append(icon.render());
      return icon;
    });
  }
}

class StyleSheetManager {
  final Builder builder;
  final IconSetElement iconSet = new IconSetElement();
  final HashMap<int, StyleSheetElement> styleSheets =
      new HashMap<int, StyleSheetElement>();

  StyleSheetManager([this.builder = const Builder()]) {
    html.document.head.append(iconSet.element);
  }

  void _include(List<StyleSheet> styles) {
    for (final s in styles) {
      final StyleSheetElement e = styleSheets.putIfAbsent(s.id, () {
        _include(s.require);

        print(s);
        for (final icon in s.icons) {
          iconSet.add(icon);
        }

        final ret = new StyleSheetElement(s, builder.compile(s));
        html.document.head.append(ret.element);
        return ret;
      });
    }
  }

  void include(List<StyleSheet> styles, [bool disableUnused = true]) {
    _include(styles);

    final List<StyleSheetElement> elements = styles.map((s) => styleSheets[s.id]).toList();

    styleSheets.forEach((k, v) {
      final e = elements.firstWhere((e) => e.required.contains(k), orElse: () => null);
      if (e == null) {
        v.disable();
      } else {
        v.enable();
      }
    });
  }
}<commit_msg>Remove print statement<commit_after>// Copyright (c) 2014, the vcss project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library vcss.browser;

import 'dart:collection';
import 'dart:svg' as svg;
import 'dart:html' as html;
import 'svg_icon.dart';
import 'stylesheet.dart';
import 'builder.dart';

class StyleSheetElement {
  final StyleSheet styleSheet;
  final html.StyleElement element = new html.StyleElement();
  final HashSet<int> required = new HashSet<int>();
  bool disabled = false;
  int _index = 0;

  StyleSheetElement(this.styleSheet, List<String> rules) {
    _collectDependencies(styleSheet);
    element.append(new html.Text(rules.join('\n')));
  }

  void _collectDependencies(StyleSheet s) {
    for (final d in s.require) {
      _collectDependencies(d);
    }
    required.add(s.id);
  }

  void enable() {
    if (disabled) {
      disabled = false;
      assert(() {
        element.setAttribute('disabled', '');
        return true;
      });
      element.disabled = false;
    }
  }

  void disable() {
    if (!disabled) {
      disabled = true;
      assert(() {
        element.setAttribute('disabled', 'true');
        return true;
      });
      element.disabled = true;
    }
  }
}

class IconSetElement {
  final element = new svg.SvgSvgElement();
  final defs = new svg.DefsElement();
  final HashMap<String, SvgIcon> icons = new HashMap<String, SvgIcon>();

  IconSetElement() {
    element.append(defs);
  }

  void add(SvgIcon icon) {
    icons.putIfAbsent(icon.id, () {
      defs.append(icon.render());
      return icon;
    });
  }
}

class StyleSheetManager {
  final Builder builder;
  final IconSetElement iconSet = new IconSetElement();
  final HashMap<int, StyleSheetElement> styleSheets =
      new HashMap<int, StyleSheetElement>();

  StyleSheetManager([this.builder = const Builder()]) {
    html.document.head.append(iconSet.element);
  }

  void _include(List<StyleSheet> styles) {
    for (final s in styles) {
      final StyleSheetElement e = styleSheets.putIfAbsent(s.id, () {
        _include(s.require);

        for (final icon in s.icons) {
          iconSet.add(icon);
        }

        final ret = new StyleSheetElement(s, builder.compile(s));
        html.document.head.append(ret.element);
        return ret;
      });
    }
  }

  void include(List<StyleSheet> styles, [bool disableUnused = true]) {
    _include(styles);

    final List<StyleSheetElement> elements = styles.map((s) => styleSheets[s.id]).toList();

    styleSheets.forEach((k, v) {
      final e = elements.firstWhere((e) => e.required.contains(k), orElse: () => null);
      if (e == null) {
        v.disable();
      } else {
        v.enable();
      }
    });
  }
}
{'fraction_non_alphanumeric': 0.1328991374757965, 'fraction_numerical': 0.0017602534765006161, 'mean_word_length': 2.9186206896551723, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6773889', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1757, 'n_words': 437}