Add editorconfig, resolve differences

This includes newlines, tabs, among other things.
This commit is contained in:
impinball 2016-06-18 02:59:42 -04:00
parent 80d0a69dab
commit b4fb21475c
90 changed files with 1707 additions and 1701 deletions

View file

@ -5,16 +5,16 @@ var parseQueryString = require("../querystring/parse")
module.exports = function($window) {
var supportsPushState = typeof $window.history.pushState === "function" && $window.location.protocol !== "file:"
var prefix = "#!"
function setPrefix(value) {prefix = value}
function normalize(fragment) {
var data = $window.location[fragment].replace(/(?:%[a-f89][a-f0-9])+/gim, decodeURIComponent)
if (fragment === "pathname" && data[0] !== "/") data = "/" + data
return data
}
function parsePath(path, queryData, hashData) {
var queryIndex = path.indexOf("?")
var hashIndex = path.indexOf("#")
@ -50,13 +50,13 @@ module.exports = function($window) {
return data[token]
})
}
var query = buildQueryString(queryData)
if (query) path += "?" + query
var hash = buildQueryString(hashData)
if (hash) path += "#" + hash
if (supportsPushState) {
if (options && options.replace) $window.history.replaceState(null, null, prefix + path)
else $window.history.pushState(null, null, prefix + path)
@ -64,20 +64,20 @@ module.exports = function($window) {
}
else $window.location.href = prefix + path
}
function defineRoutes(routes, resolve, reject) {
if (supportsPushState) $window.onpopstate = resolveRoute
else if (prefix.charAt(0) === "#") $window.onhashchange = resolveRoute
resolveRoute()
function resolveRoute() {
var path = getPath()
var params = {}
var pathname = parsePath(path, params, params)
for (var route in routes) {
var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "\/?$")
if (matcher.test(pathname)) {
pathname.replace(matcher, function() {
var keys = route.match(/:[^\/]+/g) || []
@ -90,12 +90,12 @@ module.exports = function($window) {
return
}
}
reject(path, params)
}
return resolveRoute
}
function link(vnode) {
vnode.dom.setAttribute("href", prefix + vnode.attrs.href)
vnode.dom.onclick = function(e) {
@ -103,6 +103,6 @@ module.exports = function($window) {
setPath(vnode.attrs.href, undefined, undefined)
}
}
return {setPrefix: setPrefix, getPath: getPath, setPath: setPath, defineRoutes: defineRoutes, link: link}
}
}

View file

@ -15,7 +15,7 @@
<script src="test-defineRoutes.js"></script>
<script src="test-getPath.js"></script>
<script src="test-setPath.js"></script>
<script>require("../../ospec/ospec").run()</script>
</body>
</html>
</html>

View file

@ -8,7 +8,7 @@ o.spec("Router.defineRoutes", function() {
void ["#", "?", "", "#!", "?!", "/foo"].forEach(function(prefix) {
o.spec("using prefix `" + prefix + "`", function() {
var $window, router, onRouteChange, onFail
o.beforeEach(function() {
$window = pushStateMock()
router = new Router($window)
@ -16,195 +16,195 @@ o.spec("Router.defineRoutes", function() {
onRouteChange = o.spy()
onFail = o.spy()
})
o("resolves to route", function() {
$window.location.href = prefix + "/test"
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"])
o(onFail.callCount).equals(0)
})
o("resolves to route w/ escaped unicode", function() {
$window.location.href = prefix + "/%C3%B6?%C3%B6=%C3%B6#%C3%B6=%C3%B6"
router.defineRoutes({"/ö": {data: 2}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 2}, {"ö": "ö"}, "/ö?ö=ö#ö=ö", "/ö"])
o(onFail.callCount).equals(0)
})
o("resolves to route w/ unicode", function() {
$window.location.href = prefix + "/ö?ö=ö#ö=ö"
router.defineRoutes({"/ö": {data: 2}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 2}, {"ö": "ö"}, "/ö?ö=ö#ö=ö", "/ö"])
o(onFail.callCount).equals(0)
})
o("resolves to route on fallback mode", function() {
$window.location.href = "file://" + prefix + "/test"
router = new Router($window)
router.setPrefix(prefix)
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"])
o(onFail.callCount).equals(0)
})
o("handles parameterized route", function() {
$window.location.href = prefix + "/test/x"
router.defineRoutes({"/test/:a": {data: 1}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {a: "x"}, "/test/x", "/test/:a"])
o(onFail.callCount).equals(0)
})
o("handles multi-parameterized route", function() {
$window.location.href = prefix + "/test/x/y"
router.defineRoutes({"/test/:a/:b": {data: 1}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {a: "x", b: "y"}, "/test/x/y", "/test/:a/:b"])
o(onFail.callCount).equals(0)
})
o("handles rest parameterized route", function() {
$window.location.href = prefix + "/test/x/y"
router.defineRoutes({"/test/:a...": {data: 1}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {a: "x/y"}, "/test/x/y", "/test/:a..."])
o(onFail.callCount).equals(0)
})
o("handles route with search", function() {
$window.location.href = prefix + "/test?a=b&c=d"
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {a: "b", c: "d"}, "/test?a=b&c=d", "/test"])
o(onFail.callCount).equals(0)
})
o("handles route with hash", function() {
$window.location.href = prefix + "/test#a=b&c=d"
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {a: "b", c: "d"}, "/test#a=b&c=d", "/test"])
o(onFail.callCount).equals(0)
})
o("handles route with search and hash", function() {
$window.location.href = prefix + "/test?a=b#c=d"
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {a: "b", c: "d"}, "/test?a=b#c=d", "/test"])
o(onFail.callCount).equals(0)
})
o("calls reject", function() {
$window.location.href = prefix + "/test"
router.defineRoutes({"/other": {data: 1}}, onRouteChange, onFail)
o(onFail.callCount).equals(1)
o(onFail.args).deepEquals(["/test", {}])
})
o("calls reject w/ search and hash", function() {
$window.location.href = prefix + "/test?a=b#c=d"
router.defineRoutes({"/other": {data: 1}}, onRouteChange, onFail)
o(onFail.callCount).equals(1)
o(onFail.args).deepEquals(["/test?a=b#c=d", {a: "b", c: "d"}])
})
o("handles out of order routes", function() {
$window.location.href = prefix + "/z/y/x"
router.defineRoutes({"/z/y/x": {data: 1}, "/:a...": {data: 2}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/z/y/x", "/z/y/x"])
})
o("handles reverse out of order routes", function() {
$window.location.href = prefix + "/z/y/x"
router.defineRoutes({"/:a...": {data: 2}, "/z/y/x": {data: 1}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 2}, {a: "z/y/x"}, "/z/y/x", "/:a..."])
})
o("handles dynamically added out of order routes", function() {
var routes = {}
routes["/z/y/x"] = {data: 1}
routes["/:a..."] = {data: 2}
$window.location.href = prefix + "/z/y/x"
router.defineRoutes(routes, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/z/y/x", "/z/y/x"])
})
o("handles reversed dynamically added out of order routes", function() {
var routes = {}
routes["/:a..."] = {data: 2}
routes["/z/y/x"] = {data: 1}
$window.location.href = prefix + "/z/y/x"
router.defineRoutes(routes, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 2}, {a: "z/y/x"}, "/z/y/x", "/:a..."])
})
o("handles mixed out of order routes", function() {
var routes = {"/z/y/x": {data: 1}}
routes["/:a..."] = {data: 2}
$window.location.href = prefix + "/z/y/x"
router.defineRoutes(routes, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/z/y/x", "/z/y/x"])
})
o("handles reverse mixed out of order routes", function() {
var routes = {"/:a...": {data: 2}}
routes["/z/y/x"] = {data: 12}
$window.location.href = prefix + "/z/y/x"
router.defineRoutes(routes, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
o(onRouteChange.args).deepEquals([{data: 2}, {a: "z/y/x"}, "/z/y/x", "/:a..."])
})
o("handles non-ascii routes", function() {
$window.location.href = prefix + "/ö"
router.defineRoutes({"/ö": "aaa"}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
})
o("replays", function() {
$window.location.href = prefix + "/test"
var replay = router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
replay()
o(onRouteChange.callCount).equals(2)
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"])
o(onFail.callCount).equals(0)
})
})
})
})
})

