<commit_before>/* See license.txt for terms of usage */

define([
    "firebug/lib/trace",
    "firebug/lib/string",
],
function (FBTrace, Str) {

// ********************************************************************************************* //
// Constants

const Cc = Components.classes;
const Ci = Components.interfaces;

const ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);

// ********************************************************************************************* //
// Implementation

var Url = {};

// ************************************************************************************************
// Regular expressions

Url.reCSS = /\.css$/;
Url.reJavascript = /\s*javascript:\s*(.*)/;
Url.reFile = /file:\/\/([^\/]*)\//;
Url.reChrome = /chrome:\/\/([^\/]*)\//;
Url.reDataURL = /data:text\/javascript;fileName=([^;]*);baseLineNumber=(\d*?),((?:.*?%0A)|(?:.*))/g;

// ************************************************************************************************
// URLs

Url.getFileName = function(url)
{
    var split = Url.splitURLBase(url);
    return split.name;
};

Url.getProtocol = function(url)
{
    var split = Url.splitURLBase(url);
    return split.protocol;
};

Url.splitURLBase = function(url)
{
    if (Url.isDataURL(url))
        return Url.splitDataURL(url);
    return Url.splitURLTrue(url);
};

Url.splitDataURL = function(url)
{
    var mark = url.indexOf("data:");
    if (mark != 0)
        return false; //  the first 5 chars must be 'data:'

    var point = url.indexOf(",", 5);
    if (point < 5)
        return false; // syntax error

    var props = { protocol: "data", encodedContent: url.substr(point+1) };

    var metadataBuffer = url.substring(5, point);
    var metadata = metadataBuffer.split(";");
    for (var i = 0; i < metadata.length; i++)
    {
        var nv = metadata[i].split("=");
        if (nv.length == 2)
            props[nv[0]] = nv[1];
    }

    // Additional Firebug-specific properties
    if (props.hasOwnProperty("fileName"))
    {
         var caller_URL = decodeURIComponent(props["fileName"]);
         var caller_split = Url.splitURLTrue(caller_URL);

         props["fileName"] = caller_URL;

        if (props.hasOwnProperty("baseLineNumber"))  // this means it's probably an eval()
        {
            props["path"] = caller_split.path;
            props["line"] = props["baseLineNumber"];
            var hint = decodeURIComponent(props["encodedContent"]).substr(0,200).replace(/\s*$/, "");
            props["name"] =  "eval->"+hint;
        }
        else
        {
            props["name"] = caller_split.name;
            props["path"] = caller_split.path;
        }
    }
    else
    {
        if (!props.hasOwnProperty("path"))
            props["path"] = "data:";
        if (!props.hasOwnProperty("name"))
            props["name"] =  decodeURIComponent(props["encodedContent"]).substr(0,200).replace(/\s*$/, "");
    }

    return props;
};

const reSplitFile = /(.*?):\/{2,3}(.*?)([^\/]*?)($|\?.*)/;
Url.splitURLTrue = function(url)
{
    var m = reSplitFile.exec(url);
    if (!m)
        return {name: url, path: url};
    else
        return {protocol: m[1], path: m[2], name: m[3]+m[4]};
};

Url.getFileExtension = function(url)
{
    if (!url)
        return null;

    // Remove query string from the URL if any.
    var queryString = url.indexOf("?");
    if (queryString != -1)
        url = url.substr(0, queryString);

    // Now get the file extension.
    var lastDot = url.lastIndexOf(".");
    return url.substr(lastDot+1);
};

Url.isSystemURL = function(url)
{
    if (!url) return true;
    if (url.length == 0) return true;
    if (url[0] == "h") return false;
    if (url.substr(0, 9) == "resource:")
        return true;
    else if (url.substr(0, 16) == "chrome://firebug")
        return true;
    else if (url  == "XPCSafeJSObjectWrapper.cpp")
        return true;
    else if (url.substr(0, 6) == "about:")<commit_msg>[1.9][&1.8] Issue 4811 (No file name shown for requested URL paths) http://code.google.com/p/fbug/issues/detail?id=4811<commit_after>/* See license.txt for terms of usage */

define([
    "firebug/lib/trace",
    "firebug/lib/string",
],
function (FBTrace, Str) {

// ********************************************************************************************* //
// Constants

const Cc = Components.classes;
const Ci = Components.interfaces;

const ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);

// ********************************************************************************************* //
// Implementation

var Url = {};

// ************************************************************************************************
// Regular expressions

Url.reCSS = /\.css$/;
Url.reJavascript = /\s*javascript:\s*(.*)/;
Url.reFile = /file:\/\/([^\/]*)\//;
Url.reChrome = /chrome:\/\/([^\/]*)\//;
Url.reDataURL = /data:text\/javascript;fileName=([^;]*);baseLineNumber=(\d*?),((?:.*?%0A)|(?:.*))/g;

// ************************************************************************************************
// URLs

Url.getFileName = function(url)
{
    var split = Url.splitURLBase(url);
    return split.name;
};

Url.getProtocol = function(url)
{
    var split = Url.splitURLBase(url);
    return split.protocol;
};

Url.splitURLBase = function(url)
{
    if (Url.isDataURL(url))
        return Url.splitDataURL(url);
    return Url.splitURLTrue(url);
};

Url.splitDataURL = function(url)
{
    var mark = url.indexOf("data:");
    if (mark != 0)
        return false; //  the first 5 chars must be 'data:'

    var point = url.indexOf(",", 5);
    if (point < 5)
        return false; // syntax error

    var props = { protocol: "data", encodedContent: url.substr(point+1) };

    var metadataBuffer = url.substring(5, point);
    var metadata = metadataBuffer.split(";");
    for (var i = 0; i < metadata.length; i++)
    {
        var nv = metadata[i].split("=");
        if (nv.length == 2)
            props[nv[0]] = nv[1];
    }

    // Additional Firebug-specific properties
    if (props.hasOwnProperty("fileName"))
    {
         var caller_URL = decodeURIComponent(props["fileName"]);
         var caller_split = Url.splitURLTrue(caller_URL);

         props["fileName"] = caller_URL;

        if (props.hasOwnProperty("baseLineNumber"))  // this means it's probably an eval()
        {
            props["path"] = caller_split.path;
            props["line"] = props["baseLineNumber"];
            var hint = decodeURIComponent(props["encodedContent"]).substr(0,200).replace(/\s*$/, "");
            props["name"] =  "eval->"+hint;
        }
        else
        {
            props["name"] = caller_split.name;
            props["path"] = caller_split.path;
        }
    }
    else
    {
        if (!props.hasOwnProperty("path"))
            props["path"] = "data:";
        if (!props.hasOwnProperty("name"))
            props["name"] =  decodeURIComponent(props["encodedContent"]).substr(0,200).replace(/\s*$/, "");
    }

    return props;
};

const reSplitFile = /(.*?):\/{2,3}([^\/]*)(.*?)([^\/]*?)($|\?.*)/;
Url.splitURLTrue = function(url)
{
    var m = reSplitFile.exec(url);
    if (!m)
        return {name: url, path: url};
    else if (m[4] == "" && m[5] == "")
        return {protocol: m[1], domain: m[2], path: m[3], name: m[3] != "/" ? m[3] : m[2]};
    else
        return {protocol: m[1], domain: m[2], path: m[2]+m[3], name: m[4]+m[5]};
};

Url.getFileExtension = function(url)
{
    if (!url)
        return null;

    // Remove query string from the URL if any.
    var queryString = url.indexOf("?");
    if (queryString != -1)
        url = url.substr(0, queryString);

    // Now get the file extension.
    var lastDot = url.lastIndexOf(".");
    return url.substr(lastDot+1);
};

Url.isSystemURL = function(url)
{
    if (!url) return true;
    if (url.length == 0) return true;
    if (url[0] == "h") return false;
    if (url.substr(0, 9) == "resource:")
        return true;
    else if (url.substr(0, 16) == "chrome://firebug")
        return true;
    else if (url  == "XPCSafeJSObjectWrapper.cpp")
        return true;
    else if (url.substr(0, 6) == "about:")
{'fraction_non_alphanumeric': 0.2685658631604578, 'fraction_numerical': 0.011930849768687607, 'mean_word_length': 2.9249880554228382, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6184335', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2575, 'n_words': 545}