Comments (4)
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.
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.
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.
👍
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)
- Different results between 1.19.1 and 1.19.2 when using uri.scheme HOT 4
- What is the point of directory, suffix, etc? HOT 2
- Support for handling search as ES6 Map instead of Object properties
- setSearch() breaks previous url parameters
- Property for File Name without File Extension
- A reference resolution does not match RFC3986
- Test if URI/URL is templated
- URI.min.js 1.19.4 throws "does not have either property: href, src, action, cite" HOT 2
- [Feature request] Support for nested objects inside template builder.
- Hope to join the 'filenameNoExtension' api
- directory function is prepending not appending
- Append segments
- Trying to get in touch with you regarding a security issue HOT 1
- Handling of relative URLs that have a protocol in latest 1.19.7 HOT 1
- @types/urijs/index can only be default-imported using the 'allowSyntheticDefaultImports' flag HOT 1
- charset=Shift_JIS will fail to parse URI.js with a syntax error caused by non-ASCII characters in RegExp HOT 2
- Make invalid port check optional to support url templates
- Can we change separator?
- URI malformed exception is thrown when overriding path/pathname HOT 1
- Please do not omit the file extension for the value of the "main" field in the package.json
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uri.js.