element-closest
Advanced tools
Comparing version 2.0.2 to 3.0.0
# Changes to closest | ||
# 2.0.2 (October 26, 2016) | ||
### 3.0.0 (December 20, 2018) | ||
- Rewritten for usage within Node environments | ||
- Improved documentation | ||
### 2.0.2 (October 26, 2016) | ||
- Updated `Element` reference as `window.Element` | ||
- Updated project configuration | ||
# 2.0.1 (April 8, 2016) | ||
### 2.0.1 (April 8, 2016) | ||
@@ -10,0 +15,0 @@ - Fixed an issue referencing the current nodeType |
132
LICENSE.md
@@ -33,75 +33,77 @@ # CC0 1.0 Universal | ||
to, the following: | ||
1. the right to reproduce, adapt, distribute, perform, display, | ||
communicate, and translate a Work; | ||
2. moral rights retained by the original author(s) and/or performer(s); | ||
3. publicity and privacy rights pertaining to a person’s image or likeness | ||
depicted in a Work; | ||
4. rights protecting against unfair competition in regards to a Work, | ||
subject to the limitations in paragraph 4(i), below; | ||
5. rights protecting the extraction, dissemination, use and reuse of data | ||
in a Work; | ||
6. database rights (such as those arising under Directive 96/9/EC of the | ||
European Parliament and of the Council of 11 March 1996 on the legal | ||
protection of databases, and under any national implementation thereof, | ||
including any amended or successor version of such directive); and | ||
7. other similar, equivalent or corresponding rights throughout the world | ||
based on applicable law or treaty, and any national implementations | ||
thereof. | ||
1. the right to reproduce, adapt, distribute, perform, display, communicate, | ||
and translate a Work; | ||
2. moral rights retained by the original author(s) and/or performer(s); | ||
3. publicity and privacy rights pertaining to a person’s image or likeness | ||
depicted in a Work; | ||
4. rights protecting against unfair competition in regards to a Work, | ||
subject to the limitations in paragraph 4(i), below; | ||
5. rights protecting the extraction, dissemination, use and reuse of data in | ||
a Work; | ||
6. database rights (such as those arising under Directive 96/9/EC of the | ||
European Parliament and of the Council of 11 March 1996 on the legal | ||
protection of databases, and under any national implementation thereof, | ||
including any amended or successor version of such directive); and | ||
7. other similar, equivalent or corresponding rights throughout the world | ||
based on applicable law or treaty, and any national implementations | ||
thereof. | ||
2. Waiver. To the greatest extent permitted by, but not in contravention of, | ||
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and | ||
unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright | ||
and Related Rights and associated claims and causes of action, whether now | ||
known or unknown (including existing as well as future claims and causes of | ||
action), in the Work (i) in all territories worldwide, (ii) for the maximum | ||
duration provided by applicable law or treaty (including future time | ||
extensions), (iii) in any current or future medium and for any number of | ||
copies, and (iv) for any purpose whatsoever, including without limitation | ||
commercial, advertising or promotional purposes (the “Waiver”). Affirmer makes | ||
the Waiver for the benefit of each member of the public at large and to the | ||
detriment of Affirmer’s heirs and successors, fully intending that such Waiver | ||
shall not be subject to revocation, rescission, cancellation, termination, or | ||
any other legal or equitable action to disrupt the quiet enjoyment of the Work | ||
by the public as contemplated by Affirmer’s express Statement of Purpose. | ||
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and | ||
unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright | ||
and Related Rights and associated claims and causes of action, whether now | ||
known or unknown (including existing as well as future claims and causes of | ||
action), in the Work (i) in all territories worldwide, (ii) for the maximum | ||
duration provided by applicable law or treaty (including future time | ||
extensions), (iii) in any current or future medium and for any number of | ||
copies, and (iv) for any purpose whatsoever, including without limitation | ||
commercial, advertising or promotional purposes (the “Waiver”). Affirmer | ||
makes the Waiver for the benefit of each member of the public at large and | ||
to the detriment of Affirmer’s heirs and successors, fully intending that | ||
such Waiver shall not be subject to revocation, rescission, cancellation, | ||
termination, or any other legal or equitable action to disrupt the quiet | ||
enjoyment of the Work by the public as contemplated by Affirmer’s express | ||
Statement of Purpose. | ||
3. Public License Fallback. Should any part of the Waiver for any reason be | ||
judged legally invalid or ineffective under applicable law, then the Waiver | ||
shall be preserved to the maximum extent permitted taking into account | ||
Affirmer’s express Statement of Purpose. In addition, to the extent the Waiver | ||
is so judged Affirmer hereby grants to each affected person a royalty-free, non | ||
transferable, non sublicensable, non exclusive, irrevocable and unconditional | ||
license to exercise Affirmer’s Copyright and Related Rights in the Work (i) in | ||
all territories worldwide, (ii) for the maximum duration provided by applicable | ||
law or treaty (including future time extensions), (iii) in any current or | ||
future medium and for any number of copies, and (iv) for any purpose | ||
whatsoever, including without limitation commercial, advertising or promotional | ||
purposes (the “License”). The License shall be deemed effective as of the date | ||
CC0 was applied by Affirmer to the Work. Should any part of the License for any | ||
reason be judged legally invalid or ineffective under applicable law, such | ||
partial invalidity or ineffectiveness shall not invalidate the remainder of the | ||
License, and in such case Affirmer hereby affirms that he or she will not (i) | ||
exercise any of his or her remaining Copyright and Related Rights in the Work | ||
or (ii) assert any associated claims and causes of action with respect to the | ||
Work, in either case contrary to Affirmer’s express Statement of Purpose. | ||
judged legally invalid or ineffective under applicable law, then the Waiver | ||
shall be preserved to the maximum extent permitted taking into account | ||
Affirmer’s express Statement of Purpose. In addition, to the extent the | ||
Waiver is so judged Affirmer hereby grants to each affected person a | ||
royalty-free, non transferable, non sublicensable, non exclusive, | ||
irrevocable and unconditional license to exercise Affirmer’s Copyright and | ||
Related Rights in the Work (i) in all territories worldwide, (ii) for the | ||
maximum duration provided by applicable law or treaty (including future time | ||
extensions), (iii) in any current or future medium and for any number of | ||
copies, and (iv) for any purpose whatsoever, including without limitation | ||
commercial, advertising or promotional purposes (the “License”). The License | ||
shall be deemed effective as of the date CC0 was applied by Affirmer to the | ||
Work. Should any part of the License for any reason be judged legally | ||
invalid or ineffective under applicable law, such partial invalidity or | ||
ineffectiveness shall not invalidate the remainder of the License, and in | ||
such case Affirmer hereby affirms that he or she will not (i) exercise any | ||
of his or her remaining Copyright and Related Rights in the Work or (ii) | ||
assert any associated claims and causes of action with respect to the Work, | ||
in either case contrary to Affirmer’s express Statement of Purpose. | ||
4. Limitations and Disclaimers. | ||
1. No trademark or patent rights held by Affirmer are waived, abandoned, | ||
surrendered, licensed or otherwise affected by this document. | ||
2. Affirmer offers the Work as-is and makes no representations or | ||
warranties of any kind concerning the Work, express, implied, statutory | ||
or otherwise, including without limitation warranties of title, | ||
merchantability, fitness for a particular purpose, non infringement, or | ||
the absence of latent or other defects, accuracy, or the present or | ||
absence of errors, whether or not discoverable, all to the greatest | ||
extent permissible under applicable law. | ||
3. Affirmer disclaims responsibility for clearing rights of other persons | ||
that may apply to the Work or any use thereof, including without | ||
limitation any person’s Copyright and Related Rights in the Work. | ||
Further, Affirmer disclaims responsibility for obtaining any necessary | ||
consents, permissions or other rights required for any use of the Work. | ||
4. Affirmer understands and acknowledges that Creative Commons is not a | ||
party to this document and has no duty or obligation with respect to | ||
this CC0 or use of the Work. | ||
1. No trademark or patent rights held by Affirmer are waived, abandoned, | ||
surrendered, licensed or otherwise affected by this document. | ||
2. Affirmer offers the Work as-is and makes no representations or warranties | ||
of any kind concerning the Work, express, implied, statutory or | ||
otherwise, including without limitation warranties of title, | ||
merchantability, fitness for a particular purpose, non infringement, or | ||
the absence of latent or other defects, accuracy, or the present or | ||
absence of errors, whether or not discoverable, all to the greatest | ||
extent permissible under applicable law. | ||
3. Affirmer disclaims responsibility for clearing rights of other persons | ||
that may apply to the Work or any use thereof, including without | ||
limitation any person’s Copyright and Related Rights in the Work. | ||
Further, Affirmer disclaims responsibility for obtaining any necessary | ||
consents, permissions or other rights required for any use of the Work. | ||
4. Affirmer understands and acknowledges that Creative Commons is not a | ||
party to this document and has no duty or obligation with respect to this | ||
CC0 or use of the Work. | ||
For more information, please see | ||
http://creativecommons.org/publicdomain/zero/1.0/. |
{ | ||
"name": "element-closest", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"description": "Return the closest element matching a selector up the DOM tree", | ||
"author": "Jonathan Neal <jonathantneal@hotmail.com> (http://jonathantneal.com)", | ||
"author": "Jonathan Neal <jonathantneal@hotmail.com>", | ||
"license": "CC0-1.0", | ||
@@ -10,34 +10,51 @@ "repository": "jonathantneal/closest", | ||
"bugs": "https://github.com/jonathantneal/closest/issues", | ||
"main": "element-closest.js", | ||
"main": "index.js", | ||
"module": "index.mjs", | ||
"files": [ | ||
"element-closest.js" | ||
"index.js", | ||
"index.js.map", | ||
"index.mjs", | ||
"index.mjs.map", | ||
"browser.js" | ||
], | ||
"scripts": { | ||
"test": "eslint *.js --ignore-path .gitignore && jscs *.js" | ||
"build": "npm run build:node && npm run build:browser", | ||
"build:node": "rollup --config", | ||
"build:browser": "cross-env NODE_ENV=browser rollup --config", | ||
"prepublish": "npm run test && npm run build", | ||
"test": "eslint src/*.js --cache --ignore-path .gitignore --quiet" | ||
}, | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=6.0.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^3.8.1", | ||
"eslint-config-10up": "github:jonathantneal/eslint-config-10up", | ||
"jscs": "^3.0.7", | ||
"jscs-config-10up": "github:jonathantneal/jscs-config-10up" | ||
"@babel/core": "^7.2.2", | ||
"@babel/preset-env": "^7.2.3", | ||
"babel-eslint": "^10.0.1", | ||
"cross-env": "^5.2.0", | ||
"eslint": "^5.10.0", | ||
"eslint-config-dev": "^2.0.0", | ||
"pre-commit": "^1.2.2", | ||
"rollup": "^0.68.1", | ||
"rollup-plugin-babel": "^4.1.0", | ||
"rollup-plugin-terser": "^3.0.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": "10up" | ||
"extends": "dev", | ||
"parser": "babel-eslint" | ||
}, | ||
"jscsConfig": { | ||
"preset": "10up" | ||
}, | ||
"keywords": [ | ||
"polyfill", | ||
"postcss", | ||
"css", | ||
"postcss-plugin", | ||
"dom", | ||
"document", | ||
"element", | ||
"node", | ||
"closest", | ||
"matches", | ||
"standard", | ||
"specification" | ||
"polyfill", | ||
"web", | ||
"standard" | ||
] | ||
} |
@@ -1,19 +0,19 @@ | ||
# closest | ||
# closest [<img src="https://jonathantneal.github.io/dom-logo.svg" alt="closest" width="90" height="90" align="right">][closest] | ||
> Return the closest element matching a selector up the DOM tree | ||
[![NPM Version][npm-img]][npm-url] | ||
[![Build Status][cli-img]][cli-url] | ||
[![Licensing][lic-image]][lic-url] | ||
[![Changelog][log-image]][log-url] | ||
[![Gitter Chat][git-image]][git-url] | ||
[![Support Chat][git-img]][git-url] | ||
[closest] is a polyfill for [`#Element.closest`]. | ||
The [`#Element.closest`] method returns the closest ancestor of the current | ||
element (or the current element itself) which matches the selectors given in | ||
parameter. If there isn't such an ancestor, it returns null. | ||
```bash | ||
npm install element-closest | ||
``` | ||
The [`#Element.closest`] method returns the closest element that matches a | ||
selector. It returns the element itself, one of its ancestor, or `null` if | ||
there isn't any match. | ||
```js | ||
element.closest(selectorString) //=> element | ||
element.closest(selectorString) //=> Element or null | ||
``` | ||
@@ -25,30 +25,28 @@ | ||
document.addEventListener('click', function (event) { | ||
// find nearest element up the tree that is an <a> | ||
var link = event.target.closest('a'); | ||
// find nearest element up the tree that is an <a> | ||
var link = event.target.closest('a'); | ||
if (link) { | ||
// do something with the <a> | ||
event.preventDefault(); | ||
} | ||
if (link) { | ||
// do something with the <a> | ||
event.preventDefault(); | ||
} | ||
}); | ||
``` | ||
## matches | ||
The script is approximately 258 bytes when minified and gzipped. | ||
The also polyfills [`#Element.matches`], which is | ||
widely supported but often vendor-prefixed. | ||
## Usage | ||
```js | ||
element.matches(selectorString) //=> boolean | ||
For immediate usage, add this script to your document: | ||
```html | ||
<script src="https://unpkg.com/element-closest/browser"></script> | ||
``` | ||
`matches` is especially useful for short-handing `hasAttribute` or | ||
`classList.contains` with selectors. | ||
For Node usage, run [closest] with your `window` object: | ||
```js | ||
var widget = document.querySelector('.custom-widget'); | ||
const elementClosest = require('element-closest'); | ||
if (widget.matches('[data-active]') || widget.matches('.widget--active')) { | ||
// do something with the active widget | ||
} | ||
elementClosest(window); // this is used to reference window.Element | ||
``` | ||
@@ -63,3 +61,3 @@ | ||
| Chrome | 41+ | 4 - 40 | | ||
| Edge | ✘ | 12+ | | ||
| Edge | 15 | 12 - 14 | | ||
| Firefox | 35+ | 3.5 - 34 | | ||
@@ -77,16 +75,32 @@ | Internet Explorer | ✘ | 8+ | | ||
[closest]: https://github.com/jonathantneal/closest | ||
## matches | ||
This library also polyfills [`#Element.matches`], which is widely supported but | ||
often vendor-prefixed. | ||
```js | ||
element.matches(selectorString) //=> boolean | ||
``` | ||
`matches` is especially useful for short-handing `hasAttribute` or | ||
`classList.contains` with selectors. | ||
```js | ||
const widget = document.querySelector('.custom-widget'); | ||
if (widget.matches('[data-active]') || widget.matches('.widget--active')) { | ||
// do something with the active widget | ||
} | ||
``` | ||
[cli-img]: https://img.shields.io/travis/jonathantneal/closest/master.svg | ||
[cli-url]: https://travis-ci.org/jonathantneal/closest | ||
[git-img]: https://img.shields.io/badge/support-chat-blue.svg | ||
[git-url]: https://gitter.im/postcss/postcss | ||
[npm-img]: https://img.shields.io/npm/v/element-closest.svg | ||
[npm-url]: https://www.npmjs.com/package/element-closest | ||
[`#Element.closest`]: https://dom.spec.whatwg.org/#dom-element-closest | ||
[`#Element.matches`]: https://dom.spec.whatwg.org/#dom-element-matches | ||
[`#Element.addEventListener`]: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener#Browser_compatibility | ||
[npm-url]: https://www.npmjs.com/package/element-closest | ||
[npm-img]: https://img.shields.io/npm/v/element-closest.svg?style=flat-square | ||
[cli-url]: https://travis-ci.org/jonathantneal/closest | ||
[cli-img]: https://img.shields.io/travis/jonathantneal/closest.svg?style=flat-square | ||
[lic-url]: LICENSE.md | ||
[lic-image]: https://img.shields.io/npm/l/element-closest.svg?style=flat-square | ||
[log-url]: CHANGELOG.md | ||
[log-image]: https://img.shields.io/badge/changelog-md-blue.svg?style=flat-square | ||
[git-url]: https://gitter.im/jonathantneal/closest | ||
[git-image]: https://img.shields.io/badge/chat-gitter-blue.svg?style=flat-square | ||
[closest]: https://github.com/jonathantneal/closest |
19432
9
59
104
10