<commit_before>/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is infoRSS.
 *
 * The Initial Developer of the Original Code is
 *   <NAME> <<EMAIL>>.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   <NAME> <<EMAIL>>.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
//------------------------------------------------------------------------------
// inforss_Utils
// Author : <NAME> 2017
//------------------------------------------------------------------------------
/* jshint globalstrict: true */
/* eslint-disable strict */
"use strict";

//This module provides assorted utilities

/* exported EXPORTED_SYMBOLS */
const EXPORTED_SYMBOLS = [
  "replace_without_children", /* exported replace_without_children */
  "remove_all_children", /* exported remove_all_children */
  "make_URI", /* exported make_URI */
  "htmlFormatConvert", /* exported htmlFormatConvert */
  "format_as_hh_mm_ss", /* exported format_as_hh_mm_ss */
  "open_option_window", /* exported open_option_window */
  "option_window_displayed", /* exported option_window_displayed */
  "should_reuse_current_tab", /* exported should_reuse_current_tab */
  "read_password", /* exported read_password */
  "store_password", /* exported store_password */
];

const IoService = Components.classes[
  "@mozilla.org/network/io-service;1"].getService(
  Components.interfaces.nsIIOService);

const FormatConverter = Components.Constructor(
  "@mozilla.org/widget/htmlformatconverter;1",
  "nsIFormatConverter"
);

const WindowMediator = Components.classes[
  "@mozilla.org/appshell/window-mediator;1"].getService(
  Components.interfaces.nsIWindowMediator);

const WindowWatcher = Components.classes[
  "@mozilla.org/embedcomp/window-watcher;1"].getService(
  Components.interfaces.nsIWindowWatcher);

const SupportsString = Components.Constructor(
  "@mozilla.org/supports-string;1",
  "nsISupportsString"
);

const LoginManager = Components.classes[
  "@mozilla.org/login-manager;1"].getService(
  Components.interfaces.nsILoginManager);

const LoginInfo = Components.Constructor(
  "@mozilla.org/login-manager/loginInfo;1",
  Components.interfaces.nsILoginInfo,
  "init");


const As_HH_MM_SS = new Intl.DateTimeFormat(
  [],
  { hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: false }
);


//------------------------------------------------------------------------------
/** Removes all the children of a node
 *
 * This is the most performant way of removing all the children of a node.
 * However, it doesn't seem to work well if the GUI already has its hands on the
 * node in question.
 *
 * @param {Object} node - original node
 *
 * @returns {Object} new node
 */
function replace_without_children(node)
{
  const new_node = node.cloneNode(false);
  node.parentNode.replaceChild(new_node, node);
  return new_node;
}

//------------------------------------------------------------------------------
function remove_all_children(node)
{
  while (node.lastChild != null)
  {
    node.removeChild(node.lastChild);
  }
}

/** Make a URI from a string
 *
 * @param {string} url - url to turn into a URI
 *
 * @returns {URI} URI object
 */
function make_URI(url)
{
  return IoService.newURI(url);
}

//FIXME the only place that passes extra parameters is nntp feed. Given that,
//perhaps we should drop all the parameters and give that its own function.
/** HTML string conversion
 *
 * @param {string} str - string to convert
 * @param {boolean} keep - keep < and > if set
 * @param {string} mimeTypeFrom - mime type of string (defaults to text/html)
 * @param {string} mimeTypeTo - mime type to convert to (defaults to
 *                 text/unicode
 *
 * @returns {string} converted string
 *
 */
