Skip to content

Commit

Permalink
Allow relative pathnames
Browse files Browse the repository at this point in the history
Fixes #135
  • Loading branch information
mjackson committed Sep 29, 2016
1 parent 264d558 commit 445ce3e
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 14 deletions.
12 changes: 11 additions & 1 deletion modules/LocationUtils.js
@@ -1,6 +1,7 @@
import resolvePathname from 'resolve-pathname'
import { parsePath } from './PathUtils'

export const createLocation = (path, state, key) => {
export const createLocation = (path, state, key, currentLocation) => {
let location
if (typeof path === 'string') {
// Two-arg form: push(path, state)
Expand Down Expand Up @@ -33,6 +34,15 @@ export const createLocation = (path, state, key) => {

location.key = key

if (currentLocation) {
// Resolve incomplete/relative pathname relative to current location.
if (!location.pathname) {
location.pathname = currentLocation.pathname
} else if (location.pathname.charAt(0) !== '/') {
location.pathname = resolvePathname(location.pathname, currentLocation.pathname)
}
}

return location
}

Expand Down
6 changes: 6 additions & 0 deletions modules/__tests__/BrowserHistory-test.js
Expand Up @@ -53,6 +53,12 @@ describeHistory('a browser history', () => {
})
})

describe('push with a relative pathname', () => {
it('calls change listeners with the normalized location', (done) => {
TestSequences.PushRelativePathname(history, done)
})
})

describe('replace a new path', () => {
it('calls change listeners with the new location', (done) => {
TestSequences.ReplaceNewLocation(history, done)
Expand Down
6 changes: 6 additions & 0 deletions modules/__tests__/HashHistory-test.js
Expand Up @@ -56,6 +56,12 @@ describeHistory('a hash history', () => {
})
})

describe('push with a relative pathname', () => {
it('calls change listeners with the normalized location', (done) => {
TestSequences.PushRelativePathname(history, done)
})
})

describe('replace a new path', () => {
it('calls change listeners with the new location', (done) => {
TestSequences.ReplaceNewLocation(history, done)
Expand Down
6 changes: 6 additions & 0 deletions modules/__tests__/MemoryHistory-test.js
Expand Up @@ -44,6 +44,12 @@ describe('a memory history', () => {
})
})

describe('push with a relative pathname', () => {
it('calls change listeners with the normalized location', (done) => {
TestSequences.PushRelativePathname(history, done)
})
})

describe('replace a new path', () => {
it('calls change listeners with the new location', (done) => {
TestSequences.ReplaceNewLocation(history, done)
Expand Down
17 changes: 12 additions & 5 deletions modules/__tests__/TestSequences/PushMissingPathname.js
Expand Up @@ -8,18 +8,25 @@ export default (history, done) => {
pathname: '/'
})

history.push({
search: 'the=query',
hash: 'the-hash'
})
history.push('/home?the=query#the-hash')
},
(location, action) => {
expect(action).toBe('PUSH')
expect(location).toMatch({
pathname: '',
pathname: '/home',
search: '?the=query',
hash: '#the-hash'
})

history.push('?another=query#another-hash')
},
(location, action) => {
expect(action).toBe('PUSH')
expect(location).toMatch({
pathname: '/home',
search: '?another=query',
hash: '#another-hash'
})
}
]

Expand Down
34 changes: 34 additions & 0 deletions modules/__tests__/TestSequences/PushRelativePathname.js
@@ -0,0 +1,34 @@
import expect from 'expect'
import execSteps from './execSteps'

export default (history, done) => {
const steps = [
(location) => {
expect(location).toMatch({
pathname: '/'
})

history.push('/the/path?the=query#the-hash')
},
(location, action) => {
expect(action).toBe('PUSH')
expect(location).toMatch({
pathname: '/the/path',
search: '?the=query',
hash: '#the-hash'
})

history.push('../other/path?another=query#another-hash')
},
(location, action) => {
expect(action).toBe('PUSH')
expect(location).toMatch({
pathname: '/other/path',
search: '?another=query',
hash: '#another-hash'
})
}
]

execSteps(steps, history, done)
}
1 change: 1 addition & 0 deletions modules/__tests__/TestSequences/index.js
Expand Up @@ -17,6 +17,7 @@ export PushSamePath from './PushSamePath'
export PushSamePathWarning from './PushSamePathWarning'
export PushState from './PushState'
export PushStateWarning from './PushStateWarning'
export PushRelativePathname from './PushRelativePathname'
export ReplaceNewLocation from './ReplaceNewLocation'
export ReplaceSamePath from './ReplaceSamePath'
export ReplaceState from './ReplaceState'
Expand Down
4 changes: 2 additions & 2 deletions modules/createBrowserHistory.js
Expand Up @@ -146,7 +146,7 @@ const createBrowserHistory = (props = {}) => {
)

const action = 'PUSH'
const location = createLocation(path, state, createKey())
const location = createLocation(path, state, createKey(), history.location)

transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
if (!ok)
Expand Down Expand Up @@ -188,7 +188,7 @@ const createBrowserHistory = (props = {}) => {
)

const action = 'REPLACE'
const location = createLocation(path, state, createKey())
const location = createLocation(path, state, createKey(), history.location)

transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
if (!ok)
Expand Down
4 changes: 2 additions & 2 deletions modules/createHashHistory.js
Expand Up @@ -173,7 +173,7 @@ const createHashHistory = (props = {}) => {
)

const action = 'PUSH'
const location = createLocation(path)
const location = createLocation(path, undefined, undefined, history.location)

transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
if (!ok)
Expand Down Expand Up @@ -215,7 +215,7 @@ const createHashHistory = (props = {}) => {
)

const action = 'REPLACE'
const location = createLocation(path)
const location = createLocation(path, undefined, undefined, history.location)

transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
if (!ok)
Expand Down
4 changes: 2 additions & 2 deletions modules/createMemoryHistory.js
Expand Up @@ -47,7 +47,7 @@ const createMemoryHistory = (props = {}) => {
)

const action = 'PUSH'
const location = createLocation(path, state, createKey())
const location = createLocation(path, state, createKey(), history.location)

transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
if (!ok)
Expand Down Expand Up @@ -80,7 +80,7 @@ const createMemoryHistory = (props = {}) => {
)

const action = 'REPLACE'
const location = createLocation(path, state, createKey())
const location = createLocation(path, state, createKey(), history.location)

transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
if (!ok)
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -18,8 +18,9 @@
},
"dependencies": {
"invariant": "^2.2.1",
"warning": "^3.0.0",
"loose-envify": "^1.2.0"
"loose-envify": "^1.2.0",
"resolve-pathname": "^2.0.0",
"warning": "^3.0.0"
},
"devDependencies": {
"babel-cli": "^6.10.1",
Expand Down

0 comments on commit 445ce3e

Please sign in to comment.