element-closest
Advanced tools
Comparing version 1.0.3 to 2.0.0
{ | ||
"name": "Element.closest", | ||
"description": "Get the closest element matching a selector up the DOM tree", | ||
"name": "element-matches", | ||
"description": "Return the closest element matching a selector up the DOM tree", | ||
"main": "closest.js", | ||
"author": "Jonathan Neal <jonathantneal@hotmail.com> (http://jonathantneal.com/)", | ||
"license": "CC0-1.0", | ||
"ignore": [ | ||
"**/.*", | ||
"node_modules", | ||
"bower_components", | ||
"test", | ||
"tests" | ||
], | ||
"keywords": [ | ||
"matches", | ||
"closest", | ||
"polyfill" | ||
], | ||
"homepage": "https://github.com/jonathantneal/closest", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/jonathantneal/closest.git" | ||
}, | ||
"authors": [ | ||
"Jonathan Neal <jonathantneal+github@gmail.com> (http://jonathantneal.com)", | ||
"Bogdan Chadkin <trysound@yandex.ru>" | ||
"CHANGELOG.md", | ||
"CONTRIBUTING.md", | ||
"package.json", | ||
"README.md" | ||
] | ||
} |
@@ -1,17 +0,31 @@ | ||
(function (ELEMENT) { | ||
ELEMENT.matches = ELEMENT.matches || ELEMENT.mozMatchesSelector || ELEMENT.msMatchesSelector || ELEMENT.oMatchesSelector || ELEMENT.webkitMatchesSelector; | ||
// element-closest | CC0-1.0 | github.com/jonathantneal/closest | ||
ELEMENT.closest = ELEMENT.closest || function closest(selector) { | ||
if (typeof Element.prototype.matches !== 'function') { | ||
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.webkitMatchesSelector || function matches(selector) { | ||
var element = this; | ||
var elements = (element.document || element.ownerDocument).querySelectorAll(selector); | ||
var index = 0; | ||
while (element) { | ||
while (elements[index] && elements[index] !== element) { | ||
++index; | ||
} | ||
return Boolean(elements[index]); | ||
}; | ||
} | ||
if (typeof Element.prototype.closest !== 'function') { | ||
Element.prototype.closest = function closest(selector) { | ||
var element = this; | ||
while (element && element.nodeType !== 11) { | ||
if (element.matches(selector)) { | ||
break; | ||
return element; | ||
} | ||
element = element.parentElement; | ||
element = element.parentNode; | ||
} | ||
return element; | ||
return null; | ||
}; | ||
}(Element.prototype)); | ||
} |
# CC0 1.0 Universal License | ||
Public Domain Dedication | ||
@@ -3,0 +4,0 @@ |
{ | ||
"name": "element-closest", | ||
"version": "1.0.3", | ||
"description": "Get the closest element matching a selector up the DOM tree", | ||
"version": "2.0.0", | ||
"description": "Return the closest element matching a selector up the DOM tree", | ||
"main": "closest.js", | ||
"scripts": { | ||
"test": "eslint *.js --ignore-path .gitignore && jscs *.js" | ||
}, | ||
"repository": "jonathantneal/closest", | ||
"keywords": [ | ||
@@ -13,19 +18,10 @@ "closest", | ||
], | ||
"author": "Jonathan Neal <jonathantneal@hotmail.com> (http://jonathantneal.com)", | ||
"license": "CC0-1.0", | ||
"author": "Jonathan Neal <jonathantneal@hotmail.com> (http://jonathantneal.com/)", | ||
"contributors": [ | ||
"Bogdan Chadkin <trysound@yandex.ru>" | ||
], | ||
"main": "closest.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://jonathantneal@github.com/jonathantneal/closest.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/jonathantneal/closest/issues" | ||
}, | ||
"homepage": "https://github.com/jonathantneal/closest" | ||
"bugs": "https://github.com/jonathantneal/closest/issues", | ||
"homepage": "https://github.com/jonathantneal/closest#readme", | ||
"devDependencies": { | ||
"eslint": "^2.7.0", | ||
"jscs": "^2.11.0" | ||
} | ||
} |
# closest | ||
[closest]: https://dom.spec.whatwg.org/#dom-element-closestselectors | ||
[matches]: https://developer.mozilla.org/en-US/docs/Web/API/Element.matches | ||
> Return the closest element matching a selector up the DOM tree | ||
[Element.prototype.closest][closest] returns the first element that matches a CSS selector by traversing up the DOM tree starting from (and including) the receiver element. | ||
[![npm][npm-image]][npm-url] [![status][ci-img]][ci] | ||
[![license][license-image]][license-url] | ||
[![changelog][changelog-image]][changelog-url] | ||
[![gitter][gitter-image]][gitter-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. | ||
```js | ||
@@ -16,7 +24,7 @@ element.closest(selectorString) //=> element | ||
document.addEventListener('click', function (event) { | ||
// find the anchor element that contains or is the click target itself: | ||
// find nearest element up the tree that is an <a> | ||
var link = event.target.closest('a'); | ||
if (link) { | ||
// do something with the anchor | ||
// do something with the <a> | ||
event.preventDefault(); | ||
@@ -28,4 +36,6 @@ } | ||
## matches | ||
For the `closest` polyfill to work, this also polyfills [matches][], which is widely supported but often vendor-prefixed. | ||
The also polyfills [`#Element.matches`], which is | ||
widely supported but often vendor-prefixed. | ||
```js | ||
@@ -35,3 +45,4 @@ element.matches(selectorString) //=> boolean | ||
`matches` is especially useful for shorthanding `hasAttribute` or `classList.contains` with selectors. | ||
`matches` is especially useful for short-handing `hasAttribute` or | ||
`classList.contains` with selectors. | ||
@@ -48,20 +59,34 @@ ```js | ||
- Internet Explorer 9+ | ||
- Firefox 3.6+ | ||
- Chrome | ||
- Opera 11.5+ | ||
- Android 2.2+ | ||
- Blackberry 7+ | ||
- iOS Safari 4+ | ||
- Safari 5+ | ||
| Browser | Native Support | Polyfill Support | | ||
| ----------------- | -------------- | ---------------- | | ||
| Android | ✘ | 2.2+ | | ||
| Blackberry | ✘ | 7+ | | ||
| Chrome | 41+ | 4 - 40 | | ||
| Edge | ✘ | 12+ | | ||
| Firefox | 35+ | 3.5 - 34 | | ||
| Internet Explorer | ✘ | 8+ | | ||
| Opera | 28+ | 10 - 27 | | ||
| Opera Mini | ✘ | 5+ | | ||
| Safari (iOS) | 9 | 3.2 - 8.4 | | ||
| Safari (MacOS) | 9.2+ | 3.1 - 8 | | ||
**The legacy version extends support to** | ||
### Internet Explorer 8 | ||
- Internet Explorer 8+ | ||
- Firefox 3.5+ | ||
- iOS Safari 3.2+ | ||
- Opera 10+ | ||
- Opera Mini 5+ | ||
- Safari 3.1+. | ||
`closest` is especially useful for delegating events, but remember that | ||
Internet Explorer 8 does not support [`#Element.addEventListener`]. | ||
Those are really old browsers, and I don’t see a usecase for IE8 compatibility, which is why it is packaged separately. `closest` is especially useful when delegating events, and Internet Explorer 8 does not support [addEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener#Browser_compatibility), and [attachEvent](http://help.dottoro.com/ljinxrmt.php) is an insufficient fallback, as it fires events in the reverse order they are added (read: the opposite order of what you expect and `addEventListener`). | ||
[changelog-image]: https://img.shields.io/badge/changelog-md-blue.svg | ||
[changelog-url]: CHANGELOG.md | ||
[ci]: https://travis-ci.org/jonathantneal/closest | ||
[ci-img]: https://img.shields.io/travis/jonathantneal/closest.svg | ||
[gitter-image]: https://img.shields.io/badge/chat-gitter-blue.svg | ||
[gitter-url]: https://gitter.im/jonathantneal/closest | ||
[license-image]: https://img.shields.io/npm/l/element-closest.svg | ||
[license-url]: LICENSE.md | ||
[npm-image]: https://img.shields.io/npm/v/element-closest.svg | ||
[npm-url]: https://www.npmjs.com/package/element-closest | ||
[closest]: https://github.com/jonathantneal/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 |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the 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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
16001
14
83
0
89
2
2
1