@pirxpilot/nanohref 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +65 -0
  3. package/index.js +30 -0
  4. package/package.json +29 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Yoshua Wuyts
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ [![NPM version][npm-image]][npm-url]
2
+ [![Build Status][build-image]][build-url]
3
+ [![Dependency Status][deps-image]][deps-url]
4
+
5
+ # @pirxpilot/nanoraf
6
+
7
+ Fork of [nanohref]. Tiny href click handler library.
8
+
9
+ ## Usage
10
+ ```js
11
+ var nanohref = require('@pirxpilot/nanohref')
12
+
13
+ // Handler automatically attached to window.document
14
+ nanohref(function (location) {
15
+ console.log('new location is', location.pathname)
16
+ })
17
+
18
+ // Create DOM node
19
+ var el = document.createElement('a')
20
+ el.setAttribute('href', '/my-link')
21
+ el.innerText = 'Click me'
22
+ document.body.appendChild(el)
23
+
24
+ // Trigger click
25
+ el.click()
26
+ // => "new location is /my-link"
27
+ ```
28
+
29
+ ## Ignoring links
30
+ By default all href links are handled. The event is not handled under the
31
+ following conditions:
32
+ - the click event had `.preventDefault()` called on it
33
+ - the link has a `data-nanohref-ignore` attribute
34
+ - the link has a `target="_blank"` attribute with `rel="noopener noreferrer"`
35
+ - a modifier key is enabled (e.g. `ctrl`, `alt`, `shift` or `meta`)
36
+ - the link's href starts with protocol handler such as `mailto:` or `dat:`
37
+ - the link points to a different host
38
+ - the link has a `download` attribute
39
+
40
+ :warning: Note that we only handle `target=_blank` if they also have
41
+ `rel="noopener"` on them. This is needed to [properly sandbox web
42
+ pages](https://mathiasbynens.github.io/rel-noopener/).
43
+
44
+ ## API
45
+ ### `nanohref(handler(location))`
46
+ Create a new anchor click handler.
47
+
48
+ ## See Also
49
+ - [MDN/link-types](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types)
50
+ - [caniuse/rel=noopener](http://caniuse.com/#feat=rel-noopener)
51
+ - [mapbox/link-hijacker](https://github.com/mapbox/link-hijacker)
52
+
53
+ ## License
54
+ [MIT](https://tldrlegal.com/license/mit-license)
55
+
56
+ [nanohref]: https://npmjs.org/package/nanohref
57
+
58
+ [npm-image]: https://img.shields.io/npm/v/@pirxpilot/nanohref
59
+ [npm-url]: https://npmjs.org/package/@pirxpilot/nanohref
60
+
61
+ [build-url]: https://github.com/pirxpilot/nanohref/actions/workflows/check.yaml
62
+ [build-image]: https://img.shields.io/github/workflow/status/pirxpilot/nanohref/check
63
+
64
+ [deps-image]: https://img.shields.io/librariesio/release/npm/@pirxpilot/nanohref
65
+ [deps-url]: https://libraries.io/npm/@pirxpilot%2Fnanohref
package/index.js ADDED
@@ -0,0 +1,30 @@
1
+ const assert = require('assert')
2
+
3
+ const safeExternalLink = /noopener/
4
+ const protocolLink = /^[\w-_]+:/
5
+
6
+ module.exports = href
7
+
8
+ function href (cb) {
9
+ assert(typeof cb === 'function', 'nanohref: cb should be type function')
10
+
11
+ window.addEventListener('click', e => {
12
+ if ((e.button && e.button !== 0) ||
13
+ e.ctrlKey || e.metaKey || e.altKey || e.shiftKey ||
14
+ e.defaultPrevented) return
15
+
16
+ const anchor = e.target.closest('a[href]')
17
+ if (!anchor) return
18
+
19
+ if (window.location.protocol !== anchor.protocol ||
20
+ window.location.hostname !== anchor.hostname ||
21
+ window.location.port !== anchor.port ||
22
+ anchor.hasAttribute('data-nanohref-ignore') ||
23
+ anchor.hasAttribute('download') ||
24
+ (anchor.getAttribute('target') === '_blank' && safeExternalLink.test(anchor.getAttribute('rel'))) ||
25
+ protocolLink.test(anchor.getAttribute('href'))) return
26
+
27
+ e.preventDefault()
28
+ cb(anchor)
29
+ })
30
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@pirxpilot/nanohref",
3
+ "description": "Tiny href click handler library",
4
+ "repository": "pirxpilot/nanohref",
5
+ "version": "1.0.0",
6
+ "scripts": {
7
+ "test": "standard"
8
+ },
9
+ "browser": {
10
+ "assert": "@pirxpilot/nanoassert"
11
+ },
12
+ "dependencies": {
13
+ "@pirxpilot/nanoassert": "~1"
14
+ },
15
+ "devDependencies": {
16
+ "standard": "~17"
17
+ },
18
+ "keywords": [
19
+ "nano",
20
+ "browser",
21
+ "href",
22
+ "click",
23
+ "anchor"
24
+ ],
25
+ "license": "MIT",
26
+ "files": [
27
+ "index.js"
28
+ ]
29
+ }