Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

urlResolve() does not parse content:// URL's correctly in Chrome  #13091

Open
@antoinevg

Description

@antoinevg

When resolving the following URL in Chrome:

"content://io.trigger.forgeac179a2a726911e5bb021231392b77b0/tab/chats/3

the urlResolve() function will return:

{
    "href": "content://io.trigger.forgeac179a2a726911e5bb021231392b77b0/tab/chats/3", 
    "protocol": "content:", 
    "host": "", 
    "search": "", 
    "hash": "", 
    "hostname": "", 
    "port": "", 
    "pathname": "//io.trigger.forgeac179a2a726911e5bb021231392b77b0/tab/chats/3"
}

Calling urlResolve() from Safari or the NodeJS environment will return the far saner:

{
    "href": "content://io.trigger.forgeac179a2a726911e5bb021231392b77b0/tab/chats/3", 
    "protocol": "content", 
    "host": "io.trigger.forgeac179a2a726911e5bb021231392b77b0", 
    "search": "", 
    "hash": "", 
    "hostname": "io.trigger.forgeac179a2a726911e5bb021231392b77b0", 
    "port": "", 
    "pathname": "/tab/chats/3"
}

This, of course, ends up breaking navigation in Angular.

Investigation revealed that the problem is rooted in the way Chrome parses URL's assigned to DOM anchor nodes.

One way to fix this would be to explicitly check for content URL's that may not have been parsed correctly and normalise as follows:

function urlResolve(url) {
    var href = url;

    if (msie) {
        // Normalize before parse.  Refer Implementation Notes on why this is
        // done in two steps on IE.
        urlParsingNode.setAttribute('href', href);
        href = urlParsingNode.href;
    }

    urlParsingNode.setAttribute('href', href);

    // -- begin new code ----
    var normalizeContentURL = false;
    if (urlParsingNode.host.length === 0 && urlParsingNode.hostname.length === 0
        && urlParsingNode.protocol.indexOf("content") === 0) {
        // Normalize url and replace protocol as Chrome url parser does not parse
        // host, hostname & pathname correctly for content:// URL's
        normalizeContentURL = true;
        url = urlParsingNode.href;
        urlParsingNode.setAttribute('href', url.replace("content://", "http://"));
    }
    // -- end new code ----

    // urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
    var ret = {
        href: urlParsingNode.href,
        protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
        host: urlParsingNode.host,
        search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
        hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
        hostname: urlParsingNode.hostname,
        port: urlParsingNode.port,
        pathname: (urlParsingNode.pathname.charAt(0) === '/')
                ? urlParsingNode.pathname
                : '/' + urlParsingNode.pathname
    };

    // -- begin new code ----
    if (normalizeContentURL) {
        // restore protocol if necessary
        ret.protocol = "content";
        ret.href = url;
    }
    // -- end new code ----

    return ret;
}

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions