Skip to content

Commit 8dde115

Browse files
authored
Migrate several types from web-html and update relevant functions to use them. (#58)
1 parent 568a1ee commit 8dde115

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ Notable changes to this project are documented in this file. The format is based
55
## [Unreleased]
66

77
Breaking changes:
8+
- The `id` function returns an `ElementId` instead of a `String`. (#58 by @nsaunders)
9+
- The `setId` function is parameterized by `ElementId` instead of `String`. (#58 by @nsaunders)
10+
- The `getElementById` function is parameterized by `ElementId` instead of `String`. (#58 by @nsaunders)
11+
- The `className` function returns a `ClassName` instead of a `String`. (#58 by @nsaunders)
12+
- The `setClassName` and `getElementsByClassName` functions are parameterized by `ClassName` instead of `String`. (#58 by @nsaunders)
13+
- The `getAttribute`, `setAttribute`, `hasAttribute`, and `removeAttribute` functions are parameterized by `AttrName` instead of `String`. (#58 by @nsaunders)
814

915
New features:
16+
- `AttrName`, `ClassName`, and `PropName` types have been added, migrated from [web-html](https://github.com/purescript-web/purescript-web-html). (#58 by @nsaunders)
17+
- A new `ElementId` type, representing the value of an `id` property/attribute, has been added. (#58 by @nsaunders)
1018

1119
Bugfixes:
1220

src/Web/DOM/Element.purs

+47-10
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ module Web.DOM.Element
4242
, DOMRect
4343
, ShadowRootInit
4444
, attachShadow
45+
, AttrName(..)
46+
, ClassName(..)
47+
, ElementId(..)
48+
, PropName(..)
4549
) where
4650

4751
import Prelude
4852

4953
import Data.Maybe (Maybe)
54+
import Data.Newtype (class Newtype)
5055
import Data.Nullable (Nullable, toMaybe, toNullable)
5156
import Effect (Effect)
5257
import Unsafe.Coerce (unsafeCoerce)
@@ -102,11 +107,11 @@ foreign import _prefix :: Element -> Nullable String
102107
foreign import localName :: Element -> String
103108
foreign import tagName :: Element -> String
104109

105-
foreign import id :: Element -> Effect String
106-
foreign import setId :: String -> Element -> Effect Unit
107-
foreign import className :: Element -> Effect String
110+
foreign import id :: Element -> Effect ElementId
111+
foreign import setId :: ElementId -> Element -> Effect Unit
112+
foreign import className :: Element -> Effect ClassName
108113
foreign import classList :: Element -> Effect DOMTokenList
109-
foreign import setClassName :: String -> Element -> Effect Unit
114+
foreign import setClassName :: ClassName -> Element -> Effect Unit
110115

111116
foreign import getElementsByTagName :: String -> Element -> Effect HTMLCollection
112117

@@ -115,16 +120,16 @@ getElementsByTagNameNS = _getElementsByTagNameNS <<< toNullable
115120

116121
foreign import _getElementsByTagNameNS :: Nullable String -> String -> Element -> Effect HTMLCollection
117122

118-
foreign import getElementsByClassName :: String -> Element -> Effect HTMLCollection
123+
foreign import getElementsByClassName :: ClassName -> Element -> Effect HTMLCollection
119124

120-
foreign import setAttribute :: String -> String -> Element -> Effect Unit
125+
foreign import setAttribute :: AttrName -> String -> Element -> Effect Unit
121126

122-
getAttribute :: String -> Element -> Effect (Maybe String)
127+
getAttribute :: AttrName -> Element -> Effect (Maybe String)
123128
getAttribute attr = map toMaybe <<< _getAttribute attr
124129

125-
foreign import _getAttribute :: String -> Element -> Effect (Nullable String)
126-
foreign import hasAttribute :: String -> Element -> Effect Boolean
127-
foreign import removeAttribute :: String -> Element -> Effect Unit
130+
foreign import _getAttribute :: AttrName -> Element -> Effect (Nullable String)
131+
foreign import hasAttribute :: AttrName -> Element -> Effect Boolean
132+
foreign import removeAttribute :: AttrName -> Element -> Effect Unit
128133

129134
foreign import matches :: QuerySelector -> Element -> Effect Boolean
130135

@@ -179,3 +184,35 @@ initToProps init = {
179184
}
180185

181186
foreign import _attachShadow :: ShadowRootProps -> Element -> Effect ShadowRoot
187+
188+
-- | A wrapper for property names.
189+
-- |
190+
-- | The phantom type `value` describes the type of value which this property
191+
-- | requires.
192+
newtype PropName :: Type -> Type
193+
newtype PropName value = PropName String
194+
195+
derive instance newtypePropName :: Newtype (PropName value) _
196+
derive newtype instance eqPropName :: Eq (PropName value)
197+
derive newtype instance ordPropName :: Ord (PropName value)
198+
199+
-- | A wrapper for attribute names.
200+
newtype AttrName = AttrName String
201+
202+
derive instance newtypeAttrName :: Newtype AttrName _
203+
derive newtype instance eqAttrName :: Eq AttrName
204+
derive newtype instance ordAttrName :: Ord AttrName
205+
206+
-- | A wrapper for strings which are used as CSS classes.
207+
newtype ClassName = ClassName String
208+
209+
derive instance newtypeClassName :: Newtype ClassName _
210+
derive newtype instance eqClassName :: Eq ClassName
211+
derive newtype instance ordClassName :: Ord ClassName
212+
213+
-- | A wrapper for strings which are used as element identifiers.
214+
newtype ElementId = ElementId String
215+
216+
derive instance newtypeElementId :: Newtype ElementId _
217+
derive newtype instance eqElementId :: Eq ElementId
218+
derive newtype instance ordElementId :: Ord ElementId

src/Web/DOM/NonElementParentNode.purs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import Prelude
88
import Data.Maybe (Maybe)
99
import Data.Nullable (Nullable, toMaybe)
1010
import Effect (Effect)
11-
import Web.DOM.Element (Element)
11+
import Web.DOM.Element (Element, ElementId)
1212

1313
foreign import data NonElementParentNode :: Type
1414

1515
-- | The first element within node's descendants with a matching ID, or null if
1616
-- | no such element exists.
17-
foreign import _getElementById :: String -> NonElementParentNode -> Effect (Nullable Element)
17+
foreign import _getElementById :: ElementId -> NonElementParentNode -> Effect (Nullable Element)
1818

19-
getElementById :: String -> NonElementParentNode -> Effect (Maybe Element)
19+
getElementById :: ElementId -> NonElementParentNode -> Effect (Maybe Element)
2020
getElementById eid = map toMaybe <<< _getElementById eid

0 commit comments

Comments
 (0)