Skip to content

Commit

Permalink
feat: update to purescript v0.13.6, to halogen-vdom v6.1.0, replace d…
Browse files Browse the repository at this point in the history
…eprecated package maps with ordered-collections
  • Loading branch information
srghma committed Mar 30, 2020
1 parent 2277c7f commit 09fafb8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
6 changes: 4 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"output"
],
"dependencies": {
"purescript-prelude": "^3.0.0",
"purescript-halogen-vdom": "^2.0.0"
"purescript-prelude": "^4.1.1",
"purescript-halogen-vdom": "^6.1.0",
"purescript-ordered-collections": "^1.6.1",
"purescript-foreign": "^5.0.0"
}
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"build": "pulp build -- --censor-lib --strict"
},
"devDependencies": {
"pulp": "^11.0.0",
"purescript-psa": "^0.5.1",
"purescript": "^0.11.4",
"rimraf": "^2.6.1"
"pulp": "^14.0.0",
"purescript-psa": "^0.7.3",
"purescript": "^0.13.6",
"rimraf": "^3.0.2"
}
}
15 changes: 6 additions & 9 deletions src/Halogen/VDom/DOM/StringRenderer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import Prelude
import Halogen.VDom (VDom, ElemName(..))
import Halogen.VDom.DOM.Prop (Prop(..), PropValue)
import Data.Array as A
import Data.Foreign (toForeign, typeOf)
import Foreign (unsafeToForeign, typeOf)
import Data.Maybe (Maybe(..))
import Data.String as S
import Data.StrMap as SM
import Data.Tuple (Tuple(..))
import Data.Set as Set
import Halogen.VDom.StringRenderer as VSR
import Halogen.VDom.StringRenderer.Util (escape)
import Unsafe.Coerce (unsafeCoerce)
Expand All @@ -18,7 +17,7 @@ render renderWidget = VSR.render getTagType renderProps renderWidget

getTagType ElemName VSR.TagType
getTagType (ElemName en)
| SM.member en voidElements = VSR.SelfClosingTag
| Set.member en voidElements = VSR.SelfClosingTag
| otherwise = VSR.NormalTag

renderProps i. Array (Prop i) String
Expand All @@ -41,19 +40,17 @@ propNameToAttrName = case _ of
p → p

renderProperty String PropValue Maybe String
renderProperty name prop = case typeOf (toForeign prop) of
renderProperty name prop = case typeOf (unsafeToForeign prop) of
"string" → renderAttr name' $ (unsafeCoerce PropValue String) prop
"number" → renderAttr name' $ show ((unsafeCoerce PropValue String) prop)
"boolean"Just $ escape name'
_ → Nothing
where
name' = propNameToAttrName name

voidElements :: SM.StrMap Unit
voidElements :: Set.Set String
voidElements =
SM.fromFoldable
$ map (flip Tuple unit)
$ names
Set.fromFoldable names
where
names =
[ "area"
Expand Down
28 changes: 14 additions & 14 deletions src/Halogen/VDom/StringRenderer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ module Halogen.VDom.StringRenderer
import Prelude

import Data.Array as A
import Data.Maybe (maybe)
import Data.Maybe (Maybe, maybe)
import Data.String as S
import Data.Tuple (snd)

import Halogen.VDom (VDom(..), ElemSpec(..), ElemName(..), Namespace(..), runGraft)
import Halogen.VDom (VDom(..), ElemName(..), Namespace(..), runGraft)
import Halogen.VDom.StringRenderer.Util (escape)

-- | Type used to determine whether an element can be rendered as self-closing
Expand All @@ -25,29 +25,29 @@ derive instance ordTagType ∷ Ord TagType
-- | Renders a `VDom` tree to a string using the specified tag type scheme,
-- | attribute renderer, and widget renderer.
render
a w
attrs widget
. (ElemName TagType)
(a String)
(w String)
VDom a w
(attrs String)
(widget String)
VDom attrs widget
String
render getTagType renderAttrs renderWidget = go
where
go VDom a w String
go VDom attrs widget String
go = case _ of
Text s → escape s
Elem elem children → renderElement elem children
Keyed elem kchildren → renderElement elem (map snd kchildren)
Widget w → renderWidget w
Elem namespace elementName attrs children → renderElement namespace elementName attrs children
Keyed namespace elementName attrs kchildren → renderElement namespace elementName attrs (map snd kchildren)
Widget widget → renderWidget widget
Grafted g → go (runGraft g)

renderElement ElemSpec a Array (VDom a w) String
renderElement (ElemSpec mns en@(ElemName name) attrs) children =
renderElement (Maybe Namespace) -> ElemName -> attrs Array (VDom attrs widget) String
renderElement maybeNamespace elemName@(ElemName name) attrs children =
let
as = renderAttrs attrs
as' = maybe as (\(Namespace ns) -> "xmlns=\"" <> escape ns <> "\"" <> if S.null as then "" else " " <> as) mns
as' = maybe as (\(Namespace ns) -> "xmlns=\"" <> escape ns <> "\"" <> if S.null as then "" else " " <> as) maybeNamespace
in
"<" <> name <> (if S.null as then "" else " ") <> as <>
if A.null children
then if getTagType en == SelfClosingTag then "/>" else "></" <> name <> ">"
then if getTagType elemName == SelfClosingTag then "/>" else "></" <> name <> ">"
else ">" <> S.joinWith "" (map go children) <> "</" <> name <> ">"

0 comments on commit 09fafb8

Please sign in to comment.