can-route-pushstate
Advanced tools
Comparing version 5.0.9 to 5.0.10
@@ -471,2 +471,46 @@ /* jshint asi:true,scripturl:true */ | ||
test('shouldCallPushState', function(){ | ||
var global = globals.getKeyValue('global'); | ||
var document = globals.getKeyValue('document'); | ||
var isNode = globals.getKeyValue('isNode'); | ||
route.register("{type}/{id}"); | ||
var a = document.createElement("a"); | ||
a.setAttribute("href", "/books/40"); | ||
var event = document.createEvent('HTMLEvents'); | ||
event.initEvent('click', true, true); | ||
globals.setKeyValue('isNode', true); | ||
QUnit.ok(route.urlData.shouldCallPushState(a, event), "Push state should be called"); | ||
globals.setKeyValue('global', global); | ||
globals.setKeyValue('document', document); | ||
globals.setKeyValue('isNode', isNode); | ||
}); | ||
test('shouldCallPushState on SVG', function(){ | ||
var global = globals.getKeyValue('global'); | ||
var document = globals.getKeyValue('document'); | ||
var isNode = globals.getKeyValue('isNode'); | ||
route.register("{type}/{id}"); | ||
var a = document.createElementNS("http://www.w3.org/1999/xlink", "a"); | ||
var event = document.createEvent('HTMLEvents'); | ||
a.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", "/books/40"); | ||
event.initEvent('click', true, true); | ||
globals.setKeyValue('isNode', true); | ||
QUnit.ok(route.urlData.shouldCallPushState(a, event), "Push state should be called"); | ||
globals.setKeyValue('global', global); | ||
globals.setKeyValue('document', document); | ||
globals.setKeyValue('isNode', isNode); | ||
}); | ||
} |
@@ -83,3 +83,6 @@ // # can-route-pushstate.js | ||
this.anchorClickHandler = function(event) { | ||
PushstateObservable.prototype.anchorClickHandler.call(this, this, event); | ||
var shouldCallPushState = PushstateObservable.prototype.shouldCallPushState.call(this, this, event); | ||
if (shouldCallPushState) { | ||
PushstateObservable.prototype.anchorClickHandler.call(this, this, event); | ||
} | ||
}; | ||
@@ -138,6 +141,5 @@ | ||
// ### anchorClickHandler | ||
// Handler function for `click` events. | ||
// Checks if a route is matched, if one is, calls `.pushState` | ||
anchorClickHandler: function(node, event) { | ||
// ### shouldCallPushState | ||
// Checks if a route is matched, if one is, returns true | ||
shouldCallPushState: function(node, event) { | ||
if (!(event.isDefaultPrevented ? event.isDefaultPrevented() : event.defaultPrevented === true)) { | ||
@@ -164,14 +166,22 @@ // If href has some JavaScript in it, let it run. | ||
if (window.location.host === linksHost) { | ||
var root = cleanRoot(); | ||
var root = cleanRoot(), | ||
pathname, | ||
href, | ||
nodePathWithSearch; | ||
if (node instanceof HTMLAnchorElement) { | ||
pathname = node.pathname; | ||
href = node.href; | ||
nodePathWithSearch = pathname + node.search; | ||
} else if (node.namespaceURI === "http://www.w3.org/1999/xlink") { | ||
pathname = href = node.getAttributeNS("http://www.w3.org/1999/xlink", "href"); | ||
nodePathWithSearch = href; | ||
} | ||
// If the link is within the `root`. | ||
if (node.pathname.indexOf(root) === 0) { | ||
if (pathname !== undefined && pathname.indexOf(root) === 0) { | ||
var url = nodePathWithSearch.substr(root.length); | ||
// Removes root from url. | ||
var nodePathWithSearch = node.pathname + node.search, | ||
url = nodePathWithSearch.substr(root.length); | ||
// If a matching route exists. | ||
if (route.rule(url) !== undefined) { | ||
// Makes it possible to have a link with a hash. | ||
@@ -181,3 +191,3 @@ // Calling .pushState will dispatch events, causing | ||
// the url without the hash. We need to retain that. | ||
if (node.href.indexOf("#") >= 0) { | ||
if (href.indexOf("#") >= 0) { | ||
this.keepHash = true; | ||
@@ -191,3 +201,2 @@ } | ||
// Test if you can preventDefault. | ||
@@ -197,6 +206,5 @@ if (shouldCallPreventDefault && event.preventDefault) { | ||
} | ||
// Update `window.location`. | ||
window.history.pushState(null, null, node.href); | ||
return true; | ||
} | ||
return false; | ||
} | ||
@@ -207,2 +215,10 @@ } | ||
// ### anchorClickHandler | ||
// Handler function for `click` events. | ||
anchorClickHandler: function(node, event) { | ||
var href = node.href ? node.href : node.getAttributeNS("http://www.w3.org/1999/xlink", "href"); | ||
// Update `window.location`. | ||
window.history.pushState(null, null, href); | ||
}, | ||
// ### onBound | ||
@@ -357,2 +373,2 @@ // Initalizes this._value. | ||
module.exports = PushstateObservable; | ||
module.exports = PushstateObservable; |
{ | ||
"name": "can-route-pushstate", | ||
"version": "5.0.9", | ||
"version": "5.0.10", | ||
"description": "Pushstate for can-route", | ||
@@ -52,7 +52,7 @@ "homepage": "https://canjs.com/doc/can-route-pushstate.html", | ||
"jshint": "^2.9.1", | ||
"steal": "^1.0.0", | ||
"steal": "^2.1.4", | ||
"steal-qunit": "^1.0.0", | ||
"steal-tools": "^1.0.0", | ||
"steal-tools": "^2.0.6", | ||
"testee": "^0.8.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
60262
1354