Skip to content

Commit

Permalink
Migrate several types from web-html and update relevant functions to …
Browse files Browse the repository at this point in the history
…use them. (#58)
  • Loading branch information
nsaunders authored Sep 24, 2023
1 parent 568a1ee commit 8dde115
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ Notable changes to this project are documented in this file. The format is based
## [Unreleased]

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

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

Bugfixes:

Expand Down
57 changes: 47 additions & 10 deletions src/Web/DOM/Element.purs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ module Web.DOM.Element
, DOMRect
, ShadowRootInit
, attachShadow
, AttrName(..)
, ClassName(..)
, ElementId(..)
, PropName(..)
) where

import Prelude

import Data.Maybe (Maybe)
import Data.Newtype (class Newtype)
import Data.Nullable (Nullable, toMaybe, toNullable)
import Effect (Effect)
import Unsafe.Coerce (unsafeCoerce)
Expand Down Expand Up @@ -102,11 +107,11 @@ foreign import _prefix :: Element -> Nullable String
foreign import localName :: Element -> String
foreign import tagName :: Element -> String

foreign import id :: Element -> Effect String
foreign import setId :: String -> Element -> Effect Unit
foreign import className :: Element -> Effect String
foreign import id :: Element -> Effect ElementId
foreign import setId :: ElementId -> Element -> Effect Unit
foreign import className :: Element -> Effect ClassName
foreign import classList :: Element -> Effect DOMTokenList
foreign import setClassName :: String -> Element -> Effect Unit
foreign import setClassName :: ClassName -> Element -> Effect Unit

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

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

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

foreign import getElementsByClassName :: String -> Element -> Effect HTMLCollection
foreign import getElementsByClassName :: ClassName -> Element -> Effect HTMLCollection

foreign import setAttribute :: String -> String -> Element -> Effect Unit
foreign import setAttribute :: AttrName -> String -> Element -> Effect Unit

getAttribute :: String -> Element -> Effect (Maybe String)
getAttribute :: AttrName -> Element -> Effect (Maybe String)
getAttribute attr = map toMaybe <<< _getAttribute attr

foreign import _getAttribute :: String -> Element -> Effect (Nullable String)
foreign import hasAttribute :: String -> Element -> Effect Boolean
foreign import removeAttribute :: String -> Element -> Effect Unit
foreign import _getAttribute :: AttrName -> Element -> Effect (Nullable String)
foreign import hasAttribute :: AttrName -> Element -> Effect Boolean
foreign import removeAttribute :: AttrName -> Element -> Effect Unit

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

Expand Down Expand Up @@ -179,3 +184,35 @@ initToProps init = {
}

foreign import _attachShadow :: ShadowRootProps -> Element -> Effect ShadowRoot

-- | A wrapper for property names.
-- |
-- | The phantom type `value` describes the type of value which this property
-- | requires.
newtype PropName :: Type -> Type
newtype PropName value = PropName String

derive instance newtypePropName :: Newtype (PropName value) _
derive newtype instance eqPropName :: Eq (PropName value)
derive newtype instance ordPropName :: Ord (PropName value)

-- | A wrapper for attribute names.
newtype AttrName = AttrName String

derive instance newtypeAttrName :: Newtype AttrName _
derive newtype instance eqAttrName :: Eq AttrName
derive newtype instance ordAttrName :: Ord AttrName

-- | A wrapper for strings which are used as CSS classes.
newtype ClassName = ClassName String

derive instance newtypeClassName :: Newtype ClassName _
derive newtype instance eqClassName :: Eq ClassName
derive newtype instance ordClassName :: Ord ClassName

-- | A wrapper for strings which are used as element identifiers.
newtype ElementId = ElementId String

derive instance newtypeElementId :: Newtype ElementId _
derive newtype instance eqElementId :: Eq ElementId
derive newtype instance ordElementId :: Ord ElementId
6 changes: 3 additions & 3 deletions src/Web/DOM/NonElementParentNode.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import Prelude
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
import Effect (Effect)
import Web.DOM.Element (Element)
import Web.DOM.Element (Element, ElementId)

foreign import data NonElementParentNode :: Type

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

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

0 comments on commit 8dde115

Please sign in to comment.