Coder Social home page Coder Social logo

Comments (4)

rodneyrehm avatar rodneyrehm commented on May 18, 2024

What you want to achieve is converting something that is parsed as a relative URI (because protocol is missing) to an absolute URI. The way you described is not the way to go about this, as modifying the "atomic component" protocol would also alter other components (hostname, port, path, …).

I would prefer something along the lines of

URI.absoluteUri = function(uri) {
  if (!uri.match(/^[a-z][a-z0-9-+-]*:\/\//i)) {
    uri = "http://" + uri;
  }

  return URI(uri);
};

URI.absoluteUri('ExAmPle.Org:80').toString();

I'm not sure if this needs to be a core function, though. Are you performing any other operations to sanitize user input?

from uri.js.

Jellyfrog avatar Jellyfrog commented on May 18, 2024

Maybe a rebuild() function, but at the same time this feels a bit weird too.
Users might get confused since _string looks OK (after protocol('http')) but _parts still look wrong,
This maybe should be stated in the docs?

I was thinking that I could use URI.js to sanitize/validate
What I want do is something like the following:

var uri = new URI('ExAmPle.Org:80/a/b/c.htm?a=b#foo');
if(uri.protocol() != "http" && uri.protocol() != "https") {
  uri.protocol('http');
}

uri.fragment('');
uri.normalize();

if(!uri.protocol() || !uri.hostname() || !uri.path()) {
  //Url could not be fixed / validated
  return false;
}

from uri.js.

rodneyrehm avatar rodneyrehm commented on May 18, 2024

While http:///some-path doesn't make sense, file:///some-path does. "foo.com" may be a directory, not necessarily a domain. So there can't be any protocol() magic. That means we can't plug this into any of the existing functions.

You can do a manual re-parse any time though:

var uri = new URI('ExAmPle.Org:80/a/b/c.htm?a=b#foo');
if (!uri.protocol()) {
  uri.protocol('http');
}

if (!uri.protocol().match(/^https?$/i)) {
  return false;
}

uri.fragment('');
// force a re-parse
uri.href(uri.href());
uri.normalize();

if(!uri.protocol() || !uri.hostname() || !uri.path()) {
  //Url could not be fixed / validated
  return false;
}

from uri.js.

Jellyfrog avatar Jellyfrog commented on May 18, 2024

👍
I went with a implementation like you suggested, must have missed href() yesterday when I looked in the docs.
Maybe there should be a part in the docs about "invalid URIs"?
Since (like I said in the first post) its a bit confusing that the _string is OK but _parts is wrong

from uri.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.