<commit_before>library domnode;

import 'dart:collection';
import 'dart:convert';
import 'dart:html';

import 'package:html_unescape/html_unescape.dart';
import 'package:petitparser/petitparser.dart';
import 'package:xml/xml.dart' as xml;

import 'src/utils.dart';

part 'dom_node.dart';
part 'src/attribute_capable.dart';
part 'src/class_capable.dart';
part 'src/content_capable.dart';
part 'src/css_capable.dart';
part 'src/event_capable.dart';
part 'src/metrics_capable.dart';
part 'src/null_tree_sanitizer.dart';

DomNode a(String str, [void callback(DomNode target)]) {
  DomNode node = new DomNode.fromString(str);
  if (callback != null) {
    Function.apply(callback, [node]);
  }
  return node;
}

/// This function has several uses:
///
/// * Searches for elements:
/// DomNode node = $('css selectors')
///
/// * Creates a node from a valid XML document
/// DomNode node = $('<span title="My SPAN">Some text...</span>');
///
/// * Creates a node from an element:
/// Element elem = document.querySelector('p');
/// DomNode node = $(elem);
///
/// * Creates a node from a document:
/// DomNode node = $(document);
///
/// * Redundant but programmatically useful:
/// DomNode target = new DomNode('span');
/// node = $(target);
/// assert(node == target);
DomNode $(dynamic /*css selectors | (Element | Document | DomNode) */ target,
    [dynamic /* Element | Document | DomNode */ context]) {
  DomNode ret;

  if (context == null) {
    context = document;
  }
  if (context != null &&
      !(context is Element || context is Document || context is DomNode)) {
    throw new ArgumentError('Invalid context');
  }

  if (target is String) {
    Parser parser = new xml.XmlParserDefinition().build();
    Result result = parser.parse(target);
    if (result.isSuccess) {
      // xml document
      ret = new DomNode.fromString(target);
    } else {
      // css selectors
      ret = $(context).query(target);
    }
  } else if (target is Element) {
    ret = new DomNode.fromElement(target);
  } else if (target is Document) {
    ret = new DomNode.fromDocument(target);
  } else if (target is DomNode) {
    ret = target;
  } else {
    throw new ArgumentError('Invalid target');
  }

  return ret;
}<commit_msg>$ and q functions<commit_after>library domnode;

import 'dart:collection';
import 'dart:convert';
import 'dart:html';

import 'package:html_unescape/html_unescape.dart';
import 'package:petitparser/petitparser.dart';
import 'package:xml/xml.dart' as xml;

import 'src/utils.dart';

part 'dom_node.dart';
part 'src/attribute_capable.dart';
part 'src/class_capable.dart';
part 'src/content_capable.dart';
part 'src/css_capable.dart';
part 'src/event_capable.dart';
part 'src/metrics_capable.dart';
part 'src/null_tree_sanitizer.dart';

/// Creates a DomNode from a given [source] and, eventually, calls
/// the [callback] function.
DomNode $(Object source, [Callback callback]) {
  DomNode node;

  // creates a node from a given source
  if (source is String) {
    node = new DomNode.fromString(source);
  } else if (source is Element) {
    node = new DomNode.fromElement(source);
  } else if (source is Document) {
    node = new DomNode.fromDocument(source);
  } else if (source is DomNode) {
    node = source;
  } else {
    throw new ArgumentError(
        'Invalid argument: String|Element|Document|DomNode');
  }

  if (callback != null) {
    Function.apply(callback, [node]);
  }

  return node;
}

/// Searches the [cssSelectors] nodes from a [context].
DomNode q(String cssSelectors, [DomNode context]) {
  if (context == null) {
    context = new DomNode.fromDocument(document);
  }
  return context.query(cssSelectors);
}
{'fraction_non_alphanumeric': 0.14918032786885246, 'fraction_numerical': 0.0, 'mean_word_length': 3.7117117117117115, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '2916291', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1183, 'n_words': 327}