Skip to content

Commit

Permalink
Fix IE href capturing (yoshuawuyts#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Jun 26, 2017
1 parent 21b984f commit 7544e84
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
11 changes: 10 additions & 1 deletion href.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ function href (cb, root) {
if (!node || node === root) return
if (node.localName !== 'a') return traverse(node.parentNode)
if (node.href === undefined) return traverse(node.parentNode)
if (window.location.host !== node.host) return traverse(node.parentNode)
if (!sameOrigin(node.href)) return
return node
})(e.target)

console.log('node', node)

if (!node) return

var isRoutingDisabled = node.hasAttribute(noRoutingAttrName)
Expand All @@ -39,3 +41,10 @@ function href (cb, root) {
})
}
}

function sameOrigin (href) {
var location = window.location
var origin = location.protocol + '//' + location.hostname
if (location.port) origin += ':' + location.port
return (href && (href.indexOf(origin) === 0))
}
34 changes: 31 additions & 3 deletions test/href.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ const window = require('global/window') // will be empty object shared with href
const sinon = require('sinon')

window.history = { pushState: sinon.spy() }
window.location = {}
window.location = {
'href': 'https://example.com/',
'origin': 'https://example.com',
'protocol': 'https:',
'host': 'example.com',
'hostname': 'example.com',
'port': '',
'pathname': '/',
'search': '',
'hash': ''
}

const goodLink = { localName: 'a', href: 'someUrl#', pathname: 'someUrl', hasAttribute: () => {} }
const goodLink = { localName: 'a', href: 'https://example.com/someUrl#', pathname: 'someUrl', hasAttribute: () => {} }

const nonCatchEvents = {
'non-links': {
Expand Down Expand Up @@ -55,7 +65,7 @@ tape('href', (t) => {
t.equal(event.preventDefault.callCount, 1)
t.equal(cb.callCount, 1)
t.deepEqual(cb.lastCall.args[0], {
hash: undefined, href: 'someUrl#', pathname: 'someUrl', search: {}
hash: undefined, href: 'https://example.com/someUrl#', pathname: 'someUrl', search: {}
})
})

Expand Down Expand Up @@ -99,4 +109,22 @@ tape('href', (t) => {
t.equal(cb.callCount, 0)
t.equal(window.history.pushState.callCount, previousPushCount)
})

t.test('should avoid other host', (t) => {
t.plan(3)
const event = {
// href is a different origin because it is another protocol
target: { localName: 'a', href: 'http://example.com', hasAttribute: () => {} },
preventDefault: sinon.spy()
}
const previousPushCount = window.history.pushState.callCount
const cb = sinon.spy()
const root = event.target.parentNode
href(cb, root)
window.onclick(event)

t.equal(event.preventDefault.callCount, 0)
t.equal(cb.callCount, 0)
t.equal(window.history.pushState.callCount, previousPushCount)
})
})

0 comments on commit 7544e84

Please sign in to comment.