local-links
Advanced tools
Comparing version 1.4.0 to 1.4.1
@@ -1,30 +0,37 @@ | ||
function isHTMLElement(obj) { | ||
return obj && (typeof obj === 'object') && | ||
(obj.nodeType === 1) && (typeof obj.style === 'object') && | ||
(typeof obj.ownerDocument ==='object'); | ||
function isHTMLElement (obj) { | ||
return obj && | ||
(typeof obj === 'object') && | ||
(obj.nodeType === 1) && | ||
(typeof obj.style === 'object') && | ||
(typeof obj.ownerDocument === 'object') | ||
} | ||
function isA(obj) { | ||
return isHTMLElement(obj) && obj.tagName === 'A'; | ||
function isA (obj) { | ||
return isHTMLElement(obj) && obj.tagName === 'A' | ||
} | ||
function closestA(checkNode) { | ||
do { | ||
if (isA(checkNode)) { | ||
return checkNode; | ||
} | ||
} while ((checkNode = checkNode.parentNode)); | ||
function closestA (checkNode) { | ||
do { | ||
if (isA(checkNode)) { | ||
return checkNode | ||
} | ||
} while ((checkNode = checkNode.parentNode)) | ||
} | ||
function normalizeLeadingSlash(pathname) { | ||
if (pathname.charAt(0) !== '/') { | ||
pathname = '/' + pathname; | ||
} | ||
return pathname; | ||
function normalizeLeadingSlash (pathname) { | ||
if (pathname.charAt(0) !== '/') { | ||
pathname = '/' + pathname | ||
} | ||
return pathname | ||
} | ||
function isSecondaryButton(event) { | ||
return (typeof event === 'object') && ('button' in event) && event.button !== 0; | ||
function isRelativeUrl (href) { | ||
var r = /^https?:\/\/|^\/\//i | ||
return !r.test(href) | ||
} | ||
function isSecondaryButton (event) { | ||
return (typeof event === 'object') && ('button' in event) && event.button !== 0 | ||
} | ||
// [1] http://blogs.msdn.com/b/ieinternals/archive/2011/02/28/internet-explorer-window-location-pathname-missing-slash-and-host-has-port.aspx | ||
@@ -36,141 +43,148 @@ // [2] https://github.com/substack/catch-links/blob/7aee219cdc2c845c78caad6070886a9380b90e4c/index.js#L13-L17 | ||
function isLocal(event, anchor, lookForHash) { | ||
event || (event = {}); | ||
function isLocal (event, anchor, lookForHash) { | ||
event || (event = {}) | ||
// Skip modifier events | ||
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) { | ||
return null; | ||
} | ||
// Skip modifier events | ||
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) { | ||
return null | ||
} | ||
// Skip non-primary clicks | ||
if (isSecondaryButton(event)) { | ||
return null; | ||
} | ||
// Skip non-primary clicks | ||
if (isSecondaryButton(event)) { | ||
return null | ||
} | ||
// If we have an anchor but its not an A tag | ||
// try to find the closest one | ||
if (anchor && !isA(anchor)) { | ||
anchor = closestA(anchor); | ||
} | ||
// If we have an anchor but its not an A tag | ||
// try to find the closest one | ||
if (anchor && !isA(anchor)) { | ||
anchor = closestA(anchor) | ||
} | ||
// Only test anchor elements | ||
if (!anchor || !isA(anchor)) { | ||
return null; | ||
} | ||
// Only test anchor elements | ||
if (!anchor || !isA(anchor)) { | ||
return null | ||
} | ||
// Dont test anchors with target=_blank | ||
if (anchor.target === '_blank') { | ||
return null; | ||
} | ||
// Dont test anchors with target=_blank | ||
if (anchor.target === '_blank') { | ||
return null | ||
} | ||
// IE9 doesn't put a leading slash on anchor.pathname [1] | ||
var aPathname = normalizeLeadingSlash(anchor.pathname); | ||
var wPathname = normalizeLeadingSlash(window.location.pathname); | ||
var aHost = anchor.host; | ||
var aPort = anchor.port; | ||
var wHost = window.location.host; | ||
var wPort = window.location.port; | ||
// IE9 doesn't put a leading slash on anchor.pathname [1] | ||
var aPathname = normalizeLeadingSlash(anchor.pathname) | ||
var wPathname = normalizeLeadingSlash(window.location.pathname) | ||
var aHost = anchor.host | ||
var aPort = anchor.port | ||
var wHost = window.location.host | ||
var wPort = window.location.port | ||
// Some browsers (Chrome 36) return an empty string for anchor.hash | ||
// even when href="#", so we also check the href | ||
var aHash = anchor.hash || (anchor.href.indexOf('#') > -1 ? '#' + anchor.href.split('#')[1] : null); | ||
var inPageHash; | ||
// In some cases, IE will have a blank host property when the href | ||
// is a relative URL. We can check for relativeness via the achor's | ||
// href attribute and then set the anchor's host to the window's host. | ||
if (aHost === '' && | ||
'attributes' in anchor && | ||
'href' in anchor.attributes && | ||
'value' in anchor.attributes.href && | ||
isRelativeUrl(anchor.attributes.href.value)) { | ||
aHost = wHost | ||
} | ||
// Window has no port, but anchor has the default port | ||
if (!wPort && aPort && (aPort === '80' || aPort === '443')) { | ||
// IE9 sometimes includes the default port (80 or 443) on anchor.host | ||
// so we append the default port to the window host in this case | ||
// so they will match for the host equality check [1] | ||
wHost += ':' + aPort; | ||
aHost += aHost.indexOf(aPort, aHost.length - aPort.length) === -1 ? ':' + aPort : ''; // [3] | ||
} | ||
// Some browsers (Chrome 36) return an empty string for anchor.hash | ||
// even when href="#", so we also check the href | ||
var aHash = anchor.hash || (anchor.href.indexOf('#') > -1 ? '#' + anchor.href.split('#')[1] : null) | ||
var inPageHash | ||
// Hosts are the same, its a local link | ||
if (aHost === wHost) { | ||
// Window has no port, but anchor has the default port | ||
if (!wPort && aPort && (aPort === '80' || aPort === '443')) { | ||
// IE9 sometimes includes the default port (80 or 443) on anchor.host | ||
// so we append the default port to the window host in this case | ||
// so they will match for the host equality check [1] | ||
wHost += ':' + aPort | ||
aHost += aHost.indexOf(aPort, aHost.length - aPort.length) === -1 ? ':' + aPort : '' // [3] | ||
} | ||
// If everything else is the same | ||
// and hash exists, then it is an in-page hash [2] | ||
inPageHash = | ||
aPathname === wPathname && | ||
anchor.search === window.location.search && | ||
aHash; | ||
// Hosts are the same, its a local link | ||
if (aHost === wHost) { | ||
// If everything else is the same | ||
// and hash exists, then it is an in-page hash [2] | ||
inPageHash = | ||
aPathname === wPathname && | ||
anchor.search === window.location.search && | ||
aHash | ||
if (lookForHash === true) { | ||
// If we are looking for the hash then this will | ||
// only return a truthy value if the link | ||
// is an *in-page* hash link | ||
return inPageHash; | ||
} else { | ||
// If this is an in page hash link | ||
// then ignore it because we werent looking for hash links | ||
return inPageHash ? | ||
null : | ||
aPathname + (anchor.search || '') + (aHash || ''); | ||
} | ||
if (lookForHash === true) { | ||
// If we are looking for the hash then this will | ||
// only return a truthy value if the link | ||
// is an *in-page* hash link | ||
return inPageHash | ||
} else { | ||
// If this is an in page hash link | ||
// then ignore it because we werent looking for hash links | ||
return inPageHash | ||
? null | ||
: aPathname + (anchor.search || '') + (aHash || '') | ||
} | ||
} | ||
return null; | ||
return null | ||
} | ||
// Take two arguments and return an ordered array of [event, anchor] | ||
function getEventAndAnchor(arg1, arg2) { | ||
var ev = null; | ||
var anchor = null; | ||
function getEventAndAnchor (arg1, arg2) { | ||
var ev = null | ||
var anchor = null | ||
if (arguments.length === 2) { | ||
// Two arguments will come in this order | ||
if (arguments.length === 2) { | ||
ev = arg1; | ||
anchor = arg2; | ||
} | ||
ev = arg1 | ||
anchor = arg2 | ||
} else if (isHTMLElement(arg1)) { | ||
// If our first arg is an element | ||
// then use that as our anchor | ||
else if (isHTMLElement(arg1)) { | ||
anchor = arg1; | ||
} | ||
anchor = arg1 | ||
} else { | ||
// Otherwise our argument is an event | ||
else { | ||
ev = arg1; | ||
} | ||
ev = arg1 | ||
} | ||
// If there is no anchor, but we have an event | ||
// then use event.target | ||
if (!anchor && ev && ev.target) { | ||
anchor = ev.target; | ||
} | ||
if (!anchor && ev && ev.target) { | ||
anchor = ev.target | ||
} | ||
// Return an array so that it can be used with Function.apply | ||
return [ev, anchor]; | ||
return [ev, anchor] | ||
} | ||
// Functions to be used in exports. Defined here for alias purposes | ||
function pathname() { | ||
return isLocal.apply(null, getEventAndAnchor.apply(null, arguments)); | ||
function pathname () { | ||
return isLocal.apply(null, getEventAndAnchor.apply(null, arguments)) | ||
} | ||
function hash() { | ||
return isLocal.apply(null, getEventAndAnchor.apply(null, arguments).concat(true)); | ||
function hash () { | ||
return isLocal.apply(null, getEventAndAnchor.apply(null, arguments).concat(true)) | ||
} | ||
function active() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var last = args[args.length - 1]; | ||
var checkPath = window.location.pathname; | ||
function active () { | ||
var args = Array.prototype.slice.call(arguments) | ||
var last = args[args.length - 1] | ||
var checkPath = window.location.pathname | ||
if (typeof last === 'string') { | ||
checkPath = last; | ||
args = args.slice(0, -1); | ||
} | ||
if (typeof last === 'string') { | ||
checkPath = last | ||
args = args.slice(0, -1) | ||
} | ||
return pathname.apply(null, args) === normalizeLeadingSlash(checkPath); | ||
return pathname.apply(null, args) === normalizeLeadingSlash(checkPath) | ||
} | ||
module.exports = { | ||
isLocal: isLocal, | ||
pathname: pathname, | ||
getLocalPathname: pathname, | ||
hash: hash, | ||
getLocalHash: hash, | ||
active: active, | ||
isActive: active | ||
}; | ||
isLocal: isLocal, | ||
pathname: pathname, | ||
getLocalPathname: pathname, | ||
hash: hash, | ||
getLocalHash: hash, | ||
active: active, | ||
isActive: active | ||
} |
{ | ||
"name": "local-links", | ||
"description": "Determine cross-browser if an event or anchor element should be handled locally.", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"author": "Luke Karrys <luke@lukekarrys.com>", | ||
@@ -10,22 +10,28 @@ "bugs": { | ||
"devDependencies": { | ||
"browserify": "^5.10.0", | ||
"domready": "0.3.0", | ||
"jshint": "^2.5.3", | ||
"lodash.partial": "^3.0.0", | ||
"phantomjs": "^1.9.10", | ||
"precommit-hook": "^1.0.7", | ||
"run-browser": "^1.3.1", | ||
"tap-spec": "^0.2.0", | ||
"tape": "^2.14.0", | ||
"tape-run": "^0.3.0", | ||
"zuul": "^1.17.1" | ||
"browserify": "^13.1.0", | ||
"electron-prebuilt": "^1.3.2", | ||
"git-validate": "^2.1.4", | ||
"jquery": "^3.1.0", | ||
"lodash.partial": "^4.2.0", | ||
"run-browser": "^2.0.2", | ||
"standard": "^8.0.0-beta.3", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.6.0", | ||
"tape-run": "^2.1.4", | ||
"zuul": "^3.10.3", | ||
"zuul-ngrok": "^4.0.0" | ||
}, | ||
"homepage": "https://github.com/lukekarrys/local-links", | ||
"keywords": [ | ||
"local", | ||
"IE", | ||
"links", | ||
"IE" | ||
"local" | ||
], | ||
"license": "MIT", | ||
"main": "local-links.js", | ||
"pre-commit": [ | ||
"lint", | ||
"test", | ||
"validate" | ||
], | ||
"repository": { | ||
@@ -36,6 +42,8 @@ "type": "git", | ||
"scripts": { | ||
"lint": "standard", | ||
"start": "run-browser test/index.js", | ||
"start-80": "run-browser test/index.js --port 80", | ||
"test": "browserify test/index.js | tape-run -b phantom | tap-spec", | ||
"test": "browserify test/index.js | tape-run -b electron | tap-spec", | ||
"test-travis": "npm test && npm run zuul", | ||
"validate": "npm ls", | ||
"zuul": "zuul --ui tape -- test/index.js", | ||
@@ -42,0 +50,0 @@ "zuul-local": "zuul --local 8080 --ui tape -- test/index.js" |
@@ -7,5 +7,3 @@ local-links | ||
[![NPM](https://nodei.co/npm/local-links.png)](https://nodei.co/npm/local-links/) | ||
[![Build Status](https://travis-ci.org/lukekarrys/local-links.png?branch=master)](https://travis-ci.org/lukekarrys/local-links) | ||
[![Sauce Test Status](https://saucelabs.com/browser-matrix/lukekarrys-loclinks.svg)](https://saucelabs.com/u/lukekarrys-loclinks) | ||
@@ -12,0 +10,0 @@ |
@@ -1,276 +0,277 @@ | ||
var test = require('tape'); | ||
var localLinks = require('../local-links'); | ||
var domready = require('domready'); | ||
var partial = require('lodash.partial'); | ||
var test = require('tape') | ||
var localLinks = require('../local-links') | ||
var jq = require('jquery') | ||
var partial = require('lodash.partial') | ||
function $(id) { | ||
return document.getElementById(id); | ||
function $ (id) { | ||
return document.getElementById(id) | ||
} | ||
function e(id) { | ||
return {target: $(id)}; | ||
function e (id) { | ||
return {target: $(id)} | ||
} | ||
function setup() { | ||
var container = document.createElement('div'); | ||
container.id = 'container'; | ||
container.innerHTML = [ | ||
'<a id="local" href="/local/page/1">Local</a>', | ||
'<a id="local2" href="/local/page/1">Local2</a>', | ||
'<a id="local3" href="/local/page/1">Local3</a>', | ||
'<a id="local-search" href="/local/page/1?param=2">Search</a>', | ||
'<a id="relative" href="page-2">Relative</a>', | ||
'<a id="global" href="http://google.com/page/number/1">Global</a>', | ||
'<a href="/local/page/1"><span id="local-nested">Nested</span></a>', | ||
'<a id="empty-in-page-hash" href="#">Empty Hash</a>', | ||
'<a id="in-page-hash" href="#modal">Hash</a>', | ||
'<a id="out-of-page-hash" href="/local/page/1#two">Out of Page hash</a>', | ||
'<a id="global-hash" href="http://google.com/#hash">Global Hash</a>', | ||
'<a id="active" href="' + window.location.pathname + '"">Active</a>', | ||
'<span id="no-anchor">No anchor</span>', | ||
'<a id="local-blank" href="/local/page/1" target="_blank">Local Blank</a>', | ||
'<a id="local-blank-hash" href="#modal2" target="_blank">Local Blank Hash</a>' | ||
].join(''); | ||
document.body.appendChild(container); | ||
function setup () { | ||
var container = document.createElement('div') | ||
container.id = 'container' | ||
container.innerHTML = [ | ||
'<a id="local" href="/local/page/1">Local</a>', | ||
'<a id="local2" href="/local/page/1">Local2</a>', | ||
'<a id="local3" href="/local/page/1">Local3</a>', | ||
'<a id="local-search" href="/local/page/1?param=2">Search</a>', | ||
'<a id="relative" href="page-2">Relative</a>', | ||
'<a id="global" href="http://google.com/page/number/1">Global</a>', | ||
'<a href="/local/page/1"><span id="local-nested">Nested</span></a>', | ||
'<a id="empty-in-page-hash" href="#">Empty Hash</a>', | ||
'<a id="in-page-hash" href="#modal">Hash</a>', | ||
'<a id="out-of-page-hash" href="/local/page/1#two">Out of Page hash</a>', | ||
'<a id="global-hash" href="http://google.com/#hash">Global Hash</a>', | ||
'<a id="active" href="' + window.location.pathname + '"">Active</a>', | ||
'<span id="no-anchor">No anchor</span>', | ||
'<a id="local-blank" href="/local/page/1" target="_blank">Local Blank</a>', | ||
'<a id="local-blank-hash" href="#modal2" target="_blank">Local Blank Hash</a>' | ||
].join('') | ||
document.body.appendChild(container) | ||
} | ||
function triggerClick(el, modified, button){ | ||
var ev; | ||
if (button === undefined) { | ||
button = 0; /*left*/ | ||
} | ||
if (document.createEvent) { | ||
ev = document.createEvent("MouseEvent"); | ||
ev.initMouseEvent( | ||
"click", | ||
true /* bubble */, | ||
true /* cancelable */, | ||
window, null, | ||
0, 0, 0, 0, /* coordinates */ | ||
!!modified, false, false, false, /* modifier keys */ | ||
button, | ||
null | ||
); | ||
el.dispatchEvent(ev); | ||
} else if (document.createEventObject) { | ||
ev = document.createEventObject(); | ||
ev.ctrlKey = !!modified; | ||
ev.button = button; | ||
el.dispatchEvent('onclick', ev); | ||
} | ||
function triggerClick (el, modified, button) { | ||
var ev | ||
if (button === undefined) { | ||
button = 0 /* left */ | ||
} | ||
if (document.createEvent) { | ||
ev = document.createEvent('MouseEvent') | ||
ev.initMouseEvent( | ||
'click', | ||
true /* bubble */, | ||
true /* cancelable */, | ||
window, null, | ||
0, 0, 0, 0, /* coordinates */ | ||
!!modified, false, false, false, /* modifier keys */ | ||
button, | ||
null | ||
) | ||
el.dispatchEvent(ev) | ||
} else if (document.createEventObject) { | ||
ev = document.createEventObject() | ||
ev.ctrlKey = !!modified | ||
ev.button = button | ||
el.dispatchEvent('onclick', ev) | ||
} | ||
} | ||
function attachClick(el, fn) { | ||
if (el.addEventListener) { | ||
el.addEventListener('click', fn, false); | ||
} else if (el.attachEvent) { | ||
el.attachEvent('onclick', fn); | ||
} | ||
function attachClick (el, fn) { | ||
if (el.addEventListener) { | ||
el.addEventListener('click', fn, false) | ||
} else if (el.attachEvent) { | ||
el.attachEvent('onclick', fn) | ||
} | ||
} | ||
domready(function () { | ||
setup(); | ||
jq(function () { | ||
setup() | ||
function _pathnameTest(method, t) { | ||
var a = $('local'); | ||
var search = $('local-search'); | ||
var outHash = $('out-of-page-hash'); | ||
var span = $('local-nested'); | ||
var global = $('global'); | ||
var relative = $('relative'); | ||
var noAnchor = $('no-anchor'); | ||
var localBlank = $('local-blank'); | ||
function _pathnameTest (method, t) { | ||
var a = $('local') | ||
var search = $('local-search') | ||
var outHash = $('out-of-page-hash') | ||
var span = $('local-nested') | ||
var global = $('global') | ||
var relative = $('relative') | ||
var noAnchor = $('no-anchor') | ||
var localBlank = $('local-blank') | ||
t.plan(8); | ||
t.plan(8) | ||
t.equal(localLinks[method](a), '/local/page/1'); | ||
t.equal(localLinks[method](span), '/local/page/1'); | ||
t.equal(localLinks[method](search), '/local/page/1?param=2'); | ||
t.equal(localLinks[method](outHash), '/local/page/1#two'); | ||
t.equal(localLinks[method](global), null); | ||
t.equal(localLinks[method](relative), '/page-2'); | ||
t.equal(localLinks[method](noAnchor), null); | ||
t.equal(localLinks[method](localBlank), null); | ||
t.equal(localLinks[method](a), '/local/page/1') | ||
t.equal(localLinks[method](span), '/local/page/1') | ||
t.equal(localLinks[method](search), '/local/page/1?param=2') | ||
t.equal(localLinks[method](outHash), '/local/page/1#two') | ||
t.equal(localLinks[method](global), null) | ||
t.equal(localLinks[method](relative), '/page-2') | ||
t.equal(localLinks[method](noAnchor), null) | ||
t.equal(localLinks[method](localBlank), null) | ||
t.end(); | ||
} | ||
t.end() | ||
} | ||
// Use this test for both pathname alias functions | ||
test('HTML elements return pathname or null', partial(_pathnameTest, 'pathname')); | ||
test('HTML elements return pathname or null', partial(_pathnameTest, 'getLocalPathname')); | ||
test('HTML elements return pathname or null', partial(_pathnameTest, 'pathname')) | ||
test('HTML elements return pathname or null', partial(_pathnameTest, 'getLocalPathname')) | ||
test('Can be called with different context', function (t) { | ||
var pathname = localLinks.pathname; | ||
var hash = localLinks.hash; | ||
var active = localLinks.active; | ||
t.plan(3); | ||
t.equal(pathname($('local')), '/local/page/1'); | ||
t.equal(hash($('in-page-hash')), '#modal'); | ||
t.equal(active($('active')), true); | ||
t.end(); | ||
}); | ||
test('Can be called with different context', function (t) { | ||
var pathname = localLinks.pathname | ||
var hash = localLinks.hash | ||
var active = localLinks.active | ||
t.plan(3) | ||
t.equal(pathname($('local')), '/local/page/1') | ||
t.equal(hash($('in-page-hash')), '#modal') | ||
t.equal(active($('active')), true) | ||
t.end() | ||
}) | ||
test('Works if the argument is an event with a target', function (t) { | ||
var ev = e('local'); | ||
var nestedEvent = e('local-nested'); | ||
var globalEvent = e('global'); | ||
test('Works if the argument is an event with a target', function (t) { | ||
var ev = e('local') | ||
var nestedEvent = e('local-nested') | ||
var globalEvent = e('global') | ||
t.plan(3); | ||
t.plan(3) | ||
t.equal(localLinks.pathname(ev), '/local/page/1'); | ||
t.equal(localLinks.pathname(nestedEvent), '/local/page/1'); | ||
t.equal(localLinks.pathname(globalEvent), null); | ||
t.equal(localLinks.pathname(ev), '/local/page/1') | ||
t.equal(localLinks.pathname(nestedEvent), '/local/page/1') | ||
t.equal(localLinks.pathname(globalEvent), null) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) | ||
test('Ignores modified events for a valid anchor', function (t) { | ||
var ev = e('local'); | ||
ev.shiftKey = true; | ||
t.plan(1); | ||
test('Ignores modified events for a valid anchor', function (t) { | ||
var ev = e('local') | ||
ev.shiftKey = true | ||
t.equal(localLinks.pathname(ev, $('local')), null); | ||
t.plan(1) | ||
t.end(); | ||
}); | ||
t.equal(localLinks.pathname(ev, $('local')), null) | ||
test('Will use anchor from second arg', function (t) { | ||
var globalEvent = e(global); | ||
var globalA = $('global'); | ||
var localA = $('local-nested'); | ||
t.end() | ||
}) | ||
t.plan(3); | ||
test('Will use anchor from second arg', function (t) { | ||
var globalEvent = e(global) | ||
var globalA = $('global') | ||
var localA = $('local-nested') | ||
t.equal(localLinks.pathname(globalEvent, localA), '/local/page/1'); | ||
t.equal(localLinks.pathname(globalA, localA), '/local/page/1'); | ||
t.equal(localLinks.pathname(localA, globalA), null); | ||
t.plan(3) | ||
t.end(); | ||
}); | ||
t.equal(localLinks.pathname(globalEvent, localA), '/local/page/1') | ||
t.equal(localLinks.pathname(globalA, localA), '/local/page/1') | ||
t.equal(localLinks.pathname(localA, globalA), null) | ||
function _hashTest(method, t) { | ||
var hash = $('in-page-hash'); | ||
var emptyHash = $('empty-in-page-hash'); | ||
var globalHash = $('global-hash'); | ||
var targetBlankHash = $('local-blank-hash'); | ||
t.end() | ||
}) | ||
t.plan(7); | ||
function _hashTest (method, t) { | ||
var hash = $('in-page-hash') | ||
var emptyHash = $('empty-in-page-hash') | ||
var globalHash = $('global-hash') | ||
var targetBlankHash = $('local-blank-hash') | ||
t.equal(localLinks.pathname(hash), null); | ||
t.equal(localLinks.pathname(emptyHash), null); | ||
t.equal(localLinks.pathname(targetBlankHash), null); | ||
t.plan(7) | ||
t.equal(localLinks[method](hash), '#modal'); | ||
t.equal(localLinks[method](emptyHash), '#'); | ||
t.equal(localLinks[method](targetBlankHash), null); | ||
t.equal(localLinks[method](globalHash), null); | ||
t.equal(localLinks.pathname(hash), null) | ||
t.equal(localLinks.pathname(emptyHash), null) | ||
t.equal(localLinks.pathname(targetBlankHash), null) | ||
t.end(); | ||
} | ||
t.equal(localLinks[method](hash), '#modal') | ||
t.equal(localLinks[method](emptyHash), '#') | ||
t.equal(localLinks[method](targetBlankHash), null) | ||
t.equal(localLinks[method](globalHash), null) | ||
t.end() | ||
} | ||
// Use this test for both hash alias functions | ||
test('Test hash links', partial(_hashTest, 'hash')); | ||
test('Test hash links', partial(_hashTest, 'getLocalHash')); | ||
test('Test hash links', partial(_hashTest, 'hash')) | ||
test('Test hash links', partial(_hashTest, 'getLocalHash')) | ||
function _activeTest(method, t) { | ||
t.plan(5); | ||
function _activeTest (method, t) { | ||
t.plan(5) | ||
t.equal(localLinks[method]($('active')), true); | ||
t.equal(localLinks[method]($('global')), false); | ||
t.equal(localLinks[method]($('local')), false); | ||
t.equal(localLinks[method]($('local'), '/local/page/1'), true); | ||
t.equal(localLinks[method]($('in-page-hash')), false); | ||
t.equal(localLinks[method]($('active')), true) | ||
t.equal(localLinks[method]($('global')), false) | ||
t.equal(localLinks[method]($('local')), false) | ||
t.equal(localLinks[method]($('local'), '/local/page/1'), true) | ||
t.equal(localLinks[method]($('in-page-hash')), false) | ||
t.end(); | ||
} | ||
t.end() | ||
} | ||
// Use this test for both active alias functions | ||
test('Active returns boolean based on current page', partial(_activeTest, 'active')); | ||
test('Active returns boolean based on current page', partial(_activeTest, 'isActive')); | ||
test('Active returns boolean based on current page', partial(_activeTest, 'active')) | ||
test('Active returns boolean based on current page', partial(_activeTest, 'isActive')) | ||
test('Return null for garbage', function (t) { | ||
t.plan(8); | ||
test('Return null for garbage', function (t) { | ||
t.plan(8) | ||
t.equal(localLinks.pathname(null), null); | ||
t.equal(localLinks.pathname($('whoops')), null); | ||
t.equal(localLinks.pathname({}, {}, {}, true), null); | ||
t.equal(localLinks.pathname('hey'), null); | ||
t.equal(localLinks.pathname(false), null); | ||
t.equal(localLinks.hash({}), null); | ||
t.equal(localLinks.hash('hey'), null); | ||
t.equal(localLinks.hash(false), null); | ||
t.equal(localLinks.pathname(null), null) | ||
t.equal(localLinks.pathname($('whoops')), null) | ||
t.equal(localLinks.pathname({}, {}, {}, true), null) | ||
t.equal(localLinks.pathname('hey'), null) | ||
t.equal(localLinks.pathname(false), null) | ||
t.equal(localLinks.hash({}), null) | ||
t.equal(localLinks.hash('hey'), null) | ||
t.equal(localLinks.hash(false), null) | ||
t.end(); | ||
}); | ||
t.end() | ||
}) | ||
test('Works on link clicks', function (t) { | ||
var local = $('local'); | ||
var global = $('global'); | ||
var plan = 2; | ||
var count = 0; | ||
var end = function () { | ||
count++; | ||
if (count === plan) { | ||
t.end(); | ||
} | ||
}; | ||
test('Works on link clicks', function (t) { | ||
var local = $('local') | ||
var global = $('global') | ||
var plan = 2 | ||
var count = 0 | ||
var end = function () { | ||
count++ | ||
if (count === plan) { | ||
t.end() | ||
} | ||
} | ||
t.plan(plan); | ||
t.plan(plan) | ||
attachClick(local, function (event) { | ||
event.preventDefault(); | ||
t.equal(localLinks.pathname(event), '/local/page/1'); | ||
end(); | ||
}); | ||
attachClick(local, function (event) { | ||
event.preventDefault() | ||
t.equal(localLinks.pathname(event), '/local/page/1') | ||
end() | ||
}) | ||
attachClick(global, function (event) { | ||
event.preventDefault(); | ||
t.equal(localLinks.pathname(event), null); | ||
end(); | ||
}); | ||
attachClick(global, function (event) { | ||
event.preventDefault() | ||
t.equal(localLinks.pathname(event), null) | ||
end() | ||
}) | ||
triggerClick(local); | ||
triggerClick(global); | ||
}); | ||
triggerClick(local) | ||
triggerClick(global) | ||
}) | ||
test('Works on modified link clicks', function (t) { | ||
var local = $('local2'); | ||
var plan = 1; | ||
var count = 0; | ||
var end = function () { | ||
count++; | ||
if (count === plan) { | ||
t.end(); | ||
} | ||
}; | ||
test('Works on modified link clicks', function (t) { | ||
var local = $('local2') | ||
var plan = 1 | ||
var count = 0 | ||
var end = function () { | ||
count++ | ||
if (count === plan) { | ||
t.end() | ||
} | ||
} | ||
t.plan(plan); | ||
t.plan(plan) | ||
attachClick(local, function (event) { | ||
event.preventDefault(); | ||
t.equal(localLinks.pathname(event), null); | ||
end(); | ||
}); | ||
attachClick(local, function (event) { | ||
event.preventDefault() | ||
t.equal(localLinks.pathname(event), null) | ||
end() | ||
}) | ||
triggerClick(local, true); | ||
}); | ||
triggerClick(local, true) | ||
}) | ||
test('Ignores middle clicks', function (t) { | ||
var local = $('local3'); | ||
var plan = 1; | ||
var count = 0; | ||
var end = function () { | ||
count++; | ||
if (count === plan) { | ||
t.end(); | ||
} | ||
}; | ||
test('Ignores middle clicks', function (t) { | ||
var local = $('local3') | ||
var plan = 1 | ||
var count = 0 | ||
var end = function () { | ||
count++ | ||
if (count === plan) { | ||
t.end() | ||
} | ||
} | ||
t.plan(plan); | ||
t.plan(plan) | ||
attachClick(local, function(event) { | ||
event.preventDefault(); | ||
t.equal(localLinks.pathname(event), null, 'should ignore middle-button clicks'); | ||
}); | ||
attachClick(local, function (event) { | ||
event.preventDefault() | ||
t.equal(localLinks.pathname(event), null, 'should ignore middle-button clicks') | ||
end() | ||
}) | ||
triggerClick(local, false, 1); | ||
}); | ||
}); | ||
triggerClick(local, false, 1) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
390
22095
12
9
136
1