Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip domain from <base href> in useBasename #139

Merged
merged 2 commits into from
Nov 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions modules/__tests__/describeBasename.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function describeBasename(createHistory) {
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)
expect(location.basename).toEqual('')

history.pushState({ the: 'state' }, '/home')
},
Expand All @@ -38,6 +39,7 @@ function describeBasename(createHistory) {
expect(location.search).toEqual('')
expect(location.state).toEqual({ the: 'state' })
expect(location.action).toEqual(PUSH)
expect(location.basename).toEqual('/base/url')
}
]

Expand All @@ -53,6 +55,7 @@ function describeBasename(createHistory) {
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)
expect(location.basename).toEqual('')

history.push('/home')
},
Expand All @@ -61,6 +64,7 @@ function describeBasename(createHistory) {
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(PUSH)
expect(location.basename).toEqual('/base/url')
}
]

Expand All @@ -76,6 +80,7 @@ function describeBasename(createHistory) {
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)
expect(location.basename).toEqual('')

history.replaceState({ the: 'state' }, '/home')
},
Expand All @@ -84,6 +89,7 @@ function describeBasename(createHistory) {
expect(location.search).toEqual('')
expect(location.state).toEqual({ the: 'state' })
expect(location.action).toEqual(REPLACE)
expect(location.basename).toEqual('/base/url')
}
]

Expand All @@ -99,6 +105,7 @@ function describeBasename(createHistory) {
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)
expect(location.basename).toEqual('')

history.replace('/home')
},
Expand All @@ -107,6 +114,7 @@ function describeBasename(createHistory) {
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(REPLACE)
expect(location.basename).toEqual('/base/url')
}
]

Expand All @@ -130,6 +138,71 @@ function describeBasename(createHistory) {
})
})
})

describe('basename through <base href>', () => {
let history, unlisten, base

before('add base element', () => {
base = document.createElement('base')
base.href = '/base/url'
document.head.appendChild(base)
})

beforeEach(() => {
history = useBasename(createHistory)()
})

describe('in createPath', () => {
it('works', function () {
expect(
history.createPath('/the/path')
).toEqual('/base/url/the/path')
})
})

describe('in createHref', () => {
it('works', function () {
expect(
stripHash(history.createHref('/the/path'))
).toEqual('/base/url/the/path')
})
})

describe('in pushState', () => {
it('works', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)
expect(location.basename).toEqual('')

history.pushState({ the: 'state' }, '/home')
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('')
expect(location.state).toEqual({ the: 'state' })
expect(location.action).toEqual(PUSH)
expect(location.basename).toEqual('/base/url')
}
]

unlisten = history.listen(execSteps(steps, done))
})
})

afterEach(() => {
if (unlisten)
unlisten()
})

after(() => {
document.head.removeChild(base)
})

})
}

export default describeBasename
18 changes: 18 additions & 0 deletions modules/extractPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import warning from 'warning'

function extractPath(string, fullUrl = false) {
const match = string.match(/^https?:\/\/[^\/]*/)

if (match == null)
return string

warning(
fullUrl,
'A path must be pathname + search + hash only, not a fully qualified URL like "%s"',
string
)

return string.substring(match[0].length)
}

export default extractPath
17 changes: 1 addition & 16 deletions modules/parsePath.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
import warning from 'warning'

function extractPath(string) {
const match = string.match(/^https?:\/\/[^\/]*/)

if (match == null)
return string

warning(
false,
'A path must be pathname + search + hash only, not a fully qualified URL like "%s"',
string
)

return string.substring(match[0].length)
}
import extractPath from './extractPath'

function parsePath(path) {
let pathname = extractPath(path)
Expand Down
7 changes: 5 additions & 2 deletions modules/useBasename.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { canUseDOM } from './ExecutionEnvironment'
import runTransitionHook from './runTransitionHook'
import parsePath from './parsePath'
import extractPath from './extractPath'

function useBasename(createHistory) {
return function (options={}) {
Expand All @@ -12,8 +13,10 @@ function useBasename(createHistory) {
if (basename == null && canUseDOM) {
let base = document.getElementsByTagName('base')[0]

if (base)
basename = base.href
if (base) {
// base.href always returns a full URL, thus we strip it
basename = extractPath(base.href, true)
}
}

function addBasename(location) {
Expand Down