View file

@ -8,7 +8,7 @@ o.spec("Router.getPath", function() {
void ["#", "?", "", "#!", "?!", '/foo'].forEach(function(prefix) {
o.spec("using prefix `" + prefix + "`", function() {
var $window, router, onRouteChange, onFail
o.beforeEach(function() {
$window = pushStateMock()
router = new Router($window)
@ -16,31 +16,31 @@ o.spec("Router.getPath", function() {
onRouteChange = o.spy()
onFail = o.spy()
})
o("gets route", function() {
$window.location.href = prefix + "/test"
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
o(router.getPath()).equals("/test")
})
o("gets route w/ params", function() {
$window.location.href = prefix + "/other/x/y/z?c=d#e=f"
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
})
o("gets route w/ escaped unicode", function() {
$window.location.href = prefix + "/%C3%B6?%C3%B6=%C3%B6#%C3%B6=%C3%B6"
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
o(router.getPath()).equals("/ö?ö=ö#ö=ö")
})
o("gets route w/ unicode", function() {
$window.location.href = prefix + "/ö?ö=ö#ö=ö"
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
o(router.getPath()).equals("/ö?ö=ö#ö=ö")
})
})
})
})
})

View file

@ -8,7 +8,7 @@ o.spec("Router.setPath", function() {
void ["#", "?", "", "#!", "?!", "/foo"].forEach(function(prefix) {
o.spec("using prefix `" + prefix + "`", function() {
var $window, router, onRouteChange, onFail
o.beforeEach(function() {
$window = pushStateMock()
router = new Router($window)
@ -16,38 +16,38 @@ o.spec("Router.setPath", function() {
onRouteChange = o.spy()
onFail = o.spy()
})
o("sets route via API", function() {
$window.location.href = prefix + "/test"
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
router.setPath("/other/x/y/z?c=d#e=f")
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
})
o("sets route w/ escaped unicode", function() {
$window.location.href = prefix + "/test"
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
router.setPath("/%C3%B6?%C3%B6=%C3%B6#%C3%B6=%C3%B6")
o(router.getPath()).equals("/ö?ö=ö#ö=ö")
})
o("sets route w/ unicode", function() {
$window.location.href = prefix + "/test"
router.defineRoutes({"/test": {data: 1}, "/ö/:a/:b...": {data: 2}}, onRouteChange, onFail)
router.setPath("/ö?ö=ö#ö=ö")
o(router.getPath()).equals("/ö?ö=ö#ö=ö")
})
o("sets route on fallback mode", function() {
$window.location.href = "file://" + prefix + "/test"
router = new Router($window)
router.setPrefix(prefix)
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
router.setPath("/other/x/y/z?c=d#e=f")
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
})
o("sets route via pushState/onpopstate", function() {
@ -55,14 +55,14 @@ o.spec("Router.setPath", function() {
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
$window.history.pushState(null, null, prefix + "/other/x/y/z?c=d#e=f")
$window.onpopstate()
o(router.getPath()).equals("/other/x/y/z?c=d#e=f")
})
o("sets parameterized route", function() {
$window.location.href = prefix + "/test"
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)
router.setPath("/other/:a/:b", {a: "x", b: "y/z", c: "d", e: "f"})
o(router.getPath()).equals("/other/x/y/z?c=d&e=f")
})
o("replace:true works", function() {
@ -70,7 +70,7 @@ o.spec("Router.setPath", function() {
router.defineRoutes({"/test": {data: 1}, "/other": {data: 2}}, onRouteChange, onFail)
router.setPath("/other", null, {replace: true})
$window.history.back()
o($window.location.href).equals("http://localhost/")
})
o("replace:false works", function() {
@ -85,4 +85,4 @@ o.spec("Router.setPath", function() {
})
})
})
})
})