Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Element matches and closest #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/DOM/Node/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,40 @@ exports.removeAttribute = function (name) {
};
};

// `matches` polyfill (https://caniuse.com/#feat=matchesselector)
if (typeof Element.prototype.matches !== "function") {
Element.prototype.matches =
Element.prototype.msMatchesSelector
|| Element.prototype.webkitMatchesSelector
|| Element.prototype.oMatchesSelector
|| Element.prototype.mozMatchesSelector;
}

exports.matches = function (selector) {
return function (element) {
return element.matches(selector);
}
}

// `closest` polyfill (https://caniuse.com/#feat=element-closest)
if (typeof Element.prototype.closest !== "function") {
Element.prototype.closest = function closest(selector) {
return (function searchUpTree(selector, element) {
return element == null
? null
: element.matches(selector)
? element
: searchUpTree(selector, element.parentElement);
}(selector, this));
}
}

exports.closest = function (selector) {
return function (element) {
return element.closest(selector);
}
}

// - CSSOM ---------------------------------------------------------------------

exports.scrollTop = function (node) {
Expand Down
5 changes: 5 additions & 0 deletions src/DOM/Node/Element.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module DOM.Node.Element
, setAttribute
, getAttribute
, removeAttribute
, matches
, closest
, scrollTop
, setScrollTop
, scrollLeft
Expand Down Expand Up @@ -65,6 +67,9 @@ getAttribute attr = map toMaybe <<< _getAttribute attr
foreign import _getAttribute :: forall eff. String -> Element -> Eff (dom :: DOM | eff) (Nullable String)
foreign import removeAttribute :: forall eff. String -> Element -> Eff (dom :: DOM | eff) Unit

foreign import matches :: forall eff. String -> Element -> Eff (dom :: DOM | eff) Boolean
foreign import closest :: forall eff. String -> Element -> Eff (dom :: DOM | eff) (Nullable Element)

foreign import scrollTop :: forall eff. Element -> Eff (dom :: DOM | eff) Number
foreign import setScrollTop :: forall eff. Number -> Element -> Eff (dom :: DOM | eff) Unit

Expand Down