local-links
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -105,2 +105,3 @@ function isHTMLElement(obj) { | ||
// Take two arguments and return an ordered array of [event, anchor] | ||
function getEventAndAnchor(arg1, arg2) { | ||
@@ -135,22 +136,33 @@ var ev = null; | ||
module.exports = { | ||
isLocal: isLocal, | ||
pathname: function () { | ||
return isLocal.apply(null, getEventAndAnchor.apply(null, arguments)); | ||
}, | ||
hash: function () { | ||
return isLocal.apply(null, getEventAndAnchor.apply(null, arguments).concat(true)); | ||
}, | ||
active: function () { | ||
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); | ||
} | ||
// Functions to be used in exports. Defined here for alias purposes | ||
function pathname () { | ||
return isLocal.apply(null, getEventAndAnchor.apply(null, arguments)); | ||
} | ||
return this.pathname.apply(null, args) === normalizeLeadingSlash(checkPath); | ||
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; | ||
if (typeof last === 'string') { | ||
checkPath = last; | ||
args = args.slice(0, -1); | ||
} | ||
return pathname.apply(null, args) === normalizeLeadingSlash(checkPath); | ||
} | ||
module.exports = { | ||
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.2.0", | ||
"version": "1.3.0", | ||
"author": "Luke Karrys <luke@lukekarrys.com>", | ||
@@ -11,4 +11,5 @@ "bugs": { | ||
"browserify": "^5.10.0", | ||
"domready": "^1.0.6", | ||
"domready": "0.3.0", | ||
"jshint": "^2.5.3", | ||
"lodash.partial": "^3.0.0", | ||
"phantomjs": "^1.9.10", | ||
@@ -19,3 +20,4 @@ "precommit-hook": "^1.0.7", | ||
"tape": "^2.14.0", | ||
"tape-run": "^0.3.0" | ||
"tape-run": "^0.3.0", | ||
"zuul": "^1.17.1" | ||
}, | ||
@@ -37,20 +39,7 @@ "homepage": "https://github.com/lukekarrys/local-links", | ||
"start-80": "run-browser test/index.js --port 80", | ||
"test": "browserify test/index.js | tape-run -b phantom | tap-spec" | ||
}, | ||
"testling": { | ||
"files": "test/*.js", | ||
"browsers": [ | ||
"ie/9..latest", | ||
"firefox/17..latest", | ||
"firefox/nightly", | ||
"chrome/22..latest", | ||
"chrome/canary", | ||
"opera/12..latest", | ||
"opera/next", | ||
"safari/5.1..latest", | ||
"ipad/6.0..latest", | ||
"iphone/6.0..latest", | ||
"android-browser/4.2..latest" | ||
] | ||
"test": "browserify test/index.js | tape-run -b phantom | tap-spec", | ||
"test-travis": "npm test && npm run zuul", | ||
"zuul": "zuul --ui tape -- test/index.js", | ||
"zuul-local": "zuul --local 8080 --ui tape -- test/index.js" | ||
} | ||
} |
@@ -8,7 +8,6 @@ local-links | ||
[![browser support](https://ci.testling.com/lukekarrys/local-links.png) | ||
](https://ci.testling.com/lukekarrys/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) | ||
## Install | ||
@@ -63,3 +62,3 @@ | ||
#### `pathname(Event or HTMLElement [, HTMLElement])` | ||
#### `getLocalPathname(Event or HTMLElement [, HTMLElement])` | ||
@@ -69,14 +68,20 @@ Returns the pathname if it is a non-hash local link, or null if it is not. | ||
#### `hash(Event or HTMLElement [, HTMLElement])` | ||
*Alias: `pathname`* | ||
#### `getLocalHash(Event or HTMLElement [, HTMLElement])` | ||
Returns the hash if it is an in-page hash link, or null if it is not. Always | ||
includes the leading `#`. | ||
#### `active(Event or HTMLElement [, String comparePath])` | ||
*Alias: `hash`* | ||
#### `isActive(Event or HTMLElement [, String comparePath])` | ||
Returns true/false depending on if the anchor pathname is equal to the `comparePath` | ||
(which defaults to `window.location.pathname`). Calls `pathname()` internally. | ||
#### `isLocal(event, anchor, [, Boolean lookForHash]) | ||
*Alias: `active`* | ||
#### `isLocal(event, anchor, [, Boolean lookForHash])` | ||
Returns the pathname (or hash if `lookForHash` is true) for local links, or null | ||
@@ -83,0 +88,0 @@ if it is not. This is used by `pathname()` and `hash()` under the hood. The main |
var test = require('tape'); | ||
var localLinks = require('../local-links'); | ||
var domready = require('domready'); | ||
var partial = require('lodash.partial'); | ||
@@ -10,8 +11,6 @@ function $(id) { | ||
function e(id) { | ||
return { | ||
target: $(id) | ||
}; | ||
return {target: $(id)}; | ||
} | ||
function setup(html) { | ||
function setup() { | ||
var container = document.createElement('div'); | ||
@@ -30,6 +29,6 @@ container.id = 'container'; | ||
'<a id="global-hash" href="http://google.com/#hash">Global Hash</a>', | ||
'<a id="active" href="/">Active</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>', | ||
'<a id="local-blank-hash" href="#modal2" target="_blank">Local Blank Hash</a>' | ||
].join(''); | ||
@@ -40,14 +39,21 @@ document.body.appendChild(container); | ||
function triggerClick(el, modified){ | ||
var ev = document.createEvent("MouseEvent"); | ||
ev.initMouseEvent( | ||
"click", | ||
true /* bubble */, | ||
true /* cancelable */, | ||
window, null, | ||
0, 0, 0, 0, /* coordinates */ | ||
!!modified, false, false, false, /* modifier keys */ | ||
0 /*left*/, | ||
null | ||
); | ||
el.dispatchEvent(ev); | ||
var ev; | ||
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 */ | ||
0 /*left*/, | ||
null | ||
); | ||
el.dispatchEvent(ev); | ||
} else if (document.createEventObject) { | ||
ev = document.createEventObject(); | ||
ev.ctrlKey = !!modified; | ||
el.dispatchEvent('onclick', ev); | ||
} | ||
} | ||
@@ -63,7 +69,6 @@ | ||
domready(function () { | ||
setup(); | ||
test('HTML elements return pathname or null', function (t) { | ||
function _pathnameTest(method, t) { | ||
var a = $('local'); | ||
@@ -80,12 +85,26 @@ var search = $('local-search'); | ||
t.equal(localLinks.pathname(a), '/local/page/1'); | ||
t.equal(localLinks.pathname(span), '/local/page/1'); | ||
t.equal(localLinks.pathname(search), '/local/page/1?param=2'); | ||
t.equal(localLinks.pathname(outHash), '/local/page/1#two'); | ||
t.equal(localLinks.pathname(global), null); | ||
t.equal(localLinks.pathname(relative), '/page-2'); | ||
t.equal(localLinks.pathname(noAnchor), null); | ||
t.equal(localLinks.pathname(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(); | ||
} | ||
// 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('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(); | ||
}); | ||
@@ -132,3 +151,3 @@ | ||
test('Test hash links', function (t) { | ||
function _hashTest(method, t) { | ||
var hash = $('in-page-hash'); | ||
@@ -145,41 +164,28 @@ var emptyHash = $('empty-in-page-hash'); | ||
t.equal(localLinks.hash(hash), '#modal'); | ||
t.equal(localLinks.hash(emptyHash), '#'); | ||
t.equal(localLinks.hash(targetBlankHash), null); | ||
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.hash(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', function (t) { | ||
var hash = $('in-page-hash'); | ||
var emptyHash = $('empty-in-page-hash'); | ||
var globalHash = $('global-hash'); | ||
function _activeTest(method, t) { | ||
t.plan(5); | ||
t.equal(localLinks.pathname(hash), null); | ||
t.equal(localLinks.pathname(emptyHash), null); | ||
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.hash(hash), '#modal'); | ||
t.equal(localLinks.hash(emptyHash), '#'); | ||
t.equal(localLinks.hash(globalHash), null); | ||
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', function (t) { | ||
t.plan(5); | ||
t.equal(localLinks.active($('active')), true); | ||
t.equal(localLinks.active($('global')), false); | ||
t.equal(localLinks.active($('local')), false); | ||
t.equal(localLinks.active($('local'), '/local/page/1'), true); | ||
t.equal(localLinks.active($('in-page-hash')), false); | ||
t.end(); | ||
}); | ||
test('Return null for garbage', function (t) { | ||
@@ -186,0 +192,0 @@ t.plan(8); |
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
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
21499
10
346
138
11