<commit_before>module Document

  # Returns the body element of the current page as an {Element}
  # instance. Returns nil if the document hasn't finished loading
  # (which might mean the body isn't actually ready yet).
  #
  # @example
  #
  #     Document.body     # => <body>
  #
  # @return [Element, nil] return this documents body element
  def self.body
    %x{
      if (this.body) {
        return this.body;
      }

      if (document.body) {
        return this.body = #{ Element.new `document.body` };
      }

      return nil;
    }
  end

  # Returns the head element of this document as an {Element}.
  #
  # @return [Element] head element
  def self.head
    %x{
      if (!this.head) {
        var head = document.getElementsByTagName('head')[0]
        this.head = #{ Element.new `head` };
      }

      return this.head;
    }
  end
end<commit_msg>Added Document.ready? method for observing ready state<commit_after>module Document

  # Returns the body element of the current page as an {Element}
  # instance. Returns nil if the document hasn't finished loading
  # (which might mean the body isn't actually ready yet).
  #
  # @example
  #
  #     Document.body     # => <body>
  #
  # @return [Element, nil] return this documents body element
  def self.body
    %x{
      if (this.body) {
        return this.body;
      }

      if (document.body) {
        return this.body = #{ Element.new `document.body` };
      }

      return nil;
    }
  end

  # Returns the head element of this document as an {Element}.
  #
  # @return [Element] head element
  def self.head
    %x{
      if (!this.head) {
        var head = document.getElementsByTagName('head')[0]
        this.head = #{ Element.new `head` };
      }

      return this.head;
    }
  end

  %x{
    var loaded = false, callbacks = [];

    var trigger = function() {
      if (loaded) return;
      loaded = true;

      for (var i = 0, length = callbacks.length; i < length; i++) {
        var callback = callbacks[i];
        callback.call(callback._s);
      }
    };

    if (document.addEventListener) {
      document.addEventListener('DOMContentLoaded', trigger, false);
    }
    else {
      console.log("No Document.ready? in this browser");
    }
  }

  # Used to both register blocks that should be once the Document is
  # ready, as well as returning a boolean to indicate the ready state.
  #
  # Multiple blocks can be passed to this method which will then be run
  # in order once the Document becomes ready. If no block is given then
  # the ready state is just returned.
  #
  # The ready state is an indication of whether the document is ready
  # for manipulation. Commonly, until the document is ready, there is
  # no guarantee that the {body} element is created.
  #
  # This method will also try and perform in a cross browser way to
  # eliminate any differences between browsers and their ready state.
  #
  # @example
  #
  #     Document.ready? do
  #       puts "Document is ready and loaded!"
  #     end
  #
  #     Document.ready?     # => false
  #     # page load...
  #     Document.ready?     # => true
  #
  # @return [true, false] document ready state  
  def self.ready?(&block)
    %x{
      if (block && block !== nil) {
        if (loaded) {
          block.call(block._s);
        }
        else {
          callbacks.push(block);
        }
      }

      return loaded;
    }
  end
end
{'fraction_non_alphanumeric': 0.10289389067524116, 'fraction_numerical': 0.0008769365682548962, 'mean_word_length': 2.186219739292365, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 9, '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': '4967952', 'n_tokens_mistral': 1027, 'n_tokens_neox': 1002, 'n_words': 361}