Skip to content

Commit 18b5845

Browse files
committed
Element.matches + Element.closest
1 parent 1c1b006 commit 18b5845

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Breaking changes:
1010

1111
New features:
1212
- Add support for ShadowRoot API (#34)
13+
- Add support for `Element.matches` and `Element.closest` (#39)
1314

1415
Bugfixes:
1516

src/Web/DOM/Element.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ exports.removeAttribute = function (name) {
105105
};
106106
};
107107

108+
exports.matches = function (selector) {
109+
return function(element) {
110+
return function () {
111+
return element.matches(selector);
112+
};
113+
};
114+
};
115+
116+
exports._closest = function (selector) {
117+
return function(element) {
118+
return function () {
119+
return element.closest(selector);
120+
};
121+
};
122+
};
123+
108124
// - CSSOM ---------------------------------------------------------------------
109125

110126
exports.scrollTop = function (node) {

src/Web/DOM/Element.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module Web.DOM.Element
2626
, getAttribute
2727
, hasAttribute
2828
, removeAttribute
29+
, matches
30+
, closest
2931
, scrollTop
3032
, setScrollTop
3133
, scrollLeft
@@ -52,6 +54,8 @@ import Web.DOM.Internal.Types (Element) as Exports
5254
import Web.DOM.Internal.Types (Element, HTMLCollection, Node)
5355
import Web.DOM.NonDocumentTypeChildNode (NonDocumentTypeChildNode)
5456
import Web.DOM.ParentNode (ParentNode)
57+
import Web.DOM.ParentNode (QuerySelector) as Exports
58+
import Web.DOM.ParentNode (ParentNode, QuerySelector)
5559
import Web.DOM.ShadowRoot (ShadowRoot, ShadowRootMode)
5660
import Web.Event.EventTarget (EventTarget)
5761
import Web.Internal.FFI (unsafeReadProtoTagged)
@@ -121,6 +125,13 @@ foreign import _getAttribute :: String -> Element -> Effect (Nullable String)
121125
foreign import hasAttribute :: String -> Element -> Effect Boolean
122126
foreign import removeAttribute :: String -> Element -> Effect Unit
123127

128+
foreign import matches :: QuerySelector -> Element -> Effect Boolean
129+
130+
closest :: QuerySelector -> Element -> Effect (Maybe Element)
131+
closest qs = map toMaybe <<< _closest qs
132+
133+
foreign import _closest :: QuerySelector -> Element -> Effect (Nullable Element)
134+
124135
foreign import scrollTop :: Element -> Effect Number
125136
foreign import setScrollTop :: Number -> Element -> Effect Unit
126137

0 commit comments

Comments
 (0)