-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDocument.purs
More file actions
135 lines (112 loc) · 4.52 KB
/
Document.purs
File metadata and controls
135 lines (112 loc) · 4.52 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
-- | This module provides type definitions and implementations for the
-- | `Document` interface, which is part of the W3C DOM API.
-- |
-- | The DOM API doesn't actually give you any way of getting hold of a
-- | `Document` by itself. To do that, you will need to look at one of the
-- | other APIs which build on the DOM API.
-- |
-- | For example, `window.document` is
-- | part of the HTML5 API, and so the relevant binding can be found in
-- | `Web.HTML.Window`, which is part of the `purescript-web-html` package.
-- | So here is one way to acquire an HTML5 `Document` from functions
-- | in `purescript-web-html`:
-- |
-- | ```purescript
-- | import Web.HTML (window)
-- | import Web.HTML.Window (document)
-- | import Web.HTML.HTMLDocument (toDocument)
-- |
-- | do
-- | doc <- map toDocument $ document =<< window
-- | ```
module Web.DOM.Document
( Document
, fromNode
, fromParentNode
, fromNonElementParentNode
, fromEventTarget
, toNode
, toParentNode
, toNonElementParentNode
, toEventTarget
, url
, documentURI
, origin
, compatMode
, characterSet
, contentType
, doctype
, documentElement
, getElementsByTagName
, getElementsByTagNameNS
, getElementsByClassName
, createElement
, createElementNS
, createDocumentFragment
, createTextNode
, createComment
, createProcessingInstruction
, importNode
, adoptNode
) where
import Prelude
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe, toNullable)
import Effect (Effect)
import Unsafe.Coerce (unsafeCoerce)
import Web.DOM.Comment (Comment)
import Web.DOM.DocumentFragment (DocumentFragment)
import Web.DOM.DocumentType (DocumentType)
import Web.DOM.Element (Element)
import Web.DOM.HTMLCollection (HTMLCollection)
import Web.DOM.Internal.Types (Node)
import Web.DOM.NonElementParentNode (NonElementParentNode)
import Web.DOM.ParentNode (ParentNode)
import Web.DOM.ProcessingInstruction (ProcessingInstruction)
import Web.DOM.Text (Text)
import Web.Event.EventTarget (EventTarget)
import Web.Internal.FFI (unsafeReadProtoTagged)
foreign import data Document :: Type
fromNode :: Node -> Maybe Document
fromNode = unsafeReadProtoTagged "Document"
fromParentNode :: ParentNode -> Maybe Document
fromParentNode = unsafeReadProtoTagged "Document"
fromNonElementParentNode :: NonElementParentNode -> Maybe Document
fromNonElementParentNode = unsafeReadProtoTagged "Document"
fromEventTarget :: EventTarget -> Maybe Document
fromEventTarget = unsafeReadProtoTagged "Document"
toNode :: Document -> Node
toNode = unsafeCoerce
toParentNode :: Document -> ParentNode
toParentNode = unsafeCoerce
toNonElementParentNode :: Document -> NonElementParentNode
toNonElementParentNode = unsafeCoerce
toEventTarget :: Document -> EventTarget
toEventTarget = unsafeCoerce
foreign import url :: Document -> Effect String
foreign import documentURI :: Document -> Effect String
foreign import origin :: Document -> Effect String
foreign import compatMode :: Document -> Effect String
foreign import characterSet :: Document -> Effect String
foreign import contentType :: Document -> Effect String
doctype :: Document -> Effect (Maybe DocumentType)
doctype = map toMaybe <<< _doctype
foreign import _doctype :: Document -> Effect (Nullable DocumentType)
documentElement :: Document -> Effect (Maybe Element)
documentElement = map toMaybe <<< _documentElement
foreign import _documentElement :: Document -> Effect (Nullable Element)
foreign import getElementsByTagName :: String -> Document -> Effect HTMLCollection
getElementsByTagNameNS :: Maybe String -> String -> Document -> Effect HTMLCollection
getElementsByTagNameNS = _getElementsByTagNameNS <<< toNullable
foreign import _getElementsByTagNameNS :: Nullable String -> String -> Document -> Effect HTMLCollection
foreign import getElementsByClassName :: String -> Document -> Effect HTMLCollection
foreign import createElement :: String -> Document -> Effect Element
createElementNS :: Maybe String -> String -> Document -> Effect Element
createElementNS = _createElementNS <<< toNullable
foreign import _createElementNS :: Nullable String -> String -> Document -> Effect Element
foreign import createDocumentFragment :: Document -> Effect DocumentFragment
foreign import createTextNode :: String -> Document -> Effect Text
foreign import createComment :: String -> Document -> Effect Comment
foreign import createProcessingInstruction :: String -> String -> Document -> Effect ProcessingInstruction
foreign import importNode :: Node -> Boolean -> Document -> Effect Node
foreign import adoptNode :: Node -> Document -> Effect Node