<commit_before>goog.provide('treesaver.layout.Page');

goog.require('treesaver.array');
goog.require('treesaver.dimensions');
goog.require('treesaver.dom');
goog.require('treesaver.scheduler');
goog.require('treesaver.layout.BreakRecord');
goog.require('treesaver.layout.Grid');
goog.require('treesaver.layout.Block');
goog.require('treesaver.ui.Scrollable');

/**
  * Page class
  * @constructor
  * @param {!treesaver.layout.Content} content
  * @param {!Array.<treesaver.layout.Grid>} grids
  * @param {!treesaver.layout.BreakRecord} br The current breakRecord.
  */
treesaver.layout.Page = function(content, grids, br) {
  var best = treesaver.layout.Grid.best(content, grids, br),
      host = document.createElement('div'),
      originalBr = br.clone(),
      containerFilled = false;

  /**
   * @type {boolean}
   */
  this.ignore;

  if (!best || !best.grid) {
    // Might have leftover figures that just won't fit
    br.finished = br.atEnd(content) ||
    br.figureIndex === content.figures.length;

    if (br.finished) {
      treesaver.debug.info('Finished article in face of error.');
      this.ignore = true;
    }
    else {
      treesaver.debug.error('No best grid found: ' + arguments);
      this.error = true;
    }

    return;
  }

  // Store state
  /**
   * @type {!treesaver.dimensions.Metrics}
   */
  this.size = best.grid.stretchedSize.clone();
  /**
   * @type {!treesaver.layout.ContentPosition}
   */
  this.begin = br.getPosition();

  // Create our host for measuring and producing HTML
  treesaver.dom.addClass(host, 'offscreen');
  // TODO: Only add to body if needed?
  // TODO: Perhaps not, since IE has innerHTML issues when disconnected
  document.body.appendChild(host);
  host.innerHTML = best.grid.html;
  /**
   * @type {?Element}
   */
  this.node = /** @type {!Element} */ (host.firstChild);

  // Manually set dimensions on the page
  treesaver.dimensions.setCssPx(this.node, 'width', this.size.w);
  treesaver.dimensions.setCssPx(this.node, 'height', this.size.h);

  treesaver.dom.getElementsByProperty('data-template', 'document', null, this.node).forEach(function (el) {
    // FIXME: cache the compiled template
    el.innerHTML = Handlebars.compile(el.innerHTML)(content.doc.meta);
  });

  // Containers
  treesaver.dom.getElementsByClassName('container', this.node).forEach(function(containerNode, i) {
    var mapping = best.containers[i],
        figure, figureIndex, success;
<commit_msg>No sense in caching as it is only evaluated once as opposed to the Chrome.<commit_after>goog.provide('treesaver.layout.Page');

goog.require('treesaver.array');
goog.require('treesaver.dimensions');
goog.require('treesaver.dom');
goog.require('treesaver.scheduler');
goog.require('treesaver.layout.BreakRecord');
goog.require('treesaver.layout.Grid');
goog.require('treesaver.layout.Block');
goog.require('treesaver.ui.Scrollable');

/**
  * Page class
  * @constructor
  * @param {!treesaver.layout.Content} content
  * @param {!Array.<treesaver.layout.Grid>} grids
  * @param {!treesaver.layout.BreakRecord} br The current breakRecord.
  */
treesaver.layout.Page = function(content, grids, br) {
  var best = treesaver.layout.Grid.best(content, grids, br),
      host = document.createElement('div'),
      originalBr = br.clone(),
      containerFilled = false;

  /**
   * @type {boolean}
   */
  this.ignore;

  if (!best || !best.grid) {
    // Might have leftover figures that just won't fit
    br.finished = br.atEnd(content) ||
    br.figureIndex === content.figures.length;

    if (br.finished) {
      treesaver.debug.info('Finished article in face of error.');
      this.ignore = true;
    }
    else {
      treesaver.debug.error('No best grid found: ' + arguments);
      this.error = true;
    }

    return;
  }

  // Store state
  /**
   * @type {!treesaver.dimensions.Metrics}
   */
  this.size = best.grid.stretchedSize.clone();
  /**
   * @type {!treesaver.layout.ContentPosition}
   */
  this.begin = br.getPosition();

  // Create our host for measuring and producing HTML
  treesaver.dom.addClass(host, 'offscreen');
  // TODO: Only add to body if needed?
  // TODO: Perhaps not, since IE has innerHTML issues when disconnected
  document.body.appendChild(host);
  host.innerHTML = best.grid.html;
  /**
   * @type {?Element}
   */
  this.node = /** @type {!Element} */ (host.firstChild);

  // Manually set dimensions on the page
  treesaver.dimensions.setCssPx(this.node, 'width', this.size.w);
  treesaver.dimensions.setCssPx(this.node, 'height', this.size.h);

  treesaver.dom.getElementsByProperty('data-template', 'document', null, this.node).forEach(function (el) {
    el.innerHTML = Handlebars.compile(el.innerHTML)(content.doc.meta);
  });

  // Containers
  treesaver.dom.getElementsByClassName('container', this.node).forEach(function(containerNode, i) {
    var mapping = best.containers[i],
        figure, figureIndex, success;

{'fraction_non_alphanumeric': 0.15450294775360846, 'fraction_numerical': 0.0, 'mean_word_length': 3.6240601503759398, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7514024', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1536, 'n_words': 321}