All files / framework/preferences/js URLUtilities.js

90.36% Statements 75/83
66.67% Branches 22/33
91.67% Functions 11/12
91.25% Lines 73/80
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                        14x   14x         14x   14x 12x     14x 36x 36x 36x 36x 36x 36x         36x 36x     14x 24x 24x 24x   24x             14x 12x 12x     12x 12x   12x 30x   12x 12x   12x     14x                     14x 12x 12x 213x 36x   177x 9x 9x 9x     12x             14x 42x 42x 42x 42x   588x   42x 42x 42x     42x     14x                         14x 24x 24x 24x     14x 18x 18x     14x 18x 6x   12x 12x 12x 12x 12x 12x 12x 111x   12x 12x 12x 12x        
/*
Copyright 2009-2010 University of Cambridge
Copyright 2011 Lucendo Development Ltd.
 
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
 
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
 
var fluid_3_0_0 = fluid_3_0_0 || {};
 
(function ($, fluid) {
    "use strict";
 
    /** URL utilities salvaged from kettle - these should go into core framework **/
 
    fluid.registerNamespace("fluid.url");
 
    fluid.url.generateDepth = function (depth) {
        return fluid.generate(depth, "../").join("");
    };
 
    fluid.url.parsePathInfo = function (pathInfo) {
        var togo = {};
        var segs = pathInfo.split("/");
        Eif (segs.length > 0) {
            var top = segs.length - 1;
            var dotpos = segs[top].indexOf(".");
            Iif (dotpos !== -1) {
                togo.extension = segs[top].substring(dotpos + 1);
                segs[top] = segs[top].substring(0, dotpos);
            }
        }
        togo.pathInfo = segs;
        return togo;
    };
 
    fluid.url.parsePathInfoTrim = function (pathInfo) {
        var togo = fluid.url.parsePathInfo(pathInfo);
        Eif (togo.pathInfo[togo.pathInfo.length - 1] === "") {
            togo.pathInfo.length--;
        }
        return togo;
    };
 
    /* Collapse the array of segments into a URL path, starting at the specified
     * segment index - this will not terminate with a slash, unless the final segment
     * is the empty string
     */
    fluid.url.collapseSegs = function (segs, from, to) {
        var togo = "";
        Iif (from === undefined) {
            from = 0;
        }
        Eif (to === undefined) {
            to = segs.length;
        }
        for (var i = from; i < to - 1; ++i) {
            togo += segs[i] + "/";
        }
        Eif (to > from) { // TODO: bug in Kettle version
            togo += segs[to - 1];
        }
        return togo;
    };
 
    fluid.url.makeRelPath = function (parsed, index) {
        var togo = fluid.kettle.collapseSegs(parsed.pathInfo, index);
        if (parsed.extension) {
            togo += "." + parsed.extension;
        }
        return togo;
    };
 
    /* Canonicalise IN PLACE the supplied segment array derived from parsing a
     * pathInfo structure. Warning, this destructively modifies the argument.
     */
    fluid.url.cononocolosePath = function (pathInfo) {
        var consume = 0;
        for (var i = 0; i < pathInfo.length; ++i) {
            if (pathInfo[i] === "..") {
                ++consume;
            }
            else if (consume !== 0) {
                pathInfo.splice(i - consume * 2, consume * 2);
                i -= consume * 2;
                consume = 0;
            }
        }
        return pathInfo;
    };
 
    // parseUri 1.2.2
    // (c) Steven Levithan <stevenlevithan.com>
    // MIT License
 
    fluid.url.parseUri = function (str) {
        var o  = fluid.url.parseUri.options,
            m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
            uri = {},
            i   = 14;
 
        while (i--) { uri[o.key[i]] = m[i] || ""; }
 
        uri[o.q.name] = {};
        uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
            Iif ($1) { uri[o.q.name][$1] = $2; }
        });
 
        return uri;
    };
 
    fluid.url.parseUri.options = {
        strictMode: true,
        key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
        q:   {
            name:   "queryKey",
            parser: /(?:^|&)([^&=]*)=?([^&]*)/g
        },
        parser: {
            strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
            loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
        }
    };
 
    fluid.url.parseSegs = function (url) {
        var parsed = fluid.url.parseUri(url);
        var parsedSegs = fluid.url.parsePathInfoTrim(parsed.directory);
        return parsedSegs.pathInfo;
    };
 
    fluid.url.isAbsoluteUrl = function (url) {
        var parseRel = fluid.url.parseUri(url);
        return (parseRel.host || parseRel.protocol || parseRel.directory.charAt(0) === "/");
    };
 
    fluid.url.computeRelativePrefix = function (outerLocation, iframeLocation, relPath) {
        if (fluid.url.isAbsoluteUrl(relPath)) {
            return relPath;
        }
        var relSegs = fluid.url.parsePathInfo(relPath).pathInfo;
        var parsedOuter = fluid.url.parseSegs(outerLocation);
        var parsedRel = parsedOuter.concat(relSegs);
        fluid.url.cononocolosePath(parsedRel);
        var parsedInner = fluid.url.parseSegs(iframeLocation);
        var seg = 0;
        for (; seg < parsedRel.length; ++seg) {
            if (parsedRel[seg] !== parsedInner[seg]) { break; }
        }
        var excess = parsedInner.length - seg;
        var back = fluid.url.generateDepth(excess);
        var front = fluid.url.collapseSegs(parsedRel, seg);
        return back + front;
    };
 
})(jQuery, fluid_3_0_0);