<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const {registerOverlay, unloadWindow} = require("windows");
const cothreads = lazyRequire("cothreads", "CoThreadInterleaved");
const timers = lazyRequire("timers", "createTimeout", "destroy");

// l10n
lazy(this, "strings", (function()
  Services.strings.createBundle("chrome://repagination/locale/repagination.properties")));
function _(id) {
  let args = Array.slice(arguments, 1);
  if (args.length) {
    return strings.formatStringFromName(id, args, args.length);
  }
  return strings.GetStringFromName(id);
};


function getFirstSnapshot(doc, node, query) doc
  .evaluate(query, node, null, 7, null)
  .snapshotItem(0);

function checkSameOrigin(node, tryLoadUri) {
  try {
    if (!(tryLoadUri instanceof Ci.nsIURI)) {
      tryLoadUri = Services.io.newURI(tryLoadUri, null, null);
    }
    if (tryLoadUri.schemeIs("data")) {
      return true;
    }
    node.nodePrincipal.checkMayLoad(tryLoadUri, false);
    return true;
  }
  catch (ex) {
    log(LOG_DEBUG, "denied load of " + (tryLoadUri.spec || tryLoadUri), ex);
    return false;
  }
}

function createFrame(window, src, loadFun) {
  log(LOG_INFO, "creating frame for " + src);
  if (!checkSameOrigin(window.document, src)) {
    throw new Error("same origin mismatch; frame creation denied");
  }
  let frame = window.document.createElement("iframe");
  frame.style.display = "none";
  window.document.body.appendChild(frame);
  let docShell = frame.contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShell);
  docShell.allowImages = false;
  docShell.allowPlugins = false;
  docShell.allowDNSPrefetch = false;

  frame.src = src;
  frame.addEventListener("load", function loadHandler() {
    frame.removeEventListener("load", loadHandler, false);
    log(LOG_INFO, "frame loaded, going to process");
    try {
      loadFun.call(frame);
    }
    catch (ex) {
      log(LOG_ERROR, "failed to invoke callback on frame", ex);
    }
  }, false);
  let errorCount = 0;
  let errorHandler = function(evt) {
    log(LOG_INFO, "frame err'ed out");
    if (++errorCount > 5) {
      frame.removeEventListener("error", errorHandler, false);
      frame.removeEventListener("abort", errorHandler, false);
      log(LOG_ERROR, "frame err'ed out, giving up");
      try {
        loadFun.call(frame);
      }
      catch (ex) {
        log(LOG_ERROR, "failed to invoke callback on frame", ex);
      }
      return;
    }
    if (frame.history) {
      frame.history.reload();
    }
    else {
      frame.src = src;
    }
  };
  frame.addEventListener("error", errorHandler, false);
  frame.addEventListener("abort", errorHandler, false);
  
  return frame;
}