function htmlFormatConvert(str, keep, mimeTypeFrom, mimeTypeTo)
{
  if (str == null)
  {
    return null;
  }

  let convertedString = null;

  //This is called from inforssNntp with keep false, converting from plain to
  //html. Arguably it should have its own method.
  if (keep == null)
  {
    keep = true;
  }<commit_msg>fixes an issue with some feeds which send stories without titles...<commit_after>/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is infoRSS.
 *
 * The Initial Developer of the Original Code is
 *   <NAME> <<EMAIL>>.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   <NAME> <<EMAIL>>.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
//------------------------------------------------------------------------------
// inforss_Utils
// Author : <NAME> 2017
//------------------------------------------------------------------------------
/* jshint globalstrict: true */
/* eslint-disable strict */
"use strict";

//This module provides assorted utilities

/* exported EXPORTED_SYMBOLS */
const EXPORTED_SYMBOLS = [
  "replace_without_children", /* exported replace_without_children */
  "remove_all_children", /* exported remove_all_children */
  "make_URI", /* exported make_URI */
  "htmlFormatConvert", /* exported htmlFormatConvert */
  "format_as_hh_mm_ss", /* exported format_as_hh_mm_ss */
  "open_option_window", /* exported open_option_window */
  "option_window_displayed", /* exported option_window_displayed */
  "should_reuse_current_tab", /* exported should_reuse_current_tab */
  "read_password", /* exported read_password */
  "store_password", /* exported store_password */
];

const IoService = Components.classes[
  "@mozilla.org/network/io-service;1"].getService(
  Components.interfaces.nsIIOService);

const FormatConverter = Components.Constructor(
  "@mozilla.org/widget/htmlformatconverter;1",
  "nsIFormatConverter"
);

const WindowMediator = Components.classes[
  "@mozilla.org/appshell/window-mediator;1"].getService(
  Components.interfaces.nsIWindowMediator);

const WindowWatcher = Components.classes[
  "@mozilla.org/embedcomp/window-watcher;1"].getService(
  Components.interfaces.nsIWindowWatcher);

const SupportsString = Components.Constructor(
  "@mozilla.org/supports-string;1",
  "nsISupportsString"
);

const LoginManager = Components.classes[
  "@mozilla.org/login-manager;1"].getService(
  Components.interfaces.nsILoginManager);

const LoginInfo = Components.Constructor(
  "@mozilla.org/login-manager/loginInfo;1",
  Components.interfaces.nsILoginInfo,
  "init");


const As_HH_MM_SS = new Intl.DateTimeFormat(
  [],
  { hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: false }
);


//------------------------------------------------------------------------------
/** Removes all the children of a node
 *
 * This is the most performant way of removing all the children of a node.
 * However, it doesn't seem to work well if the GUI already has its hands on the
 * node in question.
 *
 * @param {Object} node - original node
 *
 * @returns {Object} new node
 */
function replace_without_children(node)
{
  const new_node = node.cloneNode(false);
  node.parentNode.replaceChild(new_node, node);
  return new_node;
}

//------------------------------------------------------------------------------
function remove_all_children(node)
{
  while (node.lastChild != null)
  {
    node.removeChild(node.lastChild);
  }
}

/** Make a URI from a string
 *
 * @param {string} url - url to turn into a URI
 *
 * @returns {URI} URI object
 */
function make_URI(url)
{
  return IoService.newURI(url);
}

//FIXME the only place that passes extra parameters is nntp feed. Given that,
//perhaps we should drop all the parameters and give that its own function.
/** HTML string conversion
 *
 * @param {string} str - string to convert
 * @param {boolean} keep - keep < and > if set
 * @param {string} mimeTypeFrom - mime type of string (defaults to text/html)
 * @param {string} mimeTypeTo - mime type to convert to (defaults to
 *                 text/unicode
 *
 * @returns {string} converted string
 *
 */
function htmlFormatConvert(str, keep, mimeTypeFrom, mimeTypeTo)
{
  if (str == null)
  {
    return ""; //Seriously - this happens
  }

  let convertedString = null;

  //This is called from inforssNntp with keep false, converting from plain to
  //html. Arguably it should have its own method.
  if (keep == null)
  {
    keep = true;
  }
{'fraction_non_alphanumeric': 0.15527441521798488, 'fraction_numerical': 0.0050969327386911805, 'mean_word_length': 3.0116830960204455, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '522326', 'n_tokens_mistral': 3171, 'n_tokens_neox': 3056, 'n_words': 1132}