question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

document URL parsing

See original GitHub issue

Hi, I did notice a miss behavior on URL parsing when using Jest (closed issue) which should be bug in or missing feature of jsdom.

I would be glad to help you further but I will need some, perhaps, some guidance.

From the issue I did open for Jest

What is the current behavior?

function parseURL (url) {
    var parser = document.createElement('a');

    parser.setAttribute('href', url);

    return {
        protocol: parser.protocol,
        username: parser.username,
        password: parser.password,
        host: parser.host,
        hostname: parser.hostname,
        port: parser.port,
        pathname: parser.pathname,
        search: parser.search,
        hash: parser.hash
    };
}
Object.defineProperty(window.location, 'href', {
    writable: true,
    value: 'http://github.com'
});

parseURL('//somehost.com');
// {
//     "hash": "", 
//     "host": "", 
//     "hostname": "", 
//     "password": "", 
//     "pathname": "", 
//     "port": "", 
//     "protocol": ":", 
//     "search": "", 
//     "username": ""
// }

This is the Firefox and Google Chrome behavior when the source code runs on a page whose document.location is about:blank what is not expected as the window.location.href property is defined by (Issue#890)

Object.defineProperty(window.location, 'href', {
    writable: true,
    value: 'http://github.com'
});

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

What is the expected behavior?

parseURL('//somehost.com');
// {
//     protocol: "https:",
//     username: "",
//     password: "",
//     host: "somehost.com",
//     hostname: "somehost.com",
//     port: "",
//     pathname: "/",
//     search: "",
//     hash: ""
// }

This is the Firefox and Google Chrome behavior when the source code run on a page whose document.location is other than about:blank.


This happened with jsdom v9.8.3 (as a Jest dependency).

Regards, Paulo

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
domeniccommented, Dec 28, 2016

No, we don’t have any intention of implementing a fake navigation that only changes the URL. The most important parts of navigation are things like replacing every object (Window, Document, etc.) and until we can implement that, it’s better to just output a not-implemented warning.

In the end it sounds like you have a problem not with jsdom, but with jest. jsdom already has a solution to your problem, with jsdom.changeURL. So I am going to close this issue.

0reactions
PauloASilvacommented, Dec 28, 2016

I do not agree that they are “an entirely separate piece of functionality” as when implementing the navigation part you will have to set the URL. I would say that right now it would be quite easy to make a partial implementation, just adding a setter with jsdom.changeURL() behavior.

What do you think?

I am checking with the jest guys whether or not the jsdom instance should be exposed on the global scope/environment (issue).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Parse URL in JavaScript: hostname, pathname, query ...
How to easily parse URL in JavaScript and access components like hostname, pathname, query, or hash.
Read more >
How do I parse a URL into hostname and path in javascript?
The modern way: new URL("http://example.com/aa/bb/"). Returns an object with properties hostname and pathname , along with a few others.
Read more >
urllib.parse — Parse URLs into components — Python 3.11.1 ...
The URL parsing functions focus on splitting a URL string into its components, or on combining URL components into a URL string. urllib.parse.urlparse(urlstring ......
Read more >
URL - Web APIs - MDN Web Docs - Mozilla
Chrome Edge URL Full support. Chrome32. more. Toggle history Full support. Edge12. Togg... URL() constructor Full support. Chrome19. Toggle history Full support. Edge12. footn... createObjectURL Full...
Read more >
How to parse URL using JavaScript ? - GeeksforGeeks
Method 1: In this method, we will use createElement() method to create a HTML element, anchor tag and then use it for parsing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found