Git Product home page Git Product logo

js-url's Introduction

url

A simple light weight JavaScript url parser.

Sponsor

If you like this plugin please consider sponsoring.

Availability

Test

There are a number of files for testing in the test folder including QUnit.

Build

To build simply run:

> npm install
> npm run build

License

MIT licensed

Copyright (C) 2011-2020 Websanova http://www.websanova.com

js-url's People

Contributors

aripalo avatar bettiolo avatar chromeq avatar jamesrom avatar jneen avatar rmurphey avatar websanova avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-url's Issues

Issues parsing arguments that are arrays with non-numeric keys

There seems to be an issue parsing non-numeric keys in array arguments. Check out the following,

> JSON.stringify(url('?','?t[0]=x&t[1]=y'));
"{"t":["x","y"]}"
> JSON.stringify(url('?','?t[a]=x&t[b]=y'));
"{"t[a]":"x","t[b]":"y"}"

The expected behavior is,

> JSON.stringify(url('?','?t[0]=x&t[1]=y'));
"{"t":["x","y"]}"
> JSON.stringify(url('?','?t[a]=x&t[b]=y'));
"{"t":{"a":"x","b":"y"}}"

Various Issues

You might want to note that your parser is incompatible with Internet Explorer 7 and below. Alternatively you may want to replace arg[0] (line 48 and others) by arg.substring(0, 1) or something.

in Line 23 you're assuming that //hostname equals http://hostname. That is only true if the scheme of the document's resource the script is executed in is http. Should the page be served via https, this little line defeats the purpose of "scheme agnostic URLs".

According to RFC 3986 Section 3.3, this is a a valid path: "/foo/xy://bar" - but url("path", "/foo/xy://bar") === "//bar/" - which is wrong…

I have stopped reviewing the code on line 32 because I found it unbearable to read without "running every single expression in my head".

Missing field argument in the example

I believe that documentation example

url('?'); // {query1: 'test', silly: 'willy'}

is missing the field parameter which would be an array of length 3, but with 2 elements. Don't know how to denote that clearly, so I made an issue :)

Handle params after hash, format example: #details?id=1

I am doing development with a popular framework KendoUI which uses this format for Single Page Application (SPA) on mobile, eg:

theurl = "http://localhost:8080/index.html#details?id=1";

I really like this library, currently using: window.url( '#details?id', theurl); which returns 1.

It would be great to handle the case window.url( '?id', url); and return 1 in my example or if that conflicts with another case just add this example to the tests.js to save others time trying combinations to get this case working. Cheers!

Fails for country specific TLDs

$.url('host', 'http://www.google.com.br')
> "com.br"
$.url('sub', 'http://www.google.com.br')
> "www.google"
$.url('tld', 'http://www.google.com.br')
> "br"

Issues parsing multidimensional array arguments

Multidimensional arrays are not parsed correctly,

> JSON.stringify(url('?','?t[0][0]=x'));
"{"t[0]":["x"]}"
> JSON.stringify(url('?','?t[0][0]=x&t[1][0]=y'));
"{"t[0]":["x"],"t[1]":["y"]}"

The expected behavior is as follows,

> JSON.stringify(url('?','?t[0][0]=x'));
"{"t":[["x"]]}"
> JSON.stringify(url('?','?t[0][0]=x&t[1][0]=y'));
"{"t":[["x"],["y"]]}"

Error in IE8

You are using arg[0] to access character of a string.
This returned "" for me in IE8.
I have changed all arg[x] to arg.chatAt[x] and it worked well.

url.min.js BUG

there is a bug on your url.min.js
url('domain') not work (undefined)
the url.js works but url.min.js not work.

url('file') is undefined with url end with no extension

version: 2.0.2

case 1, end with no extension or forward slash:

var urlString = http://localhost/news/list
window.url('file', urlString): undefined        //should be "list"
window.url('filename', urlString): undefined    //should be "list"
window.url('-1', urlString): list

case 2, end with dot, no extension:

var urlString = http://localhost/news/list.
window.url('file', urlString): undefined        //should be "list."
window.url('filename', urlString): undefined    //should be "list"
window.url('-1', urlString): list.

case 3, end with forward slash:

var urlString = http://localhost/news/list/
window.url('file', urlString): undefined
window.url('filename', urlString): undefined
window.url('-1', urlString): list               //should be ""

case 4, regular:

var urlString = http://localhost/news/list.html
window.url('file', urlString): list.html
window.url('filename', urlString): list
window.url('-1', urlString): list.html

Decode querystring values

url('?q', 'http://www.google.com/?q=a+b') returns the raw result a+b, rather than the querystring unencoded a b. That means I have to decode the string--I can't think of any case I'd want the current behavior.

Domain parsing error

Found this issue:

When trying to parse the following url: https://facebook.com\@account.google.com/x

url('domain', 'https://facebook.com\@account.google.com/x') // outputs 'google.com'

This outputs google.com as the domain instead of facebook.com

Issues parsing array arguments?

Not sure if people consider this an issues, however, arrays are apparently padded with nulls for missing values,

> JSON.stringify(url('?','?t[0]=x&t[1]=y'));
"{"t":["x","y"]}"
> JSON.stringify(url('?','?t[0]=x&t[2]=y'));
"{"t":["x",null,"y"]}"

Maybe the better option would be to return objects for sparse arrays, like so,

> JSON.stringify(url('?','?t[0]=x&t[1]=y'));
"{"t":["x","y"]}"
> JSON.stringify(url('?','?t[0]=x&t[2]=y'));
"{"0":"x","2":"y"}"

double decoding resulting in incorrect params when query string contains any of URL-encoding characters

The library seems to be performing double decoding when user tries to access param or attr method.

How to reproduce:
I just used chrome console:

$.getScript('/js/purl.js')
var test='http://localhost:8080?id=test%255Ftest'; //where test%255Ftest is "test%5Ftest" encoded
purl(test, true).param('id'); //outputs test_test

The first decoding during parseURI phase produces correct param test%test but then it goes onto create the param list in following line

uri.param['query'] = parseString(uri.attr['query']);

parseString internally again calls decodeURI, %5F is URL encoded '_' so the final result is 'test_test'.

in summary

test%5Ftest -> encode -> test%255Ftest -> decode() Line 40 -> test%5Ftest -> decode Line 131 -> results in test_test.

Publish a bower package

It would be nice to have a bower package for js-url library.

Publishing in bower would make it easier to find and install js-url.js library.

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.