Clarify pathname docs, follow spec with fragments (#2448)
* Clarify pathname docs, follow spec with fragments - Valid URLs must not contain a `#` within its fragment. https://github.com/MithrilJS/mithril.js/issues/2445 - Our docs were a little confusing and misleading - `m.pathname` isn't aware of URLs, just path names. - Removed the relevant extension to `m.parseQueryString` required to support the hash parsing extension. Now we just shave it off and ignore it. - Fix support for arbitrary prefixes, so prefixes like `?#` are handled correctly. - Add a bunch of tests to cover various areas of confusion and unusual edge cases. * Update with PR [skip ci]
This commit is contained in:
parent
9e9b89d900
commit
85bfd0f77d
15 changed files with 85 additions and 127 deletions
|
|
@ -9,16 +9,16 @@ module.exports = function(url) {
|
|||
var queryEnd = hashIndex < 0 ? url.length : hashIndex
|
||||
var pathEnd = queryIndex < 0 ? queryEnd : queryIndex
|
||||
var path = url.slice(0, pathEnd).replace(/\/{2,}/g, "/")
|
||||
var params = {}
|
||||
|
||||
if (!path) path = "/"
|
||||
else {
|
||||
if (path[0] !== "/") path = "/" + path
|
||||
if (path.length > 1 && path[path.length - 1] === "/") path = path.slice(0, -1)
|
||||
}
|
||||
// Note: these are reversed because `parseQueryString` appends parameters
|
||||
// only if they don't exist. Please don't flip them.
|
||||
if (queryIndex >= 0) parseQueryString(url.slice(queryIndex + 1, queryEnd), params)
|
||||
if (hashIndex >= 0) parseQueryString(url.slice(hashIndex + 1), params)
|
||||
return {path: path, params: params}
|
||||
return {
|
||||
path: path,
|
||||
params: queryIndex < 0
|
||||
? {}
|
||||
: parseQueryString(url.slice(queryIndex + 1, queryEnd)),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -198,18 +198,6 @@ o.spec("compileTemplate", function() {
|
|||
o(compileTemplate("/route/:id?bar=foo")(data)).equals(false)
|
||||
o(data.params).deepEquals({foo: "bar"})
|
||||
})
|
||||
o("checks hash params match", function() {
|
||||
var data = parsePathname("/route/1#foo=bar")
|
||||
o(compileTemplate("/route/:id#foo=bar")(data)).equals(true)
|
||||
o(data.params).deepEquals({id: "1", foo: "bar"})
|
||||
})
|
||||
o("checks hash params mismatch", function() {
|
||||
var data = parsePathname("/route/1#foo=bar")
|
||||
o(compileTemplate("/route/:id#foo=1")(data)).equals(false)
|
||||
o(data.params).deepEquals({foo: "bar"})
|
||||
o(compileTemplate("/route/:id#bar=foo")(data)).equals(false)
|
||||
o(data.params).deepEquals({foo: "bar"})
|
||||
})
|
||||
o("checks dot before dot", function() {
|
||||
var data = parsePathname("/file.test.png/edit")
|
||||
o(compileTemplate("/:file.:ext/edit")(data)).equals(true)
|
||||
|
|
|
|||
|
|
@ -18,18 +18,18 @@ o.spec("parsePathname", function() {
|
|||
params: {a: "b", c: "d"}
|
||||
})
|
||||
})
|
||||
o("parses hash at start", function() {
|
||||
o("ignores hash at start", function() {
|
||||
var data = parsePathname("#a=b&c=d")
|
||||
o(data).deepEquals({
|
||||
path: "/",
|
||||
params: {a: "b", c: "d"}
|
||||
params: {}
|
||||
})
|
||||
})
|
||||
o("parses query + hash at start", function() {
|
||||
o("parses query, ignores hash at start", function() {
|
||||
var data = parsePathname("?a=1&b=2#c=3&d=4")
|
||||
o(data).deepEquals({
|
||||
path: "/",
|
||||
params: {a: "1", b: "2", c: "3", d: "4"}
|
||||
params: {a: "1", b: "2"}
|
||||
})
|
||||
})
|
||||
o("parses root", function() {
|
||||
|
|
@ -46,18 +46,18 @@ o.spec("parsePathname", function() {
|
|||
params: {a: "b", c: "d"}
|
||||
})
|
||||
})
|
||||
o("parses root + hash at start", function() {
|
||||
o("parses root, ignores hash at start", function() {
|
||||
var data = parsePathname("/#a=b&c=d")
|
||||
o(data).deepEquals({
|
||||
path: "/",
|
||||
params: {a: "b", c: "d"}
|
||||
params: {}
|
||||
})
|
||||
})
|
||||
o("parses root + query + hash at start", function() {
|
||||
o("parses root + query, ignores hash at start", function() {
|
||||
var data = parsePathname("/?a=1&b=2#c=3&d=4")
|
||||
o(data).deepEquals({
|
||||
path: "/",
|
||||
params: {a: "1", b: "2", c: "3", d: "4"}
|
||||
params: {a: "1", b: "2"}
|
||||
})
|
||||
})
|
||||
o("parses route", function() {
|
||||
|
|
@ -102,25 +102,25 @@ o.spec("parsePathname", function() {
|
|||
params: {c: "3", d: "4"}
|
||||
})
|
||||
})
|
||||
o("parses route + query + hash", function() {
|
||||
o("parses route + query, ignores hash", function() {
|
||||
var data = parsePathname("/route/foo?a=1&b=2#c=3&d=4")
|
||||
o(data).deepEquals({
|
||||
path: "/route/foo",
|
||||
params: {a: "1", b: "2", c: "3", d: "4"}
|
||||
params: {a: "1", b: "2"}
|
||||
})
|
||||
})
|
||||
o("deduplicates same-named params in query + hash", function() {
|
||||
var data = parsePathname("/route/foo?a=1&b=2#a=3&c=4")
|
||||
o(data).deepEquals({
|
||||
path: "/route/foo",
|
||||
params: {a: "3", b: "2", c: "4"}
|
||||
})
|
||||
})
|
||||
o("parses route + query + hash with lots of junk slashes", function() {
|
||||
o("parses route + query, ignores hash with lots of junk slashes", function() {
|
||||
var data = parsePathname("//route/////foo//?a=1&b=2#c=3&d=4")
|
||||
o(data).deepEquals({
|
||||
path: "/route/foo",
|
||||
params: {a: "1", b: "2", c: "3", d: "4"}
|
||||
params: {a: "1", b: "2"}
|
||||
})
|
||||
})
|
||||
o("doesn't comprehend protocols", function() {
|
||||
var data = parsePathname("https://example.com/foo/bar")
|
||||
o(data).deepEquals({
|
||||
path: "/https:/example.com/foo/bar",
|
||||
params: {}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue