summaryrefslogtreecommitdiff
path: root/src/JavaScript/Node.hs
blob: c9ad8af03f4492b428938814383981a815633aa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module JavaScript.Node (
    Node, IsNode(..),
    getTextContent, setTextContent,
    appendChild,
) where

import JavaScript.Event
import JavaScript.Val


newtype Node = Node JSVal
    deriving (ToJSVal, FromJSVal)

class IsNode a where
    toNode :: a -> Node

instance IsNode Node where
    toNode = id

instance IsEventTarget Node where
    toEventTarget = fromJSValUnchecked . toJSVal


getTextContent :: IsNode n => n -> IO String
getTextContent n = fromJSString <$> js_get_textContent (toJSVal $ toNode n)
foreign import javascript unsafe "$1.textContent"
    js_get_textContent :: JSVal -> IO JSString

setTextContent :: IsNode n => String -> n -> IO ()
setTextContent value n = js_set_textContent (toJSVal $ toNode n) (toJSString value)
foreign import javascript unsafe "$1.textContent = $2"
    js_set_textContent :: JSVal -> JSString -> IO ()


appendChild :: (IsNode c, IsNode p) => c -> p -> IO ()
appendChild child parent = js_appendChild (toJSVal $ toNode parent) (toJSVal $ toNode child)
foreign import javascript unsafe "$1.appendChild($2)"
    js_appendChild :: JSVal -> JSVal -> IO ()