function Repaginator(focusElement, count) {
  this.pageLimit = count || 0;
  this.init(focusElement);
  this.buildQuery(focusElement);
}
Repaginator.prototype = {
  slideshow: false,
  pageLimit: 0,
  seconds: 0,
  pageCount: 0,
  attemptToIncrement: true,

  init: function R_init(focusElement) {
    // find anchor
    (function findInitialAnchor() {
      for (let parent = focusElement; parent; parent = parent.parentNode) {
        if (parent.localName = "a") {
          focusElement = parent;
          return;
        }
      }
      throw new Error("No focus element");
    })();

    this._window = weak(focusElement.ownerDocument.defaultView);
  },
  buildQuery: function R_buildQuery(el) {
    function escapeXStr(str) str.replace(/'/g, "\\'");

    this.query = "";

    // Note: cannot use the id() xpath function here, as there might
    // be duplicate ids
    if (el.id) {
      this.query = "//a[@id='" + escapeXStr(el.id) + "']"; 
      this.numberToken = /(\[@id='.*?)(\d+)(.*?'\])/;
    }
    else {
      // Find an id in the ancestor chain, or alternatively a class
      // that we may operate on
      (function findPathPrefix() {
        let pieces = [];
        for (let parent = el.parentNode; parent; parent = parent.parentNode) {
          if (parent.id) {
            log(LOG_DEBUG, "got id: " + parent.id);
            pieces.unshift("//" + parent.localName + "[@id='" + escapeXStr(parent.id) + "']");<commit_msg>Fix incorrect compare<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const {registerOverlay, unloadWindow} = require("windows");
const cothreads = lazyRequire("cothreads", "CoThreadInterleaved");
const timers = lazyRequire("timers", "createTimeout", "destroy");

// l10n
lazy(this, "strings", (function()
  Services.strings.createBundle("chrome://repagination/locale/repagination.properties")));
function _(id) {
  let args = Array.slice(arguments, 1);
  if (args.length) {
    return strings.formatStringFromName(id, args, args.length);
  }
  return strings.GetStringFromName(id);
};


function getFirstSnapshot(doc, node, query) doc
  .evaluate(query, node, null, 7, null)
  .snapshotItem(0);

function checkSameOrigin(node, tryLoadUri) {
  try {
    if (!(tryLoadUri instanceof Ci.nsIURI)) {
      tryLoadUri = Services.io.newURI(tryLoadUri, null, null);
    }
    if (tryLoadUri.schemeIs("data")) {
      return true;
    }
    node.nodePrincipal.checkMayLoad(tryLoadUri, false);
    return true;
  }
  catch (ex) {
    log(LOG_DEBUG, "denied load of " + (tryLoadUri.spec || tryLoadUri), ex);
    return false;
  }
}

function createFrame(window, src, loadFun) {
  log(LOG_INFO, "creating frame for " + src);
  if (!checkSameOrigin(window.document, src)) {
    throw new Error("same origin mismatch; frame creation denied");
  }
  let frame = window.document.createElement("iframe");
  frame.style.display = "none";
  window.document.body.appendChild(frame);
  let docShell = frame.contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShell);
  docShell.allowImages = false;
  docShell.allowPlugins = false;
  docShell.allowDNSPrefetch = false;

  frame.src = src;
  frame.addEventListener("load", function loadHandler() {
    frame.removeEventListener("load", loadHandler, false);
    log(LOG_INFO, "frame loaded, going to process");
    try {
      loadFun.call(frame);
    }
    catch (ex) {
      log(LOG_ERROR, "failed to invoke callback on frame", ex);
    }
  }, false);
  let errorCount = 0;
  let errorHandler = function(evt) {
    log(LOG_INFO, "frame err'ed out");
    if (++errorCount > 5) {
      frame.removeEventListener("error", errorHandler, false);
      frame.removeEventListener("abort", errorHandler, false);
      log(LOG_ERROR, "frame err'ed out, giving up");
      try {
        loadFun.call(frame);
      }
      catch (ex) {
        log(LOG_ERROR, "failed to invoke callback on frame", ex);
      }
      return;
    }
    if (frame.history) {
      frame.history.reload();
    }
    else {
      frame.src = src;
    }
  };
  frame.addEventListener("error", errorHandler, false);
  frame.addEventListener("abort", errorHandler, false);
  
  return frame;
}

function Repaginator(focusElement, count) {
  this.pageLimit = count || 0;
  this.init(focusElement);
  this.buildQuery(focusElement);
}
Repaginator.prototype = {
  slideshow: false,
  pageLimit: 0,
  seconds: 0,
  pageCount: 0,
  attemptToIncrement: true,

  init: function R_init(focusElement) {
    // find anchor
    (function findInitialAnchor() {
      for (let parent = focusElement; parent; parent = parent.parentNode) {
        if (parent.localName == "a") {
          focusElement = parent;
          return;
        }
      }
      throw new Error("No focus element");
    })();

    this._window = weak(focusElement.ownerDocument.defaultView);
  },
  buildQuery: function R_buildQuery(el) {
    function escapeXStr(str) str.replace(/'/g, "\\'");

    this.query = "";

    // Note: cannot use the id() xpath function here, as there might
    // be duplicate ids
    if (el.id) {
      this.query = "//a[@id='" + escapeXStr(el.id) + "']"; 
      this.numberToken = /(\[@id='.*?)(\d+)(.*?'\])/;
    }
    else {
      // Find an id in the ancestor chain, or alternatively a class
      // that we may operate on
      (function findPathPrefix() {
        let pieces = [];
        for (let parent = el.parentNode; parent; parent = parent.parentNode) {
          if (parent.id) {
            log(LOG_DEBUG, "got id: " + parent.id);
            pieces.unshift("//" + parent.localName + "[@id='" + escapeXStr(parent.id) + "']");
{'fraction_non_alphanumeric': 0.14246481290765534, 'fraction_numerical': 0.0034328870580157913, 'mean_word_length': 3.212048192771084, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3410501', 'n_tokens_mistral': 2777, 'n_tokens_neox': 2730, 'n_words': 